简体   繁体   中英

Keras neural network multiple output

I would like to create a neural network with multiple outputs. With one conclusion, I succeeded in doing this, but with two - it does not work. Can you help me please? Do you know any resource with examples for keras? I attach the code and error below. (Sorry for my English, it was translated by google translator)

Code:

from keras.models import Sequential
from keras.layers import Dense
x = [[1, 1, 1, 1], [0, 1, 1, 0], [1, 0, 0, 1], [0, 0, 0, 0], [1, 1, 0, 0], [0, 1, 1, 1], [1, 1, 1, 0], [1, 0, 0, 0]]
y = [[1, 1], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [1, 0], [0, 0]]
model = Sequential()
# model.add(Dense(3, activation='sigmoid'))
model.add(Dense(2, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(1e-1), metrics=['accuracy'])
model.fit(x, y, epochs=20)
model.predict(x=[[0, 0, 1, 1]])

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-77-c805cf1cd17e> in <module>()
      3 x = [[1, 1, 1, 1], [0, 1, 1, 0], [1, 0, 0, 1], [0, 0, 0, 0], [1, 1, 0, 0], [0, 1, 1, 1], [1, 1, 1, 0], [1, 0, 0, 0]]
      4 y = [[1, 1], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [1, 0], [0, 0]]
----> 5 model = Sequential(input=x, output=y)
      6 # model.add(Dense(3, activation='sigmoid'))
      7 model.add(Dense(2, activation='sigmoid'))

/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    520     self._self_setattr_tracking = False  # pylint: disable=protected-access
    521     try:
--> 522       result = method(self, *args, **kwargs)
    523     finally:
    524       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

TypeError: __init__() got an unexpected keyword argument 'input'

UPD. I redid the code, as I was advised in the comments, but now, with each training, it outputs some kind of random result that does not lie in the range of 0 - 1.

from keras.models import Model
from keras.layers import Input
from keras.layers import Dense

x = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [[1, 1], [1, 0], [0, 1], [0, 0]]

visible = Input(shape=(2,))
hidden = Dense(2)(visible)

# hidden2 = Dense(2)(visible)
model = Model(inputs=visible, outputs=[hidden])
model.compile(loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x, y, epochs=8)
model.predict(x=[[1, 0]])

First of all, sequential model does not support multi output. If you mean of multi output, multi neurons, then you can use sequential model, and should simply just change number of neurons in last layer.

Here is the modification of your first model code:

from keras.models import Sequential
from keras.layers import Dense
x = [[1, 1, 1, 1], [0, 1, 1, 0], [1, 0, 0, 1], [0, 0, 0, 0], [1, 1, 0, 0], [0, 1, 1, 1], [1, 1, 1, 0], [1, 0, 0, 0]]
y = [[1, 1], [0, 0], [0, 0], [0, 0], [1, 0], [0, 1], [1, 0], [0, 0]]
model = Sequential()
# model.add(Dense(3, activation='sigmoid'))
model.add(Dense(2, activation='sigmoid'))
model.add(Dense(2, activation='sigmoid')) #change neurons to 2
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(1e-1), metrics=['accuracy'])
model.fit(x, y, epochs=20)
model.predict(x=[[0, 0, 1, 1]])

But if you want an example of functional api, it is here:

from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
import keras
x = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [[1, 1], [1, 0], [0, 1], [0, 0]]

visible = Input(shape=(2,))
hidden = Dense(64, activation='relu')(visible)
hidden = Dense(64, activation='relu')(hidden)
hidden = Dense(64, activation='relu')(hidden)
hidden = Dense(2, activation='sigmoid')(hidden) #use sigmoid activation for output between 0 and 1


model = Model(inputs=visible, outputs=hidden)
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(x, y, epochs=100)
model.predict(x=[[1, 0]])

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