def __init__(self, **config): super(SamNet, self).__init__() # # options # Network.default_setup(self, config) self.input_shape = config.get("input_shape" , (1, 28, 28)) self.output_shape = config.get("output_shape" , (2,)) self.batch_size = config.get("batch_size" , 64 ) self.test_batch_size = config.get("test_batch_size", 1000) self.epochs = config.get("epochs" , 3 ) self.learning_rate = config.get("learning_rate" , 0.01) self.momentum = config.get("momentum" , 0.5 ) # # layers # # 1 input image, 10 output channels, 5x5 square convolution kernel self.add_module("conv1", nn.Conv2d(1, 10, kernel_size=5)) self.add_module("conv1_pool", nn.MaxPool2d(2)) self.add_module("conv1_activation", nn.ReLU()) self.add_module("conv2", nn.Conv2d(10, 10, kernel_size=5)) self.add_module("conv2_drop", nn.Dropout2d()) self.add_module("conv2_pool", nn.MaxPool2d(2)) self.add_module("conv2_activation", nn.ReLU()) self.add_module("flatten", nn.Flatten(1)) # 1 => skip the first dimension because thats the batch dimension self.add_module("fc1", nn.Linear(self.size_of_last_layer, product(self.output_shape))) self.add_module("fc1_activation", nn.LogSoftmax(dim=1)) # # support (optimizer, loss) # self.to(self.hardware) # create an optimizer self.optimizer = optim.SGD(self.parameters(), lr=self.learning_rate, momentum=self.momentum)
def __init__(self, **config): self.input_shape = config.get("input_shape", (1, 28, 28)) self.latent_shape = config.get("latent_shape", (20, )) self.output_shape = config.get("output_shape", (2, )) self.learning_rate = config.get("learning_rate", 0.01) self.momentum = config.get("momentum", 0.5) self.log_interval = config.get("log_interval", 10) with self.setup(input_shape=self.input_shape, output_shape=self.output_shape): # # encoder # self.encoder = config.get( "encoder", ImageEncoder( input_shape=self.input_shape, output_shape=self.latent_shape, )) self.add_module("encoder", self.encoder) # # task (classifier) # self.task_network = nn.Sequential( nn.Linear(product(self.latent_shape), 2), # binary classification nn.Sigmoid(), ) self.add_module("task_network", self.task_network) self.classifier_loss_function = self.loss_function = nn.BCELoss() self.optimizer = torch.optim.SGD(self.parameters(), lr=self.learning_rate, momentum=self.momentum)
def size_of_last_layer(self): output = None try: output = product(self.input_shape if len(tuple(self.children())) == 0 else layer_output_shapes( self.children(), self.input_shape)[-1]) except Exception as error: print("Error getting self.size_of_last_layer", self) print('error = ', error) raise error return output
def __init__(_, input_shape=None, output_shape=None, loss=None, optimizer=None, layers=None, **config): # # basic setup # real_super(ImageModelSequential, self).__init__() self.suppress_output = config.get("suppress_output", False) self.show = lambda *args, **kwargs: print(*args, **kwargs) if not self.suppress_output else None # self.hardware = torch.device('cuda' if torch.cuda.is_available() else 'cpu') self.hardware = torch.device('cpu') # FIXME: I had to do this to get the loss function working self.layers = layers or nn.Sequential() self.loss = loss self.optimizer = optimizer # # upgrade image input to 3D if 2D # if len(input_shape) == 2: input_shape = (1, *input_shape) # channels, height, width = input_shape self.input_shape = input_shape self.output_shape = output_shape self.input_feature_count = product(self.input_shape) self.output_feature_count = product(self.output_shape)
def __init__(self, **config): super(ClassifierOutput, self).__init__() # # options # Network.default_setup(self, config) self.input_shape = config.get("input_shape", (1, 28, 28)) self.output_shape = config.get("output_shape", (2, )) # # layers # self.add_module( "fc2", nn.Linear(self.size_of_last_layer, product(self.output_shape))) self.add_module("fc2_activation", nn.LogSoftmax(dim=1)) # # support (optimizer, loss) # self.to(self.hardware)
def generate_confusion_matrix(self, test_loader): from tools.basics import product number_of_outputs = product(self.latent_shape) confusion_matrix = torch.zeros(number_of_outputs, number_of_outputs) test_losses = [] test_loss = 0 correct = 0 self.eval() with torch.no_grad(): for batch_of_inputs, batch_of_ideal_outputs in test_loader: latent_space_activation_batch = self.encoder.forward( batch_of_inputs) for each_activation_space, each_ideal_output in zip( latent_space_activation_batch, batch_of_ideal_outputs): # which index was chosen predicted_index = numpy.argmax(each_activation_space) actual_index = numpy.argmax(each_ideal_output) confusion_matrix[actual_index][predicted_index] += 1 return confusion_matrix
def __init__(self, **config): self.input_shape = config.get("input_shape", (1, 28, 28)) self.output_shape = config.get("output_shape", (10, )) self.learning_rate = config.get("learning_rate", 0.01) self.momentum = config.get("momentum", 0.5) self.log_interval = config.get("log_interval", 10) with self.setup(input_shape=self.input_shape, output_shape=self.output_shape): self.add_module("conv1", nn.Conv2d(1, 10, kernel_size=5)) self.add_module("conv1_pool", nn.MaxPool2d(2)) self.add_module("conv1_activation", nn.ReLU()) self.add_module("conv2", nn.Conv2d(10, 20, kernel_size=5)) self.add_module("conv2_dropout", nn.Dropout2d()) self.add_module("conv2_pool", nn.MaxPool2d(2)) self.add_module("conv2_activation", nn.ReLU()) self.add_module( "flatten", nn.Flatten(1) ) # 1 => skip the first dimension because thats the batch dimension self.add_module("fc1", nn.Linear(self.size_of_last_layer, 50)) self.add_module("fc1_activation", nn.ReLU()) self.add_module("fc1_dropout", nn.Dropout2d()) self.add_module( "fc2", nn.Linear(self.size_of_last_layer, product(self.output_shape))) self.add_module("fc2_activation", nn.LogSoftmax(dim=-1)) def NLLLoss(batch_of_actual_outputs, batch_of_ideal_outputs): output = batch_of_actual_outputs[ range(len(batch_of_ideal_outputs)), batch_of_ideal_outputs] return -output.sum() / len(output) self.loss_function = NLLLoss self.optimizer = SGD(self.parameters(), lr=self.learning_rate, momentum=self.momentum)
def size_of_last_layer(self): return product( self.input_shape if len(tuple(self.children())) == 0 else layer_output_shapes(self.children(), self.input_shape)[-1])
def size_of_last_layer(self): return product(self.input_shape if len(self._modules) == 0 else layer_output_shapes(self._modules.values(), self.input_shape)[-1])
def size_of_last_layer(self): # if no layers, then use the input size if len(self.layers) == 0: return self.input_feature_count else: return product(layer_output_shapes(self.layers, self.input_shape)[-1])
# # train and test # split = SplitAutoEncoder() classifier = SimpleClassifier() results = [] for each in [9]: result = {} train_dataset, test_dataset, train_loader, test_loader = quick_loader(binary_mnist([each]), [5, 1]) # # split # # reset the task network part (last few layers) split.task_network = nn.Sequential( nn.Linear(product(split.latent_shape), 2), # binary classification nn.Sigmoid(), ) result["split_train"] = split.fit(loader=train_loader, max_epochs=1) result["split_test"] = split.test(test_loader) # # classifier # train_dataset, test_dataset, train_loader, test_loader = binary_mnist([each]) # reset the task network part (last few layers) classifier.task_network = nn.Sequential( nn.Linear(product(classifier.latent_shape), 2), # binary classification nn.Sigmoid(), ) result["classifier_train"] = classifier.fit(loader=train_loader, max_epochs=1)