InvalidArgumentError:不兼容的形状:[32,50,1]与[32,1]
[[{{节点 training_7 / Adam / gradients / loss_8 / time_distributed_5_loss / mean_squared_error / SquaredDifference_grad / BroadcastGradientArgs}}]] [Op:__ inference_keras_scratch_graph_53470]
我正在用喀拉拉邦练习。 我将最后一层从Dense更改为TimeDsitributed(Dense)
后,遇到了此错误这是我的代码:
def generate_time_series(batch_size, n_steps):
freq1, freq2, offsets1, offsets2 = np.random.rand(4, batch_size, 1)
time = np.linspace(0, 1, n_steps)
series = 0.5 * np.sin((time - offsets1) * (freq1 * 10 + 10)) # wave 1
series += 0.2 * np.sin((time - offsets2) * (freq2 * 20 + 20)) # + wave 2
series += 0.1 * (np.random.rand(batch_size, n_steps) - 0.5) # + noise
return series[..., np.newaxis].astype(np.float32)
n_steps = 50
series = generate_time_series(10000, n_steps + 1)
X_train, y_train = series[:7000, :n_steps], series[:7000, -1]
X_valid, y_valid = series[7000:9000, :n_steps], series[7000:9000, -1]
X_test, y_test = series[9000:, :n_steps], series[9000:, -1]
model_0 = keras.models.Sequential([
keras.layers.SimpleRNN(20,return_sequences=True,input_shape=[None,1]),
keras.layers.SimpleRNN(20,return_sequences=True),
keras.layers.TimeDistributed(keras.layers.Dense(1))
])
model_0.compile(loss="mse",optimizer="adam")
history_0 = model_0.fit(X_train, y_train, epochs=20,
validation_data=(X_valid, y_valid))