Keras神经网络多重输出



我想创建一个具有多个输出的神经网络。有一个结论,我成功地做到了这一点,但有两个结论——它不起作用。你能帮我吗?你知道有关于keras的例子的资源吗?我附上下面的代码和错误。(对不起,我的英文是谷歌翻译的(

代码:

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]])

错误:

---------------------------------------------------------------------------
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。正如评论中建议的那样,我重新编码了代码,但现在,每次训练,它都会输出一些不在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]])

首先,顺序模型不支持多输出。若你们指的是多输出,多神经元,那个么你们可以使用顺序模型,只需要改变最后一层神经元的数量。

以下是您的第一个型号代码的修改:

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]])

但如果你想要一个功能api的例子,它就在这里:

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]])

相关内容

  • 没有找到相关文章

最新更新