keras中的顺序到功能模型转换

时间:2019-05-20 16:12:42

标签: python keras

我已经使用顺序api编写了一个模型,并且工作正常。这是使用顺序API的模型代码。

verbose, epochs, batch_size = 1, 50, 256
samples, n_features, n_outputs = trainX.shape[0], trainX.shape[2], trainy.shape[1]
print('trainX.shape', trainX.shape) # (7000, 200, 5)
print('testX.shape', testX.shape) # (3000, 200, 5)
print('trainy.shape', trainy.shape) # (7000, 4)
print('testy.shape', testy.shape) # (3000, 4)

# reshape into subsequences (samples, time steps, rows, cols, channels)
n_features = trainX.shape[2]
n_steps, n_length = 4, 50
trainX = trainX.reshape((samples, n_steps, 1, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, 1, n_length, n_features))

# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu',return_sequences = True, input_shape=(n_steps, 1, n_length, n_features)))
MaxPooling1D(pool_size=3, strides=None, data_format='channels_last') 
model.add(BatchNormalization())

model.add(ConvLSTM2D(filters=128, kernel_size=(1,3), return_sequences = False, activation='relu'))
MaxPooling1D(pool_size=3, strides=None, data_format='channels_last') 
model.add(BatchNormalization())

model.add(Flatten())
model.add(Dense(100, activation='relu', kernel_regularizer=regularizers.l2(l=0.01)))                
kernel_regularizer = regularizer_l2(l = 0.001)) 
model.add(Dropout(0.5))
model.add(Dense(n_outputs, activation='softmax'))   

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=epochs, batch_size=batch_size, verbose=verbose)

# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
y = model.predict(testX)

我有兴趣使用Functional API实现相同的模型。我为此编写了以下代码。

input_shape = (n_steps, 1, n_length, n_features)
model_input = Input(shape=input_shape)

clstm1 = ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu',return_sequences = True)(model_input)
clstm1 = MaxPooling1D(pool_size=3, strides=None, data_format='channels_last')(clstm1)
clstm1 = BatchNormalization()(clstm1)

clstm2 = ConvLSTM2D(filters=128, kernel_size=(1,3), activation='relu',return_sequences = False)(clstm1)
clstm2 = MaxPooling1D(pool_size=3, strides=None, data_format='channels_last')(clstm2)
conv_output = BatchNormalization()(clstm2)

flatten = Flatten(conv_output)

dense = Dense(100, activation='relu', kernel_regularizer=regularizers.l2(l=0.01))(flatten)
dense = Dropout(0.5)(dense)
output = Dense(n_outputs, activation='softmax')(dense)

model = Model(inputs=model_input, outputs=output)

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
y = model.predict(testX)

但功能性API模型给出以下错误。你能指出我错了吗?

ValueError: Input 0 is incompatible with layer max_pooling1d_28: expected ndim=3, found ndim=5

0 个答案:

没有答案