加载模型报错 The name '' looks like an (invalid) Operation name, not a Tensor.

在尝试加载已导出的模型时遇到错误,原因是模型中使用了未命名的稀疏向量。解决方案包括:一是将稀疏向量分解为三部分并分别存储,确保每个部分都有正确名称;二是改用普通向量代替稀疏向量,避免出现命名问题。通过这两种方法可以解决加载模型时报的'The name '' looks like an (invalid) Operation name, not a Tensor.'错误。" 112123322,10539645,C++字符转二进制与二进制加壳技术解析,"['二进制安全', '程序保护', '加壳技术', 'C++', '逆向工程']

使用estimator.export_saved_model('saved_model', serving_input_receiver_fn)导出模型之后,再使用tf.contrib.predictor.from_saved_model加载模型报错:ValueError: The name '' looks like an (invalid) Operation name, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".

报错详细内容:

INFO:tensorflow:Restoring parameters from saved_model/1577241037/variables/variables
Traceback (most recent call last):
  File "serve.py", line 241, in <module>
    predict_fn = tf.contrib.predictor.from_saved_model(latest)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/predictor/predictor_factories.py", line 153, in from_saved_model
    config=config)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/predictor/saved_model_predictor.py", line 162, in __init__
    for k, v in input_names.items()}
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/predictor/saved_model_predictor.py", line 162, in <dictcomp>
    for k, v in input_names.items()}
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3666, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3490, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3564, in _as_graph_element_locked
    raise ValueError(err_msg)
ValueError: The name '' looks like an (invalid) Operation name, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".

解决方法一

查了一下网上资料,是serving_input_receiver_fnl里面定义输入数据格式时,使用了稀疏向量的占位符,而稀疏向量存储到模型里没有存储名字,导致重新加载模型会报错。

打印出了报错的数据的内容,可以看到 label_ids 对应的名字是空的,而label_ids在我的代码中是稀疏向量。其他普通向量的名字都存在。

INFO:tensorflow:Restoring parameters from saved_model\1577241037\variables\variables

{('segment_ids', 'segment_ids:0'), ('input_ids', 'input_ids:0'), ('label_ids', ''), ('input_mask', 'input_mask:0')}
{('probabilities', 'loss/Sigmoid:0')}
names

Traceback (most recent call last):
  File ".\serve.py", line 246, in <module>
    predict_fn = tf.contrib.predictor.from_saved_model(latest)
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\predictor\predictor_factories.py", line 153, in from_saved_model    
    config=config)
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\predictor\saved_model_predictor.py", line 165, in __init__
    for k, v in input_names.items()}
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\predictor\saved_model_predictor.py", line 165, in <dictcomp>        
    for k, v in input_names.items()}
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3654, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3478, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "C:\Users\shaw\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3552, in _as_graph_element_locked
    raise ValueError(err_msg)
ValueError: The name '' looks like an (invalid) Operation name, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".

可以参考链接内容解决,大致是把一个稀疏向量分解成三部分分别存储:

[Feature Request]:Assign the name to SaprseTensor when build_tensor_info of it #22396
The problem can be solved by exporting the three dense tensor of the Sparse Tensor instead of exporting the Sparse Tensor itself, e.g.:
Change

sparse_output_tensor_info=tf.saved_model.utils.build_tensor_info(predict)
outputs = {‘output’:sparse_output_tensor_info}

to

indices_output_tensor_info = tf.saved_model.utils.build_tensor_info(predict.indices)
values_output_tensor_info = tf.saved_model.utils.build_tensor_info(predict.values)
dense_shape_output_tensor_info = tf.saved_model.utils.build_tensor_info(predict.dense_shape)
outputs = {‘indices’:indices_output_tensor_info,‘values’:values_output_tensor_info,‘dense_shape’:dense_shape_output_tensor_info}

Well I guess it could be an easy fix by redirecting the correct name of SparseTensor when predicting, but I am not sure.

解决方法二

我的粗暴的解决方法是,不使用稀疏向量占位符tf.sparse_placeholder,使用普通tf.placeholder。也不再报错。

def serving_input_receiver_fn():
    """Serving input_fn that builds features from placeholders
    Returns
    -------
    tf.estimator.export.ServingInputReceiver
    """
    input_ids = tf.placeholder(dtype=tf.int32, shape=[None,FLAGS.max_seq_length], name='input_ids')
    input_mask = tf.placeholder(dtype=tf.int32, shape=[None,FLAGS.max_seq_length], name='input_mask')
    segment_ids = tf.placeholder(dtype=tf.int32, shape=[None,FLAGS.max_seq_length], name='segment_ids')
    #label_ids = tf.sparse_placeholder(dtype=tf.int32, shape=[None,None], name='label_ids') # 不用这个占位符了
    
    receiver_tensors = {'input_ids': input_ids, 'input_mask': input_mask,
                        'segment_ids': segment_ids} # 注释掉 , 'label_ids': label_ids}
    features = {'input_ids': input_ids, 'input_mask': input_mask,
                'segment_ids': segment_ids}# 注释掉, 'label_ids': label_ids}
    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值