【发布时间】:2021-10-22 12:18:04
【问题描述】:
我有如下操作:
def custom_operation(input):
kernel = tf.constant([3, 4, 5], tf.float32)
frames = tf.signal.frame(input, 3, 1)
return tf.reduce_sum(tf.abs(frames - tf.reshape(kernel, (1, 3))), axis=-1)
custom_operation(tf.constant([1, 2, 3, 4, 5, 6, 7], tf.float32))
# <tf.Tensor: shape=(5,), dtype=float32, numpy=array([6., 3., 0., 3., 6.], dtype=float32)>
我想在 Keras 自定义层中使用它,其中 input 是层的输入,kernel 是具有可训练值的张量,而不是硬编码的 [3, 4, 5]。
从 Keras 调整 Conv1D 层以调用 custom_operation 而不是 tf.nn.conv1d 似乎并不难,但我不知道如何使 kernel 可训练。
【问题讨论】:
-
我认为你应该使用 tf.Variable() 让它可以训练
标签: python tensorflow keras neural-network