Python 使用tensorflow检查点进行推理
我正在将字符(Python 使用tensorflow检查点进行推理,python,tensorflow,machine-learning,nlp,inference,Python,Tensorflow,Machine Learning,Nlp,Inference,我正在将字符(x_train)输入到的示例13中定义的RNN模型。下面是对应于模型定义、输入预处理和训练的代码 def char_rnn_model(features, target): """Character level recurrent neural network model to predict classes.""" target = tf.one_hot(target, 15, 1, 0) #byte_list = tf.one_hot(features,
x_train
)输入到的示例13中定义的RNN模型。下面是对应于模型定义、输入预处理和训练的代码
def char_rnn_model(features, target):
"""Character level recurrent neural network model to predict classes."""
target = tf.one_hot(target, 15, 1, 0)
#byte_list = tf.one_hot(features, 256, 1, 0)
byte_list = tf.cast(tf.one_hot(features, 256, 1, 0), dtype=tf.float32)
byte_list = tf.unstack(byte_list, axis=1)
cell = tf.contrib.rnn.GRUCell(HIDDEN_SIZE)
_, encoding = tf.contrib.rnn.static_rnn(cell, byte_list, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(encoding, 15, activation_fn=None)
#loss = tf.contrib.losses.softmax_cross_entropy(logits, target)
loss = tf.contrib.losses.softmax_cross_entropy(logits=logits, onehot_labels=target)
train_op = tf.contrib.layers.optimize_loss(
loss,
tf.contrib.framework.get_global_step(),
optimizer='Adam',
learning_rate=0.001)
return ({
'class': tf.argmax(logits, 1),
'prob': tf.nn.softmax(logits)
}, loss, train_op)
# pre-process
char_processor = learn.preprocessing.ByteProcessor(MAX_DOCUMENT_LENGTH)
x_train = np.array(list(char_processor.fit_transform(x_train)))
x_test = np.array(list(char_processor.transform(x_test)))
# train
model_dir = "model"
classifier = learn.Estimator(model_fn=char_rnn_model,model_dir=model_dir)
count=0
n_epoch = 20
while count<n_epoch:
print("\nEPOCH " + str(count))
classifier.fit(x_train, y_train, steps=1000,batch_size=10)
y_predicted = [
p['class'] for p in classifier.predict(
x_test, as_iterable=True,batch_size=10)
]
score = metrics.accuracy_score(y_test, y_predicted)
print('Accuracy: {0:f}'.format(score))
count+=1
print(metrics.classification_report(y_test, predicted))
其中meta_file
是指向model.ckpt-?????????meta文件之一的路径
我想将经过训练的模型应用于新的角色序列。所以我打了:
new_input = ["Some Sequence of character"]
new_input_processed = np.array(list(char_processor.transform(new_input)))
output = sess.run(new_input_processed)
但我得到了以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-982f2b9b18b3> in <module>()
----> 1 output = sess.run(new_input_processed)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
898 try:
899 result = self._run(None, fetches, feed_dict, options_ptr,
--> 900 run_metadata_ptr)
901 if run_metadata:
902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
1118 # Create a fetch handler to take care of the structure of fetches.
1119 fetch_handler = _FetchHandler(
-> 1120 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1121
1122 # Run request and get response.
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, graph, fetches, feeds, feed_handles)
425 """
426 with graph.as_default():
--> 427 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
428 self._fetches = []
429 self._targets = []
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
251 if isinstance(fetch, tensor_type):
252 fetches, contraction_fn = fetch_fn(fetch)
--> 253 return _ElementFetchMapper(fetches, contraction_fn)
254 # Did not find anything.
255 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, fetches, contraction_fn)
284 raise TypeError('Fetch argument %r has invalid type %r, '
285 'must be a string or Tensor. (%s)' %
--> 286 (fetch, type(fetch), str(e)))
287 except ValueError as e:
288 raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) has invalid type <type 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1输出=sess.run(新输入已处理)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self、fetches、feed_dict、options、run_元数据)
898试试:
899结果=self.\u运行(无、取数、输入、选项、,
-->900运行(元数据(ptr)
901如果运行\u元数据:
902 proto_data=tf_session.tf_GetBuffer(run_metadata_ptr)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in\u run(self、handle、fetches、feed\u dict、options、run\u元数据)
1118#创建一个获取处理程序来处理获取的结构。
1119 fetch\u handler=\u FetchHandler(
->1120 self.\u图形、获取、馈送\u dict\u张量、馈送\u句柄=馈送\u句柄)
1121
1122#运行请求并获取响应。
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in_u__________(self、graph、fetches、feed、feed_句柄)
425 """
426与graph.as_default()一样:
-->427 self.\u fetch\u mapper=\u FetchMapper.for\u fetch(fetches)
428 self._fetches=[]
429自我目标=[]
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
251如果存在(提取,张量类型):
252取数,收缩=fetch(取数)
-->253返回元素fetchmapper(获取、收缩)
254#什么也没找到。
255 raise TypeError('提取参数%r具有无效的类型%r'(提取,
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in_u_初始化(self、fetches、construction)
284 raise TypeError('获取参数%r的类型%r无效,'
285'必须是字符串或张量。(%s)'
-->286(fetch,type(fetch),str(e)))
287除e值错误外:
288 raise VALUERROR('无法将提取参数%r解释为'
TypeError:获取参数数组([[83,111,109,101,32,83,101,113,117,101,110,99,101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,0,0,0,0,0,0,0]],dtype=uint8)的类型无效,必须是字符串或张量。(无法将数据数组转换为张量或操作。)
我使用的是Tensorflow 1.8.0和python 2.7.14
===================
编辑
===================
可能应该使用()函数export\u saved model
,但我不理解它的所有参数
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-982f2b9b18b3> in <module>()
----> 1 output = sess.run(new_input_processed)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
898 try:
899 result = self._run(None, fetches, feed_dict, options_ptr,
--> 900 run_metadata_ptr)
901 if run_metadata:
902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
1118 # Create a fetch handler to take care of the structure of fetches.
1119 fetch_handler = _FetchHandler(
-> 1120 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1121
1122 # Run request and get response.
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, graph, fetches, feeds, feed_handles)
425 """
426 with graph.as_default():
--> 427 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
428 self._fetches = []
429 self._targets = []
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
251 if isinstance(fetch, tensor_type):
252 fetches, contraction_fn = fetch_fn(fetch)
--> 253 return _ElementFetchMapper(fetches, contraction_fn)
254 # Did not find anything.
255 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
/home/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, fetches, contraction_fn)
284 raise TypeError('Fetch argument %r has invalid type %r, '
285 'must be a string or Tensor. (%s)' %
--> 286 (fetch, type(fetch), str(e)))
287 except ValueError as e:
288 raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) has invalid type <type 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)