在model.fit()中指定验证数据时出现尺寸错误

时间:2019-04-02 14:58:20

标签: python-3.x neural-network conv-neural-network

我有两个不同的输入。一个输入是100个大小为200x6000的矩阵,我给了一个卷积神经网络,另一个输入是100个长度为6000的向量,我给了一个完全连接的神经网络。我将这两个网络连接起来,然后再连接一个完全连接的神经网络。

当我明确指定验证数据而不是使用validate_split = 0.2时,就会出现问题。然后它给了我以下错误:

enter image description here

我对卷积神经网络的输入是image_train的尺寸(100,1,200,6000)。完全连接的神经网络的输入维数为(100,6000)。为了进行验证,图像image_valid的维度为(30,1,200,6000),而positions_valid的维度为(30,6000)。

训练数据的Ytest维度为(100,2),Yvalid为(30,2)

我的代码如下:

###Setting up the architecture

###Convolution neural network
i1 = Input(shape=(1, 200, 6000))

c1 = Conv2D(128*2, kernel_size=3,activation='relu',data_format='channels_first')(i1)
c1 = Conv2D(128*2, kernel_size=3, activation='relu')(c1)
c1 = Conv2D(128*2, kernel_size=4, activation='relu')(c1)
c1 = Dropout(0.2)(c1)
#c1 = Conv2D(128*2, kernel_size=4, activation='relu')(c1)
#c1 = Dropout(0.2)(c1)
c1 = Flatten()(c1)

###Fully connected neural network
i2 = Input(shape=(6000, ))
c2 = Dense(64, input_shape = (6000,), activation='relu')(i2)
#c2 = Dense(256, activation='relu', kernel_initializer='normal')(c2)
#c2 = Dense(64, input_shape = (4500,), activation='relu')(i2)
c2 = Dropout(0.2)(c2)

###Concatenating
c = concatenate([c1, c2])

x = Dense(256, activation='relu', kernel_initializer='normal')(c)
x = Dropout(0.25)(x)
output = Dense(2, activation='softmax')(x)

model = Model([i1, i2], [output])

model.summary()

model.compile(loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

history = model.fit([image_train, positions_train], Ytest, batch_size=32,
          epochs=20,
          verbose=1,
          validation_data=([image_valid, positions_valid], Yvalid))

如果我使用validation_split = 0.2,则相同的代码将起作用。但是我想指定validation_data,并在执行时给出上面提到的错误。见识将不胜感激。

0 个答案:

没有答案