Skip to content

Instantly share code, notes, and snippets.

@etcwilde
Created October 21, 2016 02:40
Show Gist options
  • Save etcwilde/350e8d88981bdb3cecd2f903b9d152de to your computer and use it in GitHub Desktop.
Save etcwilde/350e8d88981bdb3cecd2f903b9d152de to your computer and use it in GitHub Desktop.
Notes for machine learning
#!/bin/env python
# Just playing with theano. This doesn't do anything particularly useful
# other than showing how things work
import numpy
import theano
import theano.tensor as T
from theano import function
# Creates variables and the operations
x = T.dscalar()
y = T.dmatrix('y')
z = x + y
# List of parameters followed by the gra
f = function([x, y], z) # Compiles an executable function
print(f(2, [[1, 2], [3,4]]))
# Logistic equation
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x)) # 1 / (1 + e^-x)
logistic = function([x], s)
m_1 = [[1, 1], [2, 2]]
m_2 = [[1, 1], [2, 3]]
print(logistic(m_1))
print(logistic(m_2))
# Create multiple matrices simultaneously
a, b = T.dmatrices('a', 'b')
# bunch of functions
diff = a - b
abs_diff = abs(diff)
diff_sqr = diff * diff
f = function([a, b], [diff, abs_diff, diff_sqr]) # Will execute all three in one shot
# Shared variables
# ------------------------
from theano import shared
state = shared(0) # shared variable
inc = T.iscalar('inc') # Incrementer variable
# one tuple in updates for every shared variable
f = function([inc], state, updates=[(state, state+inc)])
# Full Logistic regression
# ---------------------------
rng = numpy.random
N = 400 # Number of rows
features = 700 # Features
# The dataset
D = (rng.randn(N, features), rng.randint(size=N, low=0, high=2))
training_steps = 10000
x = T.dmatrix('x')
y = T.dvector('y')
w = shared(rng.randn(features), name='weights')
# Don't forget the decimal, it will be an integer otherwise and mess everything up
b = shared(0., name='b')
# Predictions
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))
prediction = p_1 > 0.5
# cross entropy
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1)
# cost function to effect the gradients -- minimize cost
cost = xent.mean() + 0.01 * (w ** 2).sum()
# Gradients for weights and bias
gw, gb = T.grad(cost, [w, b])
# Put it all together
train = function(inputs=[x, y], outputs=[prediction, xent], updates=[(w, w - 0.1 * gw), (b, b - 0.1 * gb)])
predict = function(inputs=[x], outputs=[prediction])
# Now we need to run it -- train!
# We'll ignore the prediction and error outputs for now
for i in range(training_steps):
pred, err = train()
# Now predict -- run it in a useful way
prediction(D[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment