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.

imageimage

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

 

style.use(‚fivethirtyeight‘)

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(yY))

gradient = T.grad(cost=cost, wrt = w)

updates = [[w,wgradient * 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):

    ax1.clear()

    plt.scatter(trX, trY,  label=‚Gradient Descent on GPU‘,

                    alpha=0.3, edgecolors=’none‘)

    plt.legend()

    plt.grid(True)

    for x,y in zip (trX,trY):

        train(x,y)

        #print (w.eval())

      

    xs = [-1,1]

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

    ax1.plot(xs,ys)

   

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

 

plt.show()

       

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s