keras分类问题,model.fit命令出错
keras classification problem, error in model.fit command
'I want to solve a classification problem by keras.model, but after running model.fit I face to a dimension error. I have run following code:'
print(X_train.shape)
print(y_train.shape)
'output:'
(2588, 39436)
(2588, 6)
model = keras.Sequential(
[
keras.Input(shape=(39436,1)),
layers.Conv1D(32, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Conv1D(64, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
'After running following code, '
model.fit(X_train, y_train, batch_size=128, epochs=15, validation_split=0.3)
'I give this error:'
ValueError:在用户代码中:
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 39436]
'It would be appreciated if you guide me what would be the issue?'
根据错误消息,您的输入数组的形状为 [None, 39436]
。但是,在您的 Input
层中,您传入一个形状 [39436, 1]
,它与 [None, 39436, 1]
匹配,其中 None
代表样本维度。这是抛出的错误。
您需要通过以下任一方式匹配形状:
1.重塑输入数据 以具有 [samples, 39436, 1]
的形状,保持模型架构不变。
可以这样做(假设 train_X 是您的输入特征):
train_X = np.expand_dims(train_X, axis=2)
np.expand_dims
在数组形状的索引 2 处向数组添加一个新维度。所以在这里它将 [samples, 39436]
重塑为 [samples, 39436, 1]
.
或
2。更改 input_shape
参数 在 Input
层接受形状 [39436,]
,以匹配您的数据。
'I want to solve a classification problem by keras.model, but after running model.fit I face to a dimension error. I have run following code:'
print(X_train.shape)
print(y_train.shape)
'output:'
(2588, 39436)
(2588, 6)
model = keras.Sequential(
[
keras.Input(shape=(39436,1)),
layers.Conv1D(32, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Conv1D(64, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
'After running following code, '
model.fit(X_train, y_train, batch_size=128, epochs=15, validation_split=0.3)
'I give this error:'
ValueError:在用户代码中:
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 39436]
'It would be appreciated if you guide me what would be the issue?'
根据错误消息,您的输入数组的形状为 [None, 39436]
。但是,在您的 Input
层中,您传入一个形状 [39436, 1]
,它与 [None, 39436, 1]
匹配,其中 None
代表样本维度。这是抛出的错误。
您需要通过以下任一方式匹配形状:
1.重塑输入数据 以具有 [samples, 39436, 1]
的形状,保持模型架构不变。
可以这样做(假设 train_X 是您的输入特征):
train_X = np.expand_dims(train_X, axis=2)
np.expand_dims
在数组形状的索引 2 处向数组添加一个新维度。所以在这里它将 [samples, 39436]
重塑为 [samples, 39436, 1]
.
或
2。更改 input_shape
参数 在 Input
层接受形状 [39436,]
,以匹配您的数据。