TensorFlow笔记:数值操作

基本运算

+, -, *, /

直接对张量进行加减乘除运算会得到元素级别的运算

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

with tf.Session() as sess:
    print((v1 + v2).eval())  # [[2, 4], [6, 8]]
    print((v1 - v2).eval())  # [[0, 0], [0, 0]]
    print((v1 * v2).eval())  # [[1, 4], [9, 16]]
    print((v1 / v2).eval())  # [[1, 1], [1, 1]]

tf.matmul()函数

对两个矩阵进行矩阵相乘

# [[1, 2, 3],
#  [4, 5, 6]]
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
# [[ 7,  8],
#  [ 9, 10],
#  [11, 12]]
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
# `a` * `b`
# [[ 58,  64],
#  [139, 154]]
c = tf.matmul(a, b)

数值运算

tf.reduce_mean()函数

tf.reduce_mean(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)

对输入的张量进行求均值, axis 参数指明了求均值的维度,axis=None 时对整个张量所有元素进行求均值, axis=i 时会在第 i-1 维上进行求均值,可以理解为将第 i-1 维压缩掉了

v1 = tf.constant(np.arange(24), shape=[2, 3, 4])

with tf.Session() as sess:
    print(v1.eval())
    print('axis=0')
    print(tf.reduce_mean(v1, axis=0).eval())  # shape=(3, 4)
                                              # axis=0
                                              # [[ 6  7  8  9]
                                              # [10 11 12 13]
                                              # [14 15 16 17]]
    print('axis=1')
    print(tf.reduce_mean(v1, axis=1).eval())  # shape=(2, 4)
                                              # axis=1
                                              # [[ 4  5  6  7]
                                              # [16 17 18 19]]
    print('axis=2')
    print(tf.reduce_mean(v1, axis=2).eval())  # shape=(2, 3)
                                              # axis=2
                                              # [[ 1  5  9]
                                              # [13 17 21]]

类似的函数还有 reduce_sum() , reduce_max() , reduce_min()

tf.argmax()函数

tf.argmax(
    input,
    axis=None,
    name=None,
    dimension=None,
    output_type=tf.int64
)

返回输入张量最大值所在的下标,axis指明了操作进行的维度,和reduce操作类似

v1 = tf.constant(np.arange(24), shape=[2, 3, 4])

with tf.Session() as sess:
    print(v1.eval())
    print('axis=0')
    print(tf.argmax(v1, axis=0).eval())  # shape=(3, 4), res[i][j] = max(res[k][i][j])
    print('axis=1')
    print(tf.argmax(v1, axis=1).eval())  # shape=(2, 4), res[i][j] = max(res[i][k][j])
    print('axis=2')
    print(tf.argmax(v1, axis=2).eval())  # shape=(2, 3), res[i][j] = max(res[i][j][k])

类似的函数还有 tf.argmin()

tf.clip_by_value()函数

tf.clip_by_value(
    t,
    clip_value_min,
    clip_value_max,
    name=None
)

用来将一个值限制在最大最小值之间,即
a n s = { m i n v a l t &lt; m i n v a l t m i n v a l ≤ t ≤ m a x v a l m a x v a l m a x v a l ≤ t ans = \begin{cases} minval &amp; t &lt; minval \\ t &amp; minval \leq t \leq maxval \\ maxval &amp; maxval \leq t \end{cases} ans=minvaltmaxvalt<minvalminvaltmaxvalmaxvalt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值