【问题标题】:Custom Tensorflow Layer From Two Input Sources来自两个输入源的自定义 TensorFlow 层
【发布时间】:2020-10-04 05:37:11
【问题描述】:

我正在尝试从两个应该执行此操作的“输入源”构建自定义 TensorFlow 层

exp(源 A) + cos(源 B)

但是,我什至不知道如何设置编写这样的自定义层

注意:我真的很想了解/了解这是如何工作的,因此解决方法可能不是最佳的...

【问题讨论】:

  • 也许是一个天真的问题:这与拥有一个具有 2 个功能的单一数据源有何不同?
  • 我想让输入源每隔一层跳过一次......所以这种方式比改变输出张量的形状更自然,而且记忆效率也更高:)

标签: python-3.x tensorflow keras neural-network


【解决方案1】:

这是可能的

class custom_layer(tf.keras.layers.Layer):

    def __init__(self):
        super(custom_layer, self).__init__()
        pass

    def call(self, inputs):

        input1, input2 = inputs
        return tf.exp(input1) + tf.cos(input2)

inp1 = Input((10,))
inp2 = Input((10,))
x = custom_layer()([inp1,inp2])
x = Dense(1)(x)
model = Model([inp1,inp2],x)
model.compile('adam','mse')
model.summary()

X1 = np.random.uniform(0,1, (100,10))
X2 = np.random.uniform(0,1, (100,10))
y = np.random.uniform(0,1, 100)

model.fit([X1,X2],y, epochs=3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多