-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.1.py
37 lines (33 loc) · 1.27 KB
/
2.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#生成样本点shengcheng200哥 200行一列
x_data= np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) +noise
# 定义两个placeholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
#定义 神经网络中间问题
weights_l1 = tf.Variable(tf.random.normal([1,10]))
biasse_l1 = tf.Variable(tf.zeros([1, 10]))
wx_plus = tf.matmul(x, weights_l1) +biasse_l1
l1 = tf.nn.tanh(wx_plus) #双曲正弦 激活函数
#定义 输出 层 神经网络
weights_l2 = tf.Variable(tf.random_normal([10,1]))
biasse_l2 = tf.Variable(tf.zeros([1,1]))
wx_plus2 = tf.matmul(l1,weights_l2) +biasse_l2
pred = tf.nn.tanh(wx_plus2)
loss = tf.reduce_mean(tf.square(y - pred))
#使用梯度下降
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(20000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
pred_value= sess.run(pred, feed_dict={x:x_data})
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,pred_value,'r-', lw = 5)
plt.show()