tensorflow错误及解决办法

文章列举了在使用TensorFlow时遇到的一些典型错误,如tf.graph节点无数据、图过大、维度不匹配、模型评估时自定义auc计算的问题、预测值不在[0,1]区间、数据输入错误、模型加载不全以及模型签名和操作符不匹配等,并提供了相应的解决策略,如避免使用tf.cond、控制图大小、检查输入维度一致性、添加update_op、正确设置数据解析参数和确保模型完整传输等。

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

tf.graph

节点没有数据

tensorflow.python.framework.errors_impl.InvalidArgumentError: Retval[2] does not have value.

通过TensorFlow Retval[0] does not have value启发,怀疑是通过tf.cond构建网络图的时候,一次只走一个分支,导致另一个分支图没有数据,把tf.cond替换后,果然没有错误了。

图过大

GraphDef不能大于2GB
这个错误很多人指向重复的定义tf.constant导致的,我遇到该错误,本质原因也是这个,我使用tf.data.Dataset.from_tensor_slices,该对象也会将数据存到图上,导致图的size过大。

维度不同

Invalid argument: Incompatible shapes: [2560] vs. [2540]

这个问题很烦人,引起问题的原因可能有很多,输入的维度不一致,后续处理不当导致的维度不一致,都有可能,可以结合batch size猜测下问题原因。

模型评估

自定义auc计算

TypeError: Values of eval_metric_ops must be (metric_value, update_op) tuples

这个问题来自我自定义了auc的计算函数,需要添加一个update_op,参考评估过程中实验者的张量流混淆矩阵修改即可。

predictions must be in [0, 1]

tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [predictions must be in [0, 1]] [Condition x <= y did not hold element-wise:] [x (Sigmoid:0) = ] [[nan][nan][nan]...] [y (head/auc/Cast_1/x:0) = ] [1]
很可能是loss出现NAN导致更新参数后,计算出不合理的预测值。

tensorflow训练模型出现nan

数据输入

tf.decode_csv

Unquoted fields cannot have quotes/CRLFs inside

这个错误是tf在解析数据时node DecodeCSV字段中包含双引号导致的,可以设置decode_csv的参数use_quote_delim=False来解决。
tf.decode_csv() error: “Unquoted fields cannot have quotes/CRLFs inside”

feed dict

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a bool into a Tensor.

feed dict的输入不是tensor,而是一个bool类型的变量。

tfserving

模型加载不全

报有些变量没有初始化,这里是因为模型传送没有完成,tfserving就读取,有部分权重没有读取到。这种情况在模型较大传输速度较慢时可能容易出现。

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
	status = StatusCode.FAILED_PRECONDITION
	details = "Attempting to use uninitialized value dnn/dnn_layer_1/batch_normalization/beta/part_0
	 [[{{node dnn/dnn_layer_1/batch_normalization/beta/part_0/read}}]]"
	debug_error_string = "UNKNOWN:Error received from peer ipv4:10.133.13.16:8500 {grpc_message:"Attempting to use uninitialized value dnn/dnn_layer_1/batch_normalization/beta/part_0\n\t [[{{node dnn/dnn_layer_1/batch_normalization/beta/part_0/read}}]]", grpc_status:9, created_time:"2023-07-20T12:31:15.5256888+00:00"}"

模型签名对不上

模型保存时的签名key没有对应上,对应signature_def_map的key,可以采用默认的tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY="serving_default"

Serving signature key \"serving_default\" not found.

op不存在

这个问题有很多情况,我这里是使用阿里DeepRec保存的模型,SparseApplyAdam是DeepRec加速优化的算子,所以我重新用DeepRec提供的tfserving编译解决问题。

Not found: Op type not registered 'SparseApplyAdam' in binary running on

变量参数共享

共享的参数shape不匹配
Tensorflow报错:ValueError: Trying to share variable …, but specified shape … and found shape

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值