1. Type Error: unhashable type: ‘Dimension’
1.1 问题描述
定义以下函数:
def exposure_mat(a_embedded, model_expo, N_rays, N_samples_, chunk):
a_embedded_ = repeat(a_embedded, 'n1 c -> (n1 n2) c', n2=N_samples_)
...
调用上述函数时,运行至a_embedded_ = repeat(a_embedded.numpy(), 'n1 c -> (n1 n2) c', n2=int(N_samples_)),报错Type Error: unhashable type: 'Dimension'
1.2 解决方法:
网上看到的解决方法都没能解决,然后查询unhashable是什么意思(详见Python异常:unhashable type 是怎么回事?),最后确定原因N_samples_不是int类型,转为int就可以啦。
还有我这里用的a_embedded是用tf.get_variable()获得的,需要转化为numpy。
最后能成功运行的代码为:
def exposure_mat(a_embedded, model_expo, N_rays, N_samples_, chunk):
repeat(a_embedded.numpy(), 'n1 c -> (n1 n2) c', n2=int(N_samples_))
...
##########################################################
2. Ranks of all input tensors should match: shape[0] = [8192,74] vs. shape[1] = [8192] [Op:ConcatV2] n
2.1 问题描述
运行
tf.concat([rays, rays_o[..., 3]], axis=-1)
其中rays的shape为:(8192,74)
rays_o[..., 3]的shape为:(8192,)
报错信息为 Ranks of all input tensors should match: shape[0] = [8192,74] vs. shape[1] = [8192] [Op:ConcatV2] name: concat
2.2 问题分析
要拼接的两个tensor维度不一致,需要对rays_id进行reshape,采用tf.reshape 函数
2.3 解决方法
rays_id = tf.reshape(rays_o[..., 3],[rays.shape[0],1])
rays = tf.concat([rays, rays_id], axis=-1)
##########################################################
3. AttributeError: module ‘tensorflow.python.keras.api._v1.keras.backend’ has no attribute 'is_keras_te
3.1问题描述
运行
out = rearrange(out, '(n1 n2) c -> n1 n2 c', n1=N_rays, n2=int(N_samples_))
出现报错:
{AttributeError}module 'tensorflow.python.keras.api._v1.keras.backend' has no attribute 'is_keras_te
3.2 解决方法
网上类似的问题说是版本原因,但我有类似的代码是可以运行的。所以猜测是out数据类型的问题,把out改为array(用out.numpy())后,问题解决,代码如下:
out = rearrange(out.numpy(), '(n1 n2) c -> n1 n2 c', n1=N_rays, n2=int(N_samples_))
##########################################################
4. InvalidArgumentError: Value for attr ‘Tindices’ of float is not in the list of allowed values: int32, int64 ; NodeDef: {{node ResourceGather}};
4.1 问题描述
运行:
a_embedded = tf.gather(a_embedded, ray_batch[:, -1]))
其中a_embedded是用tf.get_variable()获得的,
出现报错:InvalidArgumentError: Value for attr ‘Tindices’ of float is not in the list of allowed values: int32, int64 ; NodeDef: {{node ResourceGather}};
4.2 解决方法
用tf.gather时,索引的列表的数据必须是int类型的,所以修改为:
a_embedded = tf.gather(a_embedded, np.array(ray_batch[:, -1]).astype(int))
本文详细介绍了在使用TensorFlow时遇到的四个典型错误,包括TypeError、Ranks不匹配、AttributeError和InvalidArgumentError,并提供了详细的错误描述、原因分析及解决方法。通过这些实例,读者可以更好地理解和解决在深度学习实践中遇到的相似问题。
773

被折叠的 条评论
为什么被折叠?



