Input 0 is incompatible with layer flatten_15: expected min_ndim=3, found ndim=2

此生再无相见时 提交于 2019-12-11 18:18:54

问题


I am trying to train ANN model on my sound data set, which has 320 rows and 50 columns, while running this code:

Model= Sequential([ Flatten(), 
     Dense(16, input_shape=(1,50), activation= 'relu' ) , 

     Dense(32, activation= 'relu' ),
     Dense(2, activation='softmax' ) , 
     ])
Model.compile(Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Model.fit(S_T_S, T_L, validation_split=0.1, batch_size=20, epochs=20, shuffle='true', verbose=2)

I am getting error of:

Input 0 is incompatible with layer flatten_15: expected min_ndim=3, found ndim=2,


回答1:


If the dataset has (N, C) shape, where N is the number of the data points and C is the number of channels for single data point, the input_shape argument of the first layer should only specify the channels.

model= Sequential([
     Dense(16, input_shape=(50,), activation= 'relu' ) , 

     Dense(32, activation= 'relu' ),
     Dense(2, activation='softmax' ) , 
     ])
model.compile(Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(S_T_S, T_L, validation_split=0.1, batch_size=20, epochs=20, shuffle='true', verbose=2)

If the number of output classes is two then an output layer with one node can work better than using the one with two nodes. In this case, the activation of the output layer should be changed to sigmoid and the loss should be changed to binary cross entropy.

model= Sequential([
     Dense(16, input_shape=(50,), activation= 'relu' ) , 

     Dense(32, activation= 'relu' ),
     Dense(1, activation='sigmoid' ) , 
     ])
model.compile(Adam(lr=0.0001), loss='sparse_binary_crossentropy', metrics=['accuracy'])


来源:https://stackoverflow.com/questions/54779433/input-0-is-incompatible-with-layer-flatten-15-expected-min-ndim-3-found-ndim-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!