tensorflow里面函数记录

numpy中axis参数说明

https://www.jianshu.com/p/25e3d216e5bd

axis=i,即沿着数组第i个下标的变化方向进行操作。
这里我们用numpy.sum(axis=i)进行说明

举例说明:

k = np.reshape(np.arange(24), [3, 2, 4])
print(k)

[[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]

 [[16 17 18 19]
  [20 21 22 23]]]

这是一个(3,2,4)维的矩阵,那么对于每个元素,均可用3个下标i,j,k表示,如:

k[i][j][k]
k[0][0][0]=0
k[0][0][3]=3
k[0][1][0]=4
k[1][0][0]=8
k[2][1][3]=23
# 坐标轴的取值范围
i={0,1,2}
j={0,1}
k={0,1,2,3}

求和操作是降维操作,k有三个轴,分别按照这三个轴进行求和

axis=0

axis=0,即沿着第0个坐标i下标变化的方向进行求和,第0轴降维,得到的shape=(2,4)

print(k.sum(axis=0))
print(k.sum(axis=0).shape)

[[24 27 30 33]
 [36 39 42 45]]
(2, 4)

下面我们进行手算,沿着第0个坐标遍历求和:

# 沿着第三个坐标 k=0,1,2,3,再沿着第二个坐标j=0,1,共计算8次
# 坐标轴的取值范围
# i={0,1,2}
# j={0,1}
# k={0,1,2,3}

k[0][0][0]+k[1][0][0]+k[2][0][0]=24
k[0][0][1]+k[1][0][1]+k[2][0][1]=27
k[0][0][2]+k[1][0][2]+k[2][0][2]=30
k[0][0][3]+k[1][0][3]+k[2][0][3]=33

k[0][1][0]+k[1][1][0]+k[2][1][0]=36
k[0][1][1]+k[1][1][1]+k[2][1][1]=39
k[0][1][2]+k[1][1][2]+k[2][1][2]=42
k[0][1][3]+k[1][1][3]+k[2][1][3]=45

sum = np.reshape(np.array([24, 27, 30, 33, 36, 39, 42, 45]), (2, 4))
sum = array([[24, 27, 30, 33],
             [36, 39, 42, 45]])

axis=1

axis=1,即沿着第1个坐标下标j变化的方向进行求和,第1轴降维,得到的shape=(3,4)

print(k.sum(axis=1))
print(k.sum(axis=1).shape)

[[ 4  6  8 10]
 [20 22 24 26]
 [36 38 40 42]]
(3, 4)

这时,我们沿着第1个坐标j进行手动求和:

# 坐标轴的取值范围
# i={0,1,2}
# j={0,1}
# k={0,1,2,3}

k[0][0][0]+k[0][1][0]=4
k[0][0][1]+k[0][1][1]=6
k[0][0][2]+k[0][1][2]=8
k[0][0][3]+k[0][1][3]=10

k[1][0][0]+k[1][1][0]=20
k[1][0][1]+k[1][1][1]=22
k[1][0][2]+k[1][1][2]=24
k[1][0][3]+k[1][1][3]=26

k[2][0][0]+k[2][1][0]=36
k[2][0][1]+k[2][1][1]=38
k[2][0][2]+k[2][1][2]=40
k[2][0][3]+k[2][1][3]=42

sum = np.reshape(np.array([4, 6, 8, 10, 20, 22, 24, 26, 36, 38, 40, 42]), (3, 4))
sum = array([[ 4,  6,  8, 10],
             [20, 22, 24, 26],
             [36, 38, 40, 42]])

axis=2,axis=-1,在Python中表示最后一个

axis=2,即沿着第2个坐标下标k变化的方向进行求和,第2轴降维,得到的shape=(3,2)

print(k.sum(axis=2))
print(k.sum(axis=2).shape)

[[ 6 22]
 [38 54]
 [70 86]]
(3, 2)

还是一样的,我们沿着k坐标的变化进行计算:

# 坐标轴的取值范围
# i={0,1,2}
# j={0,1}
# k={0,1,2,3}

k[0][0][0]+k[0][0][1]+k[0][0][2]+k[0][0][3]=6
k[0][1][0]+k[0][1][1]+k[0][1][2]+k[0][1][3]=22

k[1][0][0]+k[1][0][1]+k[1][0][2]+k[1][0][3]=38
k[1][1][0]+k[1][1][1]+k[1][1][2]+k[1][1][3]=54

k[2][0][0]+k[2][0][1]+k[2][0][2]+k[2][0][3]=70
k[2][1][0]+k[2][1][1]+k[2][1][2]+k[2][1][3]=86

sum = np.reshape(np.array([6, 22, 38, 54, 70, 86]), (3, 2))
sum = array([[ 6, 22],
             [38, 54],
             [70, 86]])

tensorflow中的axis

import tensorflow as tf

f1 = tf.argmax([[1, 5, 8, 2],
                [3, 2, 6, 7],
                [7, 4, 1, 5]], axis=0)


f2 = tf.argmax([[1, 5, 8, 2],
                [3, 2, 6, 7],
                [7, 4, 1, 5]], axis=1)


f3 = tf.argmax([[[1, 5, 8, 2],
                [3, 2, 6, 7],
                [7, 4, 1, 5]],

                [[1, 5, 8, 2],
                 [3, 2, 6, 7],
                 [7, 4, 1, 5]]], axis=2)

f4 = tf.argmax([[[1, 5, 8, 2],
                [3, 2, 6, 7],
                [7, 4, 1, 5]],

                [[1, 5, 8, 2],
                 [3, 2, 6, 7],
                 [7, 4, 1, 5]]], axis=1)


with tf.Session() as sess:
    print(sess.run(f1))
    print(sess.run(f2))
    print(sess.run(f3))
    print(sess.run(f4))

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

 

1.tf.greater(a,b)

#-*-coding:utf-8-*-
import tensorflow as tf

sess = tf.Session()
with sess.as_default():
    print(tf.greater([[1, 2, 3, 4], [5, 6, 7, 8]], 3).eval())

[[False False False  True]
 [ True  True  True  True]]

 2.tf.cast()函数

函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32。

import tensorflow as tf
 
t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)
 
print 't1: {}'.format(t1)
print 't2: {}'.format(t2)
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(t2)
    print t2.eval()
    # print(sess.run(t2))

t1: <tf.Variable 'Variable:0' shape=(5,) dtype=int32_ref>
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1.  2.  3.  4.  5.]

3.tf.maximum()函数

其实和numpy.maximum()这个函数一样

tf.maximum:用法tf.maximum(a,b),返回的是a,b之间的最大值,

tf.miniimum:用法tf.miiinimum(a,b),返回的是a,b之间的最小值,

tf.argmax:用法tf.argmax(a,dimension),返回的是a中的某个维度最大值的索引,

tf.argmain:用法tf.argmin(a,dimension),返回的是a中的某个维度最小值的索引

import tensorflow as tf

f1 = tf.maximum([1, 5, 3], 3)
f2 = tf.maximum([1, 2, 3], [4, 1, 2])
f3 = tf.argmax([1, 2, 3], 0)

with tf.Session() as sess:
    print(sess.run(f1))  # print f1.eval()
    print(sess.run(f2))
    print(sess.run(f3))

4.tf.reduce_max()函数

相当于numpy.max()函数

reduce_max(
    input_tensor,
    axis=None,
    keep_dims=False,
    name=None,
    reduction_indices=None
)
#-*-coding:utf-8-*-
import numpy as np
import tensorflow as tf

prediction = np.array([[0.53, 0.56, 0.96], [0.55, 0.66, 0.59], [0.676, 0.897, 0.965],
                  [0.333, 0.26, 0.36], [0.55, 0.56, 0.80], [0.665, 0.87, 0.35]])

classes = tf.argmax(prediction, axis=1)
scores = tf.reduce_max(prediction, axis=1)
classes1 = np.argmax(prediction, axis=1)
scores1 = np.max(prediction, axis=1)
print(classes1)
print(scores1)

with tf.Session() as sess:
    print('classes:', sess.run(classes))
    print('scores:', sess.run(scores))

 

[2 1 2 2 2 1]
[ 0.96   0.66   0.965  0.36   0.8    0.87 ]
classes: [2 1 2 2 2 1]
scores: [ 0.96   0.66   0.965  0.36   0.8    0.87 ]

5.初始化

tf.zeros()

tf.ones()

#-*-coding:utf-8-*-
import tensorflow as tf

zeros = tf.zeros(shape=(3, 3), dtype=tf.float32)
ones = tf.ones(shape=(3, 3), dtype=tf.float32)

with tf.Session() as sess:
    print('zeros:', sess.run(zeros), end='\n')
    print('ones:', sess.run(ones), end='\n')

zeros: [[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
ones: [[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

6.tf.greater()

比较函数,返回bool类型

#-*-coding:utf-8-*-
import tensorflow as tf

with tf.Session() as sess:
    print(sess.run(tf.greater([2, 3], [3, 2])))
    print(sess.run(tf.greater([2, 4], 3)))

 [False  True]
[False  True]

7.tf.logical_and,tf.logical_or

np.logical_and(逻辑与)一样

np.logical_or(逻辑或)

# -*-coding:utf-8-*-
import tensorflow as tf
import numpy as np

mask1 = tf.logical_and(True, False)
mask2 = tf.logical_and([True, False], [False, True])
x = np.arange(5)
mask3 = tf.logical_and(x > 1, x < 4)

with tf.Session() as sess:
    print(sess.run(mask1))
    print(sess.run(mask2))
    print(sess.run(mask3))

False
[False False]
[False False  True  True False]

8. tf.while_loop()

用法:

final_state = tf.while_loop(cond, loop_body, init_state)

  1.     cond 是一个函数,负责判断继续执行循环的条件。
  2.     loop_body 是每个循环体内执行的操作,负责对循环状态迸行更新。
  3.     init_state 为循环的起始状态,它可以包含多个 Tensor 或者 TensorArray 。
  4.     返回的结果是循环结束时的循环状态。
import tensorflow as tf

a = tf.get_variable("ii", dtype=tf.int32, shape=[], initializer=tf.ones_initializer())
n = tf.constant(10)


def cond(a, n):
    return a < n


def body(a, n):
    a = a + 1
    return a, n


a, n = tf.while_loop(cond, body, [a, n])
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    res = sess.run([a, n])
    print(res)

[10, 10] 

9.tf.concat()

tensorflow中用来拼接张量的函数tf.concat(),用法:

tf.concat([tensor1, tensor2, tensor3,...], axis)

先给出tf源代码中的解释:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1)  # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

这里解释了当axis=0和axis=1的情况,怎么理解这个axis呢?其实这和numpy中的np.concatenate()用法是一样的。

axis=0     代表在第0个维度拼接

axis=1     代表在第1个维度拼接  
 

10.tf.stack()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值