我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用torch.rand()。
def calc_gradient_penalty(self, netD, real_data, fake_data): alpha = torch.rand(1, 1) alpha = alpha.expand(real_data.size()) alpha = alpha.cuda() interpolates = alpha * real_data + ((1 - alpha) * fake_data) interpolates = interpolates.cuda() interpolates = Variable(interpolates, requires_grad=True) disc_interpolates = netD.forward(interpolates) gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=torch.ones(disc_interpolates.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0] gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * self.LAMBDA return gradient_penalty
def test_MaxUnpool2d_output_size(self): m = nn.MaxPool2d(3, stride=2, return_indices=True) mu = nn.MaxUnpool2d(3, stride=2) big_t = torch.rand(1, 1, 6, 6) big_t[0][0][4][4] = 100 output_big, indices_big = m(Variable(big_t)) self.assertRaises(RuntimeError, lambda: mu(output_big, indices_big)) small_t = torch.rand(1, 1, 5, 5) for i in range(0, 4, 2): for j in range(0, 4, 2): small_t[:,:,i,j] = 100 output_small, indices_small = m(Variable(small_t)) for h in range(3, 10): for w in range(3, 10): if 4 <= h <= 6 and 4 <= w <= 6: size = (h, w) if h == 5: size = torch.LongStorage(size) elif h == 6: size = torch.LongStorage((1, 1) + size) mu(output_small, indices_small, output_size=size) else: self.assertRaises(ValueError, lambda: mu(output_small, indices_small, (h, w)))
def test_L1Penalty(self): weight = 1 m = nn.L1Penalty(weight, False, False) input = torch.rand(2,10).add_(-0.5) input[0][0] = 0 m.forward(input) grad = m.backward(input, torch.ones(input.size())) self.assertEqual(input.abs().sum() * weight, m.loss) true_grad = (input.gt(0).type_as(grad) + input.lt(0).type_as(grad).mul_(-1)).mul_(weight) self.assertEqual(true_grad, grad) # Check that these don't raise errors m.__repr__() str(m)
def test_cat(self): SIZE = 10 # 2-arg cat for dim in range(3): x = torch.rand(13, SIZE, SIZE).transpose(0, dim) y = torch.rand(17, SIZE, SIZE).transpose(0, dim) res1 = torch.cat((x, y), dim) self.assertEqual(res1.narrow(dim, 0, 13), x, 0) self.assertEqual(res1.narrow(dim, 13, 17), y, 0) # Check iterables for dim in range(3): x = torch.rand(13, SIZE, SIZE).transpose(0, dim) y = torch.rand(17, SIZE, SIZE).transpose(0, dim) z = torch.rand(19, SIZE, SIZE).transpose(0, dim) res1 = torch.cat((x, y, z), dim) self.assertEqual(res1.narrow(dim, 0, 13), x, 0) self.assertEqual(res1.narrow(dim, 13, 17), y, 0) self.assertEqual(res1.narrow(dim, 30, 19), z, 0) self.assertRaises(ValueError, lambda: torch.cat([]))
def test_cholesky(self): x = torch.rand(10, 10) + 1e-1 A = torch.mm(x, x.t()) # default Case C = torch.potrf(A) B = torch.mm(C.t(), C) self.assertEqual(A, B, 1e-14) # test Upper Triangular U = torch.potrf(A, True) B = torch.mm(U.t(), U) self.assertEqual(A, B, 1e-14, 'potrf (upper) did not allow rebuilding the original matrix') # test Lower Triangular L = torch.potrf(A, False) B = torch.mm(L, L.t()) self.assertEqual(A, B, 1e-14, 'potrf (lower) did not allow rebuilding the original matrix')
def test_abs(self): size = 1000 max_val = 1000 original = torch.rand(size).mul(max_val) # Tensor filled with values from {-1, 1} switch = torch.rand(size).mul(2).floor().mul(2).add(-1) types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor'] for t in types: data = original.type(t) switch = switch.type(t) res = torch.mul(data, switch) self.assertEqual(res.abs(), data, 1e-16) # Checking that the right abs function is called for LongTensor bignumber = 2^31 + 1 res = torch.LongTensor((-bignumber,)) self.assertGreater(res.abs()[0], 0)
def testAUCMeter(self): mtr = meter.AUCMeter() test_size = 1000 mtr.add(torch.rand(test_size), torch.zeros(test_size)) mtr.add(torch.rand(test_size), torch.Tensor(test_size).fill_(1)) val, tpr, fpr = mtr.value() self.assertTrue(math.fabs(val - 0.5) < 0.1, msg="AUC Meter fails") mtr.reset() mtr.add(torch.Tensor(test_size).fill_(0), torch.zeros(test_size)) mtr.add(torch.Tensor(test_size).fill_(0.1), torch.zeros(test_size)) mtr.add(torch.Tensor(test_size).fill_(0.2), torch.zeros(test_size)) mtr.add(torch.Tensor(test_size).fill_(0.3), torch.zeros(test_size)) mtr.add(torch.Tensor(test_size).fill_(0.4), torch.zeros(test_size)) mtr.add(torch.Tensor(test_size).fill_(1), torch.Tensor(test_size).fill_(1)) val, tpr, fpr = mtr.value() self.assertEqual(val, 1.0, msg="AUC Meter fails")
def setUp(self): super(TestEncoderBase, self).setUp() self.lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True) self.encoder_base = _EncoderBase(stateful=True) tensor = Variable(torch.rand([5, 7, 3])) tensor[1, 6:, :] = 0 tensor[3, 2:, :] = 0 self.tensor = tensor mask = Variable(torch.ones(5, 7)) mask[1, 6:] = 0 mask[2, :] = 0 # <= completely masked mask[3, 2:] = 0 mask[4, :] = 0 # <= completely masked self.mask = mask self.batch_size = 5 self.num_valid = 3 sequence_lengths = get_lengths_from_binary_sequence_mask(mask) _, _, restoration_indices, sorting_indices = sort_batch_by_length(tensor, sequence_lengths) self.sorting_indices = sorting_indices self.restoration_indices = restoration_indices
def test_forward_works_even_with_empty_sequences(self): lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=11, batch_first=True) encoder = PytorchSeq2VecWrapper(lstm) tensor = torch.autograd.Variable(torch.rand([5, 7, 3])) tensor[1, 6:, :] = 0 tensor[2, :, :] = 0 tensor[3, 2:, :] = 0 tensor[4, :, :] = 0 mask = torch.autograd.Variable(torch.ones(5, 7)) mask[1, 6:] = 0 mask[2, :] = 0 mask[3, 2:] = 0 mask[4, :] = 0 results = encoder(tensor, mask) for i in (0, 1, 3): assert not (results[i] == 0.).data.all() for i in (2, 4): assert (results[i] == 0.).data.all()
def test_forward_works_even_with_empty_sequences(self): lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True) encoder = PytorchSeq2SeqWrapper(lstm) tensor = torch.autograd.Variable(torch.rand([5, 7, 3])) tensor[1, 6:, :] = 0 tensor[2, :, :] = 0 tensor[3, 2:, :] = 0 tensor[4, :, :] = 0 mask = torch.autograd.Variable(torch.ones(5, 7)) mask[1, 6:] = 0 mask[2, :] = 0 mask[3, 2:] = 0 mask[4, :] = 0 results = encoder(tensor, mask) for i in (0, 1, 3): assert not (results[i] == 0.).data.all() for i in (2, 4): assert (results[i] == 0.).data.all()
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self): lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True) encoder = PytorchSeq2SeqWrapper(lstm) tensor = torch.rand([5, 7, 3]) tensor[1, 6:, :] = 0 tensor[2, 4:, :] = 0 tensor[3, 2:, :] = 0 tensor[4, 1:, :] = 0 mask = torch.ones(5, 7) mask[1, 6:] = 0 mask[2, 4:] = 0 mask[3, 2:] = 0 mask[4, 1:] = 0 input_tensor = Variable(tensor) mask = Variable(mask) sequence_lengths = get_lengths_from_binary_sequence_mask(mask) packed_sequence = pack_padded_sequence(input_tensor, sequence_lengths.data.tolist(), batch_first=True) lstm_output, _ = lstm(packed_sequence) encoder_output = encoder(input_tensor, mask) lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True) assert_almost_equal(encoder_output.data.numpy(), lstm_tensor.data.numpy())
def test_wrapper_works_when_passed_state_with_zero_length_sequences(self): lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True) encoder = PytorchSeq2SeqWrapper(lstm) tensor = torch.rand([5, 7, 3]) mask = torch.ones(5, 7) mask[0, 3:] = 0 mask[1, 4:] = 0 mask[2, 0:] = 0 mask[3, 6:] = 0 # Initial states are of shape (num_layers * num_directions, batch_size, hidden_dim) initial_states = (Variable(torch.randn(6, 5, 7)), Variable(torch.randn(6, 5, 7))) input_tensor = Variable(tensor) mask = Variable(mask) _ = encoder(input_tensor, mask, initial_states)
def test_wrapper_stateful(self): lstm = LSTM(bidirectional=True, num_layers=2, input_size=3, hidden_size=7, batch_first=True) encoder = PytorchSeq2SeqWrapper(lstm, stateful=True) # To test the stateful functionality we need to call the encoder multiple times. # Different batch sizes further tests some of the logic. batch_sizes = [5, 10, 8] sequence_lengths = [4, 6, 7] states = [] for batch_size, sequence_length in zip(batch_sizes, sequence_lengths): tensor = Variable(torch.rand([batch_size, sequence_length, 3])) mask = Variable(torch.ones(batch_size, sequence_length)) mask.data[0, 3:] = 0 encoder_output = encoder(tensor, mask) states.append(encoder._states) # pylint: disable=protected-access # Check that the output is masked properly. assert_almost_equal(encoder_output[0, 3:, :].data.numpy(), numpy.zeros((4, 14))) for k in range(2): assert_almost_equal( states[-1][k][:, -2:, :].data.numpy(), states[-2][k][:, -2:, :].data.numpy() )
def test_wrapper_stateful_single_state_gru(self): gru = GRU(bidirectional=True, num_layers=2, input_size=3, hidden_size=7, batch_first=True) encoder = PytorchSeq2SeqWrapper(gru, stateful=True) batch_sizes = [10, 5] states = [] for batch_size in batch_sizes: tensor = Variable(torch.rand([batch_size, 5, 3])) mask = Variable(torch.ones(batch_size, 5)) mask.data[0, 3:] = 0 encoder_output = encoder(tensor, mask) states.append(encoder._states) # pylint: disable=protected-access assert_almost_equal(encoder_output[0, 3:, :].data.numpy(), numpy.zeros((2, 14))) assert_almost_equal( states[-1][0][:, -5:, :].data.numpy(), states[-2][0][:, -5:, :].data.numpy() )
def test_sort_tensor_by_length(self): tensor = torch.rand([5, 7, 9]) tensor[0, 3:, :] = 0 tensor[1, 4:, :] = 0 tensor[2, 1:, :] = 0 tensor[3, 5:, :] = 0 tensor = Variable(tensor) sequence_lengths = Variable(torch.LongTensor([3, 4, 1, 5, 7])) sorted_tensor, sorted_lengths, reverse_indices, _ = util.sort_batch_by_length(tensor, sequence_lengths) # Test sorted indices are padded correctly. numpy.testing.assert_array_equal(sorted_tensor[1, 5:, :].data.numpy(), 0.0) numpy.testing.assert_array_equal(sorted_tensor[2, 4:, :].data.numpy(), 0.0) numpy.testing.assert_array_equal(sorted_tensor[3, 3:, :].data.numpy(), 0.0) numpy.testing.assert_array_equal(sorted_tensor[4, 1:, :].data.numpy(), 0.0) assert sorted_lengths.data.equal(torch.LongTensor([7, 5, 4, 3, 1])) # Test restoration indices correctly recover the original tensor. assert sorted_tensor.index_select(0, reverse_indices).data.equal(tensor.data)
def test_weighted_sum_handles_uneven_higher_order_input(self): batch_size = 1 length_1 = 5 length_2 = 6 length_3 = 2 embedding_dim = 4 sentence_array = numpy.random.rand(batch_size, length_3, embedding_dim) attention_array = numpy.random.rand(batch_size, length_1, length_2, length_3) sentence_tensor = Variable(torch.from_numpy(sentence_array).float()) attention_tensor = Variable(torch.from_numpy(attention_array).float()) aggregated_array = util.weighted_sum(sentence_tensor, attention_tensor).data.numpy() assert aggregated_array.shape == (batch_size, length_1, length_2, embedding_dim) for i in range(length_1): for j in range(length_2): expected_array = (attention_array[0, i, j, 0] * sentence_array[0, 0] + attention_array[0, i, j, 1] * sentence_array[0, 1]) numpy.testing.assert_almost_equal(aggregated_array[0, i, j], expected_array, decimal=5)
def test_weighted_sum_handles_3d_attention_with_3d_matrix(self): batch_size = 1 length_1 = 5 length_2 = 2 embedding_dim = 4 sentence_array = numpy.random.rand(batch_size, length_2, embedding_dim) attention_array = numpy.random.rand(batch_size, length_1, length_2) sentence_tensor = Variable(torch.from_numpy(sentence_array).float()) attention_tensor = Variable(torch.from_numpy(attention_array).float()) aggregated_array = util.weighted_sum(sentence_tensor, attention_tensor).data.numpy() assert aggregated_array.shape == (batch_size, length_1, embedding_dim) for i in range(length_1): expected_array = (attention_array[0, i, 0] * sentence_array[0, 0] + attention_array[0, i, 1] * sentence_array[0, 1]) numpy.testing.assert_almost_equal(aggregated_array[0, i], expected_array, decimal=5)
def test_sequence_cross_entropy_with_logits_averages_batch_correctly(self): # test batch average is the same as dividing the batch averaged # loss by the number of batches containing any non-padded tokens. tensor = torch.rand([5, 7, 4]) tensor[0, 3:, :] = 0 tensor[1, 4:, :] = 0 tensor[2, 2:, :] = 0 tensor[3, :, :] = 0 weights = (tensor != 0.0)[:, :, 0].long().squeeze(-1) targets = torch.LongTensor(numpy.random.randint(0, 3, [5, 7])) targets *= weights tensor = Variable(tensor) targets = Variable(targets) weights = Variable(weights) loss = util.sequence_cross_entropy_with_logits(tensor, targets, weights) vector_loss = util.sequence_cross_entropy_with_logits(tensor, targets, weights, batch_average=False) # Batch has one completely padded row, so divide by 4. assert loss.data.numpy() == vector_loss.data.sum() / 4
def get_dropout_mask(dropout_probability: float, tensor_for_masking: torch.autograd.Variable): """ Computes and returns an element-wise dropout mask for a given tensor, where each element in the mask is dropped out with probability dropout_probability. Note that the mask is NOT applied to the tensor - the tensor is passed to retain the correct CUDA tensor type for the mask. Parameters ---------- dropout_probability : float, required. Probability of dropping a dimension of the input. tensor_for_masking : torch.Variable, required. Returns ------- A torch.FloatTensor consisting of the binary mask scaled by 1/ (1 - dropout_probability). This scaling ensures expected values and variances of the output of applying this mask and the original tensor are the same. """ binary_mask = tensor_for_masking.clone() binary_mask.data.copy_(torch.rand(tensor_for_masking.size()) > dropout_probability) # Scale mask by 1/keep_prob to preserve output statistics. dropout_mask = binary_mask.float().div(1.0 - dropout_probability) return dropout_mask
def test_mlpg_gradcheck(): # MLPG is performed dimention by dimention, so static_dim 1 is enough, # 2 just for in case. static_dim = 2 T = 10 for windows in _get_windows_set(): torch.manual_seed(1234) means = Variable(torch.rand(T, static_dim * len(windows)), requires_grad=True) inputs = (means,) # Unit variances case variances = torch.ones(static_dim * len(windows) ).expand(T, static_dim * len(windows)) assert gradcheck(MLPG(variances, windows), inputs, eps=1e-3, atol=1e-3) # Rand variances case variances = torch.rand(static_dim * len(windows) ).expand(T, static_dim * len(windows)) assert gradcheck(MLPG(variances, windows), inputs, eps=1e-3, atol=1e-3)
def test_multi_gpu(self): import torch from torch.autograd import Variable import torch.nn as nn from torch.nn.parallel.data_parallel import data_parallel from inferno.extensions.containers.graph import Graph input_shape = [8, 1, 3, 128, 128] model = Graph() \ .add_input_node('input') \ .add_node('conv0', nn.Conv3d(1, 10, 3, padding=1), previous='input') \ .add_node('conv1', nn.Conv3d(10, 1, 3, padding=1), previous='conv0') \ .add_output_node('output', previous='conv1') model.cuda() input = Variable(torch.rand(*input_shape).cuda()) output = data_parallel(model, input, device_ids=[0, 1, 2, 3])
def calc_gradient_penalty(D, real_data, fake_data): """Calculatge gradient penalty for WGAN-GP.""" alpha = torch.rand(params.batch_size, 1) alpha = alpha.expand(real_data.size()) alpha = make_cuda(alpha) interpolates = make_variable(alpha * real_data + ((1 - alpha) * fake_data)) interpolates.requires_grad = True disc_interpolates = D(interpolates) gradients = grad(outputs=disc_interpolates, inputs=interpolates, grad_outputs=make_cuda( torch.ones(disc_interpolates.size())), create_graph=True, retain_graph=True, only_inputs=True)[0] gradient_penalty = params.penalty_lambda * \ ((gradients.norm(2, dim=1) - 1) ** 2).mean() return gradient_penalty
def __init__(self, n, Qpenalty, nineq): super().__init__() nx = (n**2)**3 self.Q = Variable(Qpenalty*torch.eye(nx).double().cuda()) self.G1 = Variable(-torch.eye(nx).double().cuda()) self.h1 = Variable(torch.zeros(nx).double().cuda()) # if trueInit: # self.A = Parameter(torch.DoubleTensor(get_sudoku_matrix(n)).cuda()) # else: # # t = get_sudoku_matrix(n) # # self.A = Parameter(torch.rand(t.shape).double().cuda()) # # import IPython, sys; IPython.embed(); sys.exit(-1) self.A = Parameter(torch.rand(50,nx).double().cuda()) self.G2 = Parameter(torch.Tensor(128, nx).uniform_(-1,1).double().cuda()) self.z2 = Parameter(torch.zeros(nx).double().cuda()) self.s2 = Parameter(torch.ones(128).double().cuda()) # self.b = Variable(torch.ones(self.A.size(0)).double().cuda())
def __init__(self, nHidden=50, nineq=200, neq=0, eps=1e-4): super(LenetOptNet, self).__init__() self.conv1 = nn.Conv2d(1, 20, kernel_size=5) self.conv2 = nn.Conv2d(20, 50, kernel_size=5) self.qp_o = nn.Linear(50*4*4, nHidden) self.qp_z0 = nn.Linear(50*4*4, nHidden) self.qp_s0 = nn.Linear(50*4*4, nineq) assert(neq==0) self.M = Variable(torch.tril(torch.ones(nHidden, nHidden)).cuda()) self.L = Parameter(torch.tril(torch.rand(nHidden, nHidden).cuda())) self.G = Parameter(torch.Tensor(nineq,nHidden).uniform_(-1,1).cuda()) # self.z0 = Parameter(torch.zeros(nHidden).cuda()) # self.s0 = Parameter(torch.ones(nineq).cuda()) self.nHidden = nHidden self.nineq = nineq self.neq = neq self.eps = eps
def __init__(self, nFeatures, nHidden, nCls, neq, Qpenalty=0.1, eps=1e-4): super().__init__() self.nFeatures = nFeatures self.nHidden = nHidden self.nCls = nCls self.fc1 = nn.Linear(nFeatures, nHidden) self.fc2 = nn.Linear(nHidden, nCls) self.Q = Variable(Qpenalty*torch.eye(nHidden).double().cuda()) self.G = Variable(-torch.eye(nHidden).double().cuda()) self.h = Variable(torch.zeros(nHidden).double().cuda()) self.A = Parameter(torch.rand(neq,nHidden).double().cuda()) self.b = Variable(torch.ones(self.A.size(0)).double().cuda()) self.neq = neq
def test_MaxUnpool2d_output_size(self): m = nn.MaxPool2d(3, stride=2, return_indices=True) mu = nn.MaxUnpool2d(3, stride=2) big_t = torch.rand(1, 1, 6, 6) big_t[0][0][4][4] = 100 output_big, indices_big = m(Variable(big_t)) self.assertRaises(RuntimeError, lambda: mu(output_big, indices_big)) small_t = torch.rand(1, 1, 5, 5) for i in range(0, 4, 2): for j in range(0, 4, 2): small_t[:, :, i, j] = 100 output_small, indices_small = m(Variable(small_t)) for h in range(3, 10): for w in range(3, 10): if 4 <= h <= 6 and 4 <= w <= 6: size = (h, w) if h == 5: size = torch.LongStorage(size) elif h == 6: size = torch.LongStorage((1, 1) + size) mu(output_small, indices_small, output_size=size) else: self.assertRaises(ValueError, lambda: mu(output_small, indices_small, (h, w)))
def test_batchnorm_eval(self): types = (torch.FloatTensor,) if TEST_CUDA: types += (torch.cuda.FloatTensor,) for tp in types: module = nn.BatchNorm1d(3).type(tp) module.eval() data = Variable(torch.rand(4, 3).type(tp), requires_grad=True) grad = torch.rand(4, 3).type(tp) # 1st pass res1 = module(data) res1.backward(grad) grad1 = data.grad.data.clone() # 2nd pass if data.grad is not None: data.grad.data.zero_() res2 = module(data) res2.backward(grad) grad2 = data.grad.data.clone() self.assertEqual(res1, res2) self.assertEqual(grad1, grad2)
def _test_backward(self): v_t = torch.randn(5, 5) x_t = torch.randn(5, 5) y_t = torch.rand(5, 5) + 0.1 z_t = torch.randn(5, 5) grad_output = torch.randn(5, 5) v = Variable(v_t, requires_grad=True) x = Variable(x_t, requires_grad=True) y = Variable(y_t, requires_grad=True) z = Variable(z_t, requires_grad=True) v.backward(grad_output) self.assertEqual(v.grad.data, grad_output) a = x + (y * z) + 4 * z ** 2 * x / y a.backward(grad_output) x_grad = 4 * z_t.pow(2) / y_t + 1 y_grad = z_t - 4 * x_t * z_t.pow(2) / y_t.pow(2) z_grad = 8 * x_t * z_t / y_t + y_t self.assertEqual(x.grad.data, x_grad * grad_output) self.assertEqual(y.grad.data, y_grad * grad_output) self.assertEqual(z.grad.data, z_grad * grad_output)
def test_L1Penalty(self): weight = 1 m = nn.L1Penalty(weight, False, False) input = torch.rand(2, 10).add_(-0.5) input[0][0] = 0 m.forward(input) grad = m.backward(input, torch.ones(input.size())) self.assertEqual(input.abs().sum() * weight, m.loss) true_grad = (input.gt(0).type_as(grad) + input.lt(0).type_as(grad).mul_(-1)).mul_(weight) self.assertEqual(true_grad, grad) # Check that these don't raise errors m.__repr__() str(m)
def test_cat(self): SIZE = 10 for dim in range(-3, 3): pos_dim = dim if dim >= 0 else 3 + dim x = torch.rand(13, SIZE, SIZE).transpose(0, pos_dim) y = torch.rand(17, SIZE, SIZE).transpose(0, pos_dim) z = torch.rand(19, SIZE, SIZE).transpose(0, pos_dim) res1 = torch.cat((x, y, z), dim) self.assertEqual(res1.narrow(pos_dim, 0, 13), x, 0) self.assertEqual(res1.narrow(pos_dim, 13, 17), y, 0) self.assertEqual(res1.narrow(pos_dim, 30, 19), z, 0) x = torch.randn(20, SIZE, SIZE) self.assertEqual(torch.cat(torch.split(x, 7)), x) self.assertEqual(torch.cat(torch.chunk(x, 7)), x) y = torch.randn(1, SIZE, SIZE) z = torch.cat([x, y]) self.assertEqual(z.size(), (21, SIZE, SIZE)) self.assertRaises(RuntimeError, lambda: torch.cat([]))
def test_abs(self): size = 1000 max_val = 1000 original = torch.rand(size).mul(max_val) # Tensor filled with values from {-1, 1} switch = torch.rand(size).mul(2).floor().mul(2).add(-1) types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor'] for t in types: data = original.type(t) switch = switch.type(t) res = torch.mul(data, switch) # abs is used in assertEqual so we use the slow version instead self.assertTensorsSlowEqual(res.abs(), data, 1e-16) # Checking that the right abs function is called for LongTensor bignumber = 2 ^ 31 + 1 res = torch.LongTensor((-bignumber,)) self.assertGreater(res.abs()[0], 0)
def test_expand(self): tensor = torch.rand(1, 8, 1) tensor2 = torch.rand(5) template = torch.rand(4, 8, 5) target = template.size() self.assertEqual(tensor.expand_as(template).size(), target) self.assertEqual(tensor.expand(4, 8, 5).size(), target) self.assertEqual(tensor.expand(target).size(), target) self.assertEqual(tensor2.expand_as(template).size(), target) self.assertEqual(tensor2.expand(4, 8, 5).size(), target) self.assertEqual(tensor2.expand(target).size(), target) # test double expand self.assertEqual(tensor2.expand(1, 5).expand(2, 2, 5), tensor2.repeat(2, 2, 1)) # test non-contiguous noncontig = torch.randn(5, 2, 1, 3)[:, 0] assert not noncontig.is_contiguous() self.assertEqual(noncontig.expand(2, 5, 4, 3), noncontig.contiguous().repeat(2, 1, 4, 1)) # make sure it's compatible with unsqueeze expanded = tensor2.expand(1, 1, 5) unsqueezed = tensor2.unsqueeze(0).unsqueeze(1) self.assertEqual(expanded, unsqueezed) self.assertEqual(expanded.stride(), unsqueezed.stride())
def test_view(self): tensor = torch.rand(15) template = torch.rand(3, 5) empty = torch.Tensor() target = template.size() self.assertEqual(tensor.view_as(template).size(), target) self.assertEqual(tensor.view(3, 5).size(), target) self.assertEqual(tensor.view(torch.Size([3, 5])).size(), target) self.assertEqual(tensor.view(-1, 5).size(), target) self.assertEqual(tensor.view(3, -1).size(), target) tensor_view = tensor.view(5, 3) tensor_view.fill_(random.uniform(0, 1)) self.assertEqual((tensor_view - tensor).abs().max(), 0) self.assertEqual(empty.view_as(empty), empty) self.assertEqual(empty.view(0), empty) self.assertRaises(RuntimeError, lambda: tensor.view(15, 0)) self.assertRaises(RuntimeError, lambda: tensor.view(7, -1)) self.assertRaises(RuntimeError, lambda: tensor.view(15, -1, -1))
def test_bernoulli(self): t = torch.ByteTensor(10, 10) def isBinary(t): return torch.ne(t, 0).mul_(torch.ne(t, 1)).sum() == 0 p = 0.5 t.bernoulli_(p) self.assertTrue(isBinary(t)) p = torch.rand(SIZE) t.bernoulli_(p) self.assertTrue(isBinary(t)) q = torch.rand(5, 5) self.assertTrue(isBinary(q.bernoulli()))
def feedforward_test(): import torch.nn as nn import torch.nn.functional as F fc1 = nn.Linear(10,20) fc1.weight.data.normal_(0.0,1.0) fc1.bias.data.normal_(0.0,1.0) fc2 = nn.Linear(20,2) fc2.weight.data.normal_(0.0,1.0) fc2.bias.data.normal_(0.0,1.0) model = lambda x: F.log_softmax(fc2(F.relu(fc1(x)))) data = Variable(torch.rand(10,10)) out_path = 'out' if not os.path.isdir(out_path): os.mkdir(out_path) uid = str(uuid.uuid4()) torch2c.compile(model(data),'feedforward',os.path.join(out_path,uid),compile_test=True)
def act(self, state): return th.rand(self.output_size).numpy(), None
def _sample(self): if not self.processed: self._process() self.processed = True indices = (th.rand(self.batch_size) * len(self.rewards)).int() # TODO: Cleanup log_actions = [] rewards = [] critics = [] entropies = [] states = [] advantages = [] actions = [] for i in indices: actions.append(self.actions[i].value) log_actions.append(self.actions[i].log_prob) rewards.append(self.rewards[i]) critics.append(self.critics[i]) entropies.append(self.entropies[i]) states.append(self.states[i]) advantages.append(self.advantages[i]) actions = th.cat(actions, 0) log_actions = th.cat(log_actions, 0) rewards = th.cat(rewards, 0).view(-1) critics = th.cat(critics, 0).view(-1) entropies = th.cat(entropies, 0).view(-1) states = th.cat(states, 0) advantages = th.cat(advantages, 0).view(-1) return actions, log_actions, rewards, critics, entropies, states, advantages
def bnparams(n): return cast({'weight': torch.rand(n), 'bias': torch.zeros(n)})
def test_lerp(self): def TH_lerp(a, b, weight): return a + weight * (b-a); size = (100, 100) a = torch.rand(*size) b = torch.rand(*size) w = random.random() result = torch.lerp(a, b, w) expected = a.clone() expected.map2_(a, b, lambda _, a, b: TH_lerp(a, b, w)) self.assertEqual(result, expected)
def test_clamp(self): m1 = torch.rand(100).mul(5).add(-2.5) # uniform in [-2.5, 2.5] # just in case we're extremely lucky. min_val = -1 max_val = 1 m1[1] = min_val m1[2] = max_val res1 = m1.clone() res1.clamp_(min_val, max_val) res2 = m1.clone() for i in iter_indices(res2): res2[i] = max(min_val, min(max_val, res2[i])) self.assertEqual(res1, res2)