tensorflow-debug笔记

本文记录了在使用TensorFlow进行开发时遇到的一些典型错误,包括字符串转换为float失败、找不到checkpoint、张量shape不一致、SparseTensor转换、VocabularyListCategoricalColumn报错、类型错误、decode_csv问题等,并提供了相应的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Python Tensorflow线性模型不支持将字符串转换为float

tensorflow.python.framework.errors_impl.UnimplementedError: Cast string to float is not supported

2、找不到对应的checkpoint

errors_impl.NotFoundError: Key input_layer/distinct_id_embedding/embedding_weights not found in checkpoint

原因:由于tf运算图没有重置的原因,有人实验过证明第二次导入时候其中的变量名发生变化
解决:删除模型保存目录,重新生成

3、张量shape不一致

ValueError: Shapes (?, 1) and (?,) are incompatible

原因:计算过程中对两个shape分别为(?, 1) 和 (?,) 直接运算,shape不匹配报错
解决:reshape,将其中一个reshape成相同的shape
例如将(?, 1) reshape成 (?,) y = tf.reshape(y, shape=[-1,])

4、SparseTensor转Tensor

ValueError: The corresponding Tensor of numerical column must be a Tensor. SparseTensor is not supported. key: pic_emb

解决:tf.sparse.to_dense(
sp_input, default_value=None, validate_indices=True, name=None
)

5、VocabularyListCategoricalColumn报错

ValueError: Items of feature_columns must be a _DenseColumn. You can wrap a categorical column with an embedding_column or indicator_column. Given: VocabularyListCategoricalColumn(key=‘u_area_state’, vocabulary_list=(‘中国’, ‘俄罗斯’), dtype=tf.string, default_value=-1, num_oov_buckets=0)

原因:tf1.x 和 tf2.x的不同
解决:
area_state = fc.categorical_column_with_vocabulary_list(key=‘u_area_state’,
vocabulary_list=(“中国”, “俄罗斯”))

6、

TypeError: Could not build a TypeSpec for None with type NoneType

7、decode_csv的输入shape错误

ValueError: Shape must be at most rank 1 but is rank 2 for ‘DecodeCSV’

原因:decode_csv的输入shape 只能为秩为1的矩阵

8、如何将字符串Tensor转换为Tensor浮点列表

# 字符串分割,转浮点list
import tensorflow as tf
a=tf.constant(['234_784_0.343', '66_77_88', ''],dtype=tf.string)

# string_split, 输出包括indices,values,dense_shape.
# - indices 表示分词后的词的下表,和values一一对应
# - values.分割后的数据内容,返回的是一维向量!经常可以用这个把一篇文档所有分词后的词扔入一个向量中
# - dense_shape.分割前的维度是多少,会按最长的那个句子填充
b = tf.string_split(a, delimiter="_") 

# string_to_number
c = tf.string_to_number(b, tf.int32)

# 将string_split输出的一维向量reshape
# string_split对多个输入的输出也是一维向量,这里需要reshape
d = tf.reshape(c, b.dense_shape)

with tf.Session() as sess:
    print(sess.run(c))
 
# array 和 tensor 相互转换
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
b=tf.constant(a) #将array a 转换为tensor
tensor_a=tf.convert_to_tensor(a) #将array a 转换为tensor

参考:TensorFlow数据结构操作之:tf.string_split函数

9、not a valid int32

Field 0 in record 0 is not a valid int32: distinct_id [[{{node DecodeCSV}}]]

原因:csv第一行包含了header
解决:csv文件不加header保存
或者dataset = dataset.skip(skip_header_lines=1)
附:tf.decode_csv() 读取int64数据

10、 Input to reshape is a tensor with xxx values, but the requested shape has xxx

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 37
888 values, but the requested shape has 19398656

原因:张量reshape不对,实际输入元素(值)个数与所需矩阵元素个数不一致
解决:需要仔细阅读代码,定位问题,检查每一个对矩阵维度的定义

11、lhs shape= [x,x] rhs shape= [x,x]

tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tenso rs to match. lhs shape= [1312,512] rhs shape= [800,512]

原因:遇到这个问题,第一,确定修改后的网络结构和你输入的自定义数据是对应的,第二,删掉代码目录下logs(一般叫logs,也可能叫别的)文件夹,因为这里面记录了用别的数据集训练时候的网络结构,会对你的训练造成干扰。

12、tf.io.decode_csv()
将csv记录转化为张量,每一条记录映射一个张量

tf.io.decode_csv(
  records,
  record_defaults,
  field_delim=',',
  use_quote_delim=True,
   na_value='',
   select_cols=None,
  name=None
)

records:一个string类型的Tensor。每个字符串都是csv中的记录/行,所有记录都应具有相同的格式;
record_defaults:具有特定类型的Tensor对象列表。可接受的类型有float32,float64,int32,int64,string。输入记录的每列一个张量,具有该列的标量默认值或者如果需要该列则为空向量;
field_delim=’,’:可选的string。默认为","。用于分隔记录中字段的char分隔符;
use_quote_delim=True:可选的bool。默认为True。如果为false,则将双引号视为字符串字段内的常规字符;
na_value=’’:要识别为NA/NaN的附加字符串;
select_cols=None:可选的列索引的可选排序列表。如果指定,则仅解析并返回此列的子集;
name=None:操作的名称。
返回:
Tensor对象列表。与record_defaults具有相同的类型。每个张量将与记录具有相同的形状。

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值