【发布时间】:2021-01-01 18:09:48
【问题描述】:
我正在尝试在 keras/tensorflow 中构建依赖于标签的卷积过滤器。因此,卷积滤波器取决于批次中的每个示例。
# function used for tf.map_fn
def single_conv(tupl):
x, kernel = tupl
return tf.nn.conv2d(x, kernel, strides=(1, 1, 1, 1), padding='SAME')
# first dimension is None (batch size)
input_img = tf.keras.layers.Input(shape=(28,28,1), dtype=tf.float32)
label = tf.keras.layers.Input(shape=(10,), dtype=tf.float32)
# the network is learning a mapping for the label
label_encoded = tf.keras.layers.Dense(9, activation='relu')(label)
# turn mapping into conv filter
kernels = tf.keras.layers.Reshape((3,3,1,1))(label_encoded)
# class dependent filter(s)
conditional_conv = tf.map_fn(single_conv, (tf.expand_dims(input_img, 1), kernels), fn_output_signature=tf.float32)
当我运行这段代码 sn-p 时,我得到了最后一行的 TypeError: 'Tensor' object cannot be interpreted as an integer。由于最后一行使用tf.map_fn,我看到如果使用的函数(在本例中为single_conv)不可调用或函数的输出结构与fn_output_signature 不匹配,则tf.map_fn 会导致TypeError:https://www.tensorflow.org/api_docs/python/tf/map_fn#raises .
但是,我仍然不确定为什么会这样?我觉得这两个原因应该不是问题?
【问题讨论】:
标签: python tensorflow mapping conv-neural-network tf.keras