In [7]:
import numpy as np
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.images[0])


[[  0.   0.   5.  13.   9.   1.   0.   0.]
 [  0.   0.  13.  15.  10.  15.   5.   0.]
 [  0.   3.  15.   2.   0.  11.   8.   0.]
 [  0.   4.  12.   0.   0.   8.   8.   0.]
 [  0.   5.   8.   0.   0.   9.   8.   0.]
 [  0.   4.  11.   0.   1.  12.   7.   0.]
 [  0.   2.  14.   5.  10.  12.   0.   0.]
 [  0.   0.   6.  13.  10.   0.   0.   0.]]

In [111]:
class Neuron:
    def __init__(self, num_inputs, bias = .5, weights=None):
        if weights == None:
            self.weights = np.ones((num_inputs,))
            for i in range(num_inputs):
                self.weights[i] = np.random.rand()
        else:
            self.weights = weights
        if self.weights.shape != (num_inputs,):
            raise ValueError("Weights must be the same size as the number of inputs.")
        self.bias = float(bias)
    def calc_output(self, inputs):
        if inputs.shape!=self.weights.shape:
            raise ValueError("Inputs and weights must have the same shape.")
        inwsum = inputs.dot(self.weights)
        output = 1/(1+np.exp(-inwsum-self.bias))
        return output

In [112]:
n1 = Neuron(2, .5, np.array((.9, .5)))
assert n1.calc_output(np.array([0,0]))!=0
assert n1.calc_output(np.array([0,2]))!=1
assert n1.calc_output(np.array([2,4]))!=3
try:
    assert n1.calc_output(np.array([0,0,0,0]))==0
except ValueError:
    print("There was an error")


There was an error
/usr/local/lib/python3.4/dist-packages/IPython/kernel/__main__.py:3: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  app.launch_new_instance()

In [113]:
class Layer:
    def __init__(self, num_neurons, num_inputs, output_weights=None):
        self.neurons = np.empty((num_neurons,), dtype=Neuron)
        self.num_neurons = num_neurons
        self.output_weights=output_weights
        self.output_node = Neuron(num_neurons, .5, output_weights)
        for i in range(num_neurons):
            self.neurons[i] = Neuron(num_inputs)
    def propForward(self, inputs):
        outputs = np.zeros((self.num_neurons,))
        i=0
        for neuron in self.neurons:
            outputs[i]=neuron.calc_output(inputs)
            print(outputs[i])#vector of activations
            i+=1
        output = self.output_node.calc_output(outputs)
        print(output)
        return output
    def training(self, inputs,answer):
        grad_b = [np.zeros(self.num_neurons) for ]

In [114]:
net1 = Layer(20, 1, np.linspace(0,10,20))
assert net1.propForward(np.array([1]))!=0


0.65737479122
0.785885585612
0.625803954731
0.795564215745
0.712580595348
0.772133905567
0.710875969055
0.627261171279
0.681015348482
0.810315381999
0.762055549717
0.783949133141
0.6428639118
0.755265855893
0.70042303825
0.635255629971
0.678462821295
0.721369070954
0.771144491261
0.634291028354
1.0
/usr/local/lib/python3.4/dist-packages/IPython/kernel/__main__.py:3: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  app.launch_new_instance()

In [ ]: