我正在学习了解如何将卷积神经网络与一维卷积一起使用:
这是一个家庭作业示例:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.utils import np_utils
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
epochs=20
batch_size=50
num_classes=20
x_train = np.random.rand(60000,400)
x_val = np.random.rand(10000,400)
y_tain = np.eye(20)[np.random.choice(5, 60000)]
y_val = np.eye(20)[np.random.choice(5, 10000)]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(400,)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(20, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_tain,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_val, y_val))
score = model.evaluate(x_val, y_val, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
当我尝试运行它时,出现了一些错误:
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=2
如何编译 ?
答案 0 :(得分:0)
仔细查看错误消息:您的输入是二维的,而卷积层则需要三个维度...