如何在 keras 中同时训练多个神经网络? [英] How do I train multiple neural nets simultaneously in keras?

查看:78
本文介绍了如何在 keras 中同时训练多个神经网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何多次训练 1 个模型并在输出层组合它们?

How do I train 1 model multiple times and combine them at the output layer?

例如:

model_one = Sequential() #model 1
model_one.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
model_one.add(Flatten())
model_one.add(Dense(128, activation='relu'))

model_two = Sequential() #model 2
model_two.add(Dense(128, activation='relu', input_shape=(784)))
model_two.add(Dense(128, activation='relu'))

model_???.add(Dense(10, activation='softmax')) #combine them here

model.compile(loss='categorical_crossentropy', #continu together
          optimizer='adam',
          metrics=['accuracy'])


model.fit(X_train, Y_train, #continu together somehow, even though this would never work because X_train and Y_train have wrong formats
      batch_size=32, nb_epoch=10, verbose=1)

我听说我可以通过图形模型来做到这一点,但我找不到任何关于它的文档.

I've heard I can do this through a graph model but I can't find any documentation on it.

回复以下建议:

A1 = Conv2D(20,kernel_size=(5,5),activation='relu',input_shape=( 28, 28, 1))
---> B1 = MaxPooling2D(pool_size=(2,2))(A1)

抛出这个错误:

AttributeError: 'Conv2D' object has no attribute 'get_shape'

推荐答案

图形符号会为您做到.本质上,你给每一层一个唯一的句柄,然后使用末尾括号中的句柄链接回前一层:

Graph notation would do it for you. Essentially you give every layer a unique handle then link back to the previous layer using the handle in brackets at the end:

layer_handle = Layer(params)(prev_layer_handle)

注意第一层必须是一个没有先验连接的Input(shape=(x,y)).

Note that the first layer must be an Input(shape=(x,y)) with no prior connection.

然后当你制作你的模型时,你需要告诉它它需要一个列表的多个输入:

Then when you make your model you need to tell it that it expects multiple inputs with a list:

model = Model(inputs=[in_layer1, in_layer2, ..], outputs=[out_layer1, out_layer2, ..])

最后,当您训练它时,您还需要提供与您的定义相对应的输入和输出数据列表:

Finally when you train it you also need to provide a list of input and output data that corresponds with your definition:

model.fit([x_train1, x_train2, ..], [y_train1, y_train2, ..])

与此同时,其他一切都相同,因此您只需要将上述内容组合在一起即可为您提供所需的网络布局:

Meanwhile everything else is the same so you just need to combine together the above to give you the network layout that you want:

from keras.models import Model
from keras.layers import Input, Convolution2D, Flatten, Dense, Concatenate

# Note Keras 2.02, channel last dimension ordering

# Model 1
in1 = Input(shape=(28,28,1))
model_one_conv_1 = Convolution2D(32, (3, 3), activation='relu')(in1)
model_one_flat_1 = Flatten()(model_one_conv_1)
model_one_dense_1 = Dense(128, activation='relu')(model_one_flat_1)

# Model 2
in2 = Input(shape=(784, ))
model_two_dense_1 = Dense(128, activation='relu')(in2)
model_two_dense_2 = Dense(128, activation='relu')(model_two_dense_1)

# Model Final
model_final_concat = Concatenate(axis=-1)([model_one_dense_1, model_two_dense_2])
model_final_dense_1 = Dense(10, activation='softmax')(model_final_concat)

model = Model(inputs=[in1, in2], outputs=model_final_dense_1)

model.compile(loss='categorical_crossentropy', #continu together
              optimizer='adam',
              metrics=['accuracy'])

model.fit([X_train_one, X_train_two], Y_train,
          batch_size=32, nb_epoch=10, verbose=1)

文档可以在 函数模型 API 中找到.我建议阅读其他问题或查看 Keras 的 repo因为文档目前没有很多例子.

Documentation can be found in the Functional Model API. I'd recommend reading around other questions or checking out Keras' repo as well since the documentation currently doesn't have many examples.

这篇关于如何在 keras 中同时训练多个神经网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆