Skip to content

Commit 8b7a642

Browse files
Added examples for regressions
1 parent 53ec3ce commit 8b7a642

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import tensorflow as tf
4+
5+
# Create dataset
6+
N = 400
7+
data = lambda: None
8+
data.x = np.linspace(0, 1, N)
9+
data.y = np.exp(data.x) + 0.5 * np.random.rand(N)
10+
plt.scatter(data.x, data.y)
11+
12+
# Training Parameters
13+
learning_rate = 0.001
14+
num_steps = 100
15+
16+
# Setup Network
17+
X = tf.placeholder(tf.float32)
18+
Y = tf.placeholder(tf.float32)
19+
20+
# Define Network
21+
w = tf.Variable(0.0, name='w')
22+
b = tf.Variable(0.0, name='b')
23+
Y_pred = X * w + b
24+
25+
# Define loss and optimizer
26+
loss = tf.square(Y_pred - Y)
27+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)
28+
29+
# Initalize varibles, and run network
30+
init = tf.global_variables_initializer()
31+
sess = tf.Session()
32+
sess.run(init)
33+
34+
# Train
35+
for step in range(num_steps):
36+
sess.run(optimizer, feed_dict={X:data.x, Y:data.y})
37+
38+
w, b = sess.run([w, b])
39+
print(w, b)
40+
41+
# Plot Results
42+
plt.plot(data.x, w * data.x + b, 'r')
43+
plt.show()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import tensorflow as tf
4+
5+
N = 100
6+
7+
def gen():
8+
# Create dataset
9+
data = lambda: None
10+
data.x = np.linspace(0, 3, N)
11+
data.y = np.exp(data.x) + 2 * np.random.rand(N)
12+
data.x = np.reshape(data.x, (-1, 1))
13+
data.y = np.reshape(data.y, (-1, 1))
14+
return data
15+
16+
# Training Parameters
17+
learning_rate = 0.01
18+
num_steps = 1000
19+
20+
# Setup Network
21+
X = tf.placeholder(tf.float32, [None, 1])
22+
Y = tf.placeholder(tf.float32, [None, 1])
23+
24+
# Define Network
25+
fc1 = tf.layers.dense(X, 100, activation=tf.nn.relu, name='fc1')
26+
fc2 = tf.layers.dense(fc1, 100, activation=tf.nn.relu, name='fc2')
27+
Y_pred = tf.layers.dense(fc2, 1, name='out')
28+
29+
# Define loss and optimizer
30+
loss = tf.square(Y_pred - Y)
31+
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
32+
33+
# Initalize varibles, and run network
34+
init = tf.global_variables_initializer()
35+
sess = tf.Session()
36+
sess.run(init)
37+
38+
# Train
39+
for step in range(num_steps):
40+
data = gen()
41+
sess.run(optimizer, feed_dict={X:data.x, Y:data.y})
42+
43+
_x = np.reshape(np.linspace(0, 3, 10 * N), (-1,1))
44+
_y = sess.run(Y_pred, feed_dict={X:_x})
45+
46+
# Reshape
47+
x = np.reshape(data.x, (-1)); y = np.reshape(data.y, (-1))
48+
_y = np.reshape(_y, (-1)); _x = np.reshape(_x, (-1))
49+
50+
# Plot Results
51+
plt.scatter(x, y)
52+
plt.plot(_x, _y, 'r')
53+
plt.show()

0 commit comments

Comments
 (0)