想到哪写到哪。也没准哪天直接鸽了。更新速度比较慢,因为还有其他东西要写。
import tensorflow as tf
import numpy as np
A
加法:
tf.add(x,y,name=None)
>>>tf.Session().run(tf.add(3,5))
8
参数更新: 将new_value赋予state
tf.assign(state, new_value)
>>>state = tf.Variable(0, name='counter')
>>>one = tf.constant(1)
>>>new_value = tf.add(state, one)
>>>update = tf.assign(state, new_value)
>>>init = tf.global_variables_initializer()
>>>with tf.Session() as sess:
>>> sess.run(init)
>>> for _ in range(3):
>>> sess.run(update)
>>> print(sess.run(state))
1
2
3
返回最大索引值的序号:
tf.argmax(input, axis)
>>>sess.run(tf.argmax([1,5,3,2,4],axis=0))
1
>>>sess.run(tf.argmax([[1,5,3,2,4]],axis=1))
array([1])
B
C
创建一个常量甚至可以是字符串:
tf.constant(value,dtype=None,shape=None)
>>>a = tf.constant('hello world!')
>>>tf.Session().run(a)
b'hello world!'
布尔值转化为字符串(也可以让一种dtype转化为另一种,格式不变):
tf.cast(input, dtype)
>>>sess.run(tf.cast([False,True,True,False],tf.float32))
array([0., 1., 1., 0.], dtype=float32)
卷积:
tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
# 这里strides 最前面和最后面的1是固定的,中间两个分别为横向纵向移动的跨度
# x为输入端 w 为卷积核(卷积层的滤波器)shape为[长,宽,输入的高度,输出的高度]
# padding 有两种 SAME 和 VALID 请注意大小写,一般用SAME
D
E
判断x,y是否相等:
tf.equal(x,y)
>>>sess.run(tf.equal(3,5))
False
>>>sess.run(tf.equal([1,2,5,3,4],[1,2,3,4,5]))
array([ True, True, False, False, False])
创建单位矩阵:
tf.eye(num_rows,dtype=float32)
>>>sess.run(tf.eye(5))
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]], dtype=float32)
F
G
全局变量初始化(为tensor赋值): 需要在session中运行
tf.global_variables_initializer()
H
I
J
K
L
log(e为底)
tf.log(x)
>>>tf.Session().run(tf.log(2.718))
0.9998963
M
元素之间相乘(x,y必须有相同shape):
tf.multiply(x, y)
>>>sess.run(tf.multiply([1,2,3,4,5],[6,7,8,9,10]))
array([ 6, 14, 24, 36, 50], dtype=int32)
矩阵乘法: x.shape = [a,b] y.shape=[b,c] result.shape=[a,c]
tf.matmul(a,b)
>>>a = np.matrix([1,2,3]).astype('float32')
>>>b = a.T
>>>sess.run(tf.matmul(a,b))
array([[14.]], dtype=float32)
池化:
tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 这里是最大池化(对应另一种方法avg_pool均值池化)
# ksize 第一位和最后一位固定, 中间两位为池化核(池化层滤波器)的形状,这里为[2,2] 一般都是2x2
# strides 为步长,第一位和最后一位依旧固定, 中间两位为横向纵向移动的跨度。
# ksize strides 这里是官方推荐的配置(2x2 移动2个像素)
# padding 依旧分 SAME 和 VALID
N
为多个变量赋予name, 从中我们也能看到tensorflow的命名规则
with tf.name_scope('l1l1l1'):
weights = tf.Variable([1,2,3,4,5])
biases = tf.Variable([6,7,8,9,10])
>>>weights.name
'l1l1l1_1/Variable:0'
>>>biases.name
'l1l1l1_1/Variable_1:0'
O
P
占位符: 这里None表示可以是任何形状[2,1] [3,1]...都可以 通过sess.run喂入数据
tf.placeholder(dtype, shape, name)
>>>xs = tf.placeholder(tf.float32, [None, 1], name='my_input_fn')
>>>sess.run(xs, feed_dict={xs:[[1],[1]]})
array([[1.],
[1.]], dtype=float32)
Q
R
求和: reduction_indices为0时会把每一列相加,为1时把每一行相加,为None时把所有元素相加
tf.reduce_sum(x, reduction_indices=None)
>>>sess.run(tf.reduce_sum(np.arange(12).reshape(3,4)))
66
>>>sess.run(tf.reduce_sum(np.arange(12).reshape(3,4),reduction_indices=[1]))
array([ 6, 22, 38])
>>>sess.run(tf.reduce_sum(np.arange(12).reshape(3,4),reduction_indices=[0]))
array([12, 15, 18, 21])
求均值: 用法与sum类似
tf.reduce_mean(x, reduction_indices=None)
>>>sess.run(tf.reduce_mean(np.arange(12).reshape(3,4),reduction_indices=None))
5
>>>sess.run(tf.reduce_mean(np.arange(12).reshape(3,4),reduction_indices=[0]))
array([4, 5, 6, 7])
>>>sess.run(tf.reduce_mean(np.arange(12).reshape(3,4),reduction_indices=[1]))
array([1, 5, 9])
生成正态分布随机数:
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=float32,seed=None,name=None)
sess.run(tf.random_normal([3,4]))
array([[ 0.8601933 , 0.9524955 , -0.91360956, -0.31951234],
[-1.7907246 , -0.6022119 , 1.0039297 , 0.00579996],
[-1.0887033 , -0.4199891 , 0.70728874, -1.1816795 ]],
dtype=float32)
S
将变量加入到tensorboard hist当中:
tf.summary.histogram(name,value)
将变量加入到tensorboard scalar当中(一般添加loss):
tf.summary.scalar(name,value)
将图形,训练数据等合并,并将图保存下来:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('logs/', sess.graph)
for i in train_step:
# train
if i % 50 ==0:
result = sess.run(merged) #如果用placeholder 别忘了feed数据
writer.add_summary(result, i) # 训练步数i作为坐标轴
创建会话:
sess = tf.Session()
>>>a = tf.constant([1,2,3])
<tf.Tensor 'Const_2:0' shape=(3,) dtype=int32>
>>>sess.run(a)
array([1, 2, 3], dtype=int32)
平方:
tf.square(x)
>>>sess.run(tf.square(np.array([5,4,3,2,1])-np.array([1,2,3,4,5])))
array([16, 4, 0, 4, 16])
开方: 负数会出现nan值(没有考虑复数)
tf.sqrt(x)
>>>sess.run(tf.sqrt(np.array([5,4,3,2,1]).astype('float32')-
np.array([1,2,3,4,5]).astype('float32')))
array([2. , 1.4142135, 0. , nan, nan],
dtype=float32)
T
U
V
创建一个变量,默认可以训练:
tf.Variable(shape, name, trainable=True, dtype=None)
W
X
Y
Z
创建零矩阵:
tf.zeros(shape,dtype=float32)
>>>sess.run(tf.zeros([3,4]))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)