### 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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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