1、tf.reduce_sum

从tensor的维度上面计算元素之和

tf.reduce_sum(
    input_tensor,  # 输入
    axis=None,		# 表示在哪个维度进行sum操作。
    keepdims=None,	# 表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度。
    name=None,
    reduction_indices=None,
    keep_dims=None
)
import tensorflow as tf

x = tf.constant([[1, 1, 1],
                 [1, 1, 1]])
a = tf.reduce_sum(x)  # 修改这里
b = tf.reduce_sum(x, axis=0)
c = tf.reduce_sum(x, axis=1)
d = tf.reduce_sum(x, keep_dims=True)
e = tf.reduce_sum(x, keep_dims=False)
f = tf.reduce_sum(x, axis=[0, 1])

tensors = [a, b, c, d, e, f]

with tf.Session() as sess:
    for tensor in tensors:
        y = sess.run(tensor)
        print(y)
# 6
# [2 2 2]
# [3 3]
# [[6]]
# 6
# 6

tensorflow中常用的计算操作

2、tf.multiply

数乘

tf.multiply(
    x,
    y,
    name=None  # A name for the operation (optional).
)
import tensorflow as tf

a = tf.constant([[1, 2],
                 [3, 4]])
b = tf.constant([[1, 3],
                 [2, 1]])
y = tf.multiply(a, b)

with tf.Session() as sess:
    res = sess.run(y)
    print(res)
# [[1 6]
#  [6 4]]
import tensorflow as tf

x = tf.constant([[1.0, 2.0],
                 [3.0, 4.0]])

a = 0.5 * x
b = tf.multiply(0.5, x)

tensors = [a, b]

with tf.Session() as sess:
    for tensor in tensors:
        y = sess.run(tensor)
        print(y)
# [[0.5 1. ]
#  [1.5 2. ]]
# [[0.5 1. ]
#  [1.5 2. ]]

3、tf.matmul

矩阵点乘

tf.matmul(
    a,
    b,
    transpose_a=False,
    transpose_b=False,
    adjoint_a=False,
    adjoint_b=False,
    a_is_sparse=False,
    b_is_sparse=False,
    name=None
)
import tensorflow as tf

a = tf.constant([[1, 2],
                 [3, 4]])
b = tf.constant([[1, 3],
                 [2, 1]])
y = tf.matmul(a, b)

with tf.Session() as sess:
    res = sess.run(y)
    print(res)
# [[ 5  5]
#  [11 13]]

4、tf.add

import tensorflow as tf

x = tf.constant([[1, 2],
                 [3, 4]])

a = 1 + x
b = tf.add(1, x)

tensors = [a, b]

with tf.Session() as sess:
    for tensor in tensors:
        y = sess.run(tensor)
        print(y)
# [[2 3]
# [4 5]]
# [[2 3]
#  [4 5]]

5、tf.cast:用于改变某个张量的数据类型

import tensorflow as tf

x = tf.constant([[1.8, 2.2],
                 [3.0, 4.1]])
y = tf.cast(x, tf.int32)
with tf.Session() as sess:
    res = sess.run(y)
    print(res)
# [[1 2]
# [3 4]]

6、tf.cast:用于改变某个张量的数据类型

import tensorflow as tf
x = tf.constant([[1.8, 2.2],
                 [3.0, 4.1]])
y = tf.cast(x, tf.int32)
with tf.Session() as sess:
    res = sess.run(y)
    print(res)
# [[1 2]
# [3 4]]

7、tf.maximum(x, y, name=None):返回的x,y之间的最大值

import tensorflow as tf
x = [1, 5, 3]
y = [2, 4, 6]
z = tf.maximum(x, y)
with tf.Session() as sess:
    res = sess.run(z)
    print(res)
# [2 5 6]

8、tf.minimum(x, y, name=None):返回的x,y之间的最x小值

import tensorflow as tf
x = [1, 5, 3]
y = [2, 4, 6]
z = tf.minimum(x, y)
with tf.Session() as sess:
    res = sess.run(z)
    print(res)
# [1 4 3]	  每个维度返回一个最小值

9、tf.argmax、tf.argmin:返回最大值、最小值的索引

# import tensorflow as tf
a = tf.argmax([[1,3,4,5,6]], axis=1)
b = tf.argmax([[1,3,4], [2,4,1]], axis=1)
c = tf.argmin([[1,3,4,5,6]], axis=1)
d = tf.argmin([[1,3,4], [2,4,1]], axis=1)
tensors = [a ,b, c, d]

with tf.Session() as sess:
    for tensor in tensors:
        y = sess.run(tensor)
        print(y)
# [4]
# [2 1]
# [0]
# [0 2]

相关文章: