Skip to content

Commit

Permalink
python-izes classes to inherit base object instead of ()
Browse files Browse the repository at this point in the history
  • Loading branch information
drjerry committed Feb 17, 2015
1 parent b89484d commit ce419d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
14 changes: 7 additions & 7 deletions src/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Third-party libraries
import numpy as np

class Network():
class Network(object):

def __init__(self, sizes):
"""The list ``sizes`` contains the number of neurons in the
Expand All @@ -32,7 +32,7 @@ def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]

def feedforward(self, a):
Expand Down Expand Up @@ -77,9 +77,9 @@ def update_mini_batch(self, mini_batch, eta):
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
self.weights = [w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]

def backprop(self, x, y):
Expand Down Expand Up @@ -122,14 +122,14 @@ def evaluate(self, test_data):
network outputs the correct result. Note that the neural
network's output is assumed to be the index of whichever
neuron in the final layer has the highest activation."""
test_results = [(np.argmax(self.feedforward(x)), y)
test_results = [(np.argmax(self.feedforward(x)), y)
for (x, y) in test_data]
return sum(int(x == y) for (x, y) in test_results)

def cost_derivative(self, output_activations, y):
"""Return the vector of partial derivatives \partial C_x /
\partial a for the output activations."""
return (output_activations-y)
return (output_activations-y)

#### Miscellaneous functions
def sigmoid(z):
Expand Down
26 changes: 13 additions & 13 deletions src/network2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#### Define the quadratic and cross-entropy cost functions

class QuadraticCost:
class QuadraticCost(object):

@staticmethod
def fn(a, y):
Expand All @@ -39,7 +39,7 @@ def delta(z, a, y):
return (a-y) * sigmoid_prime_vec(z)


class CrossEntropyCost:
class CrossEntropyCost(object):

@staticmethod
def fn(a, y):
Expand All @@ -65,7 +65,7 @@ def delta(z, a, y):


#### Main Network class
class Network():
class Network(object):

def __init__(self, sizes, cost=CrossEntropyCost):
"""The list ``sizes`` contains the number of neurons in the respective
Expand Down Expand Up @@ -97,7 +97,7 @@ def default_weight_initializer(self):
"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)/np.sqrt(x)
self.weights = [np.random.randn(y, x)/np.sqrt(x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]

def large_weight_initializer(self):
Expand All @@ -117,7 +117,7 @@ def large_weight_initializer(self):
"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)
self.weights = [np.random.randn(y, x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]

def feedforward(self, a):
Expand All @@ -126,12 +126,12 @@ def feedforward(self, a):
a = sigmoid_vec(np.dot(w, a)+b)
return a

def SGD(self, training_data, epochs, mini_batch_size, eta,
lmbda = 0.0,
evaluation_data=None,
def SGD(self, training_data, epochs, mini_batch_size, eta,
lmbda = 0.0,
evaluation_data=None,
monitor_evaluation_cost=False,
monitor_evaluation_accuracy=False,
monitor_training_cost=False,
monitor_training_cost=False,
monitor_training_accuracy=False):
"""Train the neural network using mini-batch stochastic gradient
descent. The ``training_data`` is a list of tuples ``(x, y)``
Expand Down Expand Up @@ -201,9 +201,9 @@ def update_mini_batch(self, mini_batch, eta, lmbda, n):
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw
self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]

def backprop(self, x, y):
Expand Down Expand Up @@ -244,7 +244,7 @@ def accuracy(self, data, convert=False):
"""Return the number of inputs in ``data`` for which the neural
network outputs the correct result. The neural network's
output is assumed to be the index of whichever neuron in the
final layer has the highest activation.
final layer has the highest activation.
The flag ``convert`` should be set to False if the data set is
validation or test data (the usual case), and to True if the
Expand All @@ -264,7 +264,7 @@ def accuracy(self, data, convert=False):
"""
if convert:
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
for (x, y) in data]
else:
results = [(np.argmax(self.feedforward(x)), y)
Expand Down
34 changes: 17 additions & 17 deletions src/network3.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def shared(data):
return [shared(training_data), shared(validation_data), shared(test_data)]

#### Main class used to construct and train networks
class Network():
class Network(object):

def __init__(self, layers, mini_batch_size):
"""Takes a list of `layers`, describing the network architecture, and
a value for the `mini_batch_size` to be used during training
Expand All @@ -89,7 +89,7 @@ def __init__(self, layers, mini_batch_size):
self.layers = layers
self.mini_batch_size = mini_batch_size
self.params = [param for layer in self.layers for param in layer.params]
self.x = T.matrix("x")
self.x = T.matrix("x")
self.y = T.ivector("y")
init_layer = self.layers[0]
init_layer.set_inpt(self.x, self.x, self.mini_batch_size)
Expand All @@ -100,7 +100,7 @@ def __init__(self, layers, mini_batch_size):
self.output = self.layers[-1].output
self.output_dropout = self.layers[-1].output_dropout

def SGD(self, training_data, epochs, mini_batch_size, eta,
def SGD(self, training_data, epochs, mini_batch_size, eta,
validation_data, test_data, lmbda=0.0):
"""Train the network using mini-batch stochastic gradient descent."""
training_x, training_y = training_data
Expand All @@ -117,7 +117,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
cost = self.layers[-1].cost(self)+\
0.5*lmbda*l2_norm_squared/num_training_batches
grads = T.grad(cost, self.params)
updates = [(param, param-eta*grad)
updates = [(param, param-eta*grad)
for param, grad in zip(self.params, grads)]

# define functions to train a mini-batch, and to compute the
Expand All @@ -128,37 +128,37 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
givens={
self.x:
training_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size],
self.y:
self.y:
training_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
validate_mb_accuracy = theano.function(
[i], self.layers[-1].accuracy(self.y),
givens={
self.x:
self.x:
validation_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size],
self.y:
self.y:
validation_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
test_mb_accuracy = theano.function(
[i], self.layers[-1].accuracy(self.y),
givens={
self.x:
self.x:
test_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size],
self.y:
self.y:
test_y[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
self.test_mb_predictions = theano.function(
[i], self.layers[-1].y_out,
givens={
self.x:
self.x:
test_x[i*self.mini_batch_size: (i+1)*self.mini_batch_size]
})
# Do the actual training
best_validation_accuracy = 0.0
for epoch in xrange(epochs):
for minibatch_index in xrange(num_training_batches):
iteration = num_training_batches*epoch+minibatch_index
if iteration % 1000 == 0:
if iteration % 1000 == 0:
print("Training mini-batch number {0}".format(iteration))
cost_ij = train_mb(minibatch_index)
if (iteration+1) % num_training_batches == 0:
Expand All @@ -182,18 +182,18 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,

#### Define layer types

class ConvPoolLayer():
class ConvPoolLayer(object):
"""Used to create a combination of a convolutional and a max-pooling
layer. A more sophisticated implementation would separate the
two, but for our purposes we'll always use them together, and it
simplifies the code, so it makes sense to combine them.
"""

def __init__(self, filter_shape, image_shape, poolsize=(2, 2),
def __init__(self, filter_shape, image_shape, poolsize=(2, 2),
activation_fn=sigmoid):
"""`filter_shape` is a tuple of length 4, whose entries are the number
of filters, the number of input feature maps, the filter height, and the
of filters, the number of input feature maps, the filter height, and the
filter width.
`image_shape` is a tuple of length 4, whose entries are the
Expand Down Expand Up @@ -233,7 +233,7 @@ def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
self.output_dropout = self.output # no dropout in the convolutional layers

class FullyConnectedLayer():
class FullyConnectedLayer(object):

def __init__(self, n_in, n_out, activation_fn=sigmoid, p_dropout=0.0):
self.n_in = n_in
Expand Down Expand Up @@ -267,7 +267,7 @@ def accuracy(self, y):
"Return the accuracy for the mini-batch."
return T.mean(T.eq(y, self.y_out))

class SoftmaxLayer():
class SoftmaxLayer(object):

def __init__(self, n_in, n_out, p_dropout=0.0):
self.n_in = n_in
Expand Down
12 changes: 6 additions & 6 deletions src/old/perceptron_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Third-party library
import numpy as np

class Perceptron():
class Perceptron(object):
""" A Perceptron instance can take a function and attempt to
``learn`` a bias and set of weights that compute that function,
using the perceptron learning algorithm."""
Expand All @@ -26,7 +26,7 @@ def __init__(self, num_inputs=2):
# inputs it is: [np.array([0, 0, 0]), np.array([0, 0, 1]), ...]
self.inputs = [np.array([int(y)
for y in bin(x).lstrip("0b").zfill(num_inputs)])
for x in xrange(2**num_inputs)]
for x in xrange(2**num_inputs)]

def output(self, x):
""" Return the output (0 or 1) from the perceptron, with input
Expand All @@ -37,12 +37,12 @@ def learn(self, f, eta=0.1):
""" Find a bias and a set of weights for a perceptron that
computes the function ``f``. ``eta`` is the learning rate, and
should be a small positive number. Does not terminate when
the function cannot be computed using a perceptron."""
the function cannot be computed using a perceptron."""
# initialize the bias and weights with random values
self.bias = np.random.normal()
self.weights = np.random.randn(self.num_inputs)
number_of_errors = -1
while number_of_errors != 0:
while number_of_errors != 0:
number_of_errors = 0
print "Beginning iteration"
print "Bias: {:.3f}".format(self.bias)
Expand All @@ -56,11 +56,11 @@ def learn(self, f, eta=0.1):
self.weights = self.weights+eta*error*x
print "Number of errors:", number_of_errors, "\n"

def f(x):
def f(x):
""" Target function for the perceptron learning algorithm. I've
chosen the NAND gate, but any function is okay, with the caveat
that the algorithm won't terminate if ``f`` cannot be computed by
a perceptron."""
a perceptron."""
return int(not (x[0] and x[1]))

if __name__ == "__main__":
Expand Down

0 comments on commit ce419d0

Please sign in to comment.