1. tf.argmax()
tf.argmax(input,axis)根据axis取值的不同返回每行或者每列最大值的索引.
test = np.array([
[1, 2, 3],
[2, 3, 4],
[5, 4, 3],
[8, 7, 2]])
tf.argmax(test, 0) #输出:[3, 3, 1]
tf.argmax(test, 1) #输出:[2, 2, 0, 0]
*axis=0表示的是“按行”,其实就是对每一列的操作,同理,axis=1按列操作,
也即对每一行。可以类比图像的像素矩阵h*w,第一个坐标轴h的方向是向下的,
第二个坐标轴w的方向是向右的。*
2. tf.reduce_sum( )
reduce_sum( ) 是求和函数,在 tensorflow 里面,计算的都是 tensor,可以通过调整 axis =0,1 的维度来控制求和维度。
test = tf.constant([
[1,1,1],
[1,1,1]])
tf.reduce_sum(test, 0) #输出:[2, 2, 2]
tf.reduce_sum(test, 1) #输出:[3, 3]
tf.reduce_sum(test, [0,1]) #输出:[6]
3. tf.equal()
判断x, y 是不是相等,它的判断方法不是整体判断,而是逐个元素进行判断,如果相等就是True,不相等,就是False。由于是逐个元素判断,所以x,y 的维度要一致。
a = [[1,2,3],[4,5,6]]
b = [[1,0,3],[1,5,1]]
with tf.Session() as sess:
print(sess.run(tf.equal(a,b)))
输出:[[ True False True] [False True False]]
4.tf.cast()
数据类型转换
tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32(tf默认数据类型)。
cast(x, dtype, name=None)
第一个参数 x: 待转换的数据(张量)
第二个参数 dtype: 目标数据类型
第三个参数 name: 可选参数,定义操作的名称
5.tf.random_normal()
从“服从指定正态分布的序列”中随机取出指定个数的值。
tf.random_normal(shape, mean=0.0, stddev=1.0,
dtype=tf.float32, seed=None, name=None)
shape: 输出张量的形状,必选
mean: 正态分布的均值,默认为0
stddev: 正态分布的标准差,默认为1.0
dtype: 输出的类型,默认为tf.float32
seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
name: 操作的名称
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(w1))
输出:
[[-0.81131822 1.48459876 0.06532937]
[-2.4427042 0.0992484 0.59122431]]
6. tf.random_uniform()
tf.random_uniform((6, 6), minval=low,maxval=high,dtype=tf.float32)))
返回6*6的矩阵,产生于low和high之间,产生的值是均匀分布的。
import tensorflow as tf
with tf.Session() as sess:
print(sess.run(tf.random_uniform( (6,6),
minval=-0.5,
maxval=0.5, dtype=tf.float32)))
output:
[[ 0.47818196 -0.0463798 -0.48545432 0.48667777 0.1448754 0.31394303]
[ 0.07446766 0.37638378 0.3001852 -0.1716789 0.03881919 0.14070213]
[ 0.14747012 -0.14895666 -0.35274172 -0.19400203 -0.26068127 0.10212302]
[ 0.29586768 0.16780066 -0.34365273 -0.3228333 0.42329776 0.35237122] [-0.34602797 -0.46733367 0.46615827 -0.20312655 -0.37987483 0.41316974]
[ 0.39296162 0.32745218 -0.32554448 -0.14309132 -0.16133463 0.40627968]]
7. tf.nn.bias_add()以及tf.add()和tf.add_n()
tf.nn.bias_add(value,bias,data_format=None,name=None)
将bias添加到value.
这是tf.add的一种特殊情况,其中bias被限制为一维.支持广播机制numpy广播机制,因此value可以有任意数量的维度.
与tf.add不同的是,在两种类型都是量化的情况下,bias类型允许与value不同.
通俗解释:
一个叫bias的向量加到一个叫value的矩阵上,是向量与矩阵的每一行进行相加,得到的结果和value矩阵大小相同。
tf.add( x,y, name=None):
通俗解释:
这个情况比较多,最常见的是,一个叫x的矩阵和一个叫y的数相加,就是y分别与x的每个数相加,得到的结果和x大小相同。
import tensorflow as tf
a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32)
b=tf.constant([1,-1],dtype=tf.float32)
c=tf.constant([1],dtype=tf.float32)
with tf.Session() as sess:
print('bias_add:')
print(sess.run(tf.nn.bias_add(a, b)))
#执行下面语句错误
#print(sess.run(tf.nn.bias_add(a, c)))
print('add:')
print(sess.run(tf.add(a, c)))
output:
bias_add:
[[ 2. 0.]
[ 3. 1.]
[ 4. 2.]]
add:
[[ 2. 2.]
[ 3. 3.]
[ 4. 4.]]
tf.add_n(inputs,name=None)
通俗解释:
函数是实现一个列表的元素的相加。就是输入的对象是一个列表,列表里的元素可以是向量,矩阵等但没有广播功能
import tensorflow as tf
import numpy as np
input1 = tf.constant([1.0, 2.0, 3.0])
input2 = tf.Variable(tf.random_uniform([3]))
output = tf.add_n([input1, input2]) #注意输入是一个列表
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (sess.run(input1 + input2))
print (sess.run(output))
output:
[1.4135424 2.694611 3.2243743]
[1.4135424 2.694611 3.2243743]
8.tf.transpose(a, perm = None, name = ‘transpose’)
将a进行转置,并且根据perm参数重新排列输出维度。这是对数据的维度的进行操作的形式。
图像处理时数据集中存储数据的形式为[channel,image_height,image_width],在tensorflow中使用CNN时我们需要将其转化为[image_height,image_width,channel]的形式,只需要使用tf.transpose(input_data,[1,2,0])
输出数据tensor的第i维将根据perm[i]指定。比如,如果perm没有给定,那么默认是perm = [n-1, n-2, …, 0],其中rank(a) = n。
默认情况下,对于二维输入数据,其实就是常规的矩阵转置操作。
Example
input_data.dims = (1, 4, 3)
perm = [1, 2, 0]
#因为 output_data.dims[0] = input_data.dims[ perm[0] ]
#因为 output_data.dims[1] = input_data.dims[ perm[1] ]
#因为 output_data.dims[2] = input_data.dims[ perm[2] ]
#所以得到 output_data.dims = (4, 3, 1)
output_data.dims = (4, 3, 1)