我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用torch.addmm()。
def test_mm(self): def test_shape(di, dj, dk): x, _, _ = self._gen_sparse(2, 20, [di, dj]) t = torch.randn(di, dk) y = torch.randn(dj, dk) alpha = random.random() beta = random.random() res = torch.addmm(alpha, t, beta, x, y) expected = torch.addmm(alpha, t, beta, x.to_dense(), y) self.assertEqual(res, expected) res = torch.addmm(t, x, y) expected = torch.addmm(t, x.to_dense(), y) self.assertEqual(res, expected) res = torch.mm(x, y) expected = torch.mm(x.to_dense(), y) self.assertEqual(res, expected) test_shape(10, 100, 100) test_shape(100, 1000, 200) test_shape(64, 10000, 300)
def test_saddmm(self): def test_shape(di, dj, dk): x = self._gen_sparse(2, 20, [di, dj])[0] t = self._gen_sparse(2, 20, [di, dk])[0] y = torch.randn(dj, dk) alpha = random.random() beta = random.random() res = torch.saddmm(alpha, t, beta, x, y) expected = torch.addmm(alpha, t.to_dense(), beta, x.to_dense(), y) self.assertEqual(res.to_dense(), expected) res = torch.saddmm(t, x, y) expected = torch.addmm(t.to_dense(), x.to_dense(), y) self.assertEqual(res.to_dense(), expected) res = torch.smm(x, y) expected = torch.mm(x.to_dense(), y) self.assertEqual(res.to_dense(), expected) test_shape(7, 5, 3) test_shape(1000, 100, 100) test_shape(3000, 64, 300)
def linear(input, weight, bias=None): """ Applies a linear transformation to the incoming data: :math:`y = xA^T + b`. Shape: - Input: :math:`(N, *, in\_features)` where `*` means any number of additional dimensions - Weight: :math:`(out\_features, in\_features)` - Bias: :math:`(out\_features)` - Output: :math:`(N, *, out\_features)` """ if input.dim() == 2 and bias is not None: # fused op is marginally faster return torch.addmm(bias, input, weight.t()) output = input.matmul(weight.t()) if bias is not None: output += bias return output
def test_mm(self): def test_shape(di, dj, dk): x, _, _ = self._gen_sparse(2, 20, [di, dj]) t = torch.randn(di, dk) y = torch.randn(dj, dk) alpha = random.random() beta = random.random() res = torch.addmm(alpha, t, beta, x, y) expected = torch.addmm(alpha, t, beta, self.safeToDense(x), y) self.assertEqual(res, expected) res = torch.addmm(t, x, y) expected = torch.addmm(t, self.safeToDense(x), y) self.assertEqual(res, expected) res = torch.mm(x, y) expected = torch.mm(self.safeToDense(x), y) self.assertEqual(res, expected) test_shape(10, 100, 100) test_shape(100, 1000, 200) test_shape(64, 10000, 300)
def test_saddmm(self): def test_shape(di, dj, dk): x = self._gen_sparse(2, 20, [di, dj])[0] t = self._gen_sparse(2, 20, [di, dk])[0] y = torch.randn(dj, dk) alpha = random.random() beta = random.random() res = torch.saddmm(alpha, t, beta, x, y) expected = torch.addmm(alpha, self.safeToDense(t), beta, self.safeToDense(x), y) self.assertEqual(self.safeToDense(res), expected) res = torch.saddmm(t, x, y) expected = torch.addmm(self.safeToDense(t), self.safeToDense(x), y) self.assertEqual(self.safeToDense(res), expected) res = torch.smm(x, y) expected = torch.mm(self.safeToDense(x), y) self.assertEqual(self.safeToDense(res), expected) test_shape(7, 5, 3) test_shape(1000, 100, 100) test_shape(3000, 64, 300)
def forward(self, add_matrix, matrix1, matrix2): self.save_for_backward(matrix1, matrix2) output = self._get_output(add_matrix) return torch.addmm(output, self.alpha, add_matrix, self.beta, matrix1, matrix2)
def forward(self, input_, hx): """ Args: input_: A (batch, input_size) tensor containing input features. hx: A tuple (h_0, c_0), which contains the initial hidden and cell state, where the size of both states is (batch, hidden_size). Returns: h_1, c_1: Tensors containing the next hidden and cell state. """ h_0, c_0 = hx batch_size = h_0.size(0) bias_batch = (self.bias.unsqueeze(0) .expand(batch_size, *self.bias.size())) wh_b = torch.addmm(bias_batch, h_0, self.weight_hh) wi = torch.mm(input_, self.weight_ih) f, i, o, g = torch.split(wh_b + wi, split_size=self.hidden_size, dim=1) c_1 = torch.sigmoid(f)*c_0 + torch.sigmoid(i)*torch.tanh(g) h_1 = torch.sigmoid(o) * torch.tanh(c_1) return h_1, c_1
def forward(ctx, add_matrix, matrix1, matrix2, alpha=1, beta=1, inplace=False): ctx.alpha = alpha ctx.beta = beta ctx.save_for_backward(matrix1, matrix2) output = _get_output(ctx, add_matrix, inplace=inplace) return torch.addmm(alpha, add_matrix, beta, matrix1, matrix2, out=output)
def test_functional_blas(self): def compare(fn, *args): unpacked_args = tuple(arg.data if isinstance(arg, Variable) else arg for arg in args) self.assertEqual(fn(*args).data, fn(*unpacked_args)) def test_blas_add(fn, x, y, z): # Checks all signatures compare(fn, x, y, z) compare(fn, 0.5, x, y, z) compare(fn, 0.5, x, 0.25, y, z) def test_blas(fn, x, y): compare(fn, x, y) test_blas(torch.mm, Variable(torch.randn(2, 10)), Variable(torch.randn(10, 4))) test_blas_add(torch.addmm, Variable(torch.randn(2, 4)), Variable(torch.randn(2, 10)), Variable(torch.randn(10, 4))) test_blas(torch.bmm, Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas_add(torch.addbmm, Variable(torch.randn(2, 4)), Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas_add(torch.baddbmm, Variable(torch.randn(4, 2, 4)), Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas(torch.mv, Variable(torch.randn(2, 10)), Variable(torch.randn(10))) test_blas_add(torch.addmv, Variable(torch.randn(2)), Variable(torch.randn(2, 10)), Variable(torch.randn(10))) test_blas(torch.ger, Variable(torch.randn(5)), Variable(torch.randn(6))) test_blas_add(torch.addr, Variable(torch.randn(5, 6)), Variable(torch.randn(5)), Variable(torch.randn(6)))
def affine_nd(input, weight, bias): """ An helper function to make applying the "wx + b" operation for n-dimensional x easier. Args: input (Variable): An arbitrary input data, whose size is (d0, d1, ..., dn, input_dim) weight (Variable): A matrix of size (output_dim, input_dim) bias (Variable): A bias vector of size (output_dim,) Returns: output: The result of size (d0, ..., dn, output_dim) """ input_size = input.size() input_flat = input.view(-1, input_size[-1]) bias_expand = bias.unsqueeze(0).expand(input_flat.size(0), bias.size(0)) output_flat = torch.addmm(bias_expand, input_flat, weight) output_size = input_size[:-1] + (weight.size(1),) output = output_flat.view(*output_size) return output
def forward(ctx, add_matrix, matrix1, matrix2, alpha=1, beta=1, inplace=False): ctx.alpha = alpha ctx.beta = beta ctx.add_matrix_size = add_matrix.size() ctx.save_for_backward(matrix1, matrix2) output = _get_output(ctx, add_matrix, inplace=inplace) return torch.addmm(alpha, add_matrix, beta, matrix1, matrix2, out=output)
def forward(self, input_, hx): """ Args: input_: A (batch, input_size) tensor containing input features. hx: initial hidden, where the size of the state is (batch, hidden_size). Returns: newh: Tensors containing the next hidden state. """ batch_size = hx.size(0) bias_batch = (self.gate_bias.unsqueeze(0) .expand(batch_size, *self.gate_bias.size())) gate_Wh = torch.addmm(bias_batch, hx, self.gate_W) gate_Ux = torch.mm(input_, self.gate_U) r, z = torch.split(gate_Ux + gate_Wh, split_size=self.hidden_size, dim=1) Ux = torch.mm(input_, self.U) unitary = self._EUNN(hx=hx, thetaA=self.thetaA, thetaB=self.thetaB) unitary = unitary * r newh = Ux + unitary newh = self._modReLU(newh, self.bias) newh = hx * z + (1-z) * newh return newh
def test_addmm(self): types = { 'torch.DoubleTensor': 1e-8, 'torch.FloatTensor': 1e-4, } for tname, _prec in types.items(): M = torch.randn(10, 25).type(tname) m1 = torch.randn(10, 50).type(tname) m2 = torch.randn(50, 25).type(tname) res1 = torch.addmm(M, m1, m2) res2 = torch.zeros(10, 25).type(tname) res2 += M for i in range(10): for j in range(25): for k in range(50): res2[i, j] += m1[i, k] * m2[k, j] self.assertEqual(res1, res2) # Test 0-strided for tname, _prec in types.items(): M = torch.randn(10, 1).type(tname).expand(10, 25) m1 = torch.randn(10, 1).type(tname).expand(10, 50) m2 = torch.randn(50, 25).type(tname) res1 = torch.addmm(M, m1, m2) res2 = torch.zeros(10, 25).type(tname) res2 += M for i in range(10): for j in range(25): for k in range(50): res2[i, j] += m1[i, k] * m2[k, j] self.assertEqual(res1, res2)
def test_functional_blas(self): def compare(fn, *args): unpacked_args = tuple(arg.data if isinstance(arg, Variable) else arg for arg in args) unpacked_result = fn(*unpacked_args) packed_result = fn(*args).data # if non-Variable torch function returns a scalar, compare to scalar if not torch.is_tensor(unpacked_result): assert packed_result.dim() == 1 assert packed_result.nelement() == 1 packed_result = packed_result[0] self.assertEqual(packed_result, unpacked_result) def test_blas_add(fn, x, y, z): # Checks all signatures compare(fn, x, y, z) compare(fn, 0.5, x, y, z) compare(fn, 0.5, x, 0.25, y, z) def test_blas(fn, x, y): compare(fn, x, y) test_blas(torch.mm, Variable(torch.randn(2, 10)), Variable(torch.randn(10, 4))) test_blas_add(torch.addmm, Variable(torch.randn(2, 4)), Variable(torch.randn(2, 10)), Variable(torch.randn(10, 4))) test_blas(torch.bmm, Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas_add(torch.addbmm, Variable(torch.randn(2, 4)), Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas_add(torch.baddbmm, Variable(torch.randn(4, 2, 4)), Variable(torch.randn(4, 2, 10)), Variable(torch.randn(4, 10, 4))) test_blas(torch.mv, Variable(torch.randn(2, 10)), Variable(torch.randn(10))) test_blas_add(torch.addmv, Variable(torch.randn(2)), Variable(torch.randn(2, 10)), Variable(torch.randn(10))) test_blas(torch.ger, Variable(torch.randn(5)), Variable(torch.randn(6))) test_blas_add(torch.addr, Variable(torch.randn(5, 6)), Variable(torch.randn(5)), Variable(torch.randn(6))) test_blas(torch.matmul, Variable(torch.randn(6)), Variable(torch.randn(6))) test_blas(torch.matmul, Variable(torch.randn(10, 4)), Variable(torch.randn(4))) test_blas(torch.matmul, Variable(torch.randn(5)), Variable(torch.randn(5, 6))) test_blas(torch.matmul, Variable(torch.randn(2, 10)), Variable(torch.randn(10, 4))) test_blas(torch.matmul, Variable(torch.randn(5, 2, 10)), Variable(torch.randn(5, 10, 4))) test_blas(torch.matmul, Variable(torch.randn(3, 5, 2, 10)), Variable(torch.randn(3, 5, 10, 4))) test_blas(torch.matmul, Variable(torch.randn(3, 5, 2, 10)), Variable(torch.randn(10))) test_blas(torch.matmul, Variable(torch.randn(10)), Variable(torch.randn(3, 5, 10, 4)))