-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch9_gradient_descent.py
60 lines (43 loc) · 1.79 KB
/
ch9_gradient_descent.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
def load_data():
housing = fetch_california_housing()
m,n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m,1)), housing.data]
scaler = StandardScaler()
print(scaler.fit(housing_data_plus_bias))
#print(scaler.mean_)
scaled_housing_data_plus_bias = scaler.transform(housing_data_plus_bias)
#print(type(scaled_housing_data_plus_bias))
#print(type(housing.target.reshape(-1, 1)))
return scaled_housing_data_plus_bias, housing.target.reshape(-1, 1)
def main():
print("Gradient Descent Example")
X_Train, Y_Train = load_data()
m,n = X_Train.data.shape
n_epochs = 1000
learning_rate = 0.01
X = tf.constant(X_Train, dtype=tf.float32, name="X")
y = tf.constant(Y_Train, dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n, 1], -1.0, 1.0), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
# calculated using tfs gradient descent optimizer
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
# calculated using tfs momentum descent optimizer - faster
optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
training_op = optimizer.minimize(mse)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
if epoch % 100 == 0:
print("Epoch ", epoch, " MSE = ", mse.eval())
sess.run(training_op)
best_theta = theta.eval()
print(best_theta)
if __name__ == "__main__":
main()