Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0'
Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0'
我正在尝试创建一个 Keras LSTM(请注意,我是 Keras 中的 LSTM 和 RNN 的新手)。神经网络应该接受 4116 个值的输入,并输出 4116 个值。这是为 288 个时间步完成的。我有 27 个这样的时间步长(我意识到这可能会导致过度拟合;我有一个更大的数据集,但首先想用 27 个训练示例测试我的代码)。
训练数据存储在两个numpy数组x
和y
中。这些变量的形状为 (27, 288, 4116)
.
我的代码:
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])
当我运行程序时,出现以下错误:
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], []
和
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], []
我看到了其他一些类似的问题,例如 this and this,但他们没有提供解决我的问题的解决方案,或者解决方案不清楚。
我想我的问题与我错误地构建网络或错误地格式化我的数据有关。
如有任何见解,我将不胜感激。
谢谢。
您可能希望重复第一个 LSTM 层的输出,重复次数与模型输出序列中的时间步数相同(即 y
)。因此,应该是:
model.add(RepeatVector(y.shape[1]))
您的代码中有两个问题。首先,在 RepeatVector
中,您通过传递 y.shape[1:] 来发送列表。在 RepeatVector
你应该发送一个整数。
第二个问题在TimeDistributed
。将您希望重复第二维度的次数发送给它。
所以你的代码应该是:
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
我正在尝试创建一个 Keras LSTM(请注意,我是 Keras 中的 LSTM 和 RNN 的新手)。神经网络应该接受 4116 个值的输入,并输出 4116 个值。这是为 288 个时间步完成的。我有 27 个这样的时间步长(我意识到这可能会导致过度拟合;我有一个更大的数据集,但首先想用 27 个训练示例测试我的代码)。
训练数据存储在两个numpy数组x
和y
中。这些变量的形状为 (27, 288, 4116)
.
我的代码:
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])
当我运行程序时,出现以下错误:
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], []
和
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], []
我看到了其他一些类似的问题,例如 this and this,但他们没有提供解决我的问题的解决方案,或者解决方案不清楚。
我想我的问题与我错误地构建网络或错误地格式化我的数据有关。
如有任何见解,我将不胜感激。
谢谢。
您可能希望重复第一个 LSTM 层的输出,重复次数与模型输出序列中的时间步数相同(即 y
)。因此,应该是:
model.add(RepeatVector(y.shape[1]))
您的代码中有两个问题。首先,在 RepeatVector
中,您通过传递 y.shape[1:] 来发送列表。在 RepeatVector
你应该发送一个整数。
第二个问题在TimeDistributed
。将您希望重复第二维度的次数发送给它。
所以你的代码应该是:
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