-
Notifications
You must be signed in to change notification settings - Fork 111
/
polynomial_regression.py
executable file
·36 lines (31 loc) · 1.15 KB
/
polynomial_regression.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
#!/usr/bin/env python
import numpy as np
import tensorflow as tf
# Prepare train data
train_X = np.linspace(-1, 1, 100)
train_Y = 10 + 1 * train_X + 2 * np.power(train_X, 2) + 3 * np.power(
train_X, 3) + np.random.randn(*train_X.shape) * 0.33
# Define the model
X = tf.placeholder("float")
Y = tf.placeholder("float")
w1 = tf.Variable(0.0, name="weight1")
w2 = tf.Variable(0.0, name="weight2")
w3 = tf.Variable(0.0, name="weight3")
b = tf.Variable(0.0, name="bias")
loss = tf.square(Y - b - tf.mul(w1, X) - tf.mul(w2, tf.pow(X, 2)) - tf.mul(
w3, tf.pow(X, 3)))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
train_epoch_number = 100
# Create session to run
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
epoch = 1
for i in range(train_epoch_number):
for (x, y) in zip(train_X, train_Y):
_, b_value, w1_value, w2_value, w3_value = sess.run(
[train_op, b, w1, w2, w3],
feed_dict={X: x,
Y: y})
print("Epoch: {}, b: {}, w1: {}, w2: {}, w3: {}".format(
epoch, b_value, w1_value, w2_value, w3_value))
epoch += 1