Python 3.x 来自两个输入源的自定义Tensorflow层
我正在尝试从两个“输入源”构建一个定制的TensorFlow层,这应该可以做到 exp(源A)+cos(源B) 然而,我甚至不知道如何设置编写这样一个自定义层 注意:我真的很想了解/了解这是如何工作的,这样一个解决方案将是次优的…这是一种可能性Python 3.x 来自两个输入源的自定义Tensorflow层,python-3.x,tensorflow,keras,neural-network,Python 3.x,Tensorflow,Keras,Neural Network,我正在尝试从两个“输入源”构建一个定制的TensorFlow层,这应该可以做到 exp(源A)+cos(源B) 然而,我甚至不知道如何设置编写这样一个自定义层 注意:我真的很想了解/了解这是如何工作的,这样一个解决方案将是次优的…这是一种可能性 class custom_layer(tf.keras.layers.Layer): def __init__(self): super(custom_layer, self).__init__() pass
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)
也许这是一个近乎幼稚的问题:这与拥有一个具有两个特性的单一数据源有什么不同?我想让一个输入源每第二层跳过一次……因此,这种方法比改变输出张量的形状更自然,也更有效:)