本文介绍了如何在keras中连接两层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两层神经网络的示例.第一层有两个参数,并有一个输出.第二个参数应采用一个参数作为第一层的结果,并附加一个参数.看起来应该像这样:

I have an example of a neural network with two layers. The first layer takes two arguments and has one output. The second should take one argument as result of the first layer and one additional argument. It should looks like this:

x1  x2  x3
 \  /   /
  y1   /
   \  /
    y2

因此,我创建了一个具有两层的模型并尝试将它们合并,但是它返回错误:The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument.在行result.add(merged)上.

So, I'd created a model with two layers and tried to merge them but it returns an error: The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument. on the line result.add(merged).

型号:

first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))

second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))

result = Sequential()
merged = Concatenate([first, second])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.add(merged)
result.compile(optimizer=ada_grad, loss=_loss_tensor, metrics=['accuracy'])

推荐答案

您会收到错误消息,因为定义为Sequential()result只是该模型的容器,而您尚未为其定义输入.

You're getting the error because result defined as Sequential() is just a container for the model and you have not defined an input for it.

给出您要构建的内容result以获取第三个输入x3.

Given what you're trying to build set result to take the third input x3.

first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))

second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))

third = Sequential()
# of course you must provide the input to result with will be your x3
third.add(Dense(1, input_shape=(1,), activation='sigmoid'))

# lets say you add a few more layers to first and second.
# concatenate them
merged = Concatenate([first, second])

# then concatenate the two outputs

result = Concatenate([merged,  third])

ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)

result.compile(optimizer=ada_grad, loss='binary_crossentropy',
               metrics=['accuracy'])

但是,构建具有这种输入结构类型的模型的首选方法是使用功能性API .

However, my preferred way of building a model that has this type of input structure would be to use the functional api.

以下是您的入门要求的实现:

Here is an implementation of your requirements to get you started:

from keras.models import Model
from keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from keras.optimizers import Adagrad

first_input = Input(shape=(2, ))
first_dense = Dense(1, )(first_input)

second_input = Input(shape=(2, ))
second_dense = Dense(1, )(second_input)

merge_one = concatenate([first_dense, second_dense])

third_input = Input(shape=(1, ))
merge_two = concatenate([merge_one, third_input])

model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
model.compile(optimizer=ada_grad, loss='binary_crossentropy',
               metrics=['accuracy'])

要在评论中回答问题:

1)结果和合并如何连接?假设您是说它们是如何连接的.

1) How are result and merged connected? Assuming you mean how are they concatenated.

串联的工作方式如下:

  a        b         c
a b c   g h i    a b c g h i
d e f   j k l    d e f j k l

即行刚刚连接起来.

2)现在,将x1输入到第一个,将x2输入到第二个,并将x3输入到第三个.

2) Now, x1 is input to first, x2 is input into second and x3 input into third.

这篇关于如何在keras中连接两层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:54