Tensorflow 计算梯度 | clip 梯度


def test4():
    # x = tf.Variable(tf.truncated_normal([1]), name='x')
    x = tf.Variable([1.], name='x')
    goal = (x - 2) * (x - 2)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
    gra_and_var = optimizer.compute_gradients(-goal)
    gg, vv = zip(*gra_and_var)
    train_step = optimizer.apply_gradients(zip(gg, vv))
    x2 = x
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        xb_ = sess.run([x])
        print(xb_)
        for i in range(3):
            xb_, gv, optm_, gg_, vv_, x2_ = sess.run([x, gra_and_var, train_step, gg, vv,
                                                                   x2])
            print "i", i," gv:", gv
            print(xb_, x2_)
            print "gg", gg_, "type:", type(gg), "0_type:", type(gg[0])
            print "vv", vv_
            print "gra_and_var:", type(gv)

            for _ in gv:
                print "one type:", type(_), _



你可能感兴趣的:(Tensorflow)