【发布时间】:2016-09-12 21:37:45
【问题描述】:
我编写了一个小型 Tensorflow 程序,它通过相同的卷积核 num_unrollings 连续卷积图像补丁,然后尝试最小化结果值和目标输出之间的均方差。
但是,当我在num_unrollings 大于 1 的情况下运行模型时,我的损失 (tf_loss) 项相对于卷积核 (tf_kernel) 的梯度为零,因此不会发生学习。
这是我能想出的最小的代码(python 3),它重现了这个问题,对长度感到抱歉:
import tensorflow as tf
import numpy as np
batch_size = 1
kernel_size = 3
num_unrollings = 2
input_image_size = (kernel_size//2 * num_unrollings)*2 + 1
graph = tf.Graph()
with graph.as_default():
# Input data
tf_input_images = tf.random_normal(
[batch_size, input_image_size, input_image_size, 1]
)
tf_outputs = tf.random_normal(
[batch_size]
)
# Convolution kernel
tf_kernel = tf.Variable(
tf.zeros([kernel_size, kernel_size, 1, 1])
)
# Perform convolution(s)
_convolved_input = tf_input_images
for _ in range(num_unrollings):
_convolved_input = tf.nn.conv2d(
_convolved_input,
tf_kernel,
[1, 1, 1, 1],
padding="VALID"
)
tf_prediction = tf.reshape(_convolved_input, shape=[batch_size])
tf_loss = tf.reduce_mean(
tf.squared_difference(
tf_prediction,
tf_outputs
)
)
# FIXME: why is this gradient zero when num_unrollings > 1??
tf_gradient = tf.concat(0, tf.gradients(tf_loss, tf_kernel))
# Calculate and report gradient
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
gradient = session.run(tf_gradient)
print(gradient.reshape(kernel_size**2))
#prints [ 0. 0. 0. 0. 0. 0. 0. 0. 0.]
感谢您的帮助!
【问题讨论】:
-
用全零初始化内核不是一个好主意,在这种情况下会导致梯度为 0。
标签: tensorflow convolution gradient-descent