003-TensorFlow简单使用

回归:

 

 1 import tensorflow as tf
 2 import numpy as np
 3 import matplotlib.pyplot as plt
 4 
 5 #使用numpy生成200个随机点
 6 x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
 7 noise = np.random.normal(0,0.02,x_data.shape)
 8 y_data = np.square(x_data)+noise
 9 
10 
11 #定义2个placeholder
12 x = tf.placeholder(tf.float32,[None,1])
13 y = tf.placeholder(tf.float32,[None,1])
14 
15 
16 #定义神经网络中间层
17 #权重,1个x,10个权重参数
18 Weight_L1 = tf.Variable(tf.random_normal([1,10]))
19 #偏置,1个x,10个b
20 biases_L1 = tf.Variable(tf.zeros([1,10]))
21 #得分函数:
22 Wx_plas_b_L1 = tf.matmul(x,Weight_L1)+biases_L1
23 #激活函数:
24 L1 = tf.nn.tanh(Wx_plas_b_L1)
25 
26 #定义输出层
27 #全连接层:
28 Weight_L2 = tf.Variable(tf.random_normal([10,1]))
29 biases_L2 = tf.Variable(tf.zeros([1,1]))
30 Wx_plas_b_L2 = tf.matmul(L1,Weight_L2)+biases_L2
31 #预测结果
32 prediction = tf.nn.tanh(Wx_plas_b_L2)
33 
34 #二次代价函数
35 loss = tf.reduce_mean(tf.square(y-prediction))
36 #使用梯度下降:学习率0.1
37 train_step  =tf.train.GradientDescentOptimizer(0.1).minimize(loss)
38 
39 with tf.Session() as sess:
40     #变量初始化
41     sess.run(tf.global_variables_initializer())
42     for _ in range(20000):
43         sess.run(train_step,feed_dict = {x:x_data,y:y_data})
44         if _%200 ==0:
45             print(_,sess.run(loss,feed_dict = {x:x_data,y:y_data}))
46     
47     #获得预测值:
48     prediction_value = sess.run(prediction,feed_dict = {x:x_data})
49     
50     #画图
51     plt.figure()
52     plt.scatter(x_data,y_data)
53     plt.plot(x_data,prediction_value,'r-',lw = 5)
54     plt.show()

 

  

0 0.554074
200 0.00908821
400 0.000556468
600 0.000509932
800 0.0004985
1000 0.000490006
1200 0.000483575
1400 0.000478674
1600 0.000474905
1800 0.000471979
2000 0.000469677
2200 0.00046784
2400 0.000466348
2600 0.000465115
2800 0.000464074
3000 0.000463177
3200 0.000462388
3400 0.000461681
3600 0.000461035
3800 0.000460436
4000 0.000459873
4200 0.000459339
4400 0.000458826
4600 0.00045833
4800 0.000457848
5000 0.000457378
5200 0.000456916
5400 0.000456463
5600 0.000456017
5800 0.000455578
6000 0.000455142
6200 0.000454712
6400 0.000454287
6600 0.000453867
6800 0.000453453
7000 0.000453042
7200 0.000452634
7400 0.000452229
7600 0.000451829
7800 0.000451432
8000 0.000451038
8200 0.000450648
8400 0.00045026
8600 0.000449876
8800 0.000449493
9000 0.000449114
9200 0.000448737
9400 0.000448363
9600 0.000447992
9800 0.000447626
10000 0.000447263
10200 0.000446902
10400 0.000446542
10600 0.000446185
10800 0.00044583
11000 0.000445481
11200 0.000445135
11400 0.000444791
11600 0.000444448
11800 0.000444108
12000 0.000443769
12200 0.000443433
12400 0.000443103
12600 0.000442775
12800 0.000442447
13000 0.000442122
13200 0.000441799
13400 0.000441477
13600 0.00044116
13800 0.000440845
14000 0.000440533
14200 0.000440222
14400 0.000439913
14600 0.000439605
14800 0.000439299
15000 0.000438997
15200 0.000438698
15400 0.000438401
15600 0.000438105
15800 0.000437811
16000 0.000437519
16200 0.000437228
16400 0.000436939
16600 0.000436654
16800 0.000436371
17000 0.00043609
17200 0.00043581
17400 0.000435532
17600 0.000435254
17800 0.000434981
18000 0.000434708
18200 0.000434438
18400 0.000434169
18600 0.000433902
18800 0.000433637
19000 0.000433372
19200 0.00043311
19400 0.00043285
19600 0.000432592
19800 0.000432335
 

 

蓝色的点是原始输入,红色的线是拟合的结果。

 

 

posted on 2018-10-21 17:42  医疗兵皮特儿  阅读(192)  评论(0编辑  收藏  举报

导航