目前,我正在制作一个卷积神经网络(CNN),它输入一个翼型的图像文件,并输出其升力系数。模型本身是这样的:
input_img = Input(shape(100,100,1), dtype='int32', name = 'img_input')Layer = Conv2D(32,(3,3))(input_img)Layer = MaxPooling2D((3,3))(Layer)Layer = Conv2D(32,(3,3))(Layer)Layer = MaxPooling2D((3,3))(Layer)Layer = Flatten()(Layer)Layer = Dense(32, activation = 'relu')(Layer)end_out = Dense(1, kernel_initializer = 'normal')model = Model(inputs=[img_input],outputs=[end_out])model.compile(loss='mean_squared_error',optimizer='adam')
对我来说,似乎需要将图像数据与输出数据配对,然后在配对的数据集上训练网络。然而,我不确定这是否是正确的方法,如果是的话,我不知道该如何操作。如何使用图像数据作为输入,数值数据(升力系数)作为输出来训练这个模型?谢谢!
回答:
根据Keras文档,您必须拟合数据并从数据中学习权重
# 对于具有10个类别的单输入模型(分类分类):model = Sequential()model.add(Dense(32, activation='relu', input_dim=100))model.add(Dense(10, activation='softmax'))model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])# 生成虚拟数据import numpy as npdata = np.random.random((1000, 100))labels = np.random.randint(10, size=(1000, 1))# 将标签转换为分类的一热编码one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)# 以32个样本的批次迭代数据来训练模型model.fit(data, one_hot_labels, epochs=10, batch_size=32)