tensorflow运算函数笔记

import tensorflow as tf

alist = [[1, 2, 3, 4, 5, 6 ,7, 8],
         [7, 6 ,5 ,4 ,3 ,2, 1, 8],
         [3, 3, 3, 3, 3, 3, 3, 3],
         [1, 1, 1, 1, 1, 1, 1, 1],
         [2, 2, 2, 2, 2, 2, 2, 2]]
t0 = tf.constant(alist)
#元素平方
re = tf.square(t0)
#默认求和,行列和
re1 = tf.reduce_sum(re)
#列和
re2 = tf.reduce_sum(re,0)
#行和
re3 = tf.reduce_sum(re,1)
#行列和
re4 = tf.reduce_sum(re,[0,1])

with tf.Session() as sess:
    print(sess.run(t0))
    print('=================')
    print(sess.run(re))
    print('=================')
    print(sess.run(re1))
    print('=================')
    print(sess.run(re2))
    print('=================')
    print(sess.run(re3))
    print('=================')
    print(sess.run(re4))

输出:

[[1 2 3 4 5 6 7 8]
 [7 6 5 4 3 2 1 8]
 [3 3 3 3 3 3 3 3]
 [1 1 1 1 1 1 1 1]
 [2 2 2 2 2 2 2 2]]
=================
[[ 1  4  9 16 25 36 49 64]
 [49 36 25 16  9  4  1 64]
 [ 9  9  9  9  9  9  9  9]
 [ 1  1  1  1  1  1  1  1]
 [ 4  4  4  4  4  4  4  4]]
=================
520
=================
[ 64  54  48  46  48  54  64 142]
=================
[204 204  72   8  32]
=================
520

你可能感兴趣的:(python)