-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_1.py
More file actions
37 lines (26 loc) · 985 Bytes
/
demo_1.py
File metadata and controls
37 lines (26 loc) · 985 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 18 21:34:25 2019
@author: Dufert
"""
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
input_x = np.random.rand(3000, 1)
noise = np.random.uniform(0, 0.05, input_x.shape)
output_y = input_x*8 +1 + noise
weight = tf.Variable(dtype=tf.float32, initial_value=np.random.rand(input_x.shape[1], 1))
bias = tf.Variable(dtype=tf.float32, initial_value=np.random.rand(input_x.shape[1], 1))
x = tf.placeholder(tf.float32, [None, input_x.shape[1]])
y_ = tf.matmul(x, weight) + bias
# 计算loss函数
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y_-output_y),1))
train = tf.train.GradientDescentOptimizer(0.25).minimize(loss)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
sess.run(train, feed_dict={x: input_x})
print(weight.eval(), bias.eval())
test = np.mat([[1.], [2.], [3.]])
print(sess.run(y_, feed_dict={x: test}))