-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4-LinearRegressionWithCustomModel.py
49 lines (30 loc) · 1.48 KB
/
m4-LinearRegressionWithCustomModel.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
import numpy as np
from sklearn import datasets, linear_model
from returns_data import read_goog_sp500_data
xData, yData = read_goog_sp500_data()
googModel = linear_model.LinearRegression()
googModel.fit(xData.reshape(-1, 1), yData.reshape(-1, 1))
print(googModel.coef_)
print(googModel.intercept_)
### Custom Linear Regression Model
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
# Build a linear model and predict values
W = tf.get_variable('W', [1], dtype=tf.float64)
b = tf.get_variable('b', [1], dtype=tf.float64)
y = W * features['x'] + b
# Loss sub-graph
loss = tf.reduce_sum(tf.square(y - labels))
# Training sub-graph
global_step = tf.train.get_global_step()
optimizer = tf.train.FtrlOptimizer(1)
# groups steps so they are completed together
train = tf.group(optimizer.minimize(loss), tf.assign_add(global_step, 1))
# ModelFnOps connects subgraphs we built to the appropriate functionality
return tf.contrib.learn.ModelFnOps(mode=mode, predictions=y, loss=loss, train_op=train)
estimator = tf.contrib.learn.Estimator(model_fn=model)
input_fn = tf.contrib.learn.io.numpy_input_fn({"x": xData}, yData, batch_size=len(xData), num_epochs=10000)
fit = estimator.fit(input_fn=input_fn, steps=10000)
for variable_name in fit.get_variable_names():
print(variable_name, '--->', fit.get_variable_value(variable_name))