栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829007.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号