Python keras neural network (Theano) package returns an error about data dimensions

浪子不回头ぞ 提交于 2019-12-03 21:10:46

You specified the wrong output dimensions for your internal layers. See for instance this example from the Keras documentation:

model = Sequential()
model.add(Dense(20, 64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, 2, init='uniform'))
model.add(Activation('softmax'))

Note how the output size of one layer matches the input size of the next one:

20x64 -> 64x64 -> 64x2

The first number is always the input size (number of neurons on the previous layer), the second number the output size (number of neurons on the next layer). So in this example you have four layers:

  • an input layer with 20 neurons
  • a hidden layer with 64 neurons
  • a hidden layer with 64 neurons
  • an output layer with 2 neurons

The only hard restriction you have is that the first (input) layer needs to have as many neurons as you have features, and the last (output) layer needs to have as many neurons as you need for your task.

For your example, since you have three features, you need to change the input layer size to 3, and you can keep the two output neurons from this example to do binary classification (or use one, as you did, with logistic loss).

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