【发布时间】:2022-01-02 14:17:32
【问题描述】:
我正在尝试使用具有xtrain.shape ---> (2040, 2, 5000) 和ytrain.shape ---> (2040,)(真实数据)的 keras 训练数据集。样本小型化xtrain 和ytrain 数据就像
xtrain
array([[[ 2, 2, 7, 1, 5],
[ 1, 2, 3, 4, 3]],
[[ 5, 0, 3, 1, 6],
[ 5, 6, 7, 8, 6]],
[[ 2, 9, 8, 8, 7],
[ 9, 10, 11, 12, 2]],
[[ 5, 7, 7, 6, 8],
[13, 14, 15, 16, 1]]])
ytrain
array([0,1,2,3])
当我尝试使用
构建网络模型时from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam
input_shape = (2,5000)
no_classes = 7
loss_function = sparse_categorical_crossentropy
no_epochs = 100
optimizer = Adam()
validation_split = 0.2
verbosity = 1
# Create the model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(no_classes, activation='softmax'))
# Compile the model
model.compile(loss=loss_function,
optimizer=optimizer,
metrics=['accuracy'])
# Fit data to model
history = model.fit(input_train, target_train,
batch_size=batch_size,
epochs=no_epochs,
verbose=verbosity,
validation_split=validation_split)
出现以下错误
ValueError: Input 0 of layer "conv2d_2" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (None, 2, 5000)
我对在创建网络期间设置输入形状感到困惑。这是由于输入的形状吗?
【问题讨论】:
-
您是否想过 Conv2D 应该如何准确地解释您的数据?为什么它需要 4D 输入?
-
这是否意味着我必须重塑训练数据? xtrain.ndim 给出 3. 所以它是一个 3D 数据。是的,为什么它需要 4D。很奇怪
-
文档清楚地说明了为什么需要 4D 数据:keras.io/api/layers/convolution_layers/convolution2d
标签: python tensorflow keras