### TensorFlow中nd-array输入的占位符定义

我正在尝试根据这个指南构建基于LSTM的RNN:http://monik.in/a-noobs-guide-to-implementing-rnn-lstm-using-tensorflow/我的输入是一个大小为89102*39的ndarray(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_graphsess.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)'

有什么想法可能会导致这个问题吗?


回答:

这里所指出的,dynamic_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 nptrain_input=np.reshape(train_input,(len(train_input),39,1))data = tf.placeholder(tf.float32, [None, 39,1]) 

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注