-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathan_example_2.py
44 lines (29 loc) · 1.06 KB
/
an_example_2.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
# Date:2019.8.9
# author:morvanzhou
# editor:hjn
# Topic:To illustrate how tensorflow use code
# to execute the struction constructed by myself.
# 1、创建数据
import tensorflow as tf
import numpy as np
# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3
# 2、搭建模型
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data + biases
# 3、计算误差
loss = tf.reduce_mean(tf.square(y-y_data))
# 4、传播误差
optimizer = tf.train.GradientDescentOptimizer(0.001) # 学习率越低,参数要达到准确的数值,则所需的回合数越多
train = optimizer.minimize(loss)
# 5、训练
# init = tf.initialize_all_variables # tf马上要废弃这种写法
init = tf.global_variables_initializer() # 替换成这样
sess = tf.Session() # 创建会话
sess.run(init) # Very important
for step in range(200001):
sess.run(train)
if step % 20000 == 0:
print(step, sess.run(Weights), sess.run(biases))