tensorflow+Python ValueError以及解决方法(后续继续更新)

使用python 怎么可能没有ValueError,下面罗列我见到的几种错误,虽然很低级,但是不试过还是很难找到错误的

ValueError: Incompatible type conversion requested to type ‘float32’ for variable of type ‘int32_ref’

使用tensorflow时,出现在卷积层,原因在于初始化卷积权重或者偏置时, 采用整数进行初始化,导致卷积权重或者偏置为整形变量。
下面贴出我错误的代码

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
    return biases

修改结果

def bias_variable(shape, name):
    with tf.name_scope('Bias'):
        initial = tf.constant(0.0, shape=shape)
        biases = tf.Variable(initial)
        tf.summary.histogram(name+'/Biases_function', biases)
        return biases

也就是0于0.0的问题,在修改初始化参数时,一不留神就改错了,然后还不知道发生了什么。

ValueError: Incompatible shapes between op input and calculated input gradient.

我错误的原因在于使用tensorflow的反卷积tf.nn.conv2d_transpose是制定的output_shape出错了,其实函数根据输入和卷积核是可以计算输出尺寸的,也就是tf.nn.conv2d_transpose的信息是冗余的,因此,如果冗余的信息对应不上,导致误差无法反向传递。

ValueError: ‘images’ must have either 3 or 4 dimensions.

错误的代码为:

 level_map = tf.image.resize_images(gt_level, size)

说明输入的变量gt_level不是一个3-D或者4-D的tensor。查找修改即可。tf.image.resize_images的定义如下:
Signature:
tf.image.resize_images(images, size, method=0, align_corners=False)
Args:

  1. images: 4-D Tensor of shape [batch, height, width, channels] or
    3-D Tensor of shape [height, width, channels].
  2. size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The
    new size for the images.
  3. method: ResizeMethod. Defaults to ResizeMethod.BILINEAR.
  4. align_corners: bool. If True, the centers of the 4 corner pixels of the
    input and output tensors are aligned, preserving the values at the
    corner pixels. Defaults to False.
ubuntu20.04使用randla-net网络复现s3dis数据报错****EPOCH 0**** Caught a NaN error : 3 2 root error(s) found. (0) Invalid argument: ValueError: attempt to get argmin of an empty sequence Traceback (most recent call last): File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/tensorflow_core/python/ops/script_ops.py", line 235, in __call__ ret = func(*args) File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 638, in generator_py_func values = next(generator_state.get_iterator(iterator_id)) File "main_S3DIS.py", line 114, in spatially_regular_gen cloud_idx = int(np.argmin(self.min_possibility[split])) File "<__array_function__ internals>", line 6, in argmin File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1269, in argmin return _wrapfunc(a, 'argmin', axis=axis, out=out) File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 55, in _wrapfunc return _wrapit(obj, method, *args, **kwds) File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 44, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds) ValueError: attempt to get argmin of an empty sequence [[{{node PyFunc}}]] [[IteratorGetNext]] (1) Invalid argument: ValueError: attempt to get argmin of an empty sequence Traceback (most recent call last): File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/tensorflow_core/python/ops/script_ops.py", line 235, in __call__ ret = func(*args) File "/home/free/miniconda3/envs/randlanet/lib/python3.6/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 638, in generator_py_func values = next(generator_state.get_iterator(iterator_id)) File "main_S3DIS.py", line 114, in spatially_regular_gen cloud_idx = int(np.argmin(self.min_possibility[s
04-04
<think>嗯,用户遇到了在Ubuntu 20.04上运行RandLA-Net复现S3DIS数据集时出现NaN错误和“attempt to get argmin of an empty sequence”的问题。我需要帮他分析可能的原因和解决办法。 首先,NaN错误通常与数值不稳定有关,可能是输入数据的问题,比如存在无效值或极端值。或者模型中的某些操作,如除法或对数运算,输入了不合适的值。RandLA-Net在处理点云时可能涉及采样和最近邻搜索,如果某些点云区域没有足够的点,可能导致空序列,进而引发argmin错误。 然后,“argmin of an empty sequence”错误说明某个操作试图对一个空数组取最小值索引。这可能发生在点云采样过程中,比如在寻找最近邻时,如果搜索区域没有点,返回空数组,后续操作就会报错。 接下来,我需要考虑解决方案。首先检查输入数据是否正确,确保S3DIS数据集没有损坏,预处理步骤是否正确。可能需要添加数据验证步骤,比如检查NaN或Inf值。对于采样过程,可以增加空数组的检查,避免在空数组上执行argmin。例如,在KNN搜索时设置最小点数,或者使用try-except捕获异常。 另外,RandLA-Net的实现可能依赖特定版本的TensorFlow和NumPy。版本不兼容可能导致计算错误。用户应该确认使用的版本是否符合要求,必要时降级或升级。例如,TensorFlow 1.x和2.x之间的API差异可能导致问题。 调试方面,建议用户逐步运行代码,打印中间变量的形状和值,定位出错的具体位置。使用TensorFlow的调试工具,如tf.debugging.enable_check_numerics来追踪NaN的来源。 还有,随机数种子设置可能影响模型训练的稳定性,固定随机种子有助于复现问题。此外,检查损失函数是否包含可能导致NaN的操作,如对数运算,可以加入epsilon防止除以零。 最后,参考用户提供的引用,虽然内容是关于RKNN-Toolkit2安装,但可能提示环境配置的重要性。用户应确保CUDA、cuDNN等依赖正确安装,与TensorFlow版本兼容。 综合以上思路,整理出分步骤的解决方案,涵盖数据检查、代码修改、版本确认、调试方法等,帮助用户系统地解决问题。</think>### Ubuntu 20.04运行RandLA-Net复现S3DIS数据时的NaN错误及空序列argmin问题解决方案 #### 1. 问题分析 - **NaN错误**:通常由数值不稳定引起,可能涉及以下场景: - 输入数据包含无效值或极端值 - 模型参数初始化异常 - 梯度爆炸导致权重溢出 - 数学运算中出现非法操作(如除零、log(0)等) - **argmin空序列错误**:发生在KNN搜索等操作中,当候选点集为空时调用`np.argmin()`或`tf.argmin()`[^1] #### 2. 分步解决方案 **步骤1:验证数据预处理** - 检查S3DIS数据集是否完整下载和解压 - 确认点云坐标范围是否合理(建议标准化到$[-1,1]$区间) - 添加数据校验代码片段: ```python print("数据统计:", np.min(points), np.max(points), np.isnan(points).any()) ``` **步骤2:修改采样逻辑** 在KNN搜索前添加空集保护: ```python def safe_knn_search(query_points, dataset_points, k=16): distances = np.linalg.norm(dataset_points - query_points[:, None], axis=2) valid_mask = np.any(distances < 1e6, axis=1) # 根据实际场景调整阈值 if np.sum(valid_mask) < k: return np.zeros((len(query_points), k), dtype=np.int32) return np.argpartition(distances, k, axis=1)[:, :k] ``` **步骤3:版本兼容性确认** 建议使用以下组合: ```bash tensorflow-gpu==1.15.0 # 或2.4.0 numpy==1.19.5 h5py==2.10.0 ``` **步骤4:启用数值检查** 在TensorFlow中激活调试模式: ```python tf.debugging.enable_check_numerics() ``` **步骤5:损失函数加固** 修改交叉熵损失计算: ```python epsilon = 1e-7 loss = -tf.reduce_sum(y_true * tf.math.log(y_pred + epsilon)) ``` #### 3. 环境配置建议 - 确认CUDA/cuDNN版本匹配: ```bash nvcc --version # 应显示11.0以上 cat /usr/local/cuda/version.txt ``` - 安装必要依赖: ```bash sudo apt-get install libhdf5-serial-dev hdf5-tools ``` #### 4. 典型错误修复案例 某复现项目通过以下修改解决类似问题: ```diff - knn_indices = np.argmin(distances, axis=1) + valid_distances = np.where(distances < 1e5, distances, np.inf) + knn_indices = np.argmin(valid_distances, axis=1) ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值