神经网络–tensorflow函数解析
1【函数】
tf.reduce.mean()
根据给出的axis在input_tensor上求平均值。除非keep_dims为真,axis中的每个的张量秩会减少1。如果keep_dims为真,求平均值的维度的长度都会保持为1.如果不设置axis,所有维度上的元素都会被求平均值,并且只会返回一个只有一个元素的张量。
tf.reduce_mean(
input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None)
【参数】
- input_tensor:需要减少的张量。
- axis:较小的尺寸,None(默认),减少所有维度,范围[-rank(input_tensro),rank[input_tensor]]。
- keep_dims:如果为true,则保留长度为1的缩小尺寸。
- name:操作的名称(可选)。
- reduction_indices:axis的不支持使用的名称。
【返回】
返回减少的张量,相当于np.mean
【代码】
import tensorflow as tf
import numpy as np
with tf.Session as sess:
a = np.array([[1.,2.,3.],[4.,5.,6.]])
with tf.Session() as sess:
#不指定axis/reduction_indices则计算所有元素均值
mean_none = sess.run(tf.reduce_mean(x))
#axis/reduction_indices=0,列元素取均值(第一维)
mean_0 = sess.run(tf.reduce_mean(x,0))
#axis/reduction_indices=1,行元素取均值(第二维)
mean_1 = sess.run(tf.reduce_mean(x,1))
print(x)
print(mean_none)
print(mean_0)
print(mean_1)
【结果】
[[1. 2. 3.]
[4. 5. 6.]
]
3.5
[2.5 3.5 4.5]
[2. 5.]
2【函数】
tf.clip_by_value()
将para转化为[min,max]间的数据,小于min的转化为min,大于max的转化为max。
tf.reduce_mean(para,min,max)
【参数】
- para:需要转化的参数。
- min:左极限。
- max:右极限。
【返回】
返回para在区间[min,max]上的值。
【代码】
import tensorflow as tf
import numpy as np
a = np.array([[1.,2.,3.],[4.,5.,6.]])
with tf.Session() as sess:
clip = sess.run(tf.clip_by_value(a,2,5))
print(clip)
【结果】
[[ 2. 2. 3.]
[ 4. 5. 5.]]
3【函数】
tf.log()
数学公式,计算ln。
tf.log(para,name=None)
【参数】
- para:需要转化的参数。
- name:变量名
【返回】
返回ln(para)的值。
【代码】
import tensorflow as tf
a = tf.constant(2,name='param')
b = tf.log(a)
with tf.Session() as sess:
print(a)
print(a.name)
print(a.eval())
print(b.eval())
【结果】
Tensor("param:0",shape=(),dtype=int32)
param:0
2
0.693147
4【函数】
tf.placeholder()
定义过程,提供输入数据,在执行时赋值,解决数据量过大的问题。
tf.reduce_mean(dtype,shape,name)
【参数】
- dtype:数据类型,常用tf.float32,tf.float64。
- shape:数据维度,shape=(2, 3)表示2x3维数据形式,shape=(None, 2)表示2列,行可根据需求伸缩。
- name:名称。
【函数】
rand()
生成特定维度的数据。
rand(m*n)
【参数】
- m*n:生成mxn维[0,1]范围的数据。
5【函数】
RandomState()
生成随机数。
RandomState(seed)
【参数】
- seed:值为常数生成恒定随机数。
【代码】
import numpy as np
for i in [1,2,3,4]
a = np.random.RandomState(1)
b = np.random.RandomState(None)
#生成1x2维[0,1]的数据
a1 = a.rand(1,2)
b1 = b.rand(1,2)
print(i)
print(a1)
print(b1)
【结果】
1
[[ 0.417022 0.72032449]]
[[ 0.25824913 0.63262141]]
2
[[ 0.417022 0.72032449]]
[[ 0.43648111 0.16871215]]
3
[[ 0.417022 0.72032449]]
[[ 0.3552825 0.81820419]]
4
[[ 0.417022 0.72032449]]
[[ 0.5661055 0.82747111]]
6【函数】
range()
创建整数列表,在for循环中使用。
range(start, stop[,step])
【参数】
- start:从start开始计数,默认0开始。
- end:技术结束,不包括end,如range(3)整数列:0,1,2。
- step:步长。range(20)=>range(0,20)
【返回】
返回para在区间[min,max]上的值。
【代码】
a = range(10)
print(a)
for i in a:
#输出不换行end=''
print(i,end='')
【结果】
range(0,10)
0123456789
7【函数】
tf.global_variables_initializer()
初始化所有定义的变量。
import tensorflow as tf
init_op = global_variables_initializer()
a = tf.Variable(tf.random_normal(2, 3), stddev=1)
b = tf.Variable(tf.zeros([3]))
with tf.Session() as sess:
sess.run(init_op)
传统初始化
sess.run(a.initializer)
sess.run(b.initializer)
8【函数】
tf.greater(a,b)
比较元素a与b大小,返回输出结果,a>b返回True,否则False
【代码】
import tensorflow as tf
v1 = tf.constant([1.0, 2.0, 3.0, 4.0])
v2 = tf.constant([4.0, 3.0, 2.0, 1.0])
sess = tf.InteractiveSession()
print(tf.greater(v1, v2))
【结果】
[False False True True]
9【函数】
tf.where(conditon,x,y,name)
通过判断条件condition,输出相应结果,若condition为True,输出x对应元素,为False输出y对应元素。
【代码】
import tensorflow as tf
v1 = tf.constant([1.0, 2.0, 3.0, 4.0])
v2 = tf.constant([4.0, 3.0, 2.0, 1.0])
sess = tf.InteractiveSession()
print(tf.where(tf.greater(v1, v2),v1,v2))
【结果】
[4 3 3 4]
【解析】
condtion: 0 0 1 1
0对应y的第一个元素4;
0对应y的第二个元素3;
1对应x的第三个元素3;
1对应x的第四个元素4;
[拓展阅读]
[1]二分类神经网络原理及源码解析