tensorflow feed_dict包含数组而不是单个值

时间:2016-10-30 13:40:00

标签: python tensorflow

我目前正在尝试使用tensorflow来训练神经网络以进行回归,这样我就可以将输入映射到输出。输入包括一个已经采样和成帧的音频字段,每个帧都有一定的输出。

存储输入(音频帧)(以实际数据为例):

[array([[frame],[frame],...,[frame]],dtype=float32), ...]

输出存储为:

[array([[  4.53255900e+01,  -9.82247700e+00,  -1.22920200e+00,
         -6.81728800e+00,  -8.23808400e+00,  -1.42367900e+01,
         -4.64904100e+00,  -1.49673100e+01,  -4.75058700e+00,
         -7.54071600e+00,   5.11365500e+00,   9.80618400e+00,
         -6.03554700e-01],....,[...]])]

这是我的代码:

number_of_examples = len(train_data)/2
train_set_data = train_data[:number_of_examples]
train_set_output = train_output_data[:number_of_examples]

test_set_data = train_data[number_of_examples:]
test_set_output = train_output_data[number_of_examples:]
############################# Training setup ##################################
# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
n_samples = train_set_data[0].shape[0]

# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")


# Construct a linear model
pred = tf.add(tf.mul(X, W), b)


# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


# Initializing the variables
init = tf.initialize_all_variables()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for i in range(len(train_set_data)):
            for (x, y) in (train_set_data[i], train_set_output[i]):
                sess.run(optimizer, feed_dict={X: x, Y: y})

            #Display logs per epoch step
            if (epoch+1) % display_step == 0:
                c = sess.run(cost, feed_dict={X: train_set_data, Y:train_set_output})
                print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                    "W=", sess.run(W), "b=", sess.run(b)

        print "Optimization Finished!"
        training_cost = sess.run(cost, feed_dict={X: train_set_data, Y: train_set_output})
        print "Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n'

问题是我收到错误消息

File "tensorflow_datapreprocess_mfcc_extraction_rnn.py", line 191, in <module>
    for (x, y) in (train_set_data[i], train_set_output[i]):
ValueError: too many values to unpack

我不确定我是否理解错误消息,是说我无法通过数组或者我必须传递单个值?

2 个答案:

答案 0 :(得分:0)

for (x, y) in (train_set_data[i], train_set_output[i])

可能没有做你认为它正在做的事情。你的意思是使用zip()吗?

答案 1 :(得分:0)

该脚本没有按照你想要的方式做你想做的事情:

   ...
   for epoch in range(training_epochs):
       for (x, y) in zip(train_set_data, train_set_output):
           sess.run(optimizer, feed_dict={X: x, Y: y})
     ...