正文
Tensorflow神经网络进行fiting function
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
使用Tensorflow中的神经网络来拟合函数(y = x ^ 3 + 0.7)
# -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt#训练数据
x_data = np.linspace(-6.0,6.0,30)[:,np.newaxis]
y_data = np.power(x_data,3) + 0.7
#验证数据
t_data = np.linspace(-20.0,20.0,40)[:,np.newaxis]
ty_data = np.power(t_data,3) + 0.7
#占位符
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])#network
#--layer one--
l_w_1 = tf.Variable(tf.random_normal([1,10]))
l_b_1 = tf.Variable(tf.zeros([1,10]))
l_fcn_1 = tf.matmul(x, l_w_1) + l_b_1
relu_1 = tf.nn.relu(l_fcn_1)
#---layer two----
l_w_2 = tf.Variable(tf.random_normal([10,20]))
l_b_2 = tf.Variable(tf.zeros([1,20]))
l_fcn_2 = tf.matmul(relu_1, l_w_2) + l_b_2
relu_2 = tf.nn.relu(l_fcn_2)#---output---
l_w_3 = tf.Variable(tf.random_normal([20,1]))
l_b_3 = tf.Variable(tf.zeros([1,1]))
l_fcn_3 = tf.matmul(relu_2, l_w_3) + l_b_3
#relu_3 = tf.tanh(l_fcn_3)
# init
init = tf.global_variables_initializer()
#定义 loss func
loss = tf.reduce_mean(tf.square(y-l_fcn_3))
learn_rate =0.001
train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)with tf.Session() as sess:
sess.run(init);
for epoch in range(20):
for step in range(5000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
y_pred = sess.run(l_fcn_3,feed_dict={x:t_data})
print sess.run(l_fcn_3,feed_dict={x:[[10.]]})
plt.figure()
plt.scatter(t_data,ty_data)
plt.plot(t_data,y_pred,'r-')
plt.show()
实验结果