![](/img/trans.png)
[英]tensorflow TypeError: unhashable type: '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
嗎?
其他上下文: x2
和y
分配了np.array(list(vocab_processor.transform([s])))
的返回值,其中s
是一個字符串(通過傳遞不同的字符串)。 embedding_dim
, vocab_size
和W
是使用https://ireneli.eu/2017/01/17/tensorflow-07-word-embeddings-2-loading-pre-trained-vectors/上的代碼計算的。
問題發生在這里: y: tf.nn.embedding_lookup(W, y)
。 feed_dict
鍵應為TensorFlow圖中的占位符。 假設y
是一個包含目標值的numpy.ndarray
,則可以定義一個tf.placeholder y_
來將目標值饋送到網絡中,將feed_dict
的相應條目feed_dict
為y_: tf.nn.embedding_lookup(W, y)
和相應地修改其他張量(即使用張量y_
計算損耗)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.