张量流中nd-array输入的占位符定义

时间:2017-08-23 13:52:30

标签: python machine-learning tensorflow lstm recurrent-neural-network

我试图根据本指南构建LSTM RNN: http://monik.in/a-noobs-guide-to-implementing-rnn-lstm-using-tensorflow/ 我的输入是ndarray,大小为89102 * 39(89102行,39个功能)。数据有3个标签 - 0,1,2 我似乎对占位符定义有问题,但我不确定它是什么。

我的代码是:

    data = tf.placeholder(tf.float32, [None, 1000, 39])
    target = tf.placeholder(tf.float32, [None, 3])
    cell = tf.nn.rnn_cell.LSTMCell(self.num_hidden)

    val, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)
    val = tf.transpose(val, [1, 0, 2])
    last = tf.gather(val, int(val.get_shape()[0]) - 1)


    weight = tf.Variable(tf.truncated_normal([self.num_hidden, int(target.get_shape()[1])]))
    bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]))

    prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)

    cross_entropy = -tf.reduce_sum(target * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0)))

    optimizer = tf.train.AdamOptimizer()
    minimize = optimizer.minimize(cross_entropy)

    mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
    error = tf.reduce_mean(tf.cast(mistakes, tf.float32))


    init_op = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init_op)
    batch_size = 1000
    no_of_batches = int(len(train_input) / batch_size)
    epoch = 5000
    for i in range(epoch):
        ptr = 0
        for j in range(no_of_batches):
            inp, out = train_input[ptr:ptr + batch_size], train_output[ptr:ptr + batch_size]
            ptr += batch_size
            sess.run(minimize, {data: inp, target: out})
        print( "Epoch - ", str(i))

我接下来的错误:

File , line 133, in execute_graph
sess.run(minimize, {data: inp, target: out})

  File "/usr/local/lib/python3.5/dist-
packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)

  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 975, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))

ValueError: Cannot feed value of shape (1000, 39) for Tensor 'Placeholder:0', which has shape '(1000, 89102, 39)'

知道可能导致问题的原因是什么?

1 个答案:

答案 0 :(得分:2)

如上所述heredynamic_rnn函数采用形状

的批量输入

[batch_size, truncated_backprop_length, input_size]

在您提供的链接中,占位符的形状为

data = tf.placeholder(tf.float32, [None, 20,1]) 

这意味着他们选择了truncated_backprop_length=20input_size=1 他们的数据是以下3D数组:

[
 array([[0],[0],[1],[0],[0],[1],[0],[1],[1],[0],[0],[0],[1],[1],[1],[1],[1],[1],[0],[0]]), 
 array([[1],[1],[0],[0],[0],[0],[1],[1],[1],[1],[1],[0],[0],[1],[0],[0],[0],[1],[0],[1]]), 
 .....
]

根据您的代码,train_input似乎是2D数组,而不是3D数组。因此,您需要将其转换为3D数组。为此,您需要确定要用于truncated_backprop_lengthinput_size的参数。之后,您需要定义 data恰当。

例如,如果您希望truncated_backprop_lengthinput_size分别为39和1,则可以

import numpy as np
train_input=np.reshape(train_input,(len(train_input),39,1))
data = tf.placeholder(tf.float32, [None, 39,1]) 

我根据上面的讨论改变了你的代码,并在我生成的一些随机数据上运行它。它运行时没有抛出错误。请参阅以下代码:

import tensorflow as tf
import numpy as np
num_hidden=5
train_input=np.random.rand(89102,39)
train_input=np.reshape(train_input,(len(train_input),39,1))
train_output=np.random.rand(89102,3)

data = tf.placeholder(tf.float32, [None, 39, 1])
target = tf.placeholder(tf.float32, [None, 3])
cell = tf.nn.rnn_cell.LSTMCell(num_hidden)

val, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)


weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])]))
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]))

prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)

cross_entropy = -tf.reduce_sum(target * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0)))

optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(cross_entropy)

mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
error = tf.reduce_mean(tf.cast(mistakes, tf.float32))


init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
batch_size = 1000
no_of_batches = int(len(train_input) / batch_size)
epoch = 5000
for i in range(epoch):
    ptr = 0
    for j in range(no_of_batches):
        inp, out = train_input[ptr:ptr + batch_size], train_output[ptr:ptr + batch_size]
        ptr += batch_size
        sess.run(minimize, {data: inp, target: out})
    print( "Epoch - ", str(i))