Linear Regression Example with Python and theano


In my last blog post I described how to install the theano library on windows. Now this is an example of what you can do with it.

I`m currently going through the theano tutorials presented in this video by Alec Radford and adding some lines of code to make it easier to reproduce.

This is the Linear regression example in the first minutes of his video you can copy paste it into an ipython notebook:

The result will be an animated linear model that finds its minimum error after a few iterations of gradient descent.


import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import theano
from theano import tensor as T
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

trX = np.linspace(-1,1,101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33

X = T.scalar()
Y = T.scalar()

def model (X,w):
return X * w

w = theano.shared(np.asarray(0., dtype=theano.config.floatX))
y = model(X,w)

cost = T.mean(T.sqr(y-Y))
gradient = T.grad(cost=cost, wrt = w)
updates = [[w,w-gradient * 0.001]]

train = theano.function(inputs=[X,Y], outputs=cost, updates = updates, allow_input_downcast= True)

#for i in range(100):
# for x,y in zip (trX,trY):
#    train(x,y)
#   print (w.eval())

def animate(i):
plt.scatter(trX, trY,  label='Gradient Descent on GPU',
alpha=0.3, edgecolors='none')
for x,y in zip (trX,trY):
#print (w.eval())

xs = [-1,1]
ys = [-1*w.eval(),w.eval()]

ani = animation.FuncAnimation(fig, animate, interval = 250)

Comments (2)

  1. Freddy says:

    Thank you for your code. I did not know of the theano library but was interested in how to do linear regression with Python. In this post I learned about a, for me new, Python library and got to see some code on a linear model with plotting. Cool. Thanks.

    1. LukasSteindl says:

      its actually not my code. as mentioned in the blogpost it is from Alec Radford. I just added the animation plotting. Regards Lukas

Skip to main content