使用神经网络来拟合函数y = x^3 +b

 
 

我们使用一个三层的小网络来,模拟函数y = x^3+b函数
 1 import tensorflow as tf
 2 import numpy as np
 3 import matplotlib.pyplot as plt
 4 
 5 #训练数据
 6 x_data = np.linspace(-6.0,6.0,30)[:,np.newaxis]
 7 y_data = np.power(x_data,3) + 0.7
 8 #验证数据
 9 t_data = np.linspace(-20.0,20.0,40)[:,np.newaxis]
10 ty_data = np.power(t_data,3) + 0.7
11 #占位符
12 x = tf.placeholder(tf.float32,[None,1])
13 y = tf.placeholder(tf.float32,[None,1])
14 
15 #network 
16 #--layer one--
17 l_w_1 = tf.Variable(tf.random_normal([1,10]))
18 l_b_1 = tf.Variable(tf.zeros([1,10]))
19 l_fcn_1 = tf.matmul(x, l_w_1) + l_b_1
20 relu_1  = tf.nn.relu(l_fcn_1)
21 #---layer two----
22 l_w_2 = tf.Variable(tf.random_normal([10,20]))
23 l_b_2 = tf.Variable(tf.zeros([1,20]))
24 l_fcn_2 = tf.matmul(relu_1, l_w_2) + l_b_2
25 relu_2  = tf.nn.relu(l_fcn_2)
26 
27 
28 #---output---
29 l_w_3 = tf.Variable(tf.random_normal([20,1]))
30 l_b_3 = tf.Variable(tf.zeros([1,1]))
31 l_fcn_3 = tf.matmul(relu_2, l_w_3) + l_b_3
32 #relu_3  = tf.tanh(l_fcn_3)
33 # init
34 init = tf.global_variables_initializer()
35 #定义 loss func
36 loss = tf.reduce_mean(tf.square(y-l_fcn_3))
37 learn_rate =0.001
38 train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)
39 
40 with tf.Session() as sess:
41     sess.run(init);
42     for epoch in range(20):
43         for step in range(5000):
44             sess.run(train_step,feed_dict={x:x_data,y:y_data})
45     y_pred = sess.run(l_fcn_3,feed_dict={x:t_data})
46     print sess.run(l_fcn_3,feed_dict={x:[[10.]]})
47     plt.figure()
48     plt.scatter(t_data,ty_data)
49     plt.plot(t_data,y_pred,'r-')
50     plt.show()

 


[[ 533.45062256]]
 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值