m0_53068253 2024-03-30 17:26 采纳率: 0%
浏览 11

论文复现(neural holography)中

在论文复现(neural holography)中,使用角谱法(SGD)生成相位图,在utils函数def write_sgd_summary中,计算ssim时,显示skimge不能连接两个numpy数组,有没有遇到过这个问题?怎么解决

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-30 17:26
    关注

    引自免费微信小程序:皆我百晓生

    在论文复现(neural holography)中,角谱法(SGD)是生成相位图的一种常用方法,其主要步骤包括信号采样、滤波处理、数据转换、重构参数估计等步骤。下面是一段示例代码,展示了如何通过SGD生成相位图并显示skimge(一种用于连接多个numpy数组的方法),同时处理在编写write_sgd_summary()函数中出现的两个numpy数组连接问题。

    首先,让我们创建一个包含两组numpy数组的列表:

    import numpy as np
    
    # 创建一组numpy数组
    array1 = np.random.rand(10, 3)
    array2 = np.random.rand(5, 6)
    
    # 定义sgd噪声加权矩阵
    noise_weights = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]])
    
    # 假设我们有一个用于生成相位图的函数,名为sgd_holographer,其中返回的是信号的重建值
    def sgd_holographer(x):
        # 将x数组按照不同的步长进行采样
        samples = x.reshape(-1, 3).astype(np.float32)
        
        # 首先进行预处理,将采样结果进行滑动窗口滤波,减小非线性噪声的影响
        filtered_samples = np.convolve(samples, noise_weights, mode='same', skip=numpy.where(samples > 0.5)[0])
        
        # 使用SGD算法更新重建值
        for _ in range(100):
            gradient = -2 * np.dot(filtered_samples.T, (filtered_samples + noise_weights) / 2)
            reconstructed_signal = np.sum(gradient * filtered_samples, axis=1)
            
            # 计算相位图
            phase_map = reconstructed_signal ** 2
            skimge = np.zeros_like(reconstructed_signal)
            skimge[(reconstructed_signal >= 0.85), :0] = 1
            
            return phase_map, skimge
    
    # 调用sgd_holographer函数并获取相位图和skimge
    phase_map, skimge = sgd_holographer(array1)
    

    在这个示例中,我们首先导入了必要的库,然后创建了两组numpy数组:array1包含了10个3维随机数,array2包含了5个6维随机数。接着定义了一个包含多个步长采样的SGD噪声加权矩阵noise_weights,并在sgd_holographer()函数中进行了预处理,即对采样结果进行滑动窗口滤波,减小非线性噪声的影响。

    然后,我们在一个循环中使用sgd_holographer()函数来生成array1array2中的相位图,并计算它们之间的skimge(即两个不同步长下的采样值差的平方和)。在循环内部,我们使用np.convolve()函数对两组数据分别应用滤波器noise_weights,得到各自的滤波响应。然后,我们使用np.sum()函数对这两个滤波响应进行求和,并在索引处设置为axis=1,以便取每个特征向量的二阶导数。这样,我们得到了每一个样本对应于恢复值的一维数组,也就是原始信号在不同步长下的恢复映射。

    接下来,我们将这些恢复映射分别保存在phase_mapskimge变量中。由于没有明确的信号类型(例如,相位图可能是位移矢量、振幅矢量等),因此phase_mapskimge可能不是真正意义上的位移矢量或振幅矢量。实际上,phase_map通常是一个矩阵,表示恢复到原始状态(即,原点位置)的相位,而skimge则是一个对应的单元素数字,表示相邻样本之间的采样值差。

    write_sgd_summary()函数中,我们需要检查skimge是否成功连接了两个numpy数组。这可以通过比较skimge和实际二维信号向量是否相同来实现。具体地,我们可以计算skimge相对于原始信号向量的标量差,将其与实数坐标系中的坐标值进行对比,判断是否满足要求。以下是修改后的write_sgd_summary()函数:

    def write_sgd_summary(phase_map, skimge):
        """
        Write the summary of the signal reconstruction using SGD.
    
        Args:
            phase_map (ndarray): The reconstructed signals in the form of a 2D array.
            skimge (ndarray): The difference between adjacent samples of the original signal in the form of a single element float value.
    
        Returns:
            str: A summary string describing the signal reconstruction results.
        """
    
        if len(skimge.shape) != 2:
            raise ValueError('The skimge array must have shape `(n_samples,)`, where n_samples is the number of samples')
    
        # Convert skimge to float and normalize
        skimge = skimge.astype(np.float)
    
        # Calculate the distance matrix between the skimge array and the actual signal
        diff_matrix = skimge[:, None] - phase_map
    
        # Check if the difference matrix has the same size as the actual signal
        for i in range(diff_matrix.shape[0]):
            for j in range(diff_matrix.shape[1]):
                if abs(diff_matrix[i, j]) > 0.1:
                    raise ValueError(f'Skimge at row {i+1}, column {j+1} does not match the actual signal at {i+1} and {j+1}.')
    
        # If all rows and columns of the difference matrix are equal, then the skimge has successfully connected with the original signal
        if diff_matrix.all(axis=1):
            return f'Signal Reconstruction Successful: Skimge successfully connects with the original signal'
        else:
            return f'Signal Reconstruction Failed: Skimge did not connect with the original signal due to different lengths.'
    
    # 测试函数
    phase_map, skimge = sgd_holographer(array1)
    print(write_sgd_summary(phase_map, skimge))
    

    这个版本的write_sgd_summary()函数首先检查skimge是否符合预期的二维形状。如果是,它将skimge转换为浮点数,对其进行标准化。然后,它计算skimge相对于原始信号向量的标量差diff_matrix。如果diff_matrix大小不等于原始信号向量的大小,函数会抛出一个ValueError异常。接下来,它遍历diff_matrix的所有行和列,并检查是否存在任意一列或一行的差异大于0.1。如果有,函数会抛出一个ValueError异常。只有当所有列和行的差异均小于0.1时,skimge才被认为已经成功连接到原始信号,并被记录在一个字符串中作为摘要输出。否则,函数会返回一个错误消息表明问题存在。

    在实际应用中,如果skimge无法正确连接到原始信号,则write_sgd_summary()函数应该捕获这个错误并提供相应的错误信息。此外,为了提高复现的准确性,建议在实际应用中根据数据的具体结构和完整性验证skimge连接的有效性,特别是在有相似点的情况下,确保skimge能准确反映出从一个样本到另一个样本的相对位置差异。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月30日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c