def apply(self, *variables_list): ctx = Autograd.Context() input_tensors = [v.data for v in variables_list] forward_tensor = self.forward(ctx, *input_tensors) output_variable = Variable(forward_tensor) output_variable.backward_function = lambda x: self.backward(ctx, x) output_variable.backward_variables = [v for v in variables_list] return output_variable
def __init__(self, num_input, num_output): super().__init__() self.num_input = num_input self.num_output = num_output self.weights = Variable( np.random.normal(0, 0.05, (self.num_input, self.num_output))) self.biases = Variable(np.zeros(self.num_output, dtype=np.float32)) self.register_variables(self.weights, self.biases)
def apply(cls, *variables_list): new_context = Context() input_tensors = [v.data for v in variables_list] forward_tensor = cls.forward(new_context, *input_tensors) output_variable = Variable(forward_tensor) output_variable.backward_function = lambda x: cls.backward( new_context, x) output_variable.backward_variables = [v for v in variables_list] return output_variable
def __init__(self, input_channels, output_channels, kernel_size, stride=1, padding=0): super().__init__() if isinstance(kernel_size, int): kernel_size = (kernel_size, kernel_size) if isinstance(stride, int): stride = (stride, stride) self.input_channels = input_channels self.output_channels = output_channels self.kernel_size = kernel_size self.padding = padding self.stride = stride self.weights = Variable( np.random.normal(0, 0.05, (self.input_channels, self.output_channels, self.kernel_size[0], self.kernel_size[1]))) self.biases = variable.zeros(output_channels)
def test_model_acc(): correct = 0 for test_batch_in, test_batch_out in test_batches: test_output = my_model(Variable(test_batch_in)).data correct += np.sum(np.argmax(test_output, axis=1) == np.argmax(test_batch_out, axis=1)) return correct / len(test_dataset)
for test_batch_in, test_batch_out in test_batches: test_output = my_model(Variable(test_batch_in)).data correct += np.sum(np.argmax(test_output, axis=1) == np.argmax(test_batch_out, axis=1)) my_acc = correct / len(test_dataset) return my_acc finished = False for it in range(iterations): if finished: break train_batches.shuffle() for i_b, (batch_in, batch_out) in enumerate(train_batches): model_input = Variable(batch_in) good_output = Variable(batch_out) model_output = my_model(model_input) err = loss(good_output, model_output) optimizer.zero_grad() err.backward() optimizer.step() acc = test_model_acc() print("model accuracy: {}".format(acc)) if acc > 0.97: finished = True