def compute_td_loss(model, target_net, batch, gamma, device): state, action, reward, next_state, done = batch state = Variable(FloatTensor(np.float32(state))).to(device) next_state = Variable(FloatTensor(np.float32(next_state))).to(device) action = Variable(LongTensor(action)).to(device) reward = Variable(FloatTensor(reward)).to(device) done = Variable(FloatTensor(done)).to(device) q_values = model(state) next_q_values = target_net(next_state) q_value = q_values.gather(1, action.unsqueeze(-1)).squeeze(-1) next_q_value = next_q_values.max(1)[0] expected_q_value = reward + gamma * next_q_value * (1 - done) loss = (q_value - Variable(expected_q_value.data).to(device)).pow(2).mean() loss.backward()
def run_test(): """ Creates tagger model object and tests its functionality for some dummy word IDs and blindly chosen hyperparameter values """ test_ids = autograd.Variable(LongTensor([65, 7, 1, 14])) tagger = TaggerModel(numWords=100, numTags=100, embSize=100, rnnSize=100, dropoutRate=0.5, use_gpu=False) tagScores = tagger(test_ids) if tagScores.size() == ( len(test_ids), 100): #model output should be of dimension seqlen x numTags print("TaggerModel module passed test.")
def generate_toy_data(points_number=1000, dist=1.0 / (math.sqrt(1.0 * math.pi))): """ Generate a data set of points sampled between [0, 1]² each with a label 0 if x + y < dist and 1 if outside :param points_number: the number of points to sample :param dist: the value to cut the plane with :return: a tuple containing the examples and their associated labels """ examples = FloatTensor(points_number, 2).uniform_(0, 1) targets = LongTensor(points_number, 2).zero_() for i, ex in enumerate(examples): current_dist = math.sqrt(ex[0]**2 + ex[1]**2) if current_dist > dist: targets[i, 0] = 1 else: targets[i, 1] = 1 return examples, targets
def __getitem__(self, i): # read data image = cv2.imread(self.images_fps[i]) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) mask = cv2.imread(self.masks_fps[i], 0) # apply augmentations if self.augmentation: sample = self.augmentation(image=image, mask=mask) image, mask = sample['image'], sample['mask'] # apply preprocessing if self.preprocessing: sample = self.preprocessing(image=image, mask=mask) image, mask = sample['image'], sample['mask'] return image, LongTensor(mask)
def estimate_from_idxsens(self, idx_sens, normalize=False): # sentence padding pad_size = self.args.pad_size pad_sens = [ pad_sentence(s, pad_size, self.def_word2ix) for s in idx_sens ] # go through the encoder self.eval() pad_sens = Variable(LongTensor(pad_sens)) if self.use_gpu: pad_sens = pad_sens.cuda() out_embs = self(pad_sens) out_embs = out_embs.cpu().data.numpy() if normalize: out_embs = normalize_matrix_by_row(out_embs) self.train() return out_embs
def __next__(self): self.iter += 1 if self.iter > self.max_dataset_size: raise StopIteration AB, AB_paths = next(self.data_loader_iter) w_total = AB.size(3) w = int(w_total / 2) h = AB.size(2) w_offset = random.randint(0, max(0, w - self.fineSize - 1)) h_offset = random.randint(0, max(0, h - self.fineSize - 1)) A = AB[:, :, h_offset:h_offset + self.fineSize, w_offset:w_offset + self.fineSize] B = AB[:, :, h_offset:h_offset + self.fineSize, w_offset:w_offset + self.fineSize] n_rgb = 3 if self.rgb else 1 if self.blanks == 0: AA = A.clone() else: #randomly remove some of the glyphs in input # train if not self.dict: blank_ind = np.repeat( np.random.permutation( A.size(1) / n_rgb)[0:int(self.blanks * A.size(1) / n_rgb)], n_rgb) # test else: file_name = map(lambda x: x.split("/")[-1], AB_paths) if len(file_name) > 1: raise Exception('batch size should be 1') file_name = file_name[0] blank_ind = self.random_dict[file_name][0:int(self.blanks * A.size(1) / n_rgb)] rgb_inds = np.tile(range(n_rgb), int(self.blanks * A.size(1) / n_rgb)) blank_ind = blank_ind * n_rgb + rgb_inds AA = A.clone() # 按LongTensor中的索引数确定的顺序,将原tensor用1填充。 AA.index_fill_(1, LongTensor(list(blank_ind)), 1) return {'A': AA, 'A_paths': AB_paths, 'B': B, 'B_paths': AB_paths}
def __init__(self, probs): super(AliasMultinomial, self).__init__() assert math.isclose( probs.sum().item(), 1, rel_tol=1e-7), 'The noise distribution must sum to 1' cpu_probs = probs.cpu() K = len(probs) # such a name helps to avoid the namespace check for nn.Module self_prob = [0] * K self_alias = [0] * K # Sort the data into the outcomes with probabilities # that are larger and smaller than 1/K. smaller = [] larger = [] for idx, prob in enumerate(cpu_probs): self_prob[idx] = K * prob if self_prob[idx] < 1.0: smaller.append(idx) else: larger.append(idx) # Loop though and create little binary mixtures that # appropriately allocate the larger outcomes over the # overall uniform mixture. while len(smaller) > 0 and len(larger) > 0: small = smaller.pop() large = larger.pop() self_alias[small] = large self_prob[large] = (self_prob[large] - 1.0) + self_prob[small] if self_prob[large] < 1.0: smaller.append(large) else: larger.append(large) for last_one in smaller + larger: self_prob[last_one] = 1 self.register_buffer('prob', Tensor(self_prob)) self.register_buffer('alias', LongTensor(self_alias))
def roc_curve(input: Tensor, targ: Tensor): "Computes the receiver operator characteristic (ROC) curve by determining the true positive ratio (TPR) and false positive ratio (FPR) for various classification thresholds. Restricted binary classification tasks." # wassname: fix this by making LongTensor([0]=>device) targ = targ == 1 desc_score_indices = torch.flip(input.argsort(-1), [-1]) input = input[desc_score_indices] targ = targ[desc_score_indices] d = input[1:] - input[:-1] distinct_value_indices = torch.nonzero(d).transpose(0, 1)[0] threshold_idxs = torch.cat( (distinct_value_indices, LongTensor([len(targ) - 1]).to(targ.device))) tps = torch.cumsum(targ * 1, dim=-1)[threshold_idxs] fps = 1 + threshold_idxs - tps if tps[0] != 0 or fps[0] != 0: zer = torch.zeros(1, dtype=fps.dtype, device=fps.device) fps = torch.cat((zer, fps)) tps = torch.cat((zer, tps)) fpr, tpr = fps.float() / fps[-1], tps.float() / tps[-1] return fpr, tpr
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
def backward_G(self, pass_grad, iter): b,c,m,n = self.fake_B0.size() if not self.opt.lambda_C or (iter>700): self.loss_G_L1 = Variable(torch.zeros(1)) else: weight_val = 10.0 weights = torch.ones(b,c,m,n).cuda() if self.opt.gpu_ids else torch.ones(b,c,m,n) obs_ = torch.cuda.LongTensor(self.obs) if self.opt.gpu_ids else LongTensor(self.obs) weights.index_fill_(1,obs_,weight_val) weights=Variable(weights, requires_grad=False) self.loss_G_L1 = self.criterionL1(weights * self.fake_B0, weights * self.fake_B0_init.detach()) * self.opt.lambda_C self.loss_G_L1.backward(retain_graph=True) self.fake_B0.backward(pass_grad)
def _build_batches( self, tokens: List[LongTensor], src_lang: str, tgt_lang: str, skip_invalid_size_inputs: bool, max_sentences: Optional[int] = 10, max_tokens: Optional[int] = None) -> Iterator[Dict[str, Any]]: lengths = LongTensor([t.numel() for t in tokens]) batch_iterator = self.task.get_batch_iterator( dataset=self._build_dataset_for_inference(tokens, lengths, src_lang, tgt_lang), max_tokens=max_tokens, max_sentences=max_sentences, max_positions=self.max_positions[f"{src_lang}-{tgt_lang}"], ignore_invalid_inputs=skip_invalid_size_inputs, disable_iterator_cache=True, ).next_epoch_itr(shuffle=False) return batch_iterator
def font_transform(img,path, rgb_in): n_rgb = img.size()[0] target_size = img.size()[1] D_ = img.size()[2]/target_size # warnings.warn("size, %s %s"%(img.size(),D_)) if not rgb_in: img = torch.mean(img,dim=0) #only one of the RGB channels img = img[None,:,:] #(1,64,64) n_rgb =1 else: img = img.permute(1,0,2).contiguous().view(1,target_size, n_rgb*img.size()[2]) slices = [] for j in range(target_size): for i in np.arange(0,D_): slices += list(target_size * np.arange(i,D_*n_rgb,D_) + j) img = index_select(img,2,LongTensor(slices)).view(target_size,target_size,D_*n_rgb) img = img.permute(2,0,1) return img
def __init__(self, file, tokenizer: BertTokenizer, max_len, max_sent, batch_size, split_token, device, data=None): super(HyperpartisanDataset, self).__init__(file, tokenizer, max_len, max_sent, batch_size, split_token, device) if data is None: data = pd.read_json(self.file, orient='records') self.docs, self.masks = self.get_data(data) self.y = LongTensor((data['label'] == 'true').astype('int').to_numpy()) self.shuffle()
def translate_batch(self, src_sents, tgt_sents): src_sents, src_lens, tgt_sents, tgt_lens =\ self.prepare_batch(src_sents, tgt_sents) batch_size = src_sents.size(0) start_decode =\ Variable(LongTensor([[self.tgt_vocab.w2i['<s>']] * batch_size])).transpose(0, 1) if self.use_cuda: src_sents = src_sents.cuda() tgt_sents = tgt_sents.cuda() start_decode = start_decode.cuda() output, hidden_c = self.encoder(src_sents, src_lens) preds, attn = self.decoder.decode(start_decode, hidden_c, output) pred_sents = [] for pred_idx in range(batch_size): pred = preds.data[pred_idx] pred_sent = self.tgt_vocab.decode(pred) pred_sents.append(pred_sent) return pred_sents
def plot_conditional_image(model, label, device): """ Plots a single image from a trained conditional VAE. """ # create a random latent vector z = randn(1, model.z_dim).to(device) y = LongTensor([label]).view((-1, 1)) y = model.one_hot(y).to(device, dtype=z.dtype) z = cat((z, y), dim=1) reconstructed_img = model.decode(z) img = reconstructed_img.view(28, 28).data plt.figure() plt.imshow(img, cmap='gray') plt.title(label) plt.show()
def update(self, new_state, new_reward): ''' Get new state as Tensor from parameter 'new_state', with 'requires_grad_(True)' as explained in '__init__()' of 'DQN' class. Also convert type to "float" using 'float()' method in order to make easy for further processing in 'softmax()' method. ''' new_state = Tensor(new_state).requires_grad_(True).float().unsqueeze(0) ''' Add 'last_state', 'last_action', 'last_reward', 'new_state' to experience memory, using 'push()' method of 'memory' object which is used in further sampling. 'self.last_action' => It is first converted to 'int' to get proper action of either 0, 1 or 2. Then it is converted to "64-bit integer (signed)" using 'LongTensor'. 'self.last_reward' => Convert to "torch.Tensor" type for torch computational functions. ''' self.memory.push((self.last_state, LongTensor([int(self.last_action)]), Tensor([self.last_reward]), new_state)) ''' Get new action using 'select_action()' method with 'softmax' method technique. ''' new_action = self.select_action(state=new_state) ''' If experience memory length is greater than 100, we draw random batch of samples containing batches of states, actions, rewards & next states of size 100 using 'self.memory.sample(batch_size=100)'. Then in 'learn()' method, we make model to learn using Temporal Difference (TD), backpropagating TD-Loss & updating the weights of DQN model. ''' if self.memory.get_ex_mem_len() > 100: batch_states, batch_actions, batch_rewards, batch_next_states = self.memory.sample( batch_size=100) self.learn(batch_states, batch_actions, batch_rewards, batch_next_states) # Save the current state, action & reward as last ones which is needed for experience memory. self.last_state = new_state self.last_action = new_action self.last_reward = new_reward # Return the new selected action to caller. return new_action
def __init__(self, config): super().__init__() self.log = logging.getLogger(__name__) if config.net_config.net_type != "2DConvolution": raise IOError("config.net_config.net_type must be 2DConvolution") self.system_config = config.system_config self.net_config = config.net_config self.nsamples = self.system_config.n_samples self.modules = ModuleUtility(self.net_config.imports) if not hasattr(self.net_config, "algorithm"): setattr(self.net_config, "algorithm", "conv") if hasattr(self.net_config, "version"): self.version = self.net_config.version else: self.version = 0 if self.net_config.algorithm == "conv": if self.version == 0: self.model = SparseConv2DForZ( self.nsamples * 2, **DictionaryUtility.to_dict(self.net_config.hparams.conv)) else: self.model = SparseConv2DForEZ(self.nsamples * 2, out_planes=1, **DictionaryUtility.to_dict( self.net_config.hparams)) elif self.net_config.algorithm == "point": self.model = Pointwise2DForZ( self.nsamples * 2, **DictionaryUtility.to_dict(self.net_config.hparams.point)) elif self.net_config.algorithm == "features": if self.version == 0: self.model = SparseConv2DForZ( self.nsamples, **DictionaryUtility.to_dict(self.net_config.hparams.conv)) else: self.model = SparseConv2DForEZ(self.nsamples, out_planes=1, **DictionaryUtility.to_dict( self.net_config.hparams)) self.spatial_size = array([14, 11]) self.permute_tensor = LongTensor( [2, 0, 1]) # needed because spconv requires batch index first
def pad_and_embed(data): seqs = list(map(lambda x: x[1], data)) pssms = list(map(lambda x: x[2], data)) coords = list(map(lambda x: x[3], data)) mask = list(map(lambda x: x[4], data)) hydro = list(map(lambda x: x[5], data)) vocab = ['<pad>'] + sorted( set([char for char in residue_letter_codes.values()])) vectorized_seqs = [[vocab.index(tok) for tok in seq] for seq in seqs] embed = Embedding(len(vocab), len(vocab)) seq_lengths = np.array(list(map(len, vectorized_seqs)), dtype=np.int_) seq_tensor = torch.zeros((len(vectorized_seqs), seq_lengths.max())).long() extended_pssm = np.zeros( (len(vectorized_seqs), seq_lengths.max(), pssms[0].shape[1])) extended_coords = np.zeros( (len(vectorized_seqs), seq_lengths.max() * 3, 3)) extended_mask = np.zeros((len(vectorized_seqs), seq_lengths.max() * 3)) extended_hydro = torch.zeros((len(vectorized_seqs), seq_lengths.max())) for idx, (seq, seqlen) in enumerate(zip(vectorized_seqs, seq_lengths)): seq_tensor[idx, :seqlen] = LongTensor(seq) extended_hydro[idx, :seqlen] = torch.tensor([hydro[idx]]) extended_pssm[idx, :seqlen] = pssms[idx] extended_coords[idx, :seqlen * 3] = coords[idx] extended_mask[idx, :seqlen * 3] = mask[idx] # sort data and new encoded sequences by length, possibly not needed # seq_lengths, perm_idx = seq_lengths.sort(0, descending=True) # seq_tensor = seq_tensor[perm_idx] # data = data[perm_idx] embedded_seq_tensor = embed(seq_tensor) for idx, (seq, seqlen) in enumerate(zip(embedded_seq_tensor, data)): data[idx][1] = embedded_seq_tensor[idx].detach().numpy() data[idx][2] = extended_pssm[idx] data[idx][3] = extended_coords[idx] data[idx][4] = extended_mask[idx] data[idx][5] = extended_hydro[idx].detach().numpy() return data
def compute_class_AP(ssd, dl, n_classes, iou_thresh=0.5, detect_thresh=0.35, num_keep=100): tps, clas, p_scores = [], [], [] classes, n_gts = LongTensor(range(n_classes)),torch.zeros(n_classes).long() with torch.no_grad(): for input,target in progress_bar(dl): output = ssd.learn.pred_batch(batch=(input, target))#, reconstruct=True) for i in range(target[0].size(0)): op = ssd._data.y.analyze_pred((output[0][i], output[1][i]), thresh=detect_thresh, nms_overlap=iou_thresh, ssd=ssd, ret_scores=True, device=ssd._device) tgt_bbox, tgt_clas = ssd._get_y(target[0][i], target[1][i]) try: bbox_pred, preds, scores = op if len(bbox_pred) != 0 and len(tgt_bbox) != 0: ious = ssd._jaccard(bbox_pred, tgt_bbox) max_iou, matches = ious.max(1) detected = [] for i in range(len(preds)): if max_iou[i] >= iou_thresh and matches[i] not in detected and tgt_clas[matches[i]] == preds[i]: detected.append(matches[i]) tps.append(1) else: tps.append(0) clas.append(preds.cpu()) p_scores.append(scores.cpu()) except Exception as e: pass n_gts += ((tgt_clas.cpu()[:,None] - 1) == classes[None,:]).sum(0) tps, p_scores, clas = torch.tensor(tps), torch.cat(p_scores,0), torch.cat(clas,0) fps = 1-tps idx = p_scores.argsort(descending=True) tps, fps, clas = tps[idx], fps[idx], clas[idx] aps = [] for cls in range(1,n_classes+1): tps_cls, fps_cls = tps[clas==cls].float().cumsum(0), fps[clas==cls].float().cumsum(0) if tps_cls.numel() != 0 and tps_cls[-1] != 0: precision = tps_cls / (tps_cls + fps_cls + 1e-8) recall = tps_cls / (n_gts[cls - 1] + 1e-8) aps.append(compute_ap(precision, recall)) else: aps.append(0.) return aps
def forward(self, input, hidden, context, teacher_forcing): if len(input) > 0: # get input embeddings input = self.embedding(torch.squeeze(input.long())) else: # get <SOS> token embedding batch_size = hidden.size()[1] input = Variable(LongTensor([self.SOS])).repeat(batch_size) input = self.embedding(input.long()) input = self.dropout(input) if len(input.size()) == 1: input = torch.unsqueeze(input, 0) result, (hidden, context) = self.lstm(torch.unsqueeze(input, 0), (hidden, context)) output = self.lstm2output(result) return output, hidden, context
def long_variable_from_numpy(numpy_matrix, cuda=False, volatile=False): """ Convert integer numpy matrix to a Pytorch tensor for indexing operations :param volatile: :param numpy_matrix: an integer-type numpy matrix :type numpy_matrix: numpy.ndarray :param cuda: if True, output is GPU-type tensor, else CPU-type tensor :type cuda: bool :returns: A LongTensor which is used in Pytorch for indexing other Tensors :rtype: torch.LongTensor """ # noinspection PyArgumentList out = Variable(LongTensor(numpy_matrix.astype(np.int64)), volatile=volatile) if cuda: out = out.cuda() return out
def generate_data(points_number=1000, disk_radius=1.0 / (math.sqrt(2.0 * math.pi))): """ Generate a data set of points sampled between [0, 1]² each with a label 0 if outside a disk of radius disk_radius, and 1 if outside :param points_number: the number of points to sample :param disk_radius: the disk radius :return: a tuple containing the examples and their associated labels """ examples = FloatTensor(points_number, 2).uniform_(-0.5, 0.5) targets = LongTensor(points_number, 2).zero_() for i, ex in enumerate(examples): dist = math.sqrt(ex[0]**2 + ex[1]**2) if dist > disk_radius: # or ex[0] < 0: targets[i, 0] = 1 else: targets[i, 1] = 1 ex[0] += 0.5 ex[1] += 0.5 return examples, targets
def custom_collate_fn(data): """Creates mini-batch tensors from the list of list [batch_input, batch_target]. We should build a custom collate_fn rather than using default collate_fn, because merging sequences (including padding) is not supported in default. Seqeuences are padded to the maximum length of mini-batch sequences (dynamic padding). Args: data: list of list [batch_input, batch_target]. Refs: Thanks to yunjey. (https://github.com/yunjey/seq2seq-dataloader/blob/master/data_loader.py) """ # data sampler returns an indices to select into mini-batch. # than the collate_function gather the selected data. # in this example, we use pack_padded_sequence so always ordering with descending # so the targets should be ordered same here. # sort the sequences as descending order to use 'pack_padded_sequence' before loading. data.sort(key=lambda x: len(x[0]), reverse=True) tensor_targets = LongTensor([pair[1] for pair in data]) return [data, tensor_targets]
def train_data_generator(image_size, batch_size=16): all_data = glob('HFUT-VL-dataset/HFUT-VL1/HFUT-VL1_1/*') + glob( 'HFUT-VL-dataset/HFUT-VL1/HFUT-VL1_2/*') random.shuffle(all_data) for cnt in range(0, len(all_data), batch_size): img = [] img_label = [] for data in all_data[cnt:cnt + batch_size]: temp_img = Image.open(data).resize( (image_size, image_size)).convert('L') temp_img = numpy.asarray(temp_img).reshape( (1, image_size, image_size)) # one sample + one channel + 32x32 image img.append(temp_img) label_id = int(data.split('/')[-1].split('_')[0]) - 1 img_label.append(label_id) yield FloatTensor(numpy.stack(img)), LongTensor(img_label)
def validate_args(self, inputs: Optional[Any] = None, encoder_outputs: Tensor = None, teacher_forcing_ratio: float = 1.0) -> Tuple[Tensor, int, int]: """ Validate arguments """ assert encoder_outputs is not None batch_size = encoder_outputs.size(0) if inputs is None: # inference inputs = LongTensor([self.sos_id] * batch_size).view(batch_size, 1) max_length = self.max_length if torch.cuda.is_available(): inputs = inputs.cuda() if teacher_forcing_ratio > 0: raise ValueError("Teacher forcing has to be disabled (set 0) when no inputs is provided.") else: max_length = inputs.size(1) - 1 # minus the start of sequence symbol return inputs, batch_size, max_length
def _format_ssd_targets(self, targets): labels = [t[0] for t in targets] boxes = [t[1:] for t in targets] resized_width, resized_height = self.resize_to width_scale, height_scale = 720 / resized_width, 480 / resized_height new_boxes = [[ box[0] / width_scale, box[1] / height_scale, box[2] / width_scale, box[3] / height_scale ] for box in boxes] normalized_boxes = [[ box[0] / resized_width, box[1] / resized_height, box[2] / resized_width, box[3] / resized_height ] for box in new_boxes] labels = LongTensor(labels) normalized_boxes = FloatTensor(normalized_boxes) return normalized_boxes, labels
def __next__(self): self.iter += 1 if self.iter > self.max_dataset_size: raise StopIteration AB, AB_paths = next(self.data_loader_iter) w_total = AB.size(3) w = int(w_total / 2) h = AB.size(2) w_offset = random.randint(0, max(0, w - self.fineSize - 1)) h_offset = random.randint(0, max(0, h - self.fineSize - 1)) A = torch.mean(AB, dim=1) #only one of the RGB channels A = A[:, None, :, :] #(m,1,64,64*26) B = torch.mean(AB, dim=1) #only one of the RGB channels B = B[:, None, :, :] #(m,1,64,64*26) n_rgb = 3 if self.rgb else 1 target_size = A.size(2) AA = A.clone() if self.blanks != 0: #randomly remove some of the glyphs if not self.dict: blank_ind = np.random.permutation( A.size(3) / target_size)[0:int(self.blanks * A.size(3) / target_size)] else: file_name = map(lambda x: x.split("/")[-1], AB_paths) blank_ind = self.random_dict[file_name][0:int(self.blanks * A.size(3) / target_size)] blank_ind = np.tile(range(target_size), len(blank_ind)) + np.repeat( blank_ind * target_size, target_size) AA.index_fill_(3, LongTensor(list(blank_ind)), 1) # t_topil = transforms.Compose([ # transforms.ToPILImage()]) # AA_ = t_topil(AA[0,0,:,:].unsqueeze_(0)) # misc.imsave('./AA_0.png',AA_) return {'A': AA, 'A_paths': AB_paths, 'B': B, 'B_paths': AB_paths}
def forward1(self, inp_grad=False): b,c,m,n = self.real_A0.size() self.batch_ = b self.out_id = self.obs real_A1 = self.Tensor(self.opt.output_nc,self.opt.input_nc_1, m,n) if self.opt.orna: inp_orna = self.fake_B0_init else: inp_orna = self.fake_B0 for batch in range(self.opt.output_nc): if not self.opt.rgb_in and self.opt.rgb_out: real_A1[batch,0,:,:] = inp_orna.data[self.id_[batch],batch,:,:] real_A1[batch,1,:,:] = inp_orna.data[self.id_[batch],batch,:,:] real_A1[batch,2,:,:] = inp_orna.data[self.id_[batch],batch,:,:] else: #TODO real_A1[batch,:,:,:] = inp_orna.data[batch,self.out_id[batch]*np.array(self.opt.input_nc_1):(self.out_id[batch]+1)*np.array(self.opt.input_nc_1),:,:] if self.initial: self.real_A1_init = Variable(real_A1, requires_grad=False) self.initial = False self.real_A1_s = Variable(real_A1, requires_grad=inp_grad) self.real_A1 = self.real_A1_s self.fake_B1_emb = self.netE1.forward(self.real_A1) self.fake_B1 = self.netDE1.forward(self.fake_B1_emb) self.real_B1 = Variable(self.input_B0) self.real_A1_gt_s = Variable(self.all2observed(inp_orna), requires_grad=True) self.real_A1_gt = (self.real_A1_gt_s) self.fake_B1_gt_emb = self.netE1.forward(self.real_A1_gt) self.fake_B1_gt = self.netDE1.forward(self.fake_B1_gt_emb) obs_ = torch.cuda.LongTensor(self.obs) if self.opt.gpu_ids else LongTensor(self.obs) if self.opt.base_font: real_base_gt = index_select(self.real_base, 0, obs_) self.real_base_gt = (Variable(real_base_gt.data, requires_grad=False))
def get_hidden_states_window(self, input_text, window_length, stride): #Calculate number of windows if len(input_text) > window_length: num_batch = math.ceil(len(input_text) / (window_length - stride)) else: num_batch = 1 #Positions of windows start_tockens = [(window_length - stride) * i for i in range(num_batch)] #Initialize output output = torch.zeros(1, len(input_text), self.BertEmbed_size) output = output.to( torch.device("cuda" if torch.cuda.is_available() else "cpu")) #output_embed = torch.zeros(1,len(input_text),768) #output_embed = output_embed.to(torch.device("cuda" if torch.cuda.is_available() else "cpu")) for i, pos in enumerate(start_tockens): text_batch = input_text[pos:pos + min(window_length, len(input_text))] #Convert words to indeces indexed_tokens = GlobalModel.tokenizer.convert_tokens_to_ids( text_batch) tokens_tensor = LongTensor([indexed_tokens]) tokens_tensor = tokens_tensor.to( torch.device("cuda" if torch.cuda.is_available() else "cpu")) #Get Bert hidden states with no_grad(): _, _, hidden_states = GlobalModel.bert_embeddings( tokens_tensor) #bert_embeddings = self.bertEmbedding.embeddings(tokens_tensor) hidden_state = torch.cat((hidden_states[9], hidden_states[10], hidden_states[11], hidden_states[12]), 2) output[:, pos:pos + min(window_length, len(input_text))] += hidden_state #output_embed[:, pos:pos + min(window_length,len(input_text))] += bert_embeddings if i != 0: output[:, pos:pos + stride] = output[:, pos:pos + stride] / 2 #output_embed[:, pos:pos + stride] = output_embed[:, pos:pos + stride]/2 return output
def df_to_Tensor(df,topic_glove_emb,verbose=0): # use this to convert a dataframe to torch tensor # this is the predict ruling label version # WE DO NOT ADD JUDGE EMBEDDING HERE, THAT WILL BE HANDLED BY THE MODEL feature_dim = 300+300+128+1 # 300 for opinion vec, 300 for topic, 128 for citation, # and 1 value to indicate which judge X = np.zeros((df.shape[0],feature_dim)) y = np.zeros(df.shape[0]) # y is the ruling label for i in range(df.shape[0]): if verbose and i%10000==0: print(i) data_entry = df.iloc[i] # add opinion vector to X X[i,:300] = data_entry['centered_opinion_vec'] # add topic vector to X topic = data_entry['topic'] topic = str.lower(str(topic)) if topic not in topic_glove_emb: # deal with any unknown topic topic = "<UNK>" X[i,300:600] = topic_glove_emb[topic] # add citation vector to X X[i,600:600+128] = data_entry['citation'] X[i,-1] = data_entry['judge_embed_index'] # in the model, the model needs to pick a judge vector # according to this value # set y decision = data_entry['judge_decision'] y[i] = decision return FloatTensor(X),LongTensor(y)