简体   繁体   English

层顺序的输入 0 与层不兼容:

[英]Input 0 of layer sequential is incompatible with the layer:

I'm trying to predict a basic stock prices model.我正在尝试预测一个基本的股票价格模型。 Here is my code这是我的代码

data = pd.read_csv("total_cases.csv")
x = data["date"]
world_cases = data["Turkey"].fillna(0)
time = np.arange(len(world_cases), dtype="float32")

split_time = 200
x_train = time[:split_time]
x_valid = time[split_time:]

y_train = world_cases[:split_time]
y_valid = world_cases[split_time:]
window_size = 20
batch_size = 32
shuffle_buffer_size=1000
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
valid_data = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))
model = Sequential()
model.add(LSTM(16, return_sequences=True))

model.add(LSTM(16))

model.add(Dense(16, activation='relu'))
model.compile(optimizer='adam', loss='mae', metrics=['mae'])

r = model.fit(train_data, validation_data=valid_data, epochs=100)

When the model is run, the error was raised运行模型时,出现错误

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=0. Full shape received: []

EDIT Here is a part of csv file, world_cases -Turkey- column编辑这是 csv 文件的一部分,world_cases -Turkey- 列

0           0.0
1           0.0
2           0.0
3           0.0
4           0.0
         ...
258    291162.0
259    292878.0
260    294620.0
261    296391.0
262    298039.0

I reproduced your issue.我转载了你的问题。

import tensorflow as tf
inputs = tf.random.normal([])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

Output输出

ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=0. Full shape received: ()

Issue is with input data shape问题在于输入数据形状

Since Tensorflow.Keras LSTM expects input of shape 3D.由于 Tensorflow.Keras LSTM 需要形状 3D 的输入。 Modify your input as per this按照此修改您的输入

inputs: A 3D tensor with shape [batch, timesteps, feature] 
model.add(LSTM(16,return_sequences=False)).

Working sample code工作示例代码

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

Output输出

(32, 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 层序的输入0与层不兼容 - Input 0 of layer sequential is incompatible with the layer ValueError: 层序列 1 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_1 is incompatible with the layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer LSTM:层序的输入0与层不兼容 - LSTM: Input 0 of layer sequential is incompatible with the layer ValueError: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer Keras:层顺序的输入 0 与层不兼容 - Keras: Input 0 of layer sequential is incompatible with the layer "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: 构建简单的神经网络:ValueError: Input 0 of layer sequence is in compatible with the layer - Building a simple Neural Network: ValueError: Input 0 of layer sequential is incompatible with the layer 无法解决 ValueError: 层序贯_1 的输入 0 与层不兼容 - Cannot solve ValueError: Input 0 of layer sequential_1 is incompatible with the layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM