[英]Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0'
I'm trying to create a Keras LSTM (Please note that I am new to LSTMs and RNNs in Keras). 我正在尝试创建Keras LSTM(请注意,我是Keras中的LSTM和RNN的新手)。 The neural network is supposed to take an input of 4116 values, and output 4116 values. 该神经网络应该接受4116个值的输入,并输出4116个值。 This is to be done for 288 timesteps. 这将执行288个时间步。 I have 27 such timesteps (I realize this will likely lead to overfitting; I have a larger dataset, but first want to test my code with just 27 training examples). 我有27个这样的时间步长(我意识到这可能会导致过拟合;我有一个更大的数据集,但首先想仅使用27个训练示例来测试我的代码)。
The training data is stored in two numpy arrays x
and y
. 训练数据存储在两个numpy数组x
和y
。 These variables have a shape of (27, 288, 4116)
. 这些变量的形状为(27, 288, 4116)
。
My code: 我的代码:
datapoints = data.get.monthPoints(2, year)
x, y = datapoints[:-1], datapoints[1:]
del datapoints
input_shape = x.shape[1:]
output_shape = y.shape[1:]
checkpoint = ModelCheckpoint('model/files/alpha.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='auto', period=1)
early = EarlyStopping(monitor='val_loss', min_delta=0, patience=1, verbose=1, mode='auto')
model = Sequential()
model.add(LSTM(5488, activation='relu', input_shape=input_shape))
model.add(RepeatVector(output_shape))
model.add(LSTM(5488, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(output_shape)))
model.compile(loss='mse', optimizer='adam')
model.fit(x, y, epochs=100, batch_size=8, callbacks = [checkpoint, early])
When I run the program, I get the following error(s): 运行程序时,出现以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'repeat_vector/stack_1' (op: 'Pack') with input shapes: [], [2], []
and 和
During handling of the above exception, another exception occurred:
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'repeat_vector/stack_1' (op: 'Pack') with input shapes: [], [2], []
I've seen a few other similar questions like this and this , but they haven't offered solutions that fix my problem or the solutions have been unclear. 我还看到了其他类似的问题,例如this和this ,但是他们没有提供解决我的问题的解决方案,或者解决方案不清楚。
I guess my problem has something to do with me structuring the network incorrectly or formatting my data incorrectly. 我猜我的问题与我错误地构建网络或错误格式化数据有关。
Any insight would me much appreciated. 任何见解,我将不胜感激。
Thank you. 谢谢。
You probably want to repeat the output of first LSTM layer as much as the number of timesteps in model's output sequence (ie y
). 您可能希望重复第一LSTM层的输出,其重复次数与模型输出序列中的时间步长相同(即y
)。 Therefore, it should be: 因此,应为:
model.add(RepeatVector(y.shape[1]))
There are two problems in your code. 您的代码中有两个问题。 First, in RepeatVector
you are sending a list by passing y.shape[1:]. 首先,在RepeatVector
您要通过传递y.shape [1:]发送一个列表。 In RepeatVector
you should be sending an integer. 在RepeatVector
您应该发送一个整数。
Second problem is in TimeDistributed
. 第二个问题是在TimeDistributed
。 Send in it the number of times you would like your second dimension to be repeated at. 发送您希望重复第二维的次数。 So your code should be: 因此,您的代码应为:
repeat_vector_units = x.shape[1]
output_units = x.shape[2]
model = Sequential()
model.add(LSTM(5488, activation='relu', input_shape=input_shape))
model.add(RepeatVector(repeat_vector_units)) ### Change here
model.add(LSTM(5488, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(output_units))) #### Change here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.