本文整理汇总了Python中torch.randn函数的典型用法代码示例。如果您正苦于以下问题:Python randn函数的具体用法?Python randn怎么用?Python randn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了randn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
dtype = torch.cuda.FloatTensor
batch_size = 64
input_dimension = 1000
hidden_dimension = 100
output_dimension = 10
random_input = torch.randn(batch_size, input_dimension).type(dtype)
true_output = torch.randn(batch_size, output_dimension).type(dtype)
weights_1 = torch.randn(input_dimension, hidden_dimension).type(dtype)
weights_2 = torch.randn(hidden_dimension, output_dimension).type(dtype)
learning_rate = 1e-6
for t in range(500):
h = random_input.mm(weights_1)
# Compute ReLU
h_relu = h.clamp(min=0)
predicted_output = h_relu.mm(weights_2)
loss = (predicted_output - true_output).pow(2).sum()
print("Loss: {}".format(loss))
# Compute gradient
grad_y_pred = 2.0 * (predicted_output - true_output)
grad_w2 = h_relu.t().mm(grad_y_pred)
grad_h_relu = grad_y_pred.mm(weights_2.t())
grad_h = grad_h_relu.clone()
grad_h[h < 0] = 0
grad_w1 = random_input.t().mm(grad_h)
weights_1 -= learning_rate * grad_w1
weights_2 -= learning_rate * grad_w2
示例2: fit
def fit(self):
args = self.args
for epoch in range(args.max_epochs):
self.G.train()
self.D.train()
for step, inputs in enumerate(self.train_loader):
batch_size = inputs[0].size(0)
images = inputs[0].to(self.device)
labels = inputs[1].to(self.device)
# create the labels used to distingush real or fake
real_labels = torch.ones(batch_size, dtype=torch.int64).to(self.device)
fake_labels = torch.zeros(batch_size, dtype=torch.int64).to(self.device)
# train the discriminator
# discriminator <- real image
D_real, D_real_cls = self.D(images)
D_loss_real = self.loss_fn(D_real, real_labels)
D_loss_real_cls = self.loss_fn(D_real_cls, labels)
# noise vector
z = torch.randn(batch_size, args.z_dim).to(self.device)
# make label to onehot vector
y_onehot = torch.zeros((batch_size, 10)).to(self.device)
y_onehot.scatter_(1, labels.unsqueeze(1), 1)
y_onehot.requires_grad_(False)
# discriminator <- fake image
G_fake = self.G(y_onehot, z)
D_fake, D_fake_cls = self.D(G_fake)
D_loss_fake = self.loss_fn(D_fake, fake_labels)
D_loss_fake_cls = self.loss_fn(D_fake_cls, labels)
D_loss = D_loss_real + D_loss_fake + \
D_loss_real_cls + D_loss_fake_cls
self.D.zero_grad()
D_loss.backward()
self.optim_D.step()
# train the generator
z = torch.randn(batch_size, args.z_dim).to(self.device)
G_fake = self.G(y_onehot, z)
D_fake, D_fake_cls = self.D(G_fake)
G_loss = self.loss_fn(D_fake, real_labels) + \
self.loss_fn(D_fake_cls, labels)
self.G.zero_grad()
G_loss.backward()
self.optim_G.step()
if (epoch+1) % args.print_every == 0:
print("Epoch [{}/{}] Loss_D: {:.3f}, Loss_G: {:.3f}".
format(epoch+1, args.max_epochs, D_loss.item(), G_loss.item()))
self.save(args.ckpt_dir, epoch+1)
self.sample(epoch+1)
示例3: __init__
def __init__(self,
input_dim: int,
forward_combination: str = "y-x",
backward_combination: str = "y-x",
num_width_embeddings: int = None,
span_width_embedding_dim: int = None,
bucket_widths: bool = False,
use_sentinels: bool = True) -> None:
super().__init__()
self._input_dim = input_dim
self._forward_combination = forward_combination
self._backward_combination = backward_combination
self._num_width_embeddings = num_width_embeddings
self._bucket_widths = bucket_widths
if self._input_dim % 2 != 0:
raise ConfigurationError("The input dimension is not divisible by 2, but the "
"BidirectionalEndpointSpanExtractor assumes the embedded representation "
"is bidirectional (and hence divisible by 2).")
if num_width_embeddings is not None and span_width_embedding_dim is not None:
self._span_width_embedding = Embedding(num_width_embeddings, span_width_embedding_dim)
elif not all([num_width_embeddings is None, span_width_embedding_dim is None]):
raise ConfigurationError("To use a span width embedding representation, you must"
"specify both num_width_buckets and span_width_embedding_dim.")
else:
self._span_width_embedding = None
self._use_sentinels = use_sentinels
if use_sentinels:
self._start_sentinel = Parameter(torch.randn([1, 1, int(input_dim / 2)]))
self._end_sentinel = Parameter(torch.randn([1, 1, int(input_dim / 2)]))
示例4: __init__
def __init__(self):
super(Chunking, self).__init__()
self.input_size = embedding_size \
+ nb_postags \
+ postag_hn_size * 2
self.w = nn.Parameter(torch.randn(chunking_nb_layers * 2,
max_sentence_size,
chunking_hn_size))
self.h = nn.Parameter(torch.randn(chunking_nb_layers * 2,
max_sentence_size,
chunking_hn_size))
self.embedding = nn.Embedding(nb_postags, chunking_postag_emb_size)
self.aux_emb = torch.arange(0, nb_postags)
self.aux_emb = Variable(self.aux_emb).long()
self.bi_lstm = nn.LSTM(self.input_size,
chunking_hn_size,
chunking_nb_layers,
bidirectional=True)
self.fc = nn.Linear(chunking_hn_size * 2, nb_chunktags)
示例5: test_forget_mult_cuda
def test_forget_mult_cuda():
this_tests(ForgetMultGPU, BwdForgetMultGPU)
x,f = torch.randn(5,3,20).cuda().chunk(2, dim=2)
x,f = x.contiguous().requires_grad_(True),f.contiguous().requires_grad_(True)
th_x,th_f = detach_and_clone(x),detach_and_clone(f)
for (bf, bw) in [(True,True), (False,True), (True,False), (False,False)]:
forget_mult = BwdForgetMultGPU if bw else ForgetMultGPU
th_out = forget_mult_CPU(th_x, th_f, hidden_init=None, batch_first=bf, backward=bw)
th_loss = th_out.pow(2).mean()
th_loss.backward()
out = forget_mult.apply(x, f, None, bf)
loss = out.pow(2).mean()
loss.backward()
assert torch.allclose(th_out,out, rtol=1e-4, atol=1e-5)
assert torch.allclose(th_x.grad,x.grad, rtol=1e-4, atol=1e-5)
assert torch.allclose(th_f.grad,f.grad, rtol=1e-4, atol=1e-5)
for p in [x,f, th_x, th_f]:
p = p.detach()
p.grad = None
h = torch.randn((5 if bf else 3), 10).cuda().requires_grad_(True)
th_h = detach_and_clone(h)
th_out = forget_mult_CPU(th_x, th_f, hidden_init=th_h, batch_first=bf, backward=bw)
th_loss = th_out.pow(2).mean()
th_loss.backward()
out = forget_mult.apply(x.contiguous(), f.contiguous(), h, bf)
loss = out.pow(2).mean()
loss.backward()
assert torch.allclose(th_out,out, rtol=1e-4, atol=1e-5)
assert torch.allclose(th_x.grad,x.grad, rtol=1e-4, atol=1e-5)
assert torch.allclose(th_f.grad,f.grad, rtol=1e-4, atol=1e-5)
assert torch.allclose(th_h.grad,h.grad, rtol=1e-4, atol=1e-5)
for p in [x,f, th_x, th_f]:
p = p.detach()
p.grad = None
示例6: test_backward_computes_backward_pass
def test_backward_computes_backward_pass():
weight = torch.randn(4, 8, 3, 3).cuda()
input = torch.randn(4, 8, 4, 4).cuda()
input_var = Variable(input, requires_grad=True)
weight_var = Parameter(weight)
out_var = F.conv2d(
input=input_var,
weight=weight_var,
bias=None,
stride=1,
padding=1,
dilation=1,
groups=1,
)
out_var.backward(gradient=input_var.data.clone().fill_(1))
out = out_var.data
input_grad = input_var.grad.data
weight_grad = weight_var.grad.data
func = _EfficientConv2d(
stride=1,
padding=1,
dilation=1,
groups=1,
)
out_efficient = func.forward(weight, None, input)
weight_grad_efficient, _, input_grad_efficient = func.backward(
weight, None, input, input.clone().fill_(1))
assert(almost_equal(out, out_efficient))
assert(almost_equal(input_grad, input_grad_efficient))
assert(almost_equal(weight_grad, weight_grad_efficient))
示例7: test_degenerate_GPyTorchPosterior
def test_degenerate_GPyTorchPosterior(self, cuda=False):
device = torch.device("cuda") if cuda else torch.device("cpu")
for dtype in (torch.float, torch.double):
# singular covariance matrix
degenerate_covar = torch.tensor(
[[1, 1, 0], [1, 1, 0], [0, 0, 2]], dtype=dtype, device=device
)
mean = torch.rand(3, dtype=dtype, device=device)
mvn = MultivariateNormal(mean, lazify(degenerate_covar))
posterior = GPyTorchPosterior(mvn=mvn)
# basics
self.assertEqual(posterior.device.type, device.type)
self.assertTrue(posterior.dtype == dtype)
self.assertEqual(posterior.event_shape, torch.Size([3, 1]))
self.assertTrue(torch.equal(posterior.mean, mean.unsqueeze(-1)))
variance_exp = degenerate_covar.diag().unsqueeze(-1)
self.assertTrue(torch.equal(posterior.variance, variance_exp))
# rsample
with warnings.catch_warnings(record=True) as w:
# we check that the p.d. warning is emitted - this only
# happens once per posterior, so we need to check only once
samples = posterior.rsample(sample_shape=torch.Size([4]))
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
self.assertTrue("not p.d." in str(w[-1].message))
self.assertEqual(samples.shape, torch.Size([4, 3, 1]))
samples2 = posterior.rsample(sample_shape=torch.Size([4, 2]))
self.assertEqual(samples2.shape, torch.Size([4, 2, 3, 1]))
# rsample w/ base samples
base_samples = torch.randn(4, 3, 1, device=device, dtype=dtype)
samples_b1 = posterior.rsample(
sample_shape=torch.Size([4]), base_samples=base_samples
)
samples_b2 = posterior.rsample(
sample_shape=torch.Size([4]), base_samples=base_samples
)
self.assertTrue(torch.allclose(samples_b1, samples_b2))
base_samples2 = torch.randn(4, 2, 3, 1, device=device, dtype=dtype)
samples2_b1 = posterior.rsample(
sample_shape=torch.Size([4, 2]), base_samples=base_samples2
)
samples2_b2 = posterior.rsample(
sample_shape=torch.Size([4, 2]), base_samples=base_samples2
)
self.assertTrue(torch.allclose(samples2_b1, samples2_b2))
# collapse_batch_dims
b_mean = torch.rand(2, 3, dtype=dtype, device=device)
b_degenerate_covar = degenerate_covar.expand(2, *degenerate_covar.shape)
b_mvn = MultivariateNormal(b_mean, lazify(b_degenerate_covar))
b_posterior = GPyTorchPosterior(mvn=b_mvn)
b_base_samples = torch.randn(4, 2, 3, 1, device=device, dtype=dtype)
with warnings.catch_warnings(record=True) as w:
b_samples = b_posterior.rsample(
sample_shape=torch.Size([4]), base_samples=b_base_samples
)
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
self.assertTrue("not p.d." in str(w[-1].message))
self.assertEqual(b_samples.shape, torch.Size([4, 2, 3, 1]))
示例8: test_DepthConcat
def test_DepthConcat(self):
outputSize = torch.IntTensor((5, 6, 7, 8))
input = torch.randn(2, 3, 12, 12)
gradOutput = torch.randn(2, int(outputSize.sum()), 12, 12)
concat = nn.DepthConcat(1)
concat.add(nn.SpatialConvolution(3, outputSize[0], 1, 1, 1, 1)) # > 2, 5, 12, 12
concat.add(nn.SpatialConvolution(3, outputSize[1], 3, 3, 1, 1)) # > 2, 6, 10, 10
concat.add(nn.SpatialConvolution(3, outputSize[2], 4, 4, 1, 1)) # > 2, 7, 9, 9
concat.add(nn.SpatialConvolution(3, outputSize[3], 5, 5, 1, 1)) # > 2, 8, 8, 8
concat.zeroGradParameters()
# forward/backward
outputConcat = concat.forward(input)
gradInputConcat = concat.backward(input, gradOutput)
# the spatial dims are the largest, the nFilters is the sum
output = torch.Tensor(2, int(outputSize.sum()), 12, 12).zero_() # zero for padding
narrows = ((slice(None), slice(0, 5), slice(None), slice(None)),
(slice(None), slice(5, 11), slice(1, 11), slice(1, 11)),
(slice(None), slice(11, 18), slice(1, 10), slice(1, 10)),
(slice(None), slice(18, 26), slice(2, 10), slice(2, 10)))
gradInput = input.clone().zero_()
for i in range(4):
conv = concat.get(i)
gradWeight = conv.gradWeight.clone()
conv.zeroGradParameters()
output[narrows[i]].copy_(conv.forward(input))
gradInput.add_(conv.backward(input, gradOutput[narrows[i]]))
self.assertEqual(gradWeight, conv.gradWeight)
self.assertEqual(output, outputConcat)
self.assertEqual(gradInput, gradInputConcat)
# Check that these don't raise errors
concat.__repr__()
str(concat)
示例9: bisect_demo
def bisect_demo():
""" Bisect the LB/UB on specified columns.
The key is to use scatter_() to convert indices into one-hot encodings.
"""
t1t2 = torch.stack((torch.randn(5, 4), torch.randn(5, 4)), dim=-1)
lb, _ = torch.min(t1t2, dim=-1)
ub, _ = torch.max(t1t2, dim=-1)
print('LB:', lb)
print('UB:', ub)
# random idxs for testing
idxs = torch.randn_like(lb)
_, idxs = idxs.max(dim=-1) # <Batch>
print('Split idxs:', idxs)
idxs = idxs.unsqueeze(dim=-1) # Batch x 1
idxs = torch.zeros_like(lb).byte().scatter_(-1, idxs, 1) # convert into one-hot encoding
print('Reorg idxs:', idxs)
mid = (lb + ub) / 2.0
lefts_lb = lb
lefts_ub = torch.where(idxs, mid, ub) # use the one-hot encoding to call torch.where()
rights_lb = torch.where(idxs, mid, lb) # definitely faster than element-wise reassignment
rights_ub = ub
print('LEFT LB:', lefts_lb)
print('LEFT UB:', lefts_ub)
print('RIGHT LB:', rights_lb)
print('RIGHT UB:', rights_ub)
newlb = torch.cat((lefts_lb, rights_lb), dim=0)
newub = torch.cat((lefts_ub, rights_ub), dim=0)
return newlb, newub
示例10: test_backward
def test_backward(self):
a = Variable(torch.randn(2, 2), requires_grad=True)
b = Variable(torch.randn(2, 2), requires_grad=True)
x = a
y = a * b
trace, inputs = torch._C._tracer_enter((x, y), 2)
def fn(x, y):
return y * 2 * x
z = fn(*inputs)
torch._C._tracer_exit((z,))
torch._C._jit_pass_lint(trace)
# Run first backward
grad, = torch.autograd.grad(z, x, Variable(torch.ones(2, 2), requires_grad=True), create_graph=True)
torch._C._jit_pass_lint(trace)
# Run second backward
grad.sum().backward(create_graph=True)
torch._C._jit_pass_lint(trace)
# Run dead code elimination to remove unused trace nodes
torch._C._jit_pass_dce(trace)
# This is nondeterministic, see:
# https://github.com/ezyang/pytorch/issues/227
# self.assertExpectedTrace(trace)
self.skipTest("output is nondeterministic on Travis/Python 3.5")
示例11: main
def main():
dtype = torch.FloatTensor
N, d_in, H, d_out = 64, 1000, 100, 10 # d_in表示输入维度,d_out输出维度,H是隐藏层维度数
x = Variable(torch.randn(N, d_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, d_out).type(dtype), requires_grad=False)
model = torch.nn.Sequential(
torch.nn.Linear(d_in,H),
torch.nn.ReLU(),
torch.nn.Linear(H,d_out)
)
loss_fn = torch.nn.MSELoss(size_average=False)
learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for t in range(500):
y_pred = model(x)
loss = loss_fn(y_pred,y)
model.zero_grad()
loss.backward()
optimizer.step()
print(loss.data[0])
示例12: test_non_stateful_states_are_sorted_correctly
def test_non_stateful_states_are_sorted_correctly(self):
encoder_base = _EncoderBase(stateful=False)
initial_states = (torch.randn(6, 5, 7), torch.randn(6, 5, 7))
# Check that we sort the state for non-stateful encoders. To test
# we'll just use a "pass through" encoder, as we aren't actually testing
# the functionality of the encoder here anyway.
_, states, restoration_indices = encoder_base.sort_and_run_forward(lambda *x: x,
self.tensor,
self.mask,
initial_states)
# Our input tensor had 2 zero length sequences, so we need
# to concat a tensor of shape
# (num_layers * num_directions, batch_size - num_valid, hidden_dim),
# to the output before unsorting it.
zeros = torch.zeros([6, 2, 7])
# sort_and_run_forward strips fully-padded instances from the batch;
# in order to use the restoration_indices we need to add back the two
# that got stripped. What we get back should match what we started with.
for state, original in zip(states, initial_states):
assert list(state.size()) == [6, 3, 7]
state_with_zeros = torch.cat([state, zeros], 1)
unsorted_state = state_with_zeros.index_select(1, restoration_indices)
for index in [0, 1, 3]:
numpy.testing.assert_array_equal(unsorted_state[:, index, :].data.numpy(),
original[:, index, :].data.numpy())
示例13: test_inline_jit_compile_extension_multiple_sources_and_no_functions
def test_inline_jit_compile_extension_multiple_sources_and_no_functions(self):
cpp_source1 = '''
at::Tensor sin_add(at::Tensor x, at::Tensor y) {
return x.sin() + y.sin();
}
'''
cpp_source2 = '''
#include <torch/torch.h>
at::Tensor sin_add(at::Tensor x, at::Tensor y);
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("sin_add", &sin_add, "sin(x) + sin(y)");
}
'''
module = torch.utils.cpp_extension.load_inline(
name='inline_jit_extension',
cpp_sources=[cpp_source1, cpp_source2],
verbose=True)
x = torch.randn(4, 4)
y = torch.randn(4, 4)
z = module.sin_add(x, y)
self.assertEqual(z, x.sin() + y.sin())
示例14: get_batch
def get_batch(batch_size=32, randomize=False):
"""Builds a batch i.e. (x, f(x)) pair."""
random = torch.randn(batch_size)
x = make_features(random)
y = f(x)
if randomize: y += torch.randn(1)
return Variable(x), Variable(y)
示例15: test_forward
def test_forward(self):
batch = 16
len1, len2 = 21, 24
seq_len1 = torch.randint(low=len1 - 10, high=len1 + 1, size=(batch,)).long()
seq_len2 = torch.randint(low=len2 - 10, high=len2 + 1, size=(batch,)).long()
mask1 = []
for w in seq_len1:
mask1.append([1] * w.item() + [0] * (len1 - w.item()))
mask1 = torch.FloatTensor(mask1)
mask2 = []
for w in seq_len2:
mask2.append([1] * w.item() + [0] * (len2 - w.item()))
mask2 = torch.FloatTensor(mask2)
d = 200 # hidden dimension
l = 20 # number of perspective
test1 = torch.randn(batch, len1, d)
test2 = torch.randn(batch, len2, d)
test1 = test1 * mask1.view(-1, len1, 1).expand(-1, len1, d)
test2 = test2 * mask2.view(-1, len2, 1).expand(-1, len2, d)
test1_fw, test1_bw = torch.split(test1, d // 2, dim=-1)
test2_fw, test2_bw = torch.split(test2, d // 2, dim=-1)
ml_fw = BiMpmMatching.from_params(Params({"is_forward": True, "num_perspectives": l}))
ml_bw = BiMpmMatching.from_params(Params({"is_forward": False, "num_perspectives": l}))
vecs_p_fw, vecs_h_fw = ml_fw(test1_fw, mask1, test2_fw, mask2)
vecs_p_bw, vecs_h_bw = ml_bw(test1_bw, mask1, test2_bw, mask2)
vecs_p, vecs_h = torch.cat(vecs_p_fw + vecs_p_bw, dim=2), torch.cat(vecs_h_fw + vecs_h_bw, dim=2)
assert vecs_p.size() == torch.Size([batch, len1, 10 + 10 * l])
assert vecs_h.size() == torch.Size([batch, len2, 10 + 10 * l])
assert ml_fw.get_output_dim() == ml_bw.get_output_dim() == vecs_p.size(2) // 2 == vecs_h.size(2) // 2