# Import the required modules
%pylab inline
from theano import tensor as T
from theano import function
from theano import pp
from theano import Param
from theano import grad
from theano import shared
import numpy as np
# Define the symbolic Variables
X, y, W = T.matrices('training-set', 'testing-set', 'weights')
m, a = T.scalars('no-of-samples', 'learning-rate')
# Define the cost function and the gradient
cost_fn = (((T.dot(X, W) - y)**2)/(2. * m)).sum()
grad_fn = (T.dot(T.transpose(X), T.dot(X, W) - y))/(m)
updated_wts = W - a * grad_fn
# Compile the Theano functions
cost = function([X, y, W, m], cost_fn)
new_wts = function([X, y, W, m, a], updated_wts)
# Prepare the dataset
X_val = np.array([[1],[4],[6],[8],[10],[12],[14]])
bias = np.ones((X_val.shape[0], 1))
X_val = np.hstack((X_val, bias))
y_val = np.array([[2], [3], [4], [5], [6], [7], [8]])
m_val = X_val.shape[0]
W_val = np.zeros((X_val[0].shape[0],1), dtype=np.float)
a_val = .001
iterations = 10000
# Calculate the cost function and gradient descent
# and update the weights
for i in xrange(iterations):
cst = cost(X_val, y_val, W_val, m_val)
W_val = new_wts(X_val, y_val, W_val, m_val, a_val)
# Perform the perdictions
X_test = np.array([[8, 1], [9, 1], [10, 1]])
y_test = np.array([[5], [5.5], [6]])
y_pred = np.dot(X_test, W_val)
for X_test_c , y_test_c, y_pred_c in zip(X_test, y_test, y_pred):
print "Sample {} Actual Value = {} Predicted Value = {} ".format(X_test_c, y_test_c, y_pred_c)
Populating the interactive namespace from numpy and matplotlib Sample [8 1] Actual Value = [ 5.] Predicted Value = [ 5.03978118] Sample [9 1] Actual Value = [ 5.5] Predicted Value = [ 5.52552955] Sample [10 1] Actual Value = [ 6.] Predicted Value = [ 6.01127793]
fit
, predict
interfaces as in case of sklearn.¶class LinearRegression:
'''
This class performs linear regression.
'''
def __init__(self, alpha=0.001, iterations=1000):
'''
1. alpha is the learning rate.
The default value of alpha is 0.001.
2. iterations is the number of times the weights will be updated.
The default value is 1000
'''
# Set the learning rate
self.alpha = alpha
# Set the number of iterations
self.iterations = iterations
# Define the symbolic Variables
X, y, W = T.matrices('training-set', 'testing-set', 'weights')
m, a = T.scalars('no-of-samples', 'learning-rate')
# Define the cost function and the gradient functions
cost_fn = (((T.dot(X, W) - y)**2)/(2. * m)).sum()
grad_fn = (T.dot(T.transpose(X), T.dot(X, W) - y))/(m)
updated_wts = W - a * grad_fn
# Compile the Theano functions
self.cost = function([X, y, W, m], cost_fn)
self.new_wts = function([X, y, W, m, a], updated_wts)
self.pred = function([X, W], T.dot(X, W))
def fit(self, X, y):
'''
The fit member function is used to train the classifier.
1. X is array-like data vector
2. y is array-like target vector
'''
# Add the bias feature to the data samples
bias = np.ones((X.shape[0], 1))
X = np.hstack((X, bias))
# m is the number of data samples used
m = X.shape[0]
# Initialize all weights to 0
self.W = np.zeros((X[0].shape[0],1), dtype=np.float)
# Iterate and update the cost function and the weights
for i in xrange(self.iterations):
cst = self.cost(X, y, self.W, m)
self.W = self.new_wts(X, y, self.W, m, self.alpha)
# Set the coef_ and intercept_
self.coef_ = self.W[:-1]
self.intercept_ = self.W[-1]
def predict(self, X):
'''
This is function to predict target.
1. X is array-like data vector whose target vector is predicted
'''
# Return the predicted target values
return self.pred(X, self.W)
# Preparea a small dataset
# The dataset is a generated
# using the function of y = X/2 + 1
X_train = np.array([[1],[4],[6],[8],[10],[12],[14]])
y_train = np.array([[2], [3], [4], [5], [6], [7], [8]])
X_test = np.array([[8, 1], [9, 1], [10, 1]])
y_test = np.array([[5], [5.5], [6]])
# Create the classifier object
clf = LinearRegression(iterations=10000)
# Perform the training
clf.fit(X_train, y_train)
# Predict using the classifier
y_pred = clf.predict(X_test)
# Display the results
for X_test_c , y_test_c, y_pred_c in zip(X_test, y_test, y_pred):
print "Sample {} Actual Value = {} Predicted Value = {} ".format(X_test_c, y_test_c, y_pred_c)
Sample [8 1] Actual Value = [ 5.] Predicted Value = [ 5.03978118] Sample [9 1] Actual Value = [ 5.5] Predicted Value = [ 5.52552955] Sample [10 1] Actual Value = [ 6.] Predicted Value = [ 6.01127793]
# NOTE :: This graphic will only work for 1-D features, although the
# code can work for any dimensions
# Draw the regression line
plot(X_train, y_train, "*")
x_min, x_max, y_min, y_max = 0 , 15, 0, 15
axis([x_min, x_max, y_min, y_max])
y_min_val = (clf.coef_[0][0] * x_min + clf.intercept_[0])
y_max_val = (clf.coef_[0][0] * x_max + clf.intercept_[0])
plot([x_min, x_max], [y_min_val, y_max_val])
[<matplotlib.lines.Line2D at 0x7f7cc165c2d0>]