我正在尝试将一维CNN放入我的数据中。数据由MEL频率组成,并具有以下特征:
X_train.shape = (68251, 99)
y_train_hot.shape = (68251, 35)<-- one hot encoding with 35 output classes
当我尝试训练模型时,以下代码出现此值错误:
ValueError: Error when checking input: expected conv1d_5_input to have 3 dimensions, but got array with shape (68251, 99)
#hyperparameters
input_dimension = 68251
learning_rate = 0.0025
momentum = 0.85
hidden_initializer = random_uniform(seed=1)
dropout_rate = 0.2
# create model
model = Sequential()
model.add(Convolution1D(nb_filter=32, filter_length=3, input_shape=X_train.shape, activation='relu'))
model.add(Convolution1D(nb_filter=16, filter_length=1, activation='relu'))
model.add(Flatten())
model.add(Dropout(dropout_rate))
model.add(Dense(128, input_dim=input_dimension, kernel_initializer=hidden_initializer, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(64, kernel_initializer=hidden_initializer, activation='relu'))
model.add(Dense(2, kernel_initializer=hidden_initializer, activation='softmax'))
sgd = SGD(lr=learning_rate, momentum=momentum)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['acc'])
model.fit(X_train, y_train_hot, epochs=5, batch_size=128)
predictions = model.predict_proba(X_test)
ans = pd.DataFrame(predictions)
ans = ans[0]
当我将X_train和X_test分别重塑为(68251,99,1)和(17063,99,1)时,出现以下错误:
ValueError: Input 0 is incompatible with layer conv1d_7: expected ndim=3, found ndim=4
答案 0 :(得分:1)
编辑
这次我实际上编译了您的模型,发现了一些问题并纠正了这些问题:
# create model
model = Sequential()
model.add(Convolution1D(nb_filter=32, filter_length=3, input_shape=(99, 1), activation='relu'))
model.add(Convolution1D(nb_filter=16, filter_length=1, activation='relu'))
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(64, activation='relu'))
model.add(Dense(35, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['acc'])
model.fit(X_train, y_train_hot, epochs=5, batch_size=128)
您的输出为2,应为35,因为您有35个输出类,您的第一个Dense不需要input_dim,因为形状将由上一层推断出来,并且输入形状不正确。
希望这会有所帮助
答案 1 :(得分:0)
尝试更改
model.add(Convolution1D(nb_filter=32, filter_length=3, input_shape=X_train.shape, activation='relu'))
到
model.add(Convolution1D(nb_filter=32, filter_length=3, input_shape=(99,), activation='relu'))
并将X_train
传递给fit
函数而无需重塑