Example #1
0
class Sample(object):
    def __init__(self, obs, dists, values=[], pos=None, is_pad=False, is_end=False):
        self.dists = dists
        self.values = Tensor(values)  # direction
        self.obs = Tensor(obs)  # observed features
        self.is_pad = is_pad
        self.is_end = is_end
        if pos is not None: self.pos = Tensor(pos)
        self.rnn_input = None
        self.rnn_output = None
    def cuda(self, device=None):
        for i in range(len(self.dists)):
            if 'loc' in self.dists[i].__dict__:
                self.dists[i].loc = self.dists[i].loc.cuda(device)
            if 'scale' in self.dists[i].__dict__:
                self.dists[i].scale = self.dists[i].scale.cuda(device)
            if 'prob' in self.dists[i].__dict__:
                self.dists[i].prob = self.dists[i].prob.cuda(device)
        self.values = self.values.cuda(device)
        self.obs = self.obs.cuda(device)
        if self.pos is not None:
            self.pos = self.pos.cuda(device)
    def cpu(self):
        for i in range(len(self.values)):
            if 'loc' in self.dists[i].__dict__:
                self.dists[i].loc = self.dists[i].loc.cpu()
            if 'scale' in self.dists[i].__dict__:
                self.dists[i].scale = self.dists[i].scale.cpu()
            if 'prob' in self.dists[i].__dict__:
                self.dists[i].prob = self.dists[i].prob.cpu()
        self.values = self.values.cpu()
        self.obs = self.obs.cpu()
        if self.pos is not None:
            self.pos = self.pos.cpu()
Example #2
0
    def predict(self, X):
        self.model.eval()
        X_batch = X
        X_0 = FloatTensor(X_batch[:, 0])
        X_1 = FloatTensor(X_batch[:, 1])

        if self.cuda:
            X_0, X_1 = X_0.cuda(), X_1.cuda()

        X_0, X_1 = Variable(X_0), Variable(X_1)

        output1, output2 = self.model(X_0, X_1)
        return torch.sum(torch.pow(output1 - output2, 2), 1).cpu().data.numpy()
Example #3
0
    def train_epoch(self, optimizer, training_data, epoch_id='unknown'):
        epoch_time = time.time()

        accumulated_loss = 0
        average_losses = []
        training_data_length = len(training_data)
        percent_done = 0
        for index, data in enumerate(training_data):
            sample, target = FloatTensor([[data[0]]]), FloatTensor([data[1]])
            if torch.cuda.is_available():
                sample, target = sample.cuda(0), target.cuda(0)
            sample, target = Variable(sample), Variable(target)

            optimizer.zero_grad()
            output = self(sample)
            loss = self.criterion(output, target)
            loss.backward()
            optimizer.step()
            accumulated_loss += loss.data[0]

            if percent_done - 100 * index // training_data_length != 0:
                percent_done = 100 * index // training_data_length
                average_losses.append(accumulated_loss/(index+1))
                print('Finished %s%% of epoch %s | average loss: %s' % (percent_done, epoch_id+1, accumulated_loss/(index+1)))

        print "Successively trained %s epochs (epoch timer: %s)" % (epoch_id+1, DataHandler.format_time(time.time() - epoch_time))
        return average_losses
 def weighted_loss(self, prediction, target):
     weights_npy = np.array([self.weights[int(t[0])] for t in target.data])
     weights_tensor = FloatTensor(weights_npy)
     if self.use_gpu:
         weights_tensor = weights_tensor.cuda()
     return F.binary_cross_entropy_with_logits(
         prediction, target, weight=Variable(weights_tensor))
Example #5
0
    def evaluate_board(self, board, color, other_player):
        sample = FloatTensor([[board.get_representation(color)]])

        if torch.cuda.is_available():
            sample = sample.cuda(0)
        sample = Variable(sample)

        return self.model(sample)
Example #6
0
    def evaluate_each_level(self, level, mode, threshold=0):
        if mode == "train":
            evaluated_data = self.dataset
            if level > 0 and os.path.isdir('data/%s/output' % self.data_name):
                shutil.rmtree('data/%s/output' % self.data_name)
        elif mode == "validate":
            evaluated_data = self.dataset_validate
        elif mode == "test":
            evaluated_data = self.dataset_test

        number_of_class = self.classifier[level].number_of_class
        initial_tp = FloatTensor([0] * number_of_class)
        initial_pcp = FloatTensor([0] * number_of_class)
        initial_cp = FloatTensor([0] * number_of_class)
        if torch.cuda.is_available():
            initial_tp = initial_tp.cuda()
            initial_pcp = initial_pcp.cuda()
            initial_cp = initial_cp.cuda()
        all_tp = Variable(initial_tp)
        all_pcp = Variable(initial_pcp)
        all_cp = Variable(initial_cp)

        number_of_batch = 0
        for datas, labels in evaluated_data.generate_batch(
                level, self.get_batch_size(level)):
            number_of_batch = number_of_batch + 1
            datas_in = self.input_classifier(datas, level, number_of_batch,
                                             mode)
            if torch.cuda.is_available():
                datas_in = datas_in.cuda()
                labels = labels.cuda()
            datas_in = Variable(datas_in, volatile=True)
            labels_in = Variable(labels, volatile=True)
            tp, pcp, cp = self.classifier[level].evaluate_tp_pcp(
                datas_in, labels_in, threshold)
            all_tp = all_tp + tp
            all_pcp = all_pcp + pcp
            all_cp = all_cp + cp
        f1_macro, f1_micro = f1_from_tp_pcp(all_tp, all_pcp, all_cp,
                                            number_of_class)
        f1_macro = f1_macro.data.cpu().numpy()[0]
        f1_micro = f1_micro.data.cpu().numpy()[0]
        return f1_macro, f1_micro
    def next_target(self, mode, cuda, device_id):
        if mode == TRAIN_MODE: target_id = self.train.next_items(1)[0]
        elif mode == DEV_MODE: target_id = self.dev.next_items(1)[0]
        elif mode == TEST_MODE: target_id = self.test.next_items(1)[0]

        _1d_feature, _2d_feature = get_features(target_id)
        contact_map = read_contact_map(target_id)

        # Convert to FloatTensors
        _1d_feature = FloatTensor(np.expand_dims(_1d_feature, 0))
        _2d_feature = FloatTensor(np.expand_dims(_2d_feature, 0))
        contact_map = LongTensor(np.expand_dims(contact_map, 0))

        if cuda:
            _1d_feature = _1d_feature.cuda(device_id)
            _2d_feature = _2d_feature.cuda(device_id)
            contact_map = contact_map.cuda(device_id)

        return target_id, _1d_feature, _2d_feature, contact_map
Example #8
0
    def __predict_move__(self, move):
        board = deepcopy(self.current_board)
        board.apply_move(move, self.color)

        sample = FloatTensor([[board.get_representation(self.color)]])
        if torch.cuda.is_available():
            sample = sample.cuda(0)
        sample = Variable(sample)

        return self.model(sample)
Example #9
0
    def fit_batch(self, X_batch, y_batch):
        if not (X_batch.shape[3] == X_batch.shape[4] == RESNET_INPUT_SIZE):
            raise ValueError(
                f'Shape {X_batch.shape} is incompatible.\n'
                f'Dimensions 3 and 4 should be {RESNET_INPUT_SIZE}')

        self.optimizer.zero_grad()
        self.model.train()
        X_0 = FloatTensor(X_batch[:, 0])
        X_1 = FloatTensor(X_batch[:, 1])
        labels = FloatTensor(y_batch)

        if self.cuda:
            X_0, X_1, labels = X_0.cuda(), X_1.cuda(), labels.cuda()

        X_0, X_1, labels = Variable(X_0), Variable(X_1), Variable(labels)
        output1, output2 = self.model(X_0, X_1)

        loss = self.criterion(output1, output2, labels)
        self.train_history.append({'loss': loss.data[0]})
        loss.backward()
        self.optimizer.step()
        self.model.eval()
def normalize_image(image,
                    forward=True,
                    mean=(0.485, 0.456, 0.406),
                    std=(0.229, 0.224, 0.225)):

    mean = list(mean)
    std = list(std)

    im_size = image.size()
    mean = FloatTensor(mean).unsqueeze(1).unsqueeze(2)
    std = FloatTensor(std).unsqueeze(1).unsqueeze(2)
    if image.is_cuda:
        mean = mean.cuda()
        std = std.cuda()
    if isinstance(image, Variable):
        mean = Variable(mean, requires_grad=False)
        std = Variable(std, requires_grad=False)
    if forward:
        if len(im_size) == 3:
            result = image.sub(mean.expand(im_size)).div(std.expand(im_size))
        elif len(im_size) == 4:
            result = image.sub(mean.unsqueeze(0).expand(im_size)).div(
                std.unsqueeze(0).expand(im_size))
        else:
            raise TypeError("Couldn't read image due to an unexpected format")

    else:
        if len(im_size) == 3:
            result = image.mul(std.expand(im_size)).add(mean.expand(im_size))
        elif len(im_size) == 4:
            result = image.mul(std.unsqueeze(0).expand(im_size)).add(
                mean.unsqueeze(0).expand(im_size))
        else:
            raise TypeError("Couldn't read image due to an unexpected format")

    return result
Example #11
0
    def predict(self, X_eval: torch.FloatTensor) -> list:
        #check dimensions of input data
        X_eval = self.x_to_tensor(X_eval)
        if X_eval.size()[1] != self.model._input_dim:
            raise AttributeError(
                "input dataset should have the same dimensions as the expected input dimensions of the model."
            )

        #get class probabilities for each input in dataset
        if torch.cuda.is_available(): X_eval = X_eval.cuda()
        if not isinstance(X_eval, Variable): X_eval = Variable(X_eval)
        probs = self.model(X_eval)

        #predict a class for each input from these probabilties
        values, labels = probs.max(1)

        return [self._classes[l] for l in labels.data]
Example #12
0
 def forward(self, iword, owords):
     batch_size = iword.size()[0]
     context_size = owords.size()[1]
     if self.weights is not None:
         nwords = t.multinomial(self.weights, batch_size * context_size * self.n_negs, replacement=True).view(batch_size, -1)
     else:
         nwords = FT(batch_size, context_size * self.n_negs).uniform_(0, self.vocab_size - 1).long()
     ivectors = self.embedding.forward_i(iword).unsqueeze(2)
     ovectors = self.embedding.forward_o(owords)
     non_pad = FT((owords != self.pad).float())
     non_pad = non_pad.cuda() if self.embedding.ovectors.weight.is_cuda else non_pad
     N = non_pad.sum()
     nvectors = self.embedding.forward_o(nwords).neg()
     #oloss = t.bmm(ovectors, ivectors).squeeze().sigmoid().log().mean(1)
     oloss = t.sum(ls(t.bmm(ovectors, ivectors).squeeze()) * non_pad) / N
     nloss = ls(t.bmm(nvectors, ivectors).squeeze()).view(-1, context_size, self.n_negs).sum(2).mean(1)
     return -(oloss + nloss).mean()
Example #13
0
 def forward(self,
             emb_batch: torch.FloatTensor,
             length_batch: torch.LongTensor,
             features_batch: List[np.ndarray] = None) -> torch.Tensor:
     """
     :param emb_batch: (batch_size, seq_len, embed_size)
     :param length_batch: (batch_size, )
     :param features_batch: (batch_size, feat_size)
     :return:
     """
     batch_size = emb_batch.size(0)
     emb_batch = self.dropout(emb_batch)
     if torch.cuda.is_available():
         emb_batch = emb_batch.cuda()
     output, (final_hidden_state, final_cell_state) = self.lstm(emb_batch)
     final_hidden_state = final_hidden_state.view(batch_size, -1)
     return torch.mean(output, dim=1)
Example #14
0
    def train_epoch(self, optimizer, batch_size):

        training_data = DataHandler.get_training_data(batch_size=batch_size)

        for data in training_data:
            sample, target = FloatTensor([[data[0]]]), FloatTensor([data[1]])
            if torch.cuda.is_available():
                sample, target = sample.cuda(), target.cuda()
            sample, target = Variable(sample), Variable(target)
            optimizer.zero_grad()
            output = self(sample)

            #Negative Log-Likelihood loss
            criterion = nn.MSELoss()
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
Example #15
0
    def auc_test(self):

        testpos = self.datastore.test.sample(SAMPLES_PER_ITERATION)
        testneg = self.datastore.df.sample(SAMPLES_PER_ITERATION)

        batch = []
        for ind in tqdm(range(0, len(testpos))):

            pos = testpos.iloc[ind].tolist()
            neg = testneg.iloc[ind].tolist()

            if pos[1] == neg[1]:
                neg = self.datastore.sample(pos[1]).iloc[0].tolist()

            batch.append(pos + neg)

        batch = FloatTensor(batch)

        if CUDA:
            batch = batch.cuda()

        users = Variable(batch[:, 1]).long()
        pos_items = Variable(batch[:, 5:14]).long()
        neg_items = Variable(batch[:, 19:]).long()

        pos_preds = self(users, pos_items)
        neg_preds = self(users, neg_items)

        auc = 0.0
        for i in range(0, len(pos_preds)):
            sp = pos_preds[i]
            sn = neg_preds[i]

            if CUDA:
                sp = sp.data.cpu().numpy()[0]
                sn = sn.data.cpu().numpy()[0]

            if sp > sn:
                auc += 1.0
            elif sp == sn:
                auc += 0.5

        return auc / len(testpos)
Example #16
0
 def child_based_correction(self, y):
     if type(y) != np.ndarray:
         num_test = y.cpu().numpy()
     else:
         num_test = y
     for k in num_test:
         for n in range(-1 * (self.dataset.number_of_level() - 1), 0):
             n = n * -1
             start_index = self.dataset.level[n]
             last_index = self.dataset.level[n + 1]
             for i in range(start_index, last_index):
                 for p in self.dataset.parent_of[i]:
                     if k[i]:
                         k[p] = 1
     num_test = num_test.astype(float)
     correct_test = FloatTensor(num_test)
     if torch.cuda.is_available():
         correct_test = correct_test.cuda()
     return correct_test
Example #17
0
 def update(self, model=None, do_forward=True):
     super(PassageState, self).update()
     if model is None: return
     # init variables based on number of models
     self.init_model_params(model)
     if do_forward or self.parent.proposal is None:
         feature = self.extract_feature()
     if self.parent is None:
         rel_pos = np.zeros(self.ss.dim)
     else:
         # update root
         if self.parent.proposal is None:
             rel_pos = np.zeros(self.ss.dim)
             feature = self.parent.extract_feature()
             self.parent.sample = Sample(Tensor(feature),
                                         prior_dist(),
                                         pos=rel_pos)
             self.parent.init_model_params(model)
             self.parent.proposal, self.parent.hidden_state = \
                 model.forward(self.parent.sample, prev_hidden_state=self.parent.hidden_state)
         # update else
         prior_vec = [1, 0]
         if self.parent.parent is not None:
             prior_vec = np.asarray([
                 self.parent.value[0] - self.parent.parent.value[0],
                 self.parent.value[1] - self.parent.parent.value[1]
             ])
         new_vec = np.asarray([
             self.value[0] - self.parent.value[0],
             self.value[1] - self.parent.value[1]
         ])
         move = theta_from_vecs(prior_vec, new_vec)
         rel_pos = self.value - self.parent.value
         val = Tensor([move])
         if model.on_cuda: val = val.cuda()
         self.log_prob = self.parent.proposal[0].log_prob(val).item()
     if do_forward:
         self.sample = Sample(Tensor(feature), prior_dist(), pos=rel_pos)
         hidden = None if self.parent is None else self.parent.hidden_state
         self.proposal, self.hidden_state = \
              model.forward(self.sample, prev_hidden_state=hidden)
Example #18
0
    def batch_sample(self):
        batch = []

        testpos = self.train.sample(SAMPLES_PER_ITERATION)
        testneg = self.train.sample(SAMPLES_PER_ITERATION)

        for ind in range(0, len(testpos)):

            pos = testpos.iloc[ind].tolist()
            neg = testneg.iloc[ind].tolist()

            if pos[1] == neg[1]:
                neg = self.sample(pos[1]).iloc[0].tolist()

            batch.append(pos + neg)

        batch = FloatTensor(batch)

        if CUDA:
            batch = batch.cuda()

        return torch.utils.data.DataLoader(batch,
                                           batch_size=BATCH_SIZE,
                                           shuffle=True)
Example #19
0
    def evaluate(self, mode, correction=True, mandatory_leaf=False):
        if mode == "train":
            evaluated_data = self.dataset
        elif mode == "validate":
            evaluated_data = self.dataset_validate
        elif mode == "test":
            evaluated_data = self.dataset_test

        number_of_batch = 0
        number_of_class = self.dataset.number_of_classes()
        all_tp = FloatTensor([0] * number_of_class)
        all_pcp = FloatTensor([0] * number_of_class)
        all_cp = FloatTensor([0] * number_of_class)
        if torch.cuda.is_available():
            all_tp = all_tp.cuda()
            all_pcp = all_pcp.cuda()
            all_cp = all_cp.cuda()

        for datas, labels in evaluated_data.generate_batch(
                -1, self.get_batch_size(-1)):

            number_of_batch = number_of_batch + 1
            all_labels = FloatTensor([])
            if mandatory_leaf:
                all_pred = FloatTensor([])
            else:
                all_pred = ByteTensor([])
            if torch.cuda.is_available():
                all_labels = all_labels.cuda()
                all_pred = all_pred.cuda()
            for level in range(self.dataset.number_of_level()):
                datas_in = self.input_classifier(datas, level, number_of_batch,
                                                 mode)

                datas_in = Variable(datas_in, volatile=True)
                each_level = labels[:, self.dataset.level[level]:self.dataset.
                                    level[level + 1]]
                if torch.cuda.is_available():
                    datas_in = datas_in.cuda()
                    each_level = each_level.cuda()
                if mandatory_leaf:
                    pred = self.classifier[level](datas_in).data
                else:
                    pred = self.classifier[level].output_with_threshold(
                        datas_in).data
                all_labels = torch.cat((all_labels, each_level), 1)
                all_pred = torch.cat((all_pred, pred), 1)

            if mandatory_leaf:
                all_pred = self.get_leaf_node(all_pred)
                all_pred = self.to_one_hot(all_pred)
            if correction:
                all_pred = self.child_based_correction(all_pred)
            tp, pcp, cp = tp_pcp(all_labels, all_pred, use_threshold=False)
            all_tp = all_tp + tp
            all_pcp = all_pcp + pcp
            all_cp = all_cp + cp

        f1_macro, f1_micro = f1_from_tp_pcp(all_tp, all_pcp, all_cp,
                                            self.dataset.number_of_classes())
        f1_each_level = []
        for level in range(self.dataset.number_of_level()):
            each_tp = all_tp[self.dataset.level[level]:self.dataset.level[level
                                                                          + 1]]
            each_pcp = all_pcp[self.dataset.level[level]:self.dataset.
                               level[level + 1]]
            each_cp = all_cp[self.dataset.level[level]:self.dataset.level[level
                                                                          + 1]]
            each_f1_macro, each_f1_micro = f1_from_tp_pcp(
                each_tp, each_pcp, each_cp,
                self.classifier[level].number_of_class)
            f1_each_level.append((each_f1_macro, each_f1_micro))
        return f1_macro, f1_micro, f1_each_level
Example #20
0
    def export_result(self,
                      mode,
                      correction=True,
                      mandatory_leaf=False,
                      file_name=""):
        if mode == "train":
            evaluated_data = self.dataset
        elif mode == "validate":
            evaluated_data = self.dataset_validate
        elif mode == "test":
            evaluated_data = self.dataset_test

        if file_name == "":
            file_name = mode

        if not os.path.exists("export/%s/prediction" % (self.data_name)):
            os.makedirs("export/%s/prediction" % (self.data_name))
        if not os.path.exists("export/%s/probability_prediction" %
                              (self.data_name)):
            os.makedirs("export/%s/probability_prediction" % (self.data_name))
        np_all_name = np.array(self.dataset.all_name)
        f = open("export/%s/prediction/%s.txt" % (self.data_name, file_name),
                 'w')
        f2 = open(
            "export/%s/probability_prediction/%s.txt" %
            (self.data_name, file_name), 'w')

        number_of_batch = 0
        for datas, labels in evaluated_data.generate_batch(
                -1, self.get_batch_size(-1)):

            number_of_batch = number_of_batch + 1
            all_labels = FloatTensor([])
            if not mandatory_leaf:
                all_pred = ByteTensor([])
            all_prob = FloatTensor([])
            if torch.cuda.is_available():
                all_labels = all_labels.cuda()
                all_prob = all_prob.cuda()
                if not mandatory_leaf:
                    all_pred = all_pred.cuda()
            for level in range(self.dataset.number_of_level()):
                datas_in = self.input_classifier(datas, level, number_of_batch,
                                                 mode)

                datas_in = Variable(datas_in, volatile=True)
                each_level = labels[:, self.dataset.level[level]:self.dataset.
                                    level[level + 1]]
                if torch.cuda.is_available():
                    datas_in = datas_in.cuda()
                    each_level = each_level.cuda()
                prob = self.classifier[level](datas_in).data
                all_prob = torch.cat((all_prob, prob), 1)

                all_labels = torch.cat((all_labels, each_level), 1)

                if not mandatory_leaf:
                    pred = self.classifier[level].output_with_threshold(
                        datas_in).data
                    all_pred = torch.cat((all_pred, pred), 1)

            if mandatory_leaf:
                all_pred = self.get_leaf_node(all_prob)
            else:
                all_pred = self.child_based_correction(all_pred)
                all_pred = self.select_deepest_label(all_pred)
            for p in all_pred:
                f.write(" ".join(np_all_name[p]) + "\n")
            for p in F.sigmoid(all_prob).cpu().numpy():
                f2.write(" ".join(map(str, p)) + "\n")

        f.close()
        f2.close()
Example #21
0
class EachLevelClassifier(nn.Module):

    def __init__(self, input_size, number_of_class, use_dropout=True, learning_rate=0.001):
        super(EachLevelClassifier, self).__init__()
        self.input_size = input_size
        self.number_of_class = number_of_class
        self.use_dropout = use_dropout
        self.learning_rate = learning_rate
        self.best_threshold = 0.5
        self.change_ratio = 1
        self.initial_structure()
        self.optimizer = optim.Adam(self.parameters(), lr=self.learning_rate)

    def initial_structure(self):
        raise NotImplementedError

    def forward(self, x):
        raise NotImplementedError

    def train_model(self, x, y):
        self.train()
        self.optimizer.zero_grad()
        output = self(x)
        loss = self.loss_function(output, y)
        loss.backward()
        self.optimizer.step()
        output_loss = loss.data.cpu().numpy()[0]
        return output_loss

    def initial_weight(self, number_of_data, count):
        weight = []
        for i, c in enumerate(count):
            try:
                w = number_of_data / c
                weight.append(w)
            except ZeroDivisionError:
                weight.append(10000)
        self.pos_weight = FloatTensor(weight)
        if torch.cuda.is_available():
            self.pos_weight = self.pos_weight.cuda()
        self.loss_function = nn.MultiLabelSoftMarginLoss(
            size_average=True, weight=self.pos_weight)

    def evaluate(self, x, y):
        self.eval()
        output = self(x)
        f1_macro, f1_micro = f1_score(y, output, self.number_of_class, use_threshold=True,
                                      threshold=self.best_threshold)
        f1_macro = f1_macro.data.cpu().numpy()[0]
        f1_micro = f1_micro.data.cpu().numpy()[0]
        return f1_macro, f1_micro

    def evaluate_tp_pcp(self, x, y, threshold):
        self.eval()
        output = self(x)
        if threshold == 0:
            threshold = self.best_threshold
        tp, pcp, cp = tp_pcp(
            y, output, use_threshold=True, threshold=threshold)
        return tp, pcp, cp

    def output_with_threshold(self, x):
        return F.sigmoid(self(x)) > self.best_threshold
Example #22
0
    def get_saliency(
        self,
        x: torch.FloatTensor,
        target_class: str,
        guide_backprop: bool = False,
    ) -> torch.FloatTensor:
        '''Compute the saliency of a target class on an input
        vector `x`.

        Parameters
        ----------
        x : torch.FloatTensor
            [1, Genes] vector of gene expression.
        target_class : str
            class in `.class_names` for which to compute gradients.
        guide_backprop : bool
            perform "guided backpropogation" by clamping gradients
            to only positive values at each ReLU.
            see: https://arxiv.org/pdf/1412.6806.pdf

        Returns
        -------
        salience : torch.FloatTensor
            gradients on `target_class` with respect to `x`.
        '''
        if target_class not in self.class_names:
            msg = f'{target_class} is not in `.class_names`'
            raise ValueError(msg)

        target_idx = np.where(
            target_class == self.class_names
        )[0].astype(np.int)
        target_idx = int(target_idx)

        self.model.zero_grad()

        if guide_backprop:
            self.rectified_outputs = []
            self.store_rectified_grad = []            
            self._guided_backprop_hooks()

        # store gradients on the input
        if torch.cuda.is_available():
            x = x.cuda()
        x.requires_grad = True

        # module hook will record gradients here
        self.gradients = torch.zeros_like(x)

        # forward pass
        output = self.model(x)

        # create a [N, C] tensor to store gradients
        target = torch.zeros_like(output)
        # set the target class to `1`, creating a one-hot
        # of the target class
        target[:, target_idx] = 1

        # compute gradients with backprop
        output.backward(
            gradient=target,
        )

        # detach from the graph and move to main memory
        target = target.detach().cpu()

        return self.gradients
Example #23
0
    def prepare_batch(self, batch, istest=False):

        volatile = True if istest else False
        # logging.info("istest is %s and volatile is %s", istest, volatile)

        l_batch, l_lengths, \
        r_batch, r_lengths, \
        doc_bow_batch, \
        truewid_descvec_batch, \
        types_batch, \
        coherence_batch, \
        wids_batch, wid_cprobs_batch, nocands_mask_batch = batch

        # Do not mess with type casting here. torch.from_numpy is SLOW
        l_batch = MyTensor(l_batch)
        l_lengths = torch.LongTensor(l_lengths)
        r_batch = MyTensor(r_batch)
        r_lengths = torch.LongTensor(r_lengths)
        wids_batch = torch.LongTensor(wids_batch)
        # nocands_mask_batch = MyTensor(nocands_mask_batch)

        if self.args["usecoh"]:
            batch_rcs, batch_vals, shape = coherence_batch
            batch_rcs = torch.LongTensor(batch_rcs)
            batch_vals = MyTensor(batch_vals)
            coherence_batch = MySparseTensor(batch_rcs.t(), batch_vals,
                                             torch.Size(shape))

        if self.args["usedesc"]:
            truewid_descvec_batch = MyTensor(truewid_descvec_batch)
        if self.args["usetype"]:
            types_batch = MyTensor(types_batch)

        if self.args["cuda"]:
            devid = self.args["device_id"]
            l_batch = l_batch.cuda(device=devid)
            l_lengths = l_lengths.cuda(device=devid)
            r_batch = r_batch.cuda(device=devid)
            r_lengths = r_lengths.cuda(device=devid)
            wids_batch = wids_batch.cuda(device=devid)
            # nocands_mask_batch = nocands_mask_batch.cuda(device=devid)
            if self.args["usecoh"]:
                coherence_batch = coherence_batch.cuda(device=devid)
            if self.args["usedesc"]:
                truewid_descvec_batch = truewid_descvec_batch.cuda(
                    device=devid)
            if self.args["usetype"]:
                types_batch = types_batch.cuda(device=devid)

        l_batch, l_lengths = V(l_batch,
                               volatile=volatile), V(l_lengths,
                                                     volatile=volatile)
        r_batch, r_lengths = V(r_batch,
                               volatile=volatile), V(r_lengths,
                                                     volatile=volatile)
        wids_batch = V(wids_batch, volatile=volatile)
        # nocands_mask_batch = V(nocands_mask_batch)
        if self.args["usecoh"]:
            coherence_batch = V(coherence_batch, volatile=volatile)
        if self.args["usedesc"]:
            truewid_descvec_batch = V(truewid_descvec_batch, volatile=volatile)
        if self.args["usetype"]:
            types_batch = V(types_batch, volatile=volatile)

        batch = l_batch, l_lengths, \
                r_batch, r_lengths, \
                truewid_descvec_batch, \
                types_batch, \
                coherence_batch, \
                wids_batch, wid_cprobs_batch, nocands_mask_batch
        return batch
Example #24
0
def choose_action(obs: torch.FloatTensor, network: network_conv) -> int:
    return torch.argmax(network(obs.cuda())).item()
Example #25
0
 def _var(self, x: torch.FloatTensor) -> Variable:
     return Variable(x.cuda() if self.on_gpu else x)