-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.py
36 lines (26 loc) · 851 Bytes
/
practice.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
import tensorflow as tf
import numpy as np
import time
#create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1+0.3
#create tensorflow structure start#
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y = Weights*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))
optimizer=tf.train.GradientDescentOptimizer(0.5)
#optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
#initialize variable
init=tf.initialize_all_variables()
#create tensorflow structure end#
sess=tf.Session()
sess.run(init)
if __name__ == '__main__':
localtime=time.time()
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step,sess.run(Weights),sess.run(biases))
print('time_cost:',(time.time() - localtime))