Tensorflow学习(二)——遇到的报错及解决方法

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
最后,成功运行的代码是:

[En]

Finally, the code that runs successfully is:

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.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.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.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))

Original: https://blog.csdn.net/fighterlucky/article/details/124290107
Author: 猪小肥呀
Title: Tensorflow学习(二)——遇到的报错及解决方法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/497432/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球