class Model(object): def __init__(self, param): self.c1 = ConvLayer((1, 28, 28), (16, 5, 5), (1, 0), Relu(), param) # 4x24x24 self.p1 = PoolingLayer(self.c1.output_shape, ( 2, 2, ), 2, PoolingTypes.MAX) # 4x12x12 self.c2 = ConvLayer(self.p1.output_shape, (8, 3, 3), (1, 0), Relu(), param) # 4x10x10 self.p2 = PoolingLayer(self.c2.output_shape, ( 2, 2, ), 2, PoolingTypes.MAX) # 4x5x5 self.f1 = FcLayer(self.p2.output_size, 32, Relu(), param) self.f2 = FcLayer(self.f1.output_size, 10, Softmax(), param) def forward(self, x): net = self.c1.forward(x) net = self.p1.forward(net) net = self.c2.forward(net) net = self.p2.forward(net) net = self.f1.forward(net) net = self.f2.forward(net) self.output = net return self.output def backward(self, y): delta = self.output - y delta = self.f2.backward(delta, LayerIndexFlags.LastLayer) delta = self.f1.backward(delta, LayerIndexFlags.MiddleLayer) delta = self.p2.backward(delta, LayerIndexFlags.MiddleLayer) delta = self.c2.backward(delta, LayerIndexFlags.MiddleLayer) delta = self.p1.backward(delta, LayerIndexFlags.MiddleLayer) delta = self.c1.backward(delta, LayerIndexFlags.FirstLayer) def update(self, learning_rate): self.c1.update() self.c2.update() self.f1.update() self.f2.update() def save(self): self.c1.save_parameters("c1") self.c2.save_parameters("c2") self.p1.save_parameters("p1") self.p2.save_parameters("p2") self.f1.save_parameters("f1") self.f2.save_parameters("f2") def load(self): self.c1.load_parameters("c1") self.c2.load_parameters("c2") self.p1.load_parameters("p1") self.p2.load_parameters("p2") self.f1.load_parameters("f1") self.f2.load_parameters("f2")
class Model(object): def __init__(self, param): self.c1 = ConvLayer((1, 28, 28), (4, 5, 5), (1, 0), Relu(), param) # 4x24x24 self.p1 = PoolingLayer(self.c1.output_shape, ( 2, 2, ), 2, PoolingTypes.MAX) # 4x12x12 self.f1 = FcLayer(self.p1.output_size, 32, Sigmoid(), param) self.f2 = FcLayer(self.f1.output_size, 10, Softmax(), param) def forward(self, x): a_c1 = self.c1.forward(x) a_p1 = self.p1.forward(a_c1) a_f1 = self.f1.forward(a_p1) a_f2 = self.f2.forward(a_f1) self.output = a_f2 return self.output def backward(self, y): delta_in = self.output - y d_f2 = self.f2.backward(delta_in, LayerIndexFlags.LastLayer) d_f1 = self.f1.backward(d_f2, LayerIndexFlags.MiddleLayer) d_p1 = self.p1.backward(d_f1, LayerIndexFlags.MiddleLayer) d_c1 = self.c1.backward(d_p1, LayerIndexFlags.FirstLayer) def update(self, learning_rate): self.c1.update() self.f1.update() self.f2.update() def save(self): self.c1.save_parameters("c1") self.p1.save_parameters("p1") self.f1.save_parameters("f1") self.f2.save_parameters("f2") def load(self): self.c1.load_parameters("c1") self.p1.load_parameters("p1") self.f1.load_parameters("f1") self.f2.load_parameters("f2")