def mean_squared_error(y_pred, y): if utils.isNumpy(y): y = utils.numpy2var(y) if utils.isNumpy(y_pred): y_pred = utils.numpy2var(y_pred) return torch.sum((y_pred - y)**2)
def accuracy(y_pred, y): if utils.isNumpy(y): y = utils.numpy2var(y) if utils.isNumpy(y_pred): y_pred = utils.numpy2var(y_pred) _, y_pred = y_pred.topk(1) y_pred = torch.squeeze(y_pred).long() y = torch.squeeze(y).long() acc = (y_pred == y).sum().data[0] return acc
def forward_backward(self, XList, y, loss_function=None, verbose=0): y_pred = self.forward_pass(XList) if isinstance(y, (np.ndarray, list)): y = utt.numpy2var(y) if self.my_optimizer is None: print "optimizer set..." self.set_optimizer() # ----- 2.OPTIMIZER self.my_optimizer.zero_grad() if loss_function is None: loss = loss_dict[self.loss_name](y_pred, *y) else: loss = loss_function(y_pred, *y) loss.backward() self.my_optimizer.step() loss_value = loss.data[0] if verbose: print "loss: %.3f" % loss_value return loss_value
def score(self, X, y, batch_size=100, score_function=None): if score_function is None: if self.problem_type == "classification": # Returns accuracy score_function = "acc" elif self.problem_type == "regression": # Returns mean square error score_function = "mse" elif self.problem_type == "segmentation": # Returns Jaccard score_function = "jacc" # DEFINE SCORE FUNCTION score_function = sf.SCORE_DICT[score_function] if utt.isNumpy(X): X = utt.numpy2var(X) n = X.size()[0] score = 0. for b in range(0, n, batch_size): score += score_function(self.forward_pass(X[b:b + batch_size]), y[b:b + batch_size]) return score / float(n)
def get_heatmap(self, x, output=1): n, _, n_rows, n_cols = x.shape x = tu.numpy2var(x) x = F.relu(F.max_pool2d(self.conv1(x), kernel_size=2)) x = F.relu(F.max_pool2d(self.conv2(x), kernel_size=2)) x = F.max_pool2d(self.conv3(x), kernel_size=2) x = F.sigmoid(x) x = tu.get_numpy(x) return x
def forward_pass(self, XList): self.start_gpu() self.train() if not isinstance(XList, list): XList = [XList] if isinstance(XList[0], np.ndarray): XList = utt.numpy2var(*XList) if not isinstance(XList, list): XList = [XList] y_pred = self(*XList) return y_pred