Skip to main content
 首页 » 编程设计

python之Tensorflow feed_dict 值错误 : setting an array element with a sequence

2024年11月24日25hnrainll

我是 tensorflow 的新手,并尝试在 Twitter 嵌入矩阵(每个嵌入矩阵为 574x300 - 单词 x 嵌入长度)上一次运行 100 条推文。我不断收到错误 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. 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)