Aquí os dejo con un ejemplo (regresion_lineal.py
) muy sencillo y claro para realizar una regresión lineal.
import numpy as np import theano from theano import tensor as T import matplotlib.pyplot as plt 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.01]] 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.get_value()) # Resultado alrededor de 2. plt.plot(trX, trY, 'g.') plt.plot(trX, model(trX, w.get_value())) plt.show()