一. 背景
今天继续完成“字嵌入+LSTM+CRF”实现分词的大业, 不过一上来又出现了新的问题。
二. 代码
Chars, vectors, labels = read(path)
vectors = vectors.reshape((batch_size, max_seq_len, 128))
labels = labels.reshape((batch_size, max_seq_len))
with tf.Graph().as_default() as g:
inputs = tf.placeholder('float', [batch_size, max_seq_len, 128])
tags = tf.placeholder('float', [batch_size, max_seq_len])
model = Model(inputs, tags)
initer = tf.global_variables_initializer()
with tf.Session(graph=g) as sess:
sess.run(initer)
train_batch_size = batch_size * 0.9
test_batch_size = batch_size * 0.1
# batch_size = train_batch_size
for i in range(1000):
print(type(vectors))
print(type(labels))
loss, accuracy, size, _ = sess.run([model.loss, model.accuracy, model.batch_size, model.optimizer], feed_dict={inputs : inputs, tags : labels})
if i % 100 == 0:
print('loss:', loss, 'accuracy', accuracy, 'size', size)
三. 错误
以上代码报如下错误:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("Placeholder:0", shape=(1000, 10, 128), dtype=float32) which was passed to the feed with key Tensor("Placeholder:0", shape=(1000, 10, 128), dtype=float32).
这是什么鬼,怎么会这样, 明明传进入的是np.ndarray啊。
四. 解决方案
后来仔细看了好久,原来是是这里没有写对:
feed_dict={inputs : inputs, tags : labels}
改成如下就解决问题了:
feed_dict={inputs : vectors, tags : labels}