简体   繁体   中英

Why layer batch_normalization_6 is incompatible with the layer?

I want to train feature of size (10151, 1285) to lable (10151, 257), and I want to use way2. since in I want to use "feature_input" in the cost function. but it fails with error:

ValueError: Input 0 of layer batch_normalization_6 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 257).

I am wondering why?

Way1:
model = Sequential()
model.add(Dense(257, input_dim=1285))
model.add(BatchNormalization())
model.add(Activation('sigmoid'))

model.compile(optimizer='adam', loss='mse',  metrics=['mse'])
model.fit(feature, label )
model.save("./model.hdf5")

Way2:
feature_input = Input(shape=(None, 1285))
dense = Dense(257)(feature_input)
norm = BatchNormalization()(dense)
out = Activation('sigmoid')(norm)
model = Model(feature_input, out)

model.compile(optimizer='adam', loss='mse',  metrics=['mse'])
model.fit(feature, label )
model.save("./model.hdf5")

If you define the input shape as (None, 1285) , the model recognizes the input as a 3-dimensional data. I guess the None shape you entered was meant to describe the batch size, but when we compile the model we get a 3-dimensional input, and the batch dimension is automatically added. Therefore, you can use an input shape of (1285,) as an alternative.

<Summary of your model>

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, None, 1285)]      0         
_________________________________________________________________
dense (Dense)                (None, None, 257)         330502    
_________________________________________________________________
batch_normalization (BatchNo (None, None, 257)         1028      
_________________________________________________________________
activation (Activation)      (None, None, 257)         0         
=================================================================
Total params: 331,530
Trainable params: 331,016
Non-trainable params: 514
_________________________________________________________________

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM