AttributeError: ‘numpy.ndarray‘ object has no attribute ‘unsqueeze‘

  • z = z * pts_depth.unsqueeze(1)*0.1
    AttributeError: 'numpy.ndarray' object has no attribute 'unsqueeze'
    
  • 这个错误是因为unsqueeze()方法是PyTorch张量的方法,而不是NumPy数组的方法。在这里,pts_depth是一个NumPy数组,因此不能使用unsqueeze()方法。如果要在NumPy中实现类似于unsqueeze()的功能,可以使用np.newaxis关键字来添加新的维度。例如,可以使用以下代码将pts_depth的维度从[batch_size, num_points]扩展为[batch_size, 1, num_points]:

    import numpy as np
    
    # 假设pts_depth是一个NumPy数组,维度为[batch_size, num_points]\pts_depth = np.random.randn(batch_size, num_points)
    
    # 将pts_depth的维度从[batch_size, num_points]扩展为[batch_size, 1, num_points]
    pts_depth = pts_depth[:, np.newaxis, :]
    

    在上述代码中,pts_depth[:, np.newaxis, :]使用np.newaxis关键字在第二个维度上添加了一个新的维度,从而将pts_depth的维度从[batch_size, num_points]扩展为[batch_size, 1, num_points]。这样就可以避免上述错误的发生。

你可能感兴趣的:(numpy,python,深度学习)