TensorFlow和单词嵌入:传递字典

时间:2018-05-22 11:04:25

标签: python numpy tensorflow

TensorFlow and word embeddings - TypeError: unhashable type: 'numpy.ndarray'类似,我想将值传递给feed_dict的{​​{1}}部分。当我还使用sess.run()时,为什么我会收到有关不可用类型的相同错误,以及reshape(与http://www.brightideasinanalytics.com/rnn-pretrained-word-vectors/处的代码相比,我没有观察到这些差异)?

flatten
sent_toks = nltk.sent_tokenize(ctxt)
x2 = np.array(list(vocab_processor.transform(sent_toks)))
y = np.array(list(vocab_processor.transform(<some other string>)))

import tensorflow.contrib as ct

def NHIDDEN():
    return 1

def NINPUT():
    return 50

g = tf.Graph()
tf.reset_default_graph()

with g.as_default():
    with tf.Session(graph = g) as sess:
        while step < 1: # training_iters:
            x2 = np.reshape(np.array(x2.flatten()), [-1, NINPUT()])
            y_embedding = np.reshape(tf.nn.embedding_lookup(W, y), [1,-1])
            _,loss, pred_ = sess.run([optimizer, cost, pred], feed_dict =
                                 {x2: x2, y: y_embedding})
            loss_total += loss
            print("loss = " + "{:.6f}".format(loss_total))
            step += 1
        print ("Finished Optimization")

1 个答案:

答案 0 :(得分:0)

我没有尝试理解您的代码的详细信息,但确实看到我对您使用feed-dict的问题。 feed_dict的语法是键是tf图中的张量 - 通常是tf.placeholder,而值是您要提供给它的数据。这里x2既用作键又用作值,这可能是问题所在。因此,如果tf_node是您要提供数据的位置,则应使用feed_dict = {tf_node: x2}而不是feed_dict = {x2: x2}

希望这有帮助。