Tensorflow feed\u dict ValueError:使用序列设置数组元素

2024-04-30 01:35:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我是tensorflow的新手,尝试在Twitter上运行一个CNN嵌入矩阵(每个嵌入矩阵是574x300字x嵌入长度),每次100条tweet。我一直在下面一行得到错误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\u batch是一个长度为100的numpy数组,每个元素都是一个维度为575x300的矩阵(尽管当我调用x时_批处理形状,返回(100575)。y\u batch是1和0的1d numpy数组;y_批处理形状返回(100,)。我认为问题可能出在输入的维度上——有人能清楚地看到不匹配是什么吗?你知道吗

谢谢你!你知道吗


Tags: nameinputsizelabelstfwithbatchtrain
1条回答
网友
1楼 · 发布于 2024-04-30 01:35:49
  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)

相关问题 更多 >