首页 > 解决方案 > reshape 的输入是一个有 4104 个值的张量,但是请求的形状需要 96 的倍数

问题描述

我是 tensorflow 的新手,我想做一些时间序列预测并点击这个链接。我想将 36 小时的数据作为输入,下一个连续的 48 小时作为输出数据,所以我制作了 x 和 y 批次,如下所示:

""" Making our training dataset with batch size of num_period """
x_batches = {'NSW': X_NSW.transpose().reshape(-1,72,1),
             'QLD': X_QLD.transpose().reshape(-1,72,1),
             'SA': X_SA.transpose().reshape(-1,72,1),
             'TAS': X_TAS.transpose().reshape(-1,72,1),
             'VIC': X_VIC.transpose().reshape(-1,72,1)}

y_batches = {'NSW': Y_NSW.transpose().reshape(-1,96,1),
                 'QLD': Y_QLD.transpose().reshape(-1,96,1),
                 'SA': Y_SA.transpose().reshape(-1,96,1),
                 'TAS': Y_TAS.transpose().reshape(-1,96,1),
                 'VIC': Y_VIC.transpose().reshape(-1,96,1)}

然后我使用了上面链接中给出的相同代码(但根据需要进行了修改)并得到以下代码:

inputs = 1  #input vector size
hidden = 100    
output = 1  #output vector size

X = tf.placeholder(tf.float32, [None, 72, inputs])
y = tf.placeholder(tf.float32, [None, 96, output])

basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden, activation=tf.nn.relu)
rnn_output, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)

learning_rate = 0.001   #small learning rate so we don't overshoot the minimum

stacked_rnn_output = tf.reshape(rnn_output, [-1, hidden])           #change the form into a tensor
stacked_outputs = tf.layers.dense(stacked_rnn_output, output)        #specify the type of layer (dense)
outputs = tf.reshape(stacked_outputs, [-1, 96, output])          #shape of results

但是系统向我抛出以下错误

outputs = tf.reshape(stacked_outputs, [-1, 96, output])          #shape of results

...

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 4104 values, but the requested shape requires a multiple of 96
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dense/BiasAdd, Reshape_1/shape)]]

我知道我在以下行中犯了一些错误,但我无法弄清楚。

stacked_rnn_output = tf.reshape(rnn_output, [-1, hidden])           #change the form into a tensor
stacked_outputs = tf.layers.dense(stacked_rnn_output, output)        #specify the type of layer (dense)
outputs = tf.reshape(stacked_outputs, [-1, 96, output])          #shape of results

标签: pythontensorflow

解决方案


问题涉及您如何设置任务。RNN 为任何单个输入输出单个值:如果您的输入序列是 72 步长,那么您的输出序列将是 72 步长,您不能要求将其重新整形为 96 步长的序列。

两种解决方案(但显然这取决于您当前要求算法学习的内容):

  1. 剪切输出:您只对输出序列的前 72 个元素感兴趣,因为您有该大小的输入。
  2. 填充输入:您仍然对 96 个元素的输出感兴趣,但您不知道最后 96 - 72 步的输入。您为未知输入放置一个零填充,并让 RNNCell 的状态做出您的预测。

推荐阅读