def test_inverse(self):
M = torch.randn(5,5)
MI = torch.inverse(M)
E = torch.eye(5)
self.assertFalse(MI.is_contiguous(), 'MI is contiguous')
self.assertEqual(E, torch.mm(M, MI), 1e-8, 'inverse value')
self.assertEqual(E, torch.mm(MI, M), 1e-8, 'inverse value')
MII = torch.Tensor(5, 5)
torch.inverse(MII, M)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
# second call, now that MII is transposed
torch.inverse(MII, M)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
python类eye()的实例源码
def testClassErrorMeter(self):
mtr = meter.ClassErrorMeter(topk=[1])
output = torch.eye(3)
if hasattr(torch, "arange"):
target = torch.arange(0, 3)
else:
target = torch.range(0, 2)
mtr.add(output, target)
err = mtr.value()
self.assertEqual(err, [0], "All should be correct")
target[0] = 1
target[1] = 0
target[2] = 0
mtr.add(output, target)
err = mtr.value()
self.assertEqual(err, [50.0], "Half should be correct")
def __init__(self, z_dim, transition_dim):
super(GatedTransition, self).__init__()
# initialize the six linear transformations used in the neural network
self.lin_gate_z_to_hidden = nn.Linear(z_dim, transition_dim)
self.lin_gate_hidden_to_z = nn.Linear(transition_dim, z_dim)
self.lin_proposed_mean_z_to_hidden = nn.Linear(z_dim, transition_dim)
self.lin_proposed_mean_hidden_to_z = nn.Linear(transition_dim, z_dim)
self.lin_sig = nn.Linear(z_dim, z_dim)
self.lin_z_to_mu = nn.Linear(z_dim, z_dim)
# modify the default initialization of lin_z_to_mu
# so that it's starts out as the identity function
self.lin_z_to_mu.weight.data = torch.eye(z_dim)
self.lin_z_to_mu.bias.data = torch.zeros(z_dim)
# initialize the three non-linearities used in the neural network
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
self.softplus = nn.Softplus()
def torch_eye(n, m=None, out=None):
"""
Like `torch.eye()`, but works with cuda tensors.
"""
if m is None:
m = n
try:
return torch.eye(n, m, out=out)
except TypeError:
# Only catch errors due to torch.eye() not being available for cuda tensors.
module = torch.Tensor.__module__ if out is None else type(out).__module__
if module != 'torch.cuda':
raise
Tensor = getattr(torch, torch.Tensor.__name__)
cpu_out = Tensor(n, m)
cuda_out = torch.eye(m, n, out=cpu_out).cuda()
return cuda_out if out is None else out.copy_(cuda_out)
def __init__(self, shared_resources: SharedResources):
super(FastQAPyTorchModule, self).__init__()
self._shared_resources = shared_resources
input_size = shared_resources.config["repr_dim_input"]
size = shared_resources.config["repr_dim"]
self._size = size
self._with_char_embeddings = self._shared_resources.config.get("with_char_embeddings", False)
# modules & parameters
if self._with_char_embeddings:
self._conv_char_embedding = embedding.ConvCharEmbeddingModule(
len(shared_resources.char_vocab), size)
self._embedding_projection = nn.Linear(size + input_size, size)
self._embedding_highway = Highway(size, 1)
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size + size))
input_size = size
else:
self._v_wiq_w = nn.Parameter(torch.ones(1, 1, input_size))
self._bilstm = BiLSTM(input_size + 2, size)
self._answer_layer = FastQAAnswerModule(shared_resources)
# [size, 2 * size]
self._question_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
self._support_projection = nn.Parameter(torch.cat([torch.eye(size), torch.eye(size)], dim=1))
def test_forward_backward(self):
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from reid.loss import OIMLoss
criterion = OIMLoss(3, 3, scalar=1.0, size_average=False)
criterion.lut = torch.eye(3)
x = Variable(torch.randn(3, 3), requires_grad=True)
y = Variable(torch.range(0, 2).long())
loss = criterion(x, y)
loss.backward()
probs = F.softmax(x)
grads = probs.data - torch.eye(3)
abs_diff = torch.abs(grads - x.grad.data)
self.assertEquals(torch.log(probs).diag().sum(), -loss)
self.assertTrue(torch.max(abs_diff) < 1e-6)
def reset_parameters(self):
"""
Initialize parameters following the way proposed in the paper.
"""
# The input-to-hidden weight matrix is initialized orthogonally.
init.orthogonal(self.weight_ih.data)
# The hidden-to-hidden weight matrix is initialized as an identity
# matrix.
weight_hh_data = torch.eye(self.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 4)
self.weight_hh.data.set_(weight_hh_data)
# The bias is just set to zero vectors.
init.constant(self.bias.data, val=0)
# Initialization of BN parameters.
self.bn_ih.reset_parameters()
self.bn_hh.reset_parameters()
self.bn_c.reset_parameters()
self.bn_ih.bias.data.fill_(0)
self.bn_hh.bias.data.fill_(0)
self.bn_ih.weight.data.fill_(0.1)
self.bn_hh.weight.data.fill_(0.1)
self.bn_c.weight.data.fill_(0.1)
def __init__(self, params, eps=1e-2):
super(SolveNewsvendor, self).__init__()
k = len(params['d'])
self.Q = Variable(torch.diag(torch.Tensor(
[params['c_quad']] + [params['b_quad']]*k + [params['h_quad']]*k)) \
.cuda())
self.p = Variable(torch.Tensor(
[params['c_lin']] + [params['b_lin']]*k + [params['h_lin']]*k) \
.cuda())
self.G = Variable(torch.cat([
torch.cat([-torch.ones(k,1), -torch.eye(k), torch.zeros(k,k)], 1),
torch.cat([torch.ones(k,1), torch.zeros(k,k), -torch.eye(k)], 1),
-torch.eye(1 + 2*k)], 0).cuda())
self.h = Variable(torch.Tensor(
np.concatenate([-params['d'], params['d'], np.zeros(1+ 2*k)])).cuda())
self.one = Variable(torch.Tensor([1])).cuda()
self.eps_eye = eps * Variable(torch.eye(1 + 2*k).cuda()).unsqueeze(0)
def forward(self, h, Q, u):
batch_size = h.size()[0]
v, r = self.trans(h).chunk(2, dim=1)
v1 = v.unsqueeze(2)
rT = r.unsqueeze(1)
I = Variable(torch.eye(self.dim_z).repeat(batch_size, 1, 1))
if rT.data.is_cuda:
I.dada.cuda()
A = I.add(v1.bmm(rT))
B = self.fc_B(h).view(-1, self.dim_z, self.dim_u)
o = self.fc_o(h)
# need to compute the parameters for distributions
# as well as for the samples
u = u.unsqueeze(2)
d = A.bmm(Q.mu.unsqueeze(2)).add(B.bmm(u)).add(o).squeeze(2)
sample = A.bmm(h.unsqueeze(2)).add(B.bmm(u)).add(o).squeeze(2)
return sample, NormalDistribution(d, Q.sigma, Q.logsigma, v=v, r=r)
def forward(self, x):
nBatch = x.size(0)
x = self.fc1(x)
L = self.M*self.L
Q = L.mm(L.t()) + self.args.eps*Variable(torch.eye(self.nHidden)).cuda()
Q = Q.unsqueeze(0).expand(nBatch, self.nHidden, self.nHidden)
G = self.G.unsqueeze(0).expand(nBatch, self.nineq, self.nHidden)
h = self.G.mv(self.z0)+self.s0
h = h.unsqueeze(0).expand(nBatch, self.nineq)
e = Variable(torch.Tensor())
x = QPFunction()(Q, x, G, h, e, e)
x = x[:,:self.nFeatures]
return x
def __init__(self, nFeatures, args):
super().__init__()
nHidden, neq, nineq = 2*nFeatures-1,0,2*nFeatures-2
assert(neq==0)
# self.fc1 = nn.Linear(nFeatures, nHidden)
self.M = Variable(torch.tril(torch.ones(nHidden, nHidden)).cuda())
Q = 1e-8*torch.eye(nHidden)
Q[:nFeatures,:nFeatures] = torch.eye(nFeatures)
self.L = Variable(torch.potrf(Q))
self.D = Parameter(0.3*torch.randn(nFeatures-1, nFeatures))
# self.lam = Parameter(20.*torch.ones(1))
self.h = Variable(torch.zeros(nineq))
self.nFeatures = nFeatures
self.nHidden = nHidden
self.neq = neq
self.nineq = nineq
self.args = args
def forward(self, x):
nBatch = x.size(0)
L = self.M*self.L
Q = L.mm(L.t()) + self.args.eps*Variable(torch.eye(self.nHidden)).cuda()
Q = Q.unsqueeze(0).expand(nBatch, self.nHidden, self.nHidden)
nI = Variable(-torch.eye(self.nFeatures-1).type_as(Q.data))
G = torch.cat((
torch.cat(( self.D, nI), 1),
torch.cat((-self.D, nI), 1)
))
G = G.unsqueeze(0).expand(nBatch, self.nineq, self.nHidden)
h = self.h.unsqueeze(0).expand(nBatch, self.nineq)
e = Variable(torch.Tensor())
# p = torch.cat((-x, self.lam.unsqueeze(0).expand(nBatch, self.nFeatures-1)), 1)
p = torch.cat((-x, Parameter(13.*torch.ones(nBatch, self.nFeatures-1).cuda())), 1)
x = QPFunction()(Q.double(), p.double(), G.double(), h.double(), e, e).float()
x = x[:,:self.nFeatures]
return x
def forward(self, puzzles):
nBatch = puzzles.size(0)
x = puzzles.view(nBatch,-1)
x = self.fc_in(x)
e = Variable(torch.Tensor())
h = self.G.mv(self.z)+self.s
x = QPFunction(verbose=False)(
self.Q, x, self.G, h, e, e,
)
x = self.fc_out(x)
x = x.view_as(puzzles)
return x
# if __name__=="__main__":
# sudoku = SolveSudoku(2, 0.2)
# puzzle = [[4, 0, 0, 0], [0,0,4,0], [0,2,0,0], [0,0,0,1]]
# Y = Variable(torch.DoubleTensor(np.array([[np.array(np.eye(5,4,-1)[i,:]) for i in row] for row in puzzle])).cuda())
# solution = sudoku(Y.unsqueeze(0))
# print(solution.view(1,4,4,4))
def forward(self, x):
nBatch = x.size(0)
x = F.max_pool2d(self.conv1(x), 2)
x = F.max_pool2d(self.conv2(x), 2)
x = x.view(nBatch, -1)
L = self.M*self.L
Q = L.mm(L.t()) + self.eps*Variable(torch.eye(self.nHidden)).cuda()
Q = Q.unsqueeze(0).expand(nBatch, self.nHidden, self.nHidden)
G = self.G.unsqueeze(0).expand(nBatch, self.nineq, self.nHidden)
z0 = self.qp_z0(x)
s0 = self.qp_s0(x)
h = z0.mm(self.G.t())+s0
e = Variable(torch.Tensor())
inputs = self.qp_o(x)
x = QPFunction()(Q, inputs, G, h, e, e)
x = x[:,:10]
return F.log_softmax(x)
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 eye(tensor):
"""Fills the 2-dimensional input Tensor or Variable with the identity matrix. Preserves the identity of the inputs in
Linear layers, where as many inputs are preserved as possible.
Args:
tensor: a 2-dimensional torch.Tensor or autograd.Variable
Examples:
>>> w = torch.Tensor(3, 5)
>>> nn.init.eye(w)
"""
if tensor.ndimension() != 2:
raise ValueError("Only tensors with 2 dimensions are supported")
if isinstance(tensor, Variable):
eye(tensor.data)
return tensor
return tensor.copy_(torch.eye(tensor.size(0), tensor.size(1)))
def test_inverse(self):
M = torch.randn(5, 5)
MI = torch.inverse(M)
E = torch.eye(5)
self.assertFalse(MI.is_contiguous(), 'MI is contiguous')
self.assertEqual(E, torch.mm(M, MI), 1e-8, 'inverse value')
self.assertEqual(E, torch.mm(MI, M), 1e-8, 'inverse value')
MII = torch.Tensor(5, 5)
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
# second call, now that MII is transposed
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
def reset_parameters(self):
"""
Initialize parameters following the way proposed in the paper.
"""
# The input-to-hidden weight matrix is initialized orthogonally.
init.orthogonal(self.weight_ih.data)
# The hidden-to-hidden weight matrix is initialized as an identity
# matrix.
weight_hh_data = torch.eye(self.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 4)
self.weight_hh.data.set_(weight_hh_data)
# The bias is just set to zero vectors.
init.constant(self.bias.data, val=0)
# Initialization of BN parameters.
self.bn_ih.reset_parameters()
self.bn_hh.reset_parameters()
self.bn_c.reset_parameters()
self.bn_ih.bias.data.fill_(0)
self.bn_hh.bias.data.fill_(0)
self.bn_ih.weight.data.fill_(0.1)
self.bn_hh.weight.data.fill_(0.1)
self.bn_c.weight.data.fill_(0.1)
def eye(tensor):
"""Fills the 2-dimensional input Tensor or Variable with the identity matrix. Preserves the identity of the inputs in
Linear layers, where as many inputs are preserved as possible.
Args:
tensor: a 2-dimensional torch.Tensor or autograd.Variable
Examples:
>>> w = torch.Tensor(3, 5)
>>> nn.init.eye(w)
"""
if tensor.ndimension() != 2:
raise ValueError("Only tensors with 2 dimensions are supported")
if isinstance(tensor, Variable):
eye(tensor.data)
return tensor
return tensor.copy_(torch.eye(tensor.size(0), tensor.size(1)))
def test_inverse(self):
M = torch.randn(5, 5)
MI = torch.inverse(M)
E = torch.eye(5)
self.assertFalse(MI.is_contiguous(), 'MI is contiguous')
self.assertEqual(E, torch.mm(M, MI), 1e-8, 'inverse value')
self.assertEqual(E, torch.mm(MI, M), 1e-8, 'inverse value')
MII = torch.Tensor(5, 5)
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
# second call, now that MII is transposed
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
def sample_entropy(samples):
# Assume B x C input
dist_mat = pairwise_euclidean(samples)
# Get max and add it to diag
m = dist_mat.max().detach()
dist_mat_d = dist_mat + \
Variable(torch.eye(dist_mat.size(0)) * (m.data[0] + 1)).cuda()
entropy = (dist_mat_d.min(1)[0] + 1e-4).log().sum()
entropy *= (samples.size(1) + 0.) / samples.size(0)
return entropy
def forward(self, L, z):
'''
:param L: batch_size (B) x latent_size^2 (L^2)
:param z: batch_size (B) x latent_size (L)
:return: z_new = L*z
'''
# L->tril(L)
L_matrix = L.view( -1, self.args.z1_size, self.args.z1_size ) # resize to get B x L x L
LTmask = torch.tril( torch.ones(self.args.z1_size, self.args.z1_size), k=-1 ) # lower-triangular mask matrix (1s in lower triangular part)
I = Variable( torch.eye(self.args.z1_size, self.args.z1_size).expand(L_matrix.size(0), self.args.z1_size, self.args.z1_size) )
if self.args.cuda:
LTmask = LTmask.cuda()
I = I.cuda()
LTmask = Variable(LTmask)
LTmask = LTmask.unsqueeze(0).expand( L_matrix.size(0), self.args.z1_size, self.args.z1_size ) # 1 x L x L -> B x L x L
LT = torch.mul( L_matrix, LTmask ) + I # here we get a batch of lower-triangular matrices with ones on diagonal
# z_new = L * z
z_new = torch.bmm( LT , z.unsqueeze(2) ).squeeze(2) # B x L x L * B x L x 1 -> B x L
return z_new
def test_np():
npr.seed(0)
nx, nineq, neq = 4, 6, 7
Q = npr.randn(nx, nx)
G = npr.randn(nineq, nx)
A = npr.randn(neq, nx)
D = np.diag(npr.rand(nineq))
K_ = np.bmat((
(Q, np.zeros((nx, nineq)), G.T, A.T),
(np.zeros((nineq, nx)), D, np.eye(nineq), np.zeros((nineq, neq))),
(G, np.eye(nineq), np.zeros((nineq, nineq + neq))),
(A, np.zeros((neq, nineq + nineq + neq)))
))
K = block((
(Q, 0, G.T, A.T),
(0, D, 'I', 0),
(G, 'I', 0, 0),
(A, 0, 0, 0)
))
assert np.allclose(K_, K)
def eye(tensor):
"""Fills the 2-dimensional input Tensor or Variable with the identity
matrix. Preserves the identity of the inputs in Linear layers, where as
many inputs are preserved as possible.
Args:
tensor: a 2-dimensional torch.Tensor or autograd.Variable
Examples:
>>> w = torch.Tensor(3, 5)
>>> nn.init.eye(w)
"""
if tensor.ndimension() != 2:
raise ValueError("Only tensors with 2 dimensions are supported")
if isinstance(tensor, Variable):
eye(tensor.data)
return tensor
return tensor.copy_(torch.eye(tensor.size(0), tensor.size(1)))
def test_inverse(self):
M = torch.randn(5, 5)
MI = torch.inverse(M)
E = torch.eye(5)
self.assertFalse(MI.is_contiguous(), 'MI is contiguous')
self.assertEqual(E, torch.mm(M, MI), 1e-8, 'inverse value')
self.assertEqual(E, torch.mm(MI, M), 1e-8, 'inverse value')
MII = torch.Tensor(5, 5)
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
# second call, now that MII is transposed
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
def test_computes_radial_basis_function_gradient():
a = torch.Tensor([4, 2, 8]).view(3, 1)
b = torch.Tensor([0, 2, 2]).view(3, 1)
lengthscale = 2
kernel = RBFKernel().initialize(log_lengthscale=math.log(lengthscale))
kernel.eval()
param = Variable(torch.Tensor(3, 3).fill_(math.log(lengthscale)), requires_grad=True)
diffs = Variable(a.expand(3, 3) - b.expand(3, 3).transpose(0, 1))
actual_output = (-(diffs ** 2) / param.exp()).exp()
actual_output.backward(torch.eye(3))
actual_param_grad = param.grad.data.sum()
output = kernel(Variable(a), Variable(b))
output.backward(gradient=torch.eye(3))
res = kernel.log_lengthscale.grad.data
assert(torch.norm(res - actual_param_grad) < 1e-5)
def eye(tensor):
"""Fills the 2-dimensional input Tensor or Variable with the identity
matrix. Preserves the identity of the inputs in Linear layers, where as
many inputs are preserved as possible.
Args:
tensor: a 2-dimensional torch.Tensor or autograd.Variable
Examples:
>>> w = torch.Tensor(3, 5)
>>> nn.init.eye(w)
"""
if tensor.ndimension() != 2:
raise ValueError("Only tensors with 2 dimensions are supported")
if isinstance(tensor, Variable):
eye(tensor.data)
return tensor
return tensor.copy_(torch.eye(tensor.size(0), tensor.size(1)))
def test_inverse(self):
M = torch.randn(5, 5)
MI = torch.inverse(M)
E = torch.eye(5)
self.assertFalse(MI.is_contiguous(), 'MI is contiguous')
self.assertEqual(E, torch.mm(M, MI), 1e-8, 'inverse value')
self.assertEqual(E, torch.mm(MI, M), 1e-8, 'inverse value')
MII = torch.Tensor(5, 5)
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
# second call, now that MII is transposed
torch.inverse(M, out=MII)
self.assertFalse(MII.is_contiguous(), 'MII is contiguous')
self.assertEqual(MII, MI, 0, 'inverse value in-place')
def forward(self, input):
laplacian = input.exp() + self.eps
output = input.clone()
for b in range(input.size(0)):
lap = laplacian[b].masked_fill(
Variable(torch.eye(input.size(1)).cuda().ne(0)), 0)
lap = -lap + torch.diag(lap.sum(0))
# store roots on diagonal
lap[0] = input[b].diag().exp()
inv_laplacian = lap.inverse()
factor = inv_laplacian.diag().unsqueeze(1)\
.expand_as(input[b]).transpose(0, 1)
term1 = input[b].exp().mul(factor).clone()
term2 = input[b].exp().mul(inv_laplacian.transpose(0, 1)).clone()
term1[:, 0] = 0
term2[0] = 0
output[b] = term1 - term2
roots_output = input[b].diag().exp().mul(
inv_laplacian.transpose(0, 1)[0])
output[b] = output[b] + torch.diag(roots_output)
return output
def test_eye(self):
res1 = torch.eye(100, 100)
res2 = torch.Tensor()
torch.eye(res2, 100, 100)
self.assertEqual(res1, res2)