[英]Can I manually slot an input neuron into a hidden layer in Keras?
I have a CNN and I want to sneak in some extra information into one of the final layers.我有一个 CNN,我想将一些额外的信息潜入最后一层。
Here's a simplified version of the code.这是代码的简化版本。 Watch for the comment看评论
def define_model():
model = Sequential()
model.add(Conv2D(32, (3,3))
model.add(Conv2D(32, (3,3))
model.add(MaxPooling2D((2,2))
model.add(Conv2D(64, (3,3))
model.add(Conv2D(64, (3,3))
model.add(MaxPooling2D((2,2)))
model.add(Flatten())
# this next layer is where I want to sneak the neuron(s) in
model.add(Dense(1024))
model.add(Dropout(rate=0.4))
model.add(Dense(168))
model.compile()
return model
So I have some additional information about the input image which might be able to help the network.所以我有一些关于输入图像的附加信息,这些信息可能对网络有帮助。 Think of it as a clue which may or may not deserve a reasonable amount of weighting.将其视为可能值得也可能不值得合理加权的线索。
The clue is in the form of an integer which technically is in [0, inf) but practically is probably in [0, 20].线索是一个整数形式,技术上在 [0, inf) 中,但实际上可能在 [0, 20] 中。
So my questions are所以我的问题是
What's the appropriate way to represent that hint speaking in terms of NN architecture in general.用一般的 NN 架构来表示该提示的适当方式是什么。
How do I tweak the Keras model to make that happen in practice?我如何调整 Keras 模型以在实践中实现这一点?
Bonus: If I wanted to, could I prevent the subsequent dropout from ever dropping out this added feature?奖励:如果我愿意,我是否可以防止后续的退出退出此附加功能?
This could work by using Keras' functional API:这可以通过使用 Keras 的函数式 API 来实现:
def define_model():
inputs = Input(input_shape=(...))
hints = Input(input_shape=(...))
x = Conv2D(32, (3,3))(inputs)
x = Conv2D(32, (3,3))(x)
x = MaxPooling2D((2,2))(x)
x = Conv2D(64, (3,3))(x)
x = Conv2D(64, (3,3))(x)
x = MaxPooling2D((2,2))(x)
x = Flatten()(x)
x = Add()([x, hints])
x = Dense(1024)(x)
x = Dropout(rate=0.4)(x)
outputs = Dense(168)(x)
model = Model([inputs, hints], outputs)
model.compile()
return model
I don't know about protecting it from the dropout using Keras though.我不知道如何使用 Keras 保护它免受辍学。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.