CNN和RNN组合模型只能预测一类

2024-07-01 08:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我们结合了RNN和CNN模型来预测班级。不幸的是,该模型只能预测一个具有相同值的类。我们已经做了一些平衡。解决办法可能是什么

inputCNN = tf.keras.Input(shape=(28, 28, 1))
CNN = tf.keras.layers.Conv2D(32, (3,3), activation='relu')(inputCNN)
CNN = tf.keras.layers.MaxPooling2D(2,2)(CNN)
CNN = tf.keras.layers.Conv2D(64, (3,3), activation='relu')(CNN)
CNN = tf.keras.layers.MaxPooling2D(2,2)(CNN)
CNN = tf.keras.layers.Conv2D(64, (3,3), activation='relu')(CNN)
CNN = tf.keras.layers.Flatten()(CNN)
CNN = tf.keras.layers.Dense(10, activation='softmax')(CNN)
modelCNN = tf.keras.Model(inputs=inputCNN, outputs=CNN)

inputRNN = tf.keras.Input(shape=(93,13))
RNN = tf.keras.layers.LSTM(128,return_sequences=True)(inputRNN)
RNN = tf.keras.layers.Dropout(0.2)(RNN)
RNN = tf.keras.layers.LSTM(128)(RNN)
RNN = tf.keras.layers.Dropout(0.2)(RNN)
RNN = tf.keras.layers.Dense(10, activation='softmax')(RNN)
modelRNN = tf.keras.Model(inputs=inputRNN, outputs=RNN)

combined = tf.keras.layers.concatenate([modelRNN.output, modelCNN.output])

final_dense = tf.keras.layers.Dense(10, activation='relu')(combined) #ff kijken of dit slim is
final_dense = tf.keras.layers.Dense(1, activation='sigmoid')(final_dense)

final_model = tf.keras.Model(inputs=[modelCNN.input, modelRNN.input], outputs=final_dense)

np.asarray(match_trainconverted).astype('float32').reshape((-1,1))

final_model.compile(optimizer='adam',
                    loss='binary_crossentropy',
                    metrics=['accuracy'])

final_model.fit([MNIST_traincopy, RNN_traincopy], array, epochs=1, batch_size=100)

Tags: modellayerstfactivationcnnkerasdensefinal

热门问题