Tensorflow feed_dict ValueError:使用序列设置数组元素

时间:2019-05-07 12:12:19

标签: python tensorflow

我是Tensorflow的新手,它试图在Twitter嵌入矩阵上运行CNN(每个嵌入矩阵为574x300-字x嵌入长度),一次批处理100条tweets。我在底部的以下行中不断收到错误ValueError: setting an array element with a sequence.sess.run(training_op, feed_dict={input_tweets: x_batch, tweet_labels: y_batch})

filter_size = 2
embedding_size = 300
length_embedding = 575
num_filters = 100
filter_shape = [filter_size, embedding_size, 1, num_filters]
batch_size = 100
n_epochs = 10
n_inputs = length_embedding*embedding_size
n_outputs = 2 #classify between 2 categories
num_train_examples = 2000

with tf.name_scope("inputs"):
    input_tweets = tf.placeholder(tf.float32, shape = [batch_size, length_embedding], name="input_tweets")
    input_tweets_reshaped = tf.expand_dims(input_tweets, -1)
    tweet_labels = tf.placeholder(tf.int32, shape = [batch_size], name="tweet_labels")

W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
conv = tf.nn.conv2d(input_tweets_reshaped, W, 
                       strides = [1,1,1,1], padding="VALID", name="conv")
conv_bias = tf.nn.bias_add(conv, b)

#pooling
sequence_length=input_tweets_reshaped.shape[1]
with tf.name_scope("pool"):
    pool = tf.nn.max_pool(conv, ksize=[1, sequence_length - filter_size + 1, 1, 1],
                         strides=[1,1,1,1],
                         padding="VALID",
                         name="pool")
    pool_flat = tf.reshape(pool, shape=[-1, num_filters])

#fully-connected layer
with tf.name_scope("fc_layer"):
    fc_layer = tf.layers.dense(pool_flat, num_filters, activation=tf.nn.relu, name="fc_layer")

#output
with tf.name_scope("output_layer"):
    logits = tf.layers.dense(fc_layer, n_outputs, name="output_layer")
    Y_proba = tf.nn.softmax(logits, name="Y_proba")

#train
with tf.name_scope("train"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tweet_labels)
    loss=tf.reduce_mean(xentropy)
    optimizer=tf.train.AdamOptimizer()
    training_op=optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct = tf.nn.in_top_k(logits, tweet_labels, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

with tf.name_scope("init_and_save"):
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

#--run model

with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for iteration in range(num_train_examples // batch_size):
            print("iteration: "+str(iteration))
            x_batch = x_train[iteration*batch_size : (iteration+1)*batch_size]
            y_batch = y_train[iteration*batch_size : (iteration+1)*batch_size]

            sess.run(training_op, feed_dict={input_tweets: x_batch, tweet_labels: y_batch})
        acc_train = accuracy.eval(feed_dict={input_tweets: x_batch, tweet_labels: y_batch})
        acc_test = accuracy.eval(feed_dict={input_tweets: x_test, tweet_labels: y_test})
        print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)

x_batch是一个长度为100的numpy数组,每个元素都是尺寸为575 x 300的矩阵(尽管当我调用x_batch.shape时,它返回(100,575))。 y_batch是一个1和0的1d numpy数组; y_batch.shape返回(100,)。我认为问题可能出在输入的维度上,任何人都可以清楚地看到不匹配之处吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

  1. conv2d的输入必须有rank=4,但是您有rank=3
  2. embedding_size(确定过滤器的第二维)必须小于或等于输入张量的第三维。您具有等于1的第三维-扩展尺寸。因此,它不能大于1
  3. 您可以使用tf.layers.conv2d()来自动创建卷积变量。
  4. 也许您打算使用tf.layers.conv1d()作为输入的张量rank=3

我不确定您想用代码实现什么,但这是可以使用的修改后的版本:

import tensorflow as tf

import numpy as np
filter_size = 2
embedding_size = 300
length_embedding = 575
num_filters = 100
filter_shape = [filter_size, 1, 1, num_filters]
batch_size = 100
n_epochs = 10
n_inputs = length_embedding*embedding_size
n_outputs = 2 #classify between 2 categories
num_train_examples = 2000

with tf.name_scope("inputs"):
    input_tweets = tf.placeholder(tf.float32, shape = [None, length_embedding], name="input_tweets")
    input_tweets_reshaped = input_tweets[..., tf.newaxis, tf.newaxis]
    tweet_labels = tf.placeholder(tf.int32, shape = [None], name="tweet_labels")

W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
b = tf.Variable(0.1*tf.ones([num_filters]), name="b")


conv = tf.nn.conv2d(input_tweets_reshaped,
                    W, 
                    strides=[1,1,1,1],
                    padding="VALID",
                    name="conv")
conv_bias = tf.nn.bias_add(conv, b)
#pooling
sequence_length=input_tweets_reshaped.shape[1]
with tf.name_scope("pool"):
    pool = tf.nn.max_pool(conv, ksize=[1, sequence_length - filter_size + 1, 1, 1],
                         strides=[1,1,1,1],
                         padding="VALID",
                         name="pool")
    pool_flat = tf.reshape(pool, shape=[-1, num_filters])
#fully-connected layer
with tf.name_scope("fc_layer"):
    fc_layer = tf.layers.dense(pool_flat, num_filters, activation=tf.nn.relu, name="fc_layer")

#output
with tf.name_scope("output_layer"):
    logits = tf.layers.dense(fc_layer, n_outputs, name="output_layer")
    Y_proba = tf.nn.softmax(logits, name="Y_proba")

#train
with tf.name_scope("train"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tweet_labels)
    loss=tf.reduce_mean(xentropy)
    optimizer=tf.train.AdamOptimizer()
    training_op=optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct = tf.nn.in_top_k(logits, tweet_labels, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

with tf.name_scope("init_and_save"):
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()

x_train = np.random.normal(size=(10*batch_size, length_embedding, ))
y_train = np.random.randint(low=0, high=2, size=10*batch_size)
x_test = x_train
y_test = y_train
with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for iteration in range(num_train_examples // batch_size):
            print("iteration: "+str(iteration))
            x_batch = x_train[iteration*batch_size : (iteration+1)*batch_size]
            y_batch = y_train[iteration*batch_size : (iteration+1)*batch_size]
            sess.run(training_op, feed_dict={input_tweets: x_batch, tweet_labels: y_batch})
        acc_train = accuracy.eval(feed_dict={input_tweets: x_batch, tweet_labels: y_batch})
        acc_test = accuracy.eval(feed_dict={input_tweets: x_test, tweet_labels: y_test})
        print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)