merge - Keras 2.0 中的自定义合并层

标签 merge keras-2

在keras 1我曾经写过

def merge_mode(branches):
   return #merge function

Merge([...], output_shape=(num_classes,), mode=merge_mode)

但是现在在keras 2.0 合并已弃用,您只能添加预定义函数,如添加、平均、连接......

我的问题是如何在 keras 2.0 中添加自定义合并功能

最佳答案

如果您正在寻找一个没有可训练权重的层,从您的示例来看,我想是这种情况。然后你可以使用 Lambda 层。

实际上,我自己编写了一个使用平方距离度量创建孪生 CNN 的方法。 请参见下面的示例:

from keras.layers import Lambda

# I create the layer structure first.
# This can then be added to a sequential model or used in a functional model.
merge_layer = Lambda(lambda x: K.square(x[0]-[x]), output_shape=lambda x: x[0])

# Inside a functional model you would use it like this:
block_output = merge_layer([left,right])

关于merge - Keras 2.0 中的自定义合并层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45324870/

相关文章:

c - 合并2个单向链表( headless 节点)

python - 在keras中将输入与常量向量连接起来

callback - 凯拉斯 2.0.0 : how to access and modify the training data in the callback function "on_epoch_end()"?

python - 从 Keras 中的生成器获取 x_test、y_test?

xml - git rerere 如何找出两个冲突之间的相似之处?

mercurial - 如何使用 mercurial 自动 merge 2 个头

python - 创建新的 DataFrame 作为其他 DataFrame 列的有序组合

mysql - 当列应该变得唯一时合并相关数据

python - Keras 2 fit_generator 用户警告 : `steps_per_epoch` is not the same as the Keras 1 argument `samples_per_epoch`

python - Keras ModelCheckpoint 监控多个值