def test(self, input_matrix, teacher_matrix): """Function to test the network @param input_matrix -- matrix consisting of input data to the network. @param teacher_matrix -- matrix consisting of labels of input data . """ number_of_pictures = input_matrix.shape[-1] mse = 0 squared_errors = cp.dev_matrix_cmf(self.neuron_layer[-1].deltas.h, self.neuron_layer[-1].deltas.w) for batch in xrange(number_of_pictures/self.batch_size): index_begin = self.batch_size * batch index_end = index_begin + self.batch_size self.neuron_layer[0].activations = cp.push( input_matrix[:, index_begin:index_end].astype('float32').copy('F')) teachbatch = cp.push(teacher_matrix[:, index_begin:index_end].astype('float32').copy('F')) for i in xrange(self.number_of_layers): self.weight_layer[i].forward() cp.apply_binary_functor(squared_errors, self.neuron_layer[-1].deltas, cp.binary_functor.COPY) cp.apply_scalar_functor(squared_errors, cp.scalar_functor.SQUARE) mse += cp.sum(squared_errors) teachbatch.dealloc() print "MSE: ", (mse/number_of_pictures) squared_errors.dealloc()
def gray_test(ni): src = cp.push(to_cmuc(np.tile(ni,(1,4)))) dst = cp.dev_matrix_cmf(src.h,src.w) cp.fill(dst,0) cp.image_move(dst,src,128,128,1,-10,-4) res = cp.pull(dst) #set_trace() plt.matshow(res[0:128**2,0].reshape(128,128)) plt.colorbar() plt.show()
def gray_test(ni): src = cp.push(to_cmuc(np.tile(ni, (1, 4)))) dst = cp.dev_matrix_cmf(src.h, src.w) cp.fill(dst, 0) cp.image_move(dst, src, 128, 128, 1, -10, -4) res = cp.pull(dst) #set_trace() plt.matshow(res[0:128**2, 0].reshape(128, 128)) plt.colorbar() plt.show()
def color_test(ni): ts = 128 src = cp.push(to_cmuc(np.tile(ni,(1,4)))) dst = cp.dev_matrix_cmf(ts**2*3,src.w) cp.fill(dst,0) cp.image_move(dst,src,128,ts,4,-10,-4) res = cp.pull(dst) plt.matshow(res[0:ts**2,0].reshape(ts,ts), cmap = plt.cm.bone_r) plt.matshow(res[ts**2:2*ts**2,0].reshape(ts,ts), cmap = plt.cm.bone_r) plt.matshow(res[2*ts**2:3*ts**2,0].reshape(ts,ts), cmap = plt.cm.bone_r) plt.show()
def _tmp(dim1, dim2, value): """Function to create a filled matrix. This demonstrates how CUV can be extended using python. @param dim1 -- number of rows. @param dim2 -- number of columns. """ mat = cp.dev_matrix_cmf(dim1, dim2) cp.fill(mat, value) return mat
def color_test(ni): ts = 128 src = cp.push(to_cmuc(np.tile(ni, (1, 4)))) dst = cp.dev_matrix_cmf(ts**2 * 3, src.w) cp.fill(dst, 0) cp.image_move(dst, src, 128, ts, 4, -10, -4) res = cp.pull(dst) plt.matshow(res[0:ts**2, 0].reshape(ts, ts), cmap=plt.cm.bone_r) plt.matshow(res[ts**2:2 * ts**2, 0].reshape(ts, ts), cmap=plt.cm.bone_r) plt.matshow(res[2 * ts**2:3 * ts**2, 0].reshape(ts, ts), cmap=plt.cm.bone_r) plt.show()
def backward(self): """Backward pass, calculates the deltas of lower layer and later updates the weights.""" cp.prod(self.source.deltas, self.weight, self.target.deltas, 't', 'n') h = cp.dev_matrix_cmf(self.source.activations.h, self.source.activations.w) cp.apply_binary_functor(h, self.source.activations, cp.binary_functor.COPY) self.source.d_nonlinearity(h) cp.apply_binary_functor(self.source.deltas, h, cp.binary_functor.MULT) h.dealloc() self.weight_update()
def train(self, input_matrix, teacher_matrix, number_of_epochs): """Function to train the network @param input_matrix -- matrix consisting of input data to the network. @param teacher_matrix -- matrix consisting of labels of input data. @param number_of_epochs -- number of rounds the network is to be trained. """ number_of_pictures = input_matrix.shape[-1] squared_errors = cp.dev_matrix_cmf(self.neuron_layer[-1].deltas.h, self.neuron_layer[-1].deltas.w)
def weight_update(self, learnrate=0.01, decay=0.0): """Updates the weights and the bias using source activations and target deltas. @param learnrate how strongly the gradient influences the weights @param decay large values result in a regularization with to the squared weight value""" batch_size=self.source.activations.w h = cp.dev_matrix_cmf(self.weight.h, self.weight.w) cp.prod(h, self.target.deltas, self.source.activations, 'n', 't') cp.learn_step_weight_decay(self.weight, h, learnrate/batch_size, decay) h.dealloc() h = cp.get_filled_matrix(self.target.activations.h, 1, 0) cp.reduce_to_col(h.vec, self.target.deltas) cp.learn_step_weight_decay(self.bias, h, learnrate/batch_size, decay) h.dealloc()