TensorFlow和单词嵌入-TypeError:不可哈希类型:'numpy.ndarray' [英] TensorFlow and word embeddings - TypeError: unhashable type: 'numpy.ndarray'
问题描述
我希望在 http://www.brightideasinanalytics.com上修改代码/rnn-pretrained-word-vectors/(用于预测下一个单词)具有可预测问题答案的代码.
这是我遇到问题的代码的摘录:
import tensorflow.contrib as ct
def NHIDDEN():
return 1
g = tf.Graph()
tf.reset_default_graph()
with g.as_default():
# lines 97-104 of original code
# RNN output node weights and biases
weights = { 'out': tf.Variable(tf.random_normal([NHIDDEN(), embedding_dim])) }
biases = { 'out': tf.Variable(tf.random_normal([embedding_dim])) }
with tf.name_scope("embedding"):
W = tf.Variable(tf.constant(0.0, shape=[vocab_size, embedding_dim]),
trainable=False, name="W")
embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, embedding_dim])
embedding_init = W.assign(embedding_placeholder)
preimage = tf.nn.embedding_lookup(W, x2)
# lines 107-119 of original
# reshape input data
x_unstack = tf.unstack(preimage)
# create RNN cells
rnn_cell = ct.rnn.MultiRNNCell([ct.rnn.BasicLSTMCell(NHIDDEN()), ct.rnn.BasicLSTMCell(NHIDDEN())])
outputs, states = ct.rnn.static_rnn(rnn_cell, x_unstack, dtype=tf.float32)
# capture only the last output
pred = tf.matmul(outputs[-1], weights['out']) + biases['out']
# Create loss function and optimizer
cost = tf.reduce_mean(tf.nn.l2_loss(pred-y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
# lines 130, 134 and 135 of original
step = 0
acc_total = 0
loss_total = 0
with tf.Session(graph = g) as sess:
# lines 138, 160, 162, 175, 178 and 182 of original
while step < 1: # training_iters:
_,loss, pred_ = sess.run([optimizer, cost, pred], feed_dict =
{x: tf.nn.embedding_lookup(W, x2), y: tf.nn.embedding_lookup(W, y)})
loss_total += loss
print("loss = " + "{:.6f}".format(loss_total))
step += 1
print ("Finished Optimization")
我得到的错误是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-7a72d8d4f100> in <module>()
42 while step < 1: # training_iters:
43 _,loss, pred_ = sess.run([optimizer, cost, pred], feed_dict =
---> 44 {x: tf.nn.embedding_lookup(W, x2), y: tf.nn.embedding_lookup(W, y)})
45 loss_total += loss
46 print("loss = " + "{:.6f}".format(loss_total))
TypeError: unhashable type: 'numpy.ndarray'
如何修复代码?是因为unstack
ing吗?
其他上下文:为x2
和y
分配了np.array(list(vocab_processor.transform([s])))
的返回值,其中s
是字符串(通过传递不同的字符串).使用解决方案
在这里出现问题:y: tf.nn.embedding_lookup(W, y)
. feed_dict
键应该是TensorFlow图中的占位符.假设y
是包含目标值的numpy.ndarray
,则可以定义 tf .placeholder y_
将目标值输入网络,将feed_dict
的相应条目更改为y_: tf.nn.embedding_lookup(W, y)
并相应地修改其他张量(即,使用张量y_
计算损耗). /p>
I wish to modify the code at http://www.brightideasinanalytics.com/rnn-pretrained-word-vectors/, which is about predicting the next word, to have code that predicts answers to questions.
Here is an excerpt of the code I'm having trouble with:
import tensorflow.contrib as ct
def NHIDDEN():
return 1
g = tf.Graph()
tf.reset_default_graph()
with g.as_default():
# lines 97-104 of original code
# RNN output node weights and biases
weights = { 'out': tf.Variable(tf.random_normal([NHIDDEN(), embedding_dim])) }
biases = { 'out': tf.Variable(tf.random_normal([embedding_dim])) }
with tf.name_scope("embedding"):
W = tf.Variable(tf.constant(0.0, shape=[vocab_size, embedding_dim]),
trainable=False, name="W")
embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, embedding_dim])
embedding_init = W.assign(embedding_placeholder)
preimage = tf.nn.embedding_lookup(W, x2)
# lines 107-119 of original
# reshape input data
x_unstack = tf.unstack(preimage)
# create RNN cells
rnn_cell = ct.rnn.MultiRNNCell([ct.rnn.BasicLSTMCell(NHIDDEN()), ct.rnn.BasicLSTMCell(NHIDDEN())])
outputs, states = ct.rnn.static_rnn(rnn_cell, x_unstack, dtype=tf.float32)
# capture only the last output
pred = tf.matmul(outputs[-1], weights['out']) + biases['out']
# Create loss function and optimizer
cost = tf.reduce_mean(tf.nn.l2_loss(pred-y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
# lines 130, 134 and 135 of original
step = 0
acc_total = 0
loss_total = 0
with tf.Session(graph = g) as sess:
# lines 138, 160, 162, 175, 178 and 182 of original
while step < 1: # training_iters:
_,loss, pred_ = sess.run([optimizer, cost, pred], feed_dict =
{x: tf.nn.embedding_lookup(W, x2), y: tf.nn.embedding_lookup(W, y)})
loss_total += loss
print("loss = " + "{:.6f}".format(loss_total))
step += 1
print ("Finished Optimization")
The error I get is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-7a72d8d4f100> in <module>()
42 while step < 1: # training_iters:
43 _,loss, pred_ = sess.run([optimizer, cost, pred], feed_dict =
---> 44 {x: tf.nn.embedding_lookup(W, x2), y: tf.nn.embedding_lookup(W, y)})
45 loss_total += loss
46 print("loss = " + "{:.6f}".format(loss_total))
TypeError: unhashable type: 'numpy.ndarray'
How do I fix the code? Is it because of unstack
ing?
Additional context: x2
and y
are assigned the return value of np.array(list(vocab_processor.transform([s])))
where s
is a string (by passing different strings). embedding_dim
, vocab_size
and W
are computed using the code at https://ireneli.eu/2017/01/17/tensorflow-07-word-embeddings-2-loading-pre-trained-vectors/.
The problem occurs here: y: tf.nn.embedding_lookup(W, y)
. The feed_dict
keys should be placeholders from the TensorFlow graph. Assuming that y
is a numpy.ndarray
containing the target values, you could define a tf.placeholder y_
to feed the target values into the network, changing the corresponding entry of feed_dict
to y_: tf.nn.embedding_lookup(W, y)
and modifying the other tensors accordingly (i.e. use tensor y_
to compute the loss).
这篇关于TensorFlow和单词嵌入-TypeError:不可哈希类型:'numpy.ndarray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!