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]=33k[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]=45sum = 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]=10k[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]=26k[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]=42sum = 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]=22k[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]=54k[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]=86sum = 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 tff1 = 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 tfsess = 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 tft1 = 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 tff1 = 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 tfprediction = 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 tfzeros = 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 tfwith 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 npmask1 = 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 tfa = tf.get_variable("ii", dtype=tf.int32, shape=[], initializer=tf.ones_initializer())
n = tf.constant(10)def cond(a, n):return a < ndef body(a, n):a = a + 1return a, na, 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()

tensorflow里面函数记录相关推荐

  1. tensorflow函数记录

    tensorflow函数记录 函数类型一 tf.stack() axis=0,1 tf.reduce_sum() axis=0,1 tf.concat() axis=0,1 功能快捷键 合理的创建标题 ...

  2. MATLAB【十三】————仿真函数记录以及matlab变成小结

    part one:matlab 编程小结. 1.char 与string的区别,char使用的单引号 '' ,string使用的是双引号"". 2.一般标题中的输出一定要通过 nu ...

  3. tensorflow学习函数笔记

    为什么80%的码农都做不了架构师?>>>    [TensorFlow教程资源](https://my.oschina.net/u/3787228/blog/1794868](htt ...

  4. 『TensorFlow』函数查询列表_张量属性调整

    博客园 首页 新随笔 新文章 联系 订阅 管理 『TensorFlow』函数查询列表_张量属性调整 数据类型转换Casting 操作 描述 tf.string_to_number (string_te ...

  5. 我在Suse 11 Sp3上使用anaconda安装TensorFlow的过程记录

    我在Suse 11 Sp3上使用anaconda安装TensorFlow的过程记录准备安装包: gcc48 glibc-2.18.tar.gz SLES-11-SP4-DVD-x86_64-GM-DV ...

  6. tensorflow学习之常用函数总结:tensorflow.placeholder()函数

    tensorflow.placeholder()函数 tensorflow.placeholder(dtype, shape=None, name=None) 此函数可以理解为形参,用于定义过程,在执 ...

  7. tensorflow学习之常用函数总结:tensorflow.argmax()函数

    tensorflow.argmax()函数 tf.argmax(input, axis=None, name=None, dimension=None) 此函数是对矩阵按行或列计算最大值 参数 inp ...

  8. 记录 之 tensorflow 常用函数:tf.split(),tf.clip_by_value() 和 tf.cond()

    1.tf.split(axis, num_or_size_splits,value) 该函数是通道拆分函数,将原来的的多通道tensor,拆分为单通道 axis:拆分的维度 num_or_size_s ...

  9. Tensorflow常用函数汇总

    转载自:http://blog.csdn.net/lenbow/article/details/52152766 1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段 ...

最新文章

  1. 管理员信息管理之保存管理员数据
  2. c++primer plus 第13章 编程题第2题
  3. 已经yum安装的基础上,升级编译安装git
  4. DPM2012保护sharepoint场
  5. mybatis连接oracle_Mybatis 系列 0:初恋Mybatis
  6. RabbitMQ消息队列———安装(一)
  7. Redis发布订阅模式
  8. 19C新特性:Voting Disk管理
  9. java io流缓冲理解
  10. OverIQ 中文系列教程【翻译完成】
  11. 和平精英连接服务器未响应,和平精英触控失灵怎么回事 操作触屏有时候没反应介绍...
  12. 机器学习实战(MachineLearinginAction) 第一章
  13. 一次完整的zabbix监控配置
  14. 第一段冲刺 站立会议 5.5
  15. paip.svn使用小结
  16. linkin大话设计模式--命令模式
  17. PostgreSQL数据库日常学习笔记13-约束
  18. 图片尺寸怎么修改?分享2种方法快速修改图片尺寸大小
  19. 用php和mysql写一个注册登录页面
  20. 深入理解L0,L1和L2正则化

热门文章

  1. 0064-简单的平方和
  2. bzoj 1026: [SCOI2009]windy数
  3. android 相对布局例子代码
  4. GCD API记录(二)
  5. 关于0bug商用之道的第三章的少用模板
  6. 【数据结构】栈的存储实现
  7. 以服务器时间为基准显示到某一时间的倒计时
  8. C盘空间越来越小怎么办,教你27招
  9. 【elasticsearch 】logstash elasticsearch output plugin 的阻塞问题
  10. 软件测试人员的三重境界