tensorflow 函数使用杂记

懵懂的女人 提交于 2019-12-08 10:47:49

1. tf.tile

import tensorflow as tf
temp = tf.tile([[1,2,3],[1,2,3]],[1,1])
temp2 = tf.tile([[1,2,3],[1,2,3]],[2,1])
temp3 = tf.tile([[1,2,3],[1,2,3]],[2,2])
with tf.Session() as sess:
    print(sess.run(temp))
    print(sess.run(temp2))
    print(sess.run(temp3))

[[1 2 3]
[1 2 3]]

[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]]

[[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]]

2.tf.reduce_xxx(input_tensor, axis=None, keep_dims=False)

    沿着axis指定的维度上进行相应的xxx操作。当keep_dims=False时返回结果降一维;keep_dims=True时结果维度保持和输入相同。

xxx = sum 即求和

a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_sum(a)
z2=tf.reduce_sum(a,0)
z3=tf.reduce_sum(a,1)

z==>21
z2==>[5 7 9]
z3==>[6 15]

xxx = prob 即求元素的积

a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_prod(a)
z2=tf.reduce_prod(a,0)
z3=tf.reduce_prod(a,1)

z==>720
z2==>[4 10 18]
z3==>[6 120]

xxx = min 即求最小

a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_min(a)
z2=tf.reduce_min(a,0)
z3=tf.reduce_min(a,1)

z==>1
z2==>[1 2 3]
z3==>[1 4]

xxx = all 即求逻辑与

a = tf.constant([[True,True,False,False],[True,False,False,True]])
z=tf.reduce_all(a)
z2=tf.reduce_all(a,0)
z3=tf.reduce_all(a,1)

z==>False
z2==>[True False False False]
z3==>[False False]

xxx = any 即求逻辑或

a = tf.constant([[True,True,False,False],[True,False,False,True]])
z=tf.reduce_any(a)
z2=tf.reduce_any(a,0)
z3=tf.reduce_any(a,1)

z==>True
z2==>[True True False True]
z3==>[True True]

xxx = logsumexp 即按着axis维度计算log(sum(exp()))

a = tf.constant([[0,0,0],[0,0,0]],dtype=tf.float64)
z=tf.reduce_logsumexp(a)
z2=tf.reduce_logsumexp(a,0)
z3=tf.reduce_logsumexp(a,1)

z==>1.79175946923#log(6)
z2==>[0.69314718 0.69314718 0.69314718]#[log(2) log(2) log(2)]
z3==>[1.09861229 1.09861229]#[log(3) log(3)]

3.tf.greater(a, b)

    按元素比较a和b的大小,返回比较后的bool值

a = [-1,0,1,-2]
sess=tf.Session()
with sess.as_default():
    print(tf.greater(a,0).eval())
output : [False, False, True, False]

4.tf.nn.top_k(a, k)

返回top_k后的值和其在原数组中的索引

import tensorflow as tf
import numpy as np

input = tf.constant(np.random.rand(3,4))
k = 2
output = tf.nn.top_k(input, k)
with tf.Session() as sess:
    print(sess.run(input))
    print(sess.run(output))
output:
[[ 0.98925872  0.15743092  0.76471106  0.5949957 ]
 [ 0.95766488  0.67846336  0.21058844  0.2644312 ]
 [ 0.65531991  0.61445187  0.65372938  0.88111084]]
TopKV2(values=array([[ 0.98925872,  0.76471106],
       [ 0.95766488,  0.67846336],
       [ 0.88111084,  0.65531991]]), indices=array([[0, 2],
       [0, 1],
       [3, 0]]))

5.tf.boolen_mask(a,b)

 使a (m维)矩阵仅保留与b中“True”元素同下标的部分,并将结果展开到m-1维,并返回这个m-1维的矩阵

import tensorflow as tf
a = np.random.randn(3, 3,3)
b = np.max(a,-1)
c=  b >0.5
print("a="+str(a))
print("b="+str(b))
print("c="+str(c))
with tf.Session() as sess:
    d=tf.boolean_mask(a,c)
print("d="+str(d.eval(session=sess)))

a=[[[-0.14230925  0.5723012   1.07004655]
  [-0.93580522 -0.44670888 -1.05299618]
  [ 1.14027769  0.72972377 -1.03160013]]

 [[-0.44742032 -0.11966653 -0.52264924]
  [-0.26495305 -0.76930746  1.83077196]
  [ 0.46775164  1.14675178 -0.95681442]]

 [[ 0.15298233  0.17604346  1.56358203]
  [-0.28835174 -1.05588625  0.73805878]
  [ 0.4033133  -0.67741436  0.23416563]]]
b=[[ 1.07004655 -0.44670888  1.14027769]
 [-0.11966653  1.83077196  1.14675178]
 [ 1.56358203  0.73805878  0.4033133 ]]
c=[[ True False  True]
 [False  True  True]
 [ True  True False]]
d=[[-0.14230925  0.5723012   1.07004655]
 [ 1.14027769  0.72972377 -1.03160013]
 [-0.26495305 -0.76930746  1.83077196]
 [ 0.46775164  1.14675178 -0.95681442]
 [ 0.15298233  0.17604346  1.56358203]
 [-0.28835174 -1.05588625  0.73805878]]
    可以看出就是找出了a中在第三维上最大值大于0.5的所有行。此API可以用来帮助筛选符合条件的元素。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!