如何使用计算梯度_

2024-09-30 01:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用tf.test.compute_gradient_error来检查所有渐变是否合理(几乎等于零)。在

我做了一个小测试,似乎这个函数接受占位符。
计算梯度误差没有饲料占位符数据是可以的

x = tf.Variable([2], dtype=tf.float32)
xp = tf.placeholder(dtype=tf.float32, shape=[None, 5])
y = xp * 2
y2 = y * x
y3 = y2 * 3
y4 = tf.reduce_mean(y3)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # err = 9.84e-5
    err = tf.test.compute_gradient_error(y2, (2, 5), y3, (2, 5))
    # err1 = 6.94e-5
    err1 = tf.test.compute_gradient_error(y2, (2, 5), y4, ())

但当我开始检查时,它显示错误消息

^{pr2}$

lossL2是体重l2的损失

tf_vars = tf.trainable_variables()
lossL2 = tf.add_n([tf.nn.l2_loss(v) for v in tf_vars
                   if 'bias' not in v.name]) * 0.001

如果我使用参数extra_feed_dict来提供占位符数据,pycharm调试器将显示Timeout waiting for response on 113
我认为超时是一个可以接受的结果,因为我的模型在每个iteraction中使用了几乎2分钟,我需要找到一些方法来加速

编辑02/11

A = 200
B = 1
# f = np.random.rand(A, B)
# vif = tf.placeholder(tf.float32, shape=[A, B])
f = np.ones((A, B), dtype=np.float32)
vif = tf.constant(f, dtype=tf.float32)

out1 = vif + tf.floor(vif)
out2 = tf.sub(vif, tf.floor(vif))
out3 = tf.subtract(vif, tf.floor(vif))
out4 = vif - tf.floor(vif)

with tf.Session() as sess:
    # sometimes value close 500, sometimes close 1E-5
    err1 = tf.test.compute_gradient_error(vif, [A, B], out1, [A, B])
    err2 = tf.test.compute_gradient_error(vif, [A, B], out2, [A, B])
    err3 = tf.test.compute_gradient_error(vif, [A, B], out3, [A, B])
    err4 = tf.test.compute_gradient_error(vif, [A, B], out4, [A, B])

Tags: 数据testtfnperrorsesscomputedtype

热门问题