CNN相关函数说明

基本算术运算


操作描述
tf.add(x, y, name=None)求和
tf.sub(x, y, name=None)减法
tf.mul(x, y, name=None)乘法
tf.div(x, y, name=None)除法
tf.mod(x, y, name=None)取模
tf.abs(x, name=None)求绝对值
tf.neg(x, name=None)取负 (y = -x).
tf.sign(x, name=None)返回符号 y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
tf.inv(x, name=None)取反
tf.square(x, name=None)计算平方 (y = x * x = x^2).
tf.round(x, name=None)舍入最接近的整数
# ‘a’ is [0.9, 2.5, 2.3, -4.4]
tf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ]
tf.sqrt(x, name=None)开根号 (y = \sqrt{x} = x^{1/2}).
tf.pow(x, y, name=None)幂次方
# tensor ‘x’ is [[2, 2], [3, 3]]
# tensor ‘y’ is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]
tf.exp(x, name=None)计算e的次方
tf.log(x, name=None)计算log,一个输入计算e的ln,两输入以第二输入为底
tf.maximum(x, y, name=None)返回最大值 (x > y ? x : y)
tf.minimum(x, y, name=None)返回最小值 (x < y ? x : y)
tf.cos(x, name=None)三角函数cosine
tf.sin(x, name=None)三角函数sine
tf.tan(x, name=None)三角函数tan
tf.atan(x, name=None)

三角函数ctan


cast(x, dtype, name=None)
将x的数据格式转化成dtype.例如,原来x的数据格式是bool,
那么将其转化成float以后,就能够将其转化成0和1的序列。反之也可以。
a = tf.Variable([1,0,0,1,1])
b = tf.cast(a,dtype=tf.bool)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(b))

#[ True False False  True  True]

tf.assign(A, new_number): 这个函数的功能主要是把A的值变为new_number
    import tensorflow as tf;  
    A = tf.Variable(tf.constant(0.0), dtype=tf.float32)  
    with tf.Session() as sess:  
        sess.run(tf.initialize_all_variables())  
        print sess.run(A)  
        sess.run(tf.assign(A, 10))  
        print sess.run(A)  
输出:
0.0
10.0

开始给A赋值为0,经过tf.assign函数后,把A的值变为10。

one-hot向量除了某一位的数字是1以外其余各维度数字都是0。

placeholder
x = tf.placeholder( tf.float32, [ None,784 ] )
其中:x 是一个占位符,我们在TensorFlow实际运行计算时再输入这个值。参数[ None,784 ]中的 None 表示我们接受输入任意数量的图像,784 表示每一张图展开成 784 维的向量。

矩阵运算
操作描述
tf.diag(diagonal, name=None)返回一个给定对角值的对角tensor
# ‘diagonal’ is [1, 2, 3, 4]
tf.diag(diagonal) ==>
[[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]
tf.diag_part(input, name=None)功能与上面相反
tf.trace(x, name=None)求一个2维tensor足迹,即对角值diagonal之和
tf.transpose(a, perm=None, name=’transpose’)调换tensor的维度顺序
按照列表perm的维度排列调换tensor顺序,
如为定义,则perm为(n-1…0)
# ‘x’ is [[1 2 3],[4 5 6]]
tf.transpose(x) ==> [[1 4], [2 5],[3 6]]
# Equivalently
tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
tf.matmul(a, b, transpose_a=False,
transpose_b=False, a_is_sparse=False,
b_is_sparse=False, name=None)
矩阵相乘
tf.matrix_determinant(input, name=None)返回方阵的行列式
tf.matrix_inverse(input, adjoint=None, name=None)求方阵的逆矩阵,adjoint为True时,计算输入共轭矩阵的逆矩阵
tf.cholesky(input, name=None)对输入方阵cholesky分解,
即把一个对称正定的矩阵表示成一个下三角矩阵L和其转置的乘积的分解A=LL^T
tf.matrix_solve(matrix, rhs, adjoint=None, name=None)求解tf.matrix_solve(matrix, rhs, adjoint=None, name=None)

matrix为方阵shape为[M,M],rhs的shape为[M,K],output为[M,K]


reshape

tf.reshape(tensor, shape, name=None)
函数的作用是将tensor变换为参数shape的形式。

reshape(t, [3, 3]) ==> [[1, 2, 3],

                       [4, 5, 6],

                       [7, 8, 9]]

softmax

# -*- coding: utf-8 -*-
import tensorflow as tf
A = [1.0,2.0,3.0,4.0,5.0,6.0]
with tf.Session() as sess:
        print sess.run(tf.nn.softmax(A))
结果
[ 0.00426978  0.01160646  0.03154963  0.08576079  0.23312201  0.63369131]

复数操作

 

操作描述
tf.complex(real, imag, name=None)将两实数转换为复数形式
# tensor ‘real’ is [2.25, 3.25]
# tensor imag is [4.75, 5.75]
tf.complex(real, imag) ==> [[2.25 + 4.75j], [3.25 + 5.75j]]
tf.complex_abs(x, name=None)计算复数的绝对值,即长度。
# tensor ‘x’ is [[-2.25 + 4.75j], [-3.25 + 5.75j]]
tf.complex_abs(x) ==> [5.25594902, 6.60492229]
tf.conj(input, name=None)计算共轭复数
tf.imag(input, name=None)
tf.real(input, name=None)
提取复数的虚部和实部
tf.fft(input, name=None)计算一维的离散傅里叶变换,输入数据类型为complex64
归约计算(Reduction)

 reduce就是“对矩阵降维”的含义,下划线后面的部分就是降维的方式,在reduce_sum()中就是按照求和的方式对矩阵降维。那么其他reduce前缀的函数也举一反三了,比如reduce_mean()就是按照某个维度求平均值,等等。

操作描述
tf.reduce_sum(input_tensor, reduction_indices=None,
keep_dims=False, name=None)


计算输入tensor元素的和,或者安照reduction_indices指定的轴进行求和

# ‘x’ is [[1, 1, 1]
# [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]

tf.reduce_sum(x, [0, 1]) ==> 6


当arg2 = 0时,是纵向对矩阵求和,原来矩阵有几列就得到几个值;

相似地,当arg2 = 1时,是横向对矩阵求和;当省略arg2参数时,

默认对矩阵所有元素进行求和。

tf.reduce_prod(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
计算输入tensor元素的乘积,或者安照reduction_indices指定的轴进行求乘积
tf.reduce_min(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
求tensor中最小值
tf.reduce_max(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
求tensor中最大值
tf.reduce_mean(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
求tensor中平均值
tf.reduce_all(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
对tensor中各个元素求逻辑’与’
# ‘x’ is
# [[True, True]
# [False, False]]
tf.reduce_all(x) ==> False
tf.reduce_all(x, 0) ==> [False, False]
tf.reduce_all(x, 1) ==> [True, False]
tf.reduce_any(input_tensor,
reduction_indices=None,
keep_dims=False, name=None)
对tensor中各个元素求逻辑’或’
tf.accumulate_n(inputs, shape=None,
tensor_dtype=None, name=None)
计算一系列tensor的和
# tensor ‘a’ is [[1, 2], [3, 4]]
# tensor b is [[5, 0], [0, 6]]
tf.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]]
tf.cumsum(x, axis=0, exclusive=False,
reverse=False, name=None)
求累积和
tf.cumsum([a, b, c]) ==> [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0]
序列比较与索引提取(Sequence Comparison and Indexing)

 

操作描述
tf.argmin(input, dimension, name=None)返回input最小值的索引index
tf.argmax(input, dimension, name=None)返回input最大值的索引index
tf.listdiff(x, y, name=None)返回x,y中不同值的索引
tf.where(input, name=None)返回bool型tensor中为True的位置
# ‘input’ tensor is
#[[True, False]
#[True, False]]
# ‘input’ 有两个’True’,那么输出两个坐标值.
# ‘input’的rank为2, 所以每个坐标为具有两个维度.
where(input) ==>
[[0, 0],
[1, 0]]
tf.unique(x, name=None)返回一个元组tuple(y,idx),y为x的列表的唯一化数据列表,
idx为x数据对应y元素的index
# tensor ‘x’ is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
tf.invert_permutation(x, name=None)置换x数据与索引的关系
# tensor x is [3, 4, 0, 2, 1]
invert_permutation(x) ==> [2, 4, 3, 0, 1]

函数

tf.random_normal(shape, mean=0.0,stddev=1.0, dtype=tf.float32, seed=None, name=None)

从正态分布中输出随机值。

参数:

   shape: 一维的张量,也是输出的张量。

   mean: 正态分布的均值。

   stddev: 正态分布的标准差。

   dtype: 输出的类型。

   seed: 一个整数,当设置之后,每次生成的随机数都一样。

   name: 操作的名字。

代码
norm = tf.random_normal([2, 3], seed=1234)
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))
输出:
[[ 0.51340485 -0.25581399 0.65199131]
[ 1.39236379 0.37256798 0.20336303]]
[[ 0.96462417 0.34291974 0.24251089]
[ 1.05785966 1.65749764 0.82108968]]

tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
除去name参数用以指定该操作的name,与方法有关的一共五个参数:
第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式(后面会介绍)
第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

结果返回一个Tensor,这个输出,就是我们常说的feature map,shape仍然是[batch, height, width, channels]这种形式。

激活函数

tf.nn.relu(features, name = None)
解释:这个函数的作用是计算激活函数relu,即max(features, 0)。

其他还有sigmoid,

softmax_cross_entropy_with_logits

看下例子就明白了,主要还是要弄明白softmax,如下:

import tensorflow as tf

#our NN's output
logits=tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])
#step1:do softmax
y=tf.nn.softmax(logits)
#true label
y_=tf.constant([[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,0.0,1.0]])
#step2:do cross_entropy
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
#do cross_entropy just one step
cross_entropy2=tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits, y_))#dont forget tf.reduce_sum()!!

with tf.Session() as sess:
    softmax=sess.run(y)
    c_e = sess.run(cross_entropy)
    c_e2 = sess.run(cross_entropy2)
    print("step1:softmax result=")
    print(softmax)
    print("step2:cross_entropy result=")
    print(c_e)
    print("Function(softmax_cross_entropy_with_logits) result=")
    print(c_e2)

结果:

step1:softmax result=
[[ 0.09003057  0.24472848  0.66524094]
 [ 0.09003057  0.24472848  0.66524094]
 [ 0.09003057  0.24472848  0.66524094]]
step2:cross_entropy result=
1.22282
Function(softmax_cross_entropy_with_logits) result=
1.2228

tf.nn.dropout

       例子:曲线存在overfitting问题,尽管它经过了所有的训练点,但是不能很好的反应数据的趋势,预测能力严重不足,对新测试数据的适应性较差。 TensorFlow提供了强大的dropout方法来解决overfitting问题,是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层。

tf.nn.dropout(x, keep_prob, noise_shape = None, seed = None, name = None)
解释:这个函数的作用是计算神经网络层的dropout。放置过拟合。

第一个参数x:指输入。
第二个参数keep_prob: 设置神经元被选中的概率,网上解释有很多种,暂不清楚。

tf.summary

scalars:标量显示
tf.summary.scalar

histograms:变量显示
tf.summary.histogram

images:展示训练过程中记录的图像
tf.summary.image

graphs:计算图显示
tf.summary.FileWritter(path,sess.graph)

distribution:变量分布
tf.summary.histogram

embeddings:变量embeddings
加载ckpt文件,但是变量也要保存

audio:展示训练过程中记录的音频
tf.summary.audio

text:展示文本输入类型为Tensor
tf.summary.text

Session是Tensorflow中的一个重要概念,Tensorflow中的所有计算都构建在一张计算图中,这是一种对数学运算过程的可视化方法。session就是负责让图运算起来,session的主要任务就是负责分配GPU或者CPU的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值