从tf.contrib.layers.conv2d切换到tf.nn.conv2d

时间:2018-03-28 21:23:54

标签: python tensorflow

到目前为止,我正在使用tf.contrib.layers.conv2d,但是(例如,如[{3}}所述允许重量衰减过滤器)我想切换到tf.nn.conv2d实现。但是我对参数感到困惑,因为显然我需要在没有参数之前指定一些东西。

通过doc和SO条目,我试了一下。对于具有[batch_size,x,y,channels]的4D-Tensors,这两个版本是否相同? I.e。假设input_layer.shape [-1]代表input_channels中所需的filter并且我必须明确地将步幅设置为输入张量的dims数量,我是否正确:

with tf.contrib.layers.conv2d(original)

down0a = tf.contrib.layers.conv2d(input_layer, n_features, (3, 3))
down0b = tf.contrib.layers.conv2d(down0a, n_features, (3, 3))
down0c = tf.contrib.layers.max_pool2d(down0b_do, (2, 2), padding='same')

使用tf.nn.conv2d

down0a = tf.nn.conv2d(input_layer, filter=[3, 3, input_layer.shape[-1], n_features], strides=[1, 1, 1, 1], padding='SAME')
down0ar = tf.nn.relu(down0a)
down0b = tf.nn.conv2d(down0ar, filter=[3, 3, down0ar.shape[-1], n_features], strides=[1, 1, 1, 1], padding='SAME')
down0br = tf.nn.relu(down0b)
down0c = tf.nn.max_pool(down0br, [2, 2, down0br.shape[-1], n_features], strides=[1, 1, 1, 1], padding='SAME')

1 个答案:

答案 0 :(得分:1)

你似乎已经得到了正确的形状,最明显的问题似乎是你不应该告诉tf.nn.conv2d形状,你应该把它传递给实际的重量张量。

down0w = tf.get_variable("down0w", shape=[3, 3, input_layer.shape[-1], n_features], initializer=tf.contrib.layers.xavier_initializer())
down0a = tf.nn.conv2d(input_layer, filter=down0w, strides=[1, 1, 1, 1], padding='SAME')
相关问题