def reset(self): """ Resets the meter with empty member variables """ self.scores = torch.FloatTensor(torch.FloatStorage()) self.targets = torch.LongTensor(torch.LongStorage()) self.weights = torch.FloatTensor(torch.FloatStorage())
def forward(self, feature, inp): feature = self.features(feature) feature = self.pooling(feature) feature = feature.view(feature.size(0), -1) inp = inp[0] adj = gen_adj(self.A).detach() x = self.gc1(inp, adj) x = self.relu(x) x = self.gc2(x, adj) x = th.transpose(x, 0, 1) if self.is_usemfb: if self.state['use_gpu']: x_out = torch.FloatTensor(torch.FloatStorage()).cuda() else: x_out = torch.FloatTensor(torch.FloatStorage()) for i_row in range(int(feature.shape[0])): img_linear_row = self.Linear_imgdataproj( feature[i_row, :]).view(1, -1) if self.state['use_gpu']: out_row = torch.FloatTensor(torch.FloatStorage()).cuda() else: out_row = torch.FloatTensor(torch.FloatStorage()) for col in range(int(x.shape[1])): tmp_x = x[:, col].view(1, -1) classifier_linear = self.Linear_classifierproj(tmp_x) iq = torch.mul(img_linear_row, classifier_linear) iq = F.dropout(iq, self.opt.DROPOUT_RATIO, training=self.training) iq = torch.sum(iq.view(1, self.out_in_tmp, -1), 2) out_row = torch.cat((out_row, iq), 1) x_out = torch.cat((x_out, out_row), 0) else: x_out = th.matmul(feature, x) gl.GLOBAL_TENSOR = x_out if self.is_hash: hash_tmp = self.hash_layer(x_out) if gl.LOCAL_USE_TANH: hash_code_out = self.use_tanh(hash_tmp) hash_code_out[hash_code_out > 0] = 1 hash_code_out[hash_code_out <= 0] = -1 hash_code_out = hash_code_out else: hash_code_out = hash_tmp return hash_code_out return x_out
def _test_serialization_assert(self, b, c): self.assertEqual(b, c, atol=0, rtol=0) self.assertTrue(isinstance(c[0], torch.FloatTensor)) self.assertTrue(isinstance(c[1], torch.FloatTensor)) self.assertTrue(isinstance(c[2], torch.FloatTensor)) self.assertTrue(isinstance(c[3], torch.FloatTensor)) self.assertTrue(isinstance(c[4], torch.storage.TypedStorage)) self.assertEqual(c[4].dtype, torch.float) c[0].fill_(10) self.assertEqual(c[0], c[2], atol=0, rtol=0) self.assertEqual(c[4], torch.FloatStorage(25).fill_(10), atol=0, rtol=0) c[1].fill_(20) self.assertEqual(c[1], c[3], atol=0, rtol=0) # I have to do it in this roundabout fashion, because there's no # way to slice storages for i in range(4): self.assertEqual(c[4][i + 1], c[5][i]) # check that serializing the same storage view object unpickles # it as one object not two (and vice versa) views = c[7] self.assertEqual(views[0]._cdata, views[1]._cdata) self.assertEqual(views[0], views[2]) self.assertNotEqual(views[0]._cdata, views[2]._cdata) rootview = c[8] self.assertEqual(rootview.data_ptr(), c[0].data_ptr())
def forward(self, feature, inp): feature = self.features(feature) attention_feature = self.attentionNet(feature) linear_feature = self.linearNet(feature) link_features = self.linkNet(torch.mul(attention_feature, linear_feature)) new_feature = self.pooling(torch.add(self.gamma_weight*feature, (1-self.gamma_weight)*link_features)) feature = new_feature.view(new_feature.size(0), -1) inp = inp[0] adj = gen_adj(self.A).detach() x = self.gc1(inp, adj) x = self.relu(x) x = self.gc2(x, adj) x = th.transpose(x, 0, 1) if self.is_usemfb: if self.state['use_gpu']: x_out = torch.FloatTensor(torch.FloatStorage()).cuda() else: x_out = torch.FloatTensor(torch.FloatStorage()) for i_row in range(int(feature.shape[0])): img_linear_row = self.Linear_imgdataproj(feature[i_row, :]).view(1, -1) if self.state['use_gpu']: out_row = torch.FloatTensor(torch.FloatStorage()).cuda() else: out_row = torch.FloatTensor(torch.FloatStorage()) for col in range(int(x.shape[1])): tmp_x = x[:, col].view(1, -1) classifier_linear = self.Linear_classifierproj(tmp_x) iq = torch.mul(img_linear_row, classifier_linear) iq = F.dropout(iq, self.opt.DROPOUT_RATIO, training=self.training) iq = torch.sum(iq.view(1, self.out_in_tmp, -1), 2) if self.out_in_tmp != 1: temp_out = self.ML_fc_layer(out_row) out_row = temp_out out_row = torch.cat((out_row,iq),1) if self.out_in_tmp!=1: temp_out = self.ML_fc_layer(out_row) out_row = temp_out x_out = torch.cat((x_out, out_row),0) else: x_out = th.matmul(feature, x) return x_out
def reset(self): """Resets the meter with empty member variables""" self.scores = torch.FloatTensor( torch.FloatStorage() ) # self.scores will store all the dataset(train_set or test set) output info # #print("In the class AveragePrecisionMeter function reset(): self.score.shape=,self.score=,self.score.type", # self.scores.shape, "\n",self.scores,"\n",type(self.scores)) self.targets = torch.LongTensor( torch.LongStorage() ) # self.scores will store all the dataset(train_set or test set) labels info
def test_serialization_backwards_compat(self): a = [torch.arange(1 + i, 26 + i).view(5, 5).float() for i in range(2)] b = [a[i % 2] for i in range(4)] b += [a[0].storage()] b += [a[0].reshape(-1)[1:4].clone().storage()] path = download_file( 'https://download.pytorch.org/test_data/legacy_serialized.pt') c = torch.load(path) self.assertEqual(b, c, atol=0, rtol=0) self.assertTrue(isinstance(c[0], torch.FloatTensor)) self.assertTrue(isinstance(c[1], torch.FloatTensor)) self.assertTrue(isinstance(c[2], torch.FloatTensor)) self.assertTrue(isinstance(c[3], torch.FloatTensor)) self.assertTrue(isinstance(c[4], torch.storage.TypedStorage)) self.assertEqual(c[4].dtype, torch.float32) c[0].fill_(10) self.assertEqual(c[0], c[2], atol=0, rtol=0) self.assertEqual(c[4], torch.FloatStorage(25).fill_(10), atol=0, rtol=0) c[1].fill_(20) self.assertEqual(c[1], c[3], atol=0, rtol=0) # test some old tensor serialization mechanism class OldTensorBase(object): def __init__(self, new_tensor): self.new_tensor = new_tensor def __getstate__(self): return (self.new_tensor.storage(), self.new_tensor.storage_offset(), tuple(self.new_tensor.size()), self.new_tensor.stride()) class OldTensorV1(OldTensorBase): def __reduce__(self): return (torch.Tensor, (), self.__getstate__()) class OldTensorV2(OldTensorBase): def __reduce__(self): return (_rebuild_tensor, self.__getstate__()) x = torch.randn(30).as_strided([2, 3], [9, 3], 2) for old_cls in [OldTensorV1, OldTensorV2]: with tempfile.NamedTemporaryFile() as f: old_x = old_cls(x) torch.save(old_x, f) f.seek(0) load_x = torch.load(f) self.assertEqual(x.storage(), load_x.storage()) self.assertEqual(x.storage_offset(), load_x.storage_offset()) self.assertEqual(x.size(), load_x.size()) self.assertEqual(x.stride(), load_x.stride())
def reset(self): """Resets the meter with empty member variables""" if self.state['use_gpu']: self.output = torch.IntTensor(torch.IntStorage()).cuda() self.targets = torch.IntTensor(torch.IntStorage()).cuda() else: self.output = torch.IntTensor(torch.IntStorage()) self.targets = torch.IntTensor(torch.IntStorage()) if self.state['use_gpu']: self.bf_output = torch.FloatTensor(torch.FloatStorage()).cuda() self.bf_targets = torch.IntTensor(torch.IntStorage()).cuda() else: self.bf_output = torch.FloatTensor(torch.FloatStorage()) self.bf_targets = torch.IntTensor(torch.IntStorage()) self.all_item = 0 self.all_final_item = 0 self.mAP1_list = [] self.mAP2_list = [] self.P_list = [] self.R_list = []
def utt_collate_fn(batch): batch_size = len(batch) # print(batch[0].size()) feat_dim = batch[0][0].shape[1] if _use_shared_memory: utt_lengths = torch.LongStorage()._new_shared(batch_size).zero_() else: utt_lengths = torch.LongTensor(batch_size) for i in range(batch_size): utt_lengths[i] = batch[i][0].shape[0] # lengths = torch.LongTensor(np.array(lengths)) # Sort the feat lengths #### # (utt_lengths_out, indices) = torch.sort(utt_lengths, descending=True) # utt_lengths = utt_lengths[indices] (utt_lengths, indices) = torch.sort(utt_lengths, descending=True) # Sort the labels according to the lengths of the feats # labels_sort = labels[indices] # label_lengths = torch.IntTensor(label_lengths) # lengths_total = torch.sum(label_lengths) max_length = utt_lengths[0] #### FIND MAX LABEL LENGTH if _use_shared_memory: utt_stack = torch.FloatStorage()._new_shared( utt_lengths[0] * batch_size * feat_dim).new( utt_lengths[0], batch_size, feat_dim).zero_() labels_out = torch.LongStorage()._new_shared(batch_size).zero_() else: utt_stack = torch.zeros(utt_lengths[0], batch_size, feat_dim).float( ) # L, B, D, +1 in dimension is to keep the length here labels_out = torch.zeros(batch_size).long() # Create padded stacks of utterances and labels # utt_stack = torch.zeros(lengths[0], len(feats), feats[0].shape[1]+1).float() # L, B, D, +1 in dimension is to keep the length here # utt_stack = torch.zeros(lengths[0], batch_size, feats[0].shape[1]).float() # L, B, D, +1 in dimension is to keep the length here # labels_out = torch.zeros(int(lengths_total)).int() # label_stack = torch.zeros(torch.max(label_lengths), len(labels_sort), 2).long() # L_label, B, 2, +1 in dimension to keep the length here # Modify this part to create for i in range(batch_size): utt_length = utt_lengths[i] utt_stack[:utt_length, i, :] = batch[indices[i]][0] # print(batch[indices[i]][1]) # print(type(labels_out[i])) labels_out[i] = int(batch[indices[i]][1]) # +1 because Warp-CTC return (utt_stack, utt_lengths, labels_out)
def reset(self): self.scores = torch.FloatTensor(torch.FloatStorage()) self.targets = torch.LongTensor(torch.LongStorage())
def forward(self, feature, inp): feature = self.features(feature) feature = self.pooling(feature) feature = feature.view(feature.size(0), -1) inp = inp[0] branch_1 = self.A_branch_1(inp.view( (1, self.d_e, self.num_classes, 1))).view( (self.d_e_1, self.num_classes)) branch_2 = self.A_branch_2(inp.view( (1, self.d_e, self.num_classes, 1))).view( (self.d_e_1, self.num_classes)) A = torch.matmul(branch_1.t(), branch_2) / float(self.num_classes) I_c = torch.eye( A.shape[0]).cuda() if torch.cuda.is_available() else torch.eye( A.shape[0]).cpu() A_wave = A + I_c D_wave_negative_power = torch.diag( torch.pow(A_wave.sum(1).float(), -0.5)) D_wave_negative_power[torch.isnan(D_wave_negative_power)] = 0.0 D_wave_negative_power[torch.isinf(D_wave_negative_power)] = 0.0 A_hat = torch.matmul(torch.matmul(D_wave_negative_power, A_wave), D_wave_negative_power) L_A_loss = torch.abs(A_hat - I_c).sum() if L_A_loss != L_A_loss: print("A = \n", A) print("A_wave = \n", A_wave) print("A_wave sum(1) = \n", A_wave.sum(1)) print("D~ diagnose elements = \n", torch.pow(A_wave.sum(1).float(), -0.5)) print("D~ = \n", D_wave_negative_power) print("A_hat=\n", A_hat) sys.exit() adj = A_hat x = self.gc1(inp, adj) x = self.relu(x) x = self.gc2(x, adj) x = x.transpose(0, 1) if self.state['use_gpu']: x_out = torch.FloatTensor(torch.FloatStorage()).cuda() else: x_out = torch.FloatTensor(torch.FloatStorage()) if self.is_usemfb: for i_row in range(int(feature.shape[0])): img_linear_row = self.Linear_imgdataproj( feature[i_row, :]).view(1, -1) if self.state['use_gpu']: out_row = torch.FloatTensor(torch.FloatStorage()).cuda() else: out_row = torch.FloatTensor(torch.FloatStorage()) for col in range(int(x.shape[1])): tmp_x = x[:, col].view(1, -1) classifier_linear = self.Linear_classifierproj(tmp_x) iq = torch.mul(img_linear_row, classifier_linear) iq = F.dropout(iq, self.opt.DROPOUT_RATIO, training=self.training) iq = torch.sum(iq.view(1, self.out_in_tmp, -1), 2) out_row = torch.cat((out_row, iq), 1) if self.out_in_tmp != 1: temp_out = self.ML_fc_layer(out_row) out_row = temp_out x_out = torch.cat((x_out, out_row), 0) else: x_out = torch.matmul(feature, x) assert x_out.shape[0] == feature.shape[0] return x_out, L_A_loss
def assign(): torch.FloatStorage(10)[1:-1] = '1'
fn() except Exception as e: error_message = e.args[0] print('=' * 80) print(desc) print('-' * 80) print(error_message) print('') for sub in required_substrings: assert sub in error_message return raise AssertionError("given function ({}) didn't raise an error".format(desc)) check_error( 'Wrong argument types', lambda: torch.FloatStorage(object()), 'object') check_error('Unknown keyword argument', lambda: torch.FloatStorage(content=1234.), 'keyword') check_error('Invalid types inside a sequence', lambda: torch.FloatStorage(['a', 'b']), 'list', 'str') check_error('Invalid size type', lambda: torch.FloatStorage(1.5), 'float') check_error('Invalid offset',
def reset(self): """Resets the meter with empty member variables""" self.scores = torch.FloatTensor(torch.FloatStorage()) self.targets = torch.LongTensor(torch.LongStorage()) print('scores shape....', self.scores.shape) print('targets shape....', self.targets.shape)
try: fn() except Exception as e: error_message = e.args[0] print('=' * 80) print(desc) print('-' * 80) print(error_message) print('') for sub in required_substrings: assert sub in error_message return assert False, "given function ({}) didn't raise an error".format(desc) check_error('Wrong argument types', lambda: torch.FloatStorage(object()), 'object') check_error('Unknown keyword argument', lambda: torch.FloatStorage(content=1234.), 'keyword') check_error('Invalid types inside a sequence', lambda: torch.FloatStorage(['a', 'b']), 'list', 'str') check_error('Invalid size type', lambda: torch.FloatStorage(1.5), 'float') check_error('Invalid offset', lambda: torch.FloatStorage(torch.FloatStorage(2), 4), '2', '4') check_error('Negative offset', lambda: torch.FloatStorage(torch.FloatStorage(2), -1), '2', '-1')
import torch LOCAL_USE_TANH = False GLOBAL_TENSOR = torch.FloatTensor(torch.FloatStorage())