TensorFlow中的+和tf.add有没有区别?

    a = tf.get_variable(name='a', shape=(1,), initializer=tf.constant_initializer(value=0))
    b = tf.get_variable(name='b', shape=(1,), initializer=tf.constant_initializer(value=1))
    c = a + b
    d = tf.add(x=a, y=b, name='d')
    e = tf.get_variable(name='e', initializer=tf.truncated_normal(shape=(2, 3), stddev=1, mean=0))
    f = tf.get_variable(name='f', initializer=tf.truncated_normal(shape=(2, 3), stddev=1, mean=0))
    g = e + f
    h = tf.add(x=e, y=f, name='h')
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(a.name)  # a:0
        print(b.name)  # b:0
        print(c.name)  # add:0
        print(d.name)  # d:0
        print(e.name)  # e:0
        print(f.name)  # f:0
        print(sess.run(a))  # [0.]
        print(sess.run(b))  # [1.]
        print(sess.run(c))  # [1.]
        print(sess.run(d))  # [1.]
        print(sess.run(e))
        # [[-0.9891971   0.5030212  -0.48883826]
        #  [-1.4214745  -0.6322079  -0.4219446 ]]
        print(sess.run(f))
        # [[-0.10764185  0.6467568   0.03688372]
        #  [ 1.1124796  -0.89837056  0.67874044]]
        print(sess.run(g))
        # [[-1.096839    1.149778   -0.45195454]
        #  [-0.3089949  -1.5305784   0.25679585]]
        print(sess.run(h))
        # [[-1.096839    1.149778   -0.45195454]
        #  [-0.3089949  -1.5305784   0.25679585]]

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据