Min*_*yen 4 python neural-network deep-learning keras
据我所知,我们仍然需要使用Functional API创建模型并向模型添加图层.为什么人们说它用于非顺序神经网络?
为什么人们说它用于非顺序神经网络?
问题在于Sequential Model,当您调用.add()方法时,您将逐步(按顺序)定义模型.然而,在该功能API(特别是Model类)你有更多的自由,可以定义不同的层,其接收不同的输入,然后实例化与该模型Model使用任何这些层在一步步,或顺序的创造者(不必办法).
换句话说,当model = Sequential()你在调用你的那一刻实例化你的模型对象时(然后你为它添加图层和约束).在Functional API中,您可以创建图层,然后通过调用model = Model(inputs=in, outputs=out)所需的输入和输出图层来实例化模型.如您所见,两种方法都是等效的,例如,这两种方法是相同的:
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Activation
#---Using the Sequential model
model = Sequential() #Object is instantiated here
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
#---Or using the Functional API
a = Input(shape=(784,))
b = Dense(32, activation='relu')(a)
model = Model(inputs=a, outputs=b) #Object is instantiated here
Run Code Online (Sandbox Code Playgroud)
仅考虑这一点,然后选择哪种方式取决于您的个人风格和编码偏好.现在,使用Functional API而不是Sequential模型有一个主要优点,即您可以跨不同模型共享或重用层.
在编译和拟合模型时,将编译和训练其所有相关层.因此,共享此类图层的任何其他模型也将反映这些更改.这使您可以自由地执行许多操作,例如获取网络的子模型,重新定义它们,获取其相对输出,将它们合并到更复杂的模型中等,而无需再为每个子模型进行训练.
为了更清楚,这里有一个例子(基于此 Keras自动编码器博客文章),说明了最后一段中讨论的内容:
from keras.layers import Input, Dense
from keras.models import Model
#Create an autoencoder, along with its encoder and decoder model
input_img = Input(shape=(784,))
encoded = Dense(32, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
#Here we define our autoencoder model: image -> encoding -> decoded image
autoencoder = Model(input_img, decoded)
#Now here is the advantage of the Funcional API
#We can reuse those layers to obtain an encoder model (image -> encoding)
#as well as a decoder model (encoding -> image)
#but compile all three by just compiling and fitting the Autoencoder model
encoder = Model(input_img, encoded) #see how the 'encoded' layer is output
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(32,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
#compile and fit with your data
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X,Y,...)
Run Code Online (Sandbox Code Playgroud)
在此之后,您将能够单独对模型encoder和decoder模型进行预测(例如,可视化您的编码),以及使用autoencoder模型作为整体进行预测.此时,执行以下操作是等效的:
#obtain reconstructed representation directly
autoencoder_imgs = autoencoder.predict(x_test)
#obtain reconstructed representation by joining encoder and decoder models
encoder_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.就个人而言,我总是尝试使用Functional API,无论我是否要重复使用或回收图层,因为我发现它更详细,但这取决于您自己决定.
好吧,“顺序”实际上并不是最正确的术语,但它是 Keras 开发人员选择的名称。当然,所有模型都按顺序工作。
Sequential模型是一条直线。您不断添加层,每个新层都采用前一层的输出。你不能用分支制作创意图表。 Model是完全免费的,可以根据需要拥有尽可能多的分支、输入和输出。Sequential模型示例:from keras.models import Sequential
from keras.layers import *
#you create a model
model = Sequential()
#and you add layers
model.add(SomeKerasLayer(...))
model.add(AnotherKerasLayer(...))
#as you can see, this model is a straight line, you only add layers "sequentially"
Run Code Online (Sandbox Code Playgroud)
Model:现在我们开始创建真正奇特的模型。
from keras.models import Model
from keras.layers import *
Run Code Online (Sandbox Code Playgroud)
我们首先定义输入张量。我们可以有任意数量的输入!(顺序模型仅限于一个输入,您在第一层用 定义input_shape)。
input1 = Input(inputShape1)
#We can have more inputs if we want!
input2 = Input(inputShape2)
input3 = Input(inputShape3)
Run Code Online (Sandbox Code Playgroud)
我们通过创建层和“使用输入张量调用层”来工作。
当我们用输入张量调用一个层时,我们得到一个输出张量。
我们可以创建任何我们想要的路径。
#Example: two separate layers taking two separate inputs:
output1 = SomeLayer(...)(input1)
output2 = AnotherLayer(...)(input2)
Run Code Online (Sandbox Code Playgroud)
我们可以用不同的选项连接两个分支,例如加、乘、连接等:
#joining the previous tensors output1 and output2
joined1_2 = Concatenate()([output1,output2])
Run Code Online (Sandbox Code Playgroud)
我们可以重用具有不同输入的相同层,获得不同的输出:
aLayer = AKerasLayer(...) #notice I'm creating this layer but not calling it yet
#calling the same layer with two different inputs
output1 = aLayer(joined1_2)
output2 = aLayer(input3)
Run Code Online (Sandbox Code Playgroud)
最后,我们可以使用我们想要的任意数量的输入和输出来定义模型:
model = Model([input1,input2,input3],[output1, output2])
Run Code Online (Sandbox Code Playgroud)
顺序和功能 API 两种模型都可以像层一样使用。
您可以使用输入张量调用模型并获取输出张量,就像创建函数式 API 模型时所做的那样:
input1 = Input(shape)
output1 = anExistingSequentialModel(input1)
output2 = anExistingFunctionalModel(input1)
newModel = Model(input1,[output1,output2])
Run Code Online (Sandbox Code Playgroud)
您也可以在顺序模型中添加模型(注意分支,添加的模型最好有一个输入和一个输出,因为这是一个顺序模型)
seqModel = Sequential()
seqModel.add(anotherModel)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1028 次 |
| 最近记录: |