#1.Tensorflow用图(graph)来表示计算任务,图中的节点被称之为operation(op);
#2.一个节点获得0个或多个张量tensor,执行计算,产生0个或多个张量;
#3.图必须在会话(Session)里被启动,会话(Session)将图中的op分发到CPU或GPU之类的设备上,
# 同时提供执行op的方法,这些方法执行后,将产生的张量(tensor)返回;
1 import tensorflow as tf
2
3 #创建一个常量tensor
4 m1 = tf.constant([[3,3]])
5 #创建一个常量tensor
6 m2 = tf.constant([[2],[3]])
7 #创建一个矩阵乘法op,把m1和m2传入
8 product = tf.matmul(m1,m2)
9 print(product)
10
11 >>Tensor("MatMul:0", shape=(1, 1), dtype=int32)
12
13 #定义一个会话,启动默认图
14 sess = tf.Session()
15 #调用sess的run方法来执行矩阵乘法op
16 #run(product)触发了图中3个op
17 result = sess.run(product)
18 print(result)
19 sess.close()
20
21 >>[[15]]
22
23 with tf.Session() as sess:
24 #调用sess的run方法来执行矩阵乘法op
25 #run(product)触发了图中3个op
26 result = sess.run(product)
27 print(result)
28
29 >>[[15]]
eg2.变量
1 import tensorflow as tf
2
3 #1.创建Tensors(变量)
4 #2.编写Tensors间的操作符op
5 #3.初始化Tensors
6 #4.创建Session
7 #5.运行Session
8
9 y_hat = tf.constant(36,name='y_hat') #定义一个常量y_hot,值为36
10 y = tf.constant(39,name='y') #定义一个常量y,值为39
11
12 loss = tf.Variable((y-y_hat)**2,name='loss') #创建一个变量
13
14 init = tf.global_variables_initializer() #当执行session.run(init)时,变量loss会被初始化并且做好了计算的准备
15
16 with tf.Session() as sess: #创建一个Session会话用于print输出
17 sess.run(init) #初始化变量
18 print(sess.run(loss)) #运行Session会话并输出loss
1 x = tf.Variable([1,2])
2 a = tf.constant([3,3])
3 #增加一个减法op
4 sub = tf.subtract(x,a)
5 #添加一个加法op
6 add = tf.add(x,sub)
7 #初始化
8 init = tf.global_variables_initializer()
9 #定义一个会话
10 with tf.Session() as sess:
11 sess.run(init)
12 print(sess.run(sub))
13 print(sess.run(add))
eg3.实现加一操作
1 #创建一个变量初始化为0
2 state = tf.Variable(0,name='counter')
3 #创建一个op,作用是使state加1
4 new_value = tf.add(state,1)
5 #赋值op
6 update = tf.assign(state,new_value)
7 #变量初始化
8 init = tf.global_variables_initializer()
9
10 with tf.Session() as sess:
11 sess.run(init)
12 print(sess.run(state))
13 #循环5次
14 for _ in range(5): #循环5次
15 sess.run(update)
16 print(sess.run(state))
17
18 0
19 1
20 2
21 3
22 4
23 5
eg4.Fetch and Feed
1 import tensorflow as tf
2
3 #Fetch同时运行多个op
4 input1 = tf.constant(3.0)
5 input2 = tf.constant(2.0)
6 input3 = tf.constant(5.0)
7
8 add = tf.add(input2,input3)
9 mul = tf.multiply(input1,add)
10 #定义一个会话
11 with tf.Session() as sess:
12 result = sess.run([mul,add]) #sess.run(fetches)
13 print(result)
1 #Feed
2 #创建占位符,即placeholder对象
3 input1 = tf.placeholder(tf.float32)
4 input2 = tf.placeholder(tf.float32)
5 output = tf.multiply(input1,input2)
6
7 with tf.Session() as sess:
8 #feed的数据以字典的形式传入
9 print(sess.run(output,feed_dict={input1:7.0,input2:3.0})) #Shift+Tab快捷键
1 def sigmoid(z):
2
3 #创建一个placeholder对象
4 x = tf.placeholder(tf.float32,name='x')
5
6 #利用tf的sigmoid()函数计算sigmoid
7 sigmoid = tf.sigmoid(x)
8
9 #创建Session并运行
10 #通过feed_dict把z的值传给x
11 with tf.Session() as sess:
12 result = sess.run(sigmoid,feed_dict={x:z}) #调用的时候通过feed_dict把值传入(字典形式)
13 return result
1 #分别使用feeds和fetches来填充数据和抓取任意的操作结果
2 import tensorflow as tf
3 import numpy as np
4
5 #用numpy随机生成100个数据
6 x_data = np.float32(np.random.rand(2,100)) #2*100
7 y_data = np.dot([0.100,0.200],x_data) + 0.300 #点乘1*100
8
9 #构建一个线性模型
10 b = tf.Variable(tf.zeros([1])) #偏置值初始化0
11 W = tf.Variable(tf.random_uniform([1,2],-1.0,1.0))#权值1*2
12 y = tf.matmul(W,x_data) + b #矩阵乘法
13
14 #最小化方差
15 loss = tf.reduce_mean(tf.square(y-y_data)) #二次代价函数
16 optimizer = tf.train.GradientDescentOptimizer(0.5)#梯度下降法
17 train = optimizer.minimize(loss) #使loss值最小
18
19 #初始化变量
20 init = tf.initialize_all_variables()
21
22 #启动图(graph)
23 sess = tf.Session()
24 sess.run(init)
25
26 #拟合平面
27 for step in range(0,201):
28 sess.run(train)
29 if step % 20 ==0:
30 print(step,sess.run(W),sess.run(b))
31 >>>
32 0 [[ 0.02679617 0.70638967]] [ 0.14400235]
33 20 [[ 0.09697476 0.33242896]] [ 0.23280086]
34 40 [[ 0.10429206 0.23750198]] [ 0.27829513]
35 60 [[ 0.10234147 0.21088448]] [ 0.29313114]
36 80 [[ 0.10091921 0.20321539]] [ 0.29785267]
37 100 [[ 0.10032131 0.20096155]] [ 0.29933372]
38 120 [[ 0.10010624 0.20028993]] [ 0.29979423]
39 140 [[ 0.10003407 0.2000879 ]] [ 0.29993665]
40 160 [[ 0.10001072 0.20002674]] [ 0.29998055]
41 180 [[ 0.10000334 0.20000815]] [ 0.29999405]
42 200 [[ 0.10000104 0.20000252]] [ 0.29999816]
2019-05-30 10:24:15