这个
conv2d
输入通道上的运算和。因此,用1初始化内核将得到相同的输出通道,每个输出通道都是输入通道的总和。
请注意,除非您设置
bias_initializer
归零或设置
use_bias
参数到
False
,随机初始化的偏差也会影响输出。
import tensorflow as tf
import numpy as np
with tf.Graph().as_default():
x = tf.placeholder(tf.float32, [None, 500, 500, 3])
y = tf.layers.conv2d(x, 3, 1, padding='same', use_bias=False, kernel_initializer=tf.ones_initializer())
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Evaluate
np.random.seed(1)
_x = np.random.uniform(0, 1, (1, 500, 500, 3))
_y = sess.run(y, {x: _x})
# Check that the channels are identical
np.testing.assert_allclose(_y[..., 0], _y[..., 1])
# Check that each channel is the sum over channels
np.testing.assert_allclose(_y[..., 0], _x.sum(axis=-1), rtol=1e-5)