forked from 1535966643/tensorflow_study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_xianxinhuiguiTest.py
48 lines (28 loc) · 1018 Bytes
/
tf_xianxinhuiguiTest.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
38
39
40
41
42
43
44
45
46
47
48
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 准备数据
x_data = np.linspace(-1,1,100)
y_data = x_data * 3.1234 + 2.98 + np.random.randn(*x_data.shape) * 0.2
# 构建模型
train_step = 100
learning_rate = 0.05
w = tf.Variable(1.0, name='b')
b = tf.Variable(0.0, name='w')
x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name='y')
y_c = tf.multiply(w, x) + b
# mse 这样的损失函数在多元函数中,容易出现‘陷入局部最小值’
loss = tf.reduce_mean(tf.square(y-y_c))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(train_step):
for j1, j2 in zip(x_data, y_data):
sess.run(optimizer,feed_dict={x:j1, y:j2})
if i % 10 == 0:
print(sess.run(w),'---',sess.run(b))
# 测试模型
x_test = 2
print(sess.run(y_c, feed_dict={x:x_test}))