【问题标题】:TensorFlow and word embeddings - TypeError: unhashable type: 'numpy.ndarray'TensorFlow 和词嵌入 - TypeError: unhashable type: 'numpy.ndarray'
【发布时间】:2018-05-21 12:26:36
【问题描述】:

我希望修改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'

如何修复代码?是因为unstacking吗?

附加上下文:x2y 被分配了np.array(list(vocab_processor.transform([s]))) 的返回值,其中s 是一个字符串(通过传递不同的字符串)。 embedding_dimvocab_sizeW 使用 https://ireneli.eu/2017/01/17/tensorflow-07-word-embeddings-2-loading-pre-trained-vectors/ 处的代码计算。

【问题讨论】:

  • 总是显示带有错误消息的完整回溯——否则很难理解错误消息的来源。
  • @JohnZwinck 对不起,你说得很好。我已经更新了问题。

标签: python numpy tensorflow


【解决方案1】:

问题出现在这里: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_ 来计算损失)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多