1.利用numpy生成训练数据
2.训练神经网络
3.用matplotlib绘制训练数据和预测曲线
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import tensorflow as tf
mpl.rcParams['xtick.labelsize'] = 24
mpl.rcParams['ytick.labelsize'] = 24
np.random.seed(42)
size=30
x0 = np.linspace(0, 3, size,dtype=np.float32)
y_data = np.sin(x0)+np.random.normal(scale=0.03,size=size).astype(np.float32)
plt.figure('data')
plt.plot(x0, y_data, '.')
x=tf.constant(x0,shape=(size,1))
y_=tf.constant(y_data,shape=(size,1))
bias1=tf.Variable(0.)
bias2=tf.Variable(0.)
w1=tf.Variable(tf.random_normal([1,4],stddev=1,seed=21))
w2=tf.Variable(tf.random_normal([4,1],stddev=1,seed=21))
a=tf.nn.relu(tf.matmul(x,w1)+bias1)
y=tf.nn.relu(tf.matmul(a,w2)+bias2)
global_step=tf.Variable(0)
loss=tf.reduce_mean(tf.square(y_-y))
learning_rate=tf.train.exponential_decay(0.003,global_step,200,0.98,staircase=True)
train_step=tf.train.AdamOptimizer(learning_rate).minimize(loss,global_step=global_step)
steps=60001
with tf.Session() as sess:
init=tf.global_variables_initializer()
sess.run(init)
for i in range(steps):
sess.run(train_step)
if i%2000==0:
res=sess.run(loss)
print("steps=%d loss=%g"%(i,res))
plt.plot(x0,sess.run(y))
plt.show()