我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.mul()。
def node_forward(self, inputs, child_c, child_h): child_h_sum = torch.sum(child_h, dim=0, keepdim=True) iou = self.ioux(inputs) + self.iouh(child_h_sum) i, o, u = torch.split(iou, iou.size(1) // 3, dim=1) i, o, u = F.sigmoid(i), F.sigmoid(o), F.tanh(u) f = F.sigmoid( self.fh(child_h) + self.fx(inputs).repeat(len(child_h), 1) ) fc = torch.mul(f, child_c) c = torch.mul(i, u) + torch.sum(fc, dim=0, keepdim=True) h = torch.mul(o, F.tanh(c)) return c, h
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 forward(self, H_j_dec, input_x): if torch.has_cudnn: # Input is of the shape : (B, T, N) input_x = Variable(torch.from_numpy(input_x[:, self._L:-self._L, :]).cuda(), requires_grad=True) else: # Input is of the shape : (B, T, N) # Cropping some "un-necessary" frequency sub-bands input_x = Variable(torch.from_numpy(input_x[:, self._L:-self._L, :]), requires_grad=True) # Decode/Sparsify mask mask_t1 = self.relu(self.ffDec(H_j_dec)) # Apply skip-filtering connections Y_j = torch.mul(mask_t1, input_x) return Y_j, mask_t1
def cosine_cont(repr_context, relevancy, norm=False): """ cosine siminlarity betwen context and relevancy Args: repr_context - [batch_size, other_len, context_lstm_dim] relevancy - [batch_size, this_len, other_len] Return: size - [batch_size, this_len, context_lstm_dim] """ dim = repr_context.dim() temp_relevancy = relevancy.unsqueeze(dim) # [batch_size, this_len, other_len, 1] buff = repr_context.unsqueeze(1) # [batch_size, 1, other_len, context_lstm_dim] buff = torch.mul(buff, temp_relevancy) # [batch_size, this_len, other_len, context_lstm_dim] buff = buff.sum(2) # [batch_size, this_len, context_lstm_dim] if norm: relevancy = relevancy.sum(dim-1).clamp(min=1e-6) # [batch_size, this_len] relevancy = relevancy.unsqueeze(2) # [batch_size, this_len, 1] buff = buff.div(relevancy) return buff
def build_loss(self, rpn_cls_score_reshape, rpn_bbox_pred, rpn_data): # classification loss rpn_cls_score = rpn_cls_score_reshape.permute(0, 2, 3, 1).contiguous().view(-1, 2) rpn_label = rpn_data[0].view(-1) rpn_keep = Variable(rpn_label.data.ne(-1).nonzero().squeeze()).cuda() rpn_cls_score = torch.index_select(rpn_cls_score, 0, rpn_keep) rpn_label = torch.index_select(rpn_label, 0, rpn_keep) fg_cnt = torch.sum(rpn_label.data.ne(0)) rpn_cross_entropy = F.cross_entropy(rpn_cls_score, rpn_label) # box loss rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = rpn_data[1:] rpn_bbox_targets = torch.mul(rpn_bbox_targets, rpn_bbox_inside_weights) rpn_bbox_pred = torch.mul(rpn_bbox_pred, rpn_bbox_inside_weights) rpn_loss_box = F.smooth_l1_loss(rpn_bbox_pred, rpn_bbox_targets, size_average=False) / (fg_cnt + 1e-4) return rpn_cross_entropy, rpn_loss_box
def forward(self, pos_u, pos_v, neg_u, neg_v): losses = [] emb_u = [] for i in range(len(pos_u)): emb_ui = self.u_embeddings(Variable(torch.LongTensor(pos_u[i]))) emb_u.append(np.sum(emb_ui.data.numpy(), axis=0).tolist()) emb_u = Variable(torch.FloatTensor(emb_u)) emb_v = self.v_embeddings(Variable(torch.LongTensor(pos_v))) score = torch.mul(emb_u, emb_v) score = torch.sum(score, dim=1) score = F.logsigmoid(score) losses.append(sum(score)) neg_emb_u = [] for i in range(len(neg_u)): neg_emb_ui = self.u_embeddings(Variable(torch.LongTensor(neg_u[i]))) neg_emb_u.append(np.sum(neg_emb_ui.data.numpy(), axis=0).tolist()) neg_emb_u = Variable(torch.FloatTensor(neg_emb_u)) neg_emb_v = self.v_embeddings(Variable(torch.LongTensor(neg_v))) neg_score = torch.mul(neg_emb_u, neg_emb_v) neg_score = torch.sum(neg_score, dim=1) neg_score = F.logsigmoid(-1 * neg_score) losses.append(sum(neg_score)) return -1 * sum(losses)
def forward(self, ctx): idx = np.arange(ctx.size(0) // 2) # extract counts and values cnt_idx = Variable(self.to_device(torch.from_numpy(2 * idx + 0))) val_idx = Variable(self.to_device(torch.from_numpy(2 * idx + 1))) cnt = ctx.index_select(0, cnt_idx) val = ctx.index_select(0, val_idx) # embed counts and values cnt_emb = self.cnt_enc(cnt) val_emb = self.val_enc(val) # element wise multiplication to get a hidden state h = torch.mul(cnt_emb, val_emb) # run the hidden state through the MLP h = h.transpose(0, 1).contiguous().view(ctx.size(1), -1) ctx_h = self.encoder(h).unsqueeze(0) return ctx_h
def forward(self, h, u, h_mask=None, u_mask=None): config = self.config if config.q2c_att or config.c2q_att: u_a, h_a = self.bi_attention(h, u, h_mask=h_mask, u_mask=u_mask) ''' u_a: [N, M, JX, d] h_a: [N, M, d] ''' else: print("AttentionLayer: q2c_att or c2q_att False not supported") if config.q2c_att: p0 = torch.cat([h, u_a, torch.mul(h, u_a), torch.mul(h, h_a)], 3) else: print("AttentionLayer: q2c_att False not supported") return p0
def forward(self, input_v, input_q): # visual (cnn features) if 'dim_v' in self.opt: x_v = F.dropout(input_v, p=self.opt['dropout_v'], training=self.training) x_v = self.linear_v(x_v) if 'activation_v' in self.opt: x_v = getattr(F, self.opt['activation_v'])(x_v) else: x_v = input_v # question (rnn features) if 'dim_q' in self.opt: x_q = F.dropout(input_q, p=self.opt['dropout_q'], training=self.training) x_q = self.linear_q(x_q) if 'activation_q' in self.opt: x_q = getattr(F, self.opt['activation_q'])(x_q) else: x_q = input_q # hadamard product x_mm = torch.mul(x_q, x_v) return x_mm
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 forward(self, x, hidden): h, c = hidden h = h.view(h.size(1), -1) c = c.view(c.size(1), -1) x = x.view(x.size(1), -1) # Linear mappings i_t = th.mm(x, self.w_xi) + th.mm(h, self.w_hi) + self.b_i f_t = th.mm(x, self.w_xf) + th.mm(h, self.w_hf) + self.b_f o_t = th.mm(x, self.w_xo) + th.mm(h, self.w_ho) + self.b_o # activations i_t.sigmoid_() f_t.sigmoid_() o_t.sigmoid_() # cell computations c_t = th.mm(x, self.w_xc) + th.mm(h, self.w_hc) + self.b_c c_t.tanh_() c_t = th.mul(c, f_t) + th.mul(i_t, c_t) h_t = th.mul(o_t, th.tanh(c_t)) # Reshape for compatibility h_t = h_t.view(1, h_t.size(0), -1) c_t = c_t.view(1, c_t.size(0), -1) if self.dropout > 0.0: F.dropout(h_t, p=self.dropout, training=self.training, inplace=True) return h_t, (h_t, c_t)
def forward(self, x): upblock = True # Downsizing layer - Large Kernel ensures large receptive field on the residual blocks h = F.relu(self.b2(self.c1(x))) # Residual Layers for r in self.rs: h = r(h) # will go through all residual blocks in this loop if upblock: # Upsampling Layers - improvement suggested by [2] to remove "checkerboard pattern" for u in self.up: h = u(h) # will go through all upsampling blocks in this loop else: # As recommended by [1] h = F.relu(self.bc2(self.dc2(h))) h = F.relu(self.bc3(self.dc3(h))) # Last layer and scaled tanh activation - Scaled from 0 to 1 instead of 0 - 255 h = F.tanh(self.c3(h)) h = torch.add(h, 1.) h = torch.mul(h, 0.5) return h
def forward(self, input1): self.batchgrid3d = torch.zeros(torch.Size([input1.size(0)]) + self.grid3d.size()) for i in range(input1.size(0)): self.batchgrid3d[i] = self.grid3d self.batchgrid3d = Variable(self.batchgrid3d) #print(self.batchgrid3d) x = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,0:4]), 3) y = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,4:8]), 3) z = torch.sum(torch.mul(self.batchgrid3d, input1[:,:,:,8:]), 3) #print(x) r = torch.sqrt(x**2 + y**2 + z**2) + 1e-5 #print(r) theta = torch.acos(z/r)/(np.pi/2) - 1 #phi = torch.atan(y/x) phi = torch.atan(y/(x + 1e-5)) + np.pi * x.lt(0).type(torch.FloatTensor) * (y.ge(0).type(torch.FloatTensor) - y.lt(0).type(torch.FloatTensor)) phi = phi/np.pi output = torch.cat([theta,phi], 3) return output
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 forward(self, obs, action): x = F.relu(self.conv1(obs)) x = F.relu(self.conv2(x)) x = F.relu(self.conv3(x)) x = F.relu(self.conv4(x)) x = x.view((-1, self.hidden_units)) x = F.relu(self.fc5(x)) x = self.fc_encode(x) action = self.fc_action(action) x = torch.mul(x, action) x = self.fc_decode(x) x = F.relu(self.fc8(x)) x = x.view((-1, 128, 11, 8)) x = F.relu(self.deconv9(x)) x = F.relu(self.deconv10(x)) x = F.relu(self.deconv11(x)) x = self.deconv12(x) return x
def forward(self, input_1, input_2): """ :param : input_1 Size is (*, hidden_size) :param input_2: Size is (*, hidden_size) :return: Merged vectors, size is (*, 4*hidden size) """ assert input_1.size(-1) == input_2.size(-1) mult_combined_vec = torch.mul(input_1, input_2) diff_combined_vec = torch.abs(input_1 - input_2) combined_vec = torch.cat((input_1, input_2, mult_combined_vec, diff_combined_vec), input_1.dim()-1) return combined_vec
def forward(self, input): if not self.aux_loss: return self.f(input) else: identity = torch.from_numpy(np.array([[1,0,0], [0,1,0]], dtype=np.float32)) batch_identity = torch.zeros([input.size(0), 2,3]) for i in range(input.size(0)): batch_identity[i] = identity if input.is_cuda: batch_identity = Variable(batch_identity.cuda()) else: batch_identity = Variable(batch_identity) loss = torch.mul(input - batch_identity, input - batch_identity) loss = torch.sum(loss,1) loss = torch.sum(loss,2) return self.f(input), loss.view(-1,1)
def EntropicConfusion(features): batch_size = features.size(0) return torch.mul(features, torch.log(features)).sum() * (1.0 / batch_size)
def g(self, tilde_z_l, u_l): if self.use_cuda: ones = Parameter(torch.ones(tilde_z_l.size()[0], 1).cuda()) else: ones = Parameter(torch.ones(tilde_z_l.size()[0], 1)) b_a1 = ones.mm(self.a1) b_a2 = ones.mm(self.a2) b_a3 = ones.mm(self.a3) b_a4 = ones.mm(self.a4) b_a5 = ones.mm(self.a5) b_a6 = ones.mm(self.a6) b_a7 = ones.mm(self.a7) b_a8 = ones.mm(self.a8) b_a9 = ones.mm(self.a9) b_a10 = ones.mm(self.a10) mu_l = torch.mul(b_a1, torch.sigmoid(torch.mul(b_a2, u_l) + b_a3)) + \ torch.mul(b_a4, u_l) + \ b_a5 v_l = torch.mul(b_a6, torch.sigmoid(torch.mul(b_a7, u_l) + b_a8)) + \ torch.mul(b_a9, u_l) + \ b_a10 hat_z_l = torch.mul(tilde_z_l - mu_l, v_l) + mu_l return hat_z_l
def bn_gamma_beta(self, x): if self.use_cuda: ones = Parameter(torch.ones(x.size()[0], 1).cuda()) else: ones = Parameter(torch.ones(x.size()[0], 1)) t = x + ones.mm(self.bn_beta) if self.train_bn_scaling: t = torch.mul(t, ones.mm(self.bn_gamma)) return t
def forward(self, lvec, rvec): mult_dist = torch.mul(lvec, rvec) abs_dist = torch.abs(torch.add(lvec, -rvec)) vec_dist = torch.cat((mult_dist, abs_dist), 1) out = F.sigmoid(self.wh(vec_dist)) out = F.log_softmax(self.wp(out)) return out # putting the whole model together
def backward(self, grad_output): v1, v2, y = self.saved_tensors buffer = v1.new() _idx = self._new_idx(v1) gw1 = grad_output.new() gw2 = grad_output.new() gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(buffer, self.w1, self.w22) gw1.addcmul_(-1, buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(buffer, self.w1, self.w32) gw2.addcmul_(-1, buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) torch.le(_idx, self._outputs, 0) _idx = _idx.view(-1, 1).expand(gw1.size()) gw1[_idx] = 0 gw2[_idx] = 0 torch.eq(_idx, y, 1) _idx = _idx.view(-1, 1).expand(gw2.size()) gw1[_idx] = gw1[_idx].mul_(-1) gw2[_idx] = gw2[_idx].mul_(-1) if self.size_average: gw1.div_(y.size(0)) gw2.div_(y.size(0)) if grad_output[0] != 1: gw1.mul_(grad_output) gw2.mul_(grad_output) return gw1, gw2, None
def backward(self, grad_output): input, target = self.saved_tensors grad_input = input.new().resize_as_(input).copy_(target) grad_input[torch.mul(torch.eq(target, -1), torch.gt(input, self.margin))] = 0 if self.size_average: grad_input.mul_(1. / input.nelement()) if grad_output[0] != 1: grad_input.mul_(grad_output[0]) return grad_input, None
def updateOutput(self, input, target): # - log(input) * target - log(1 - input) * (1 - target) if input.nelement() != target.nelement(): raise RuntimeError("input and target size mismatch") self.buffer = self.buffer or input.new() buffer = self.buffer weights = self.weights buffer.resize_as_(input) if weights is not None and target.dim() != 1: weights = self.weights.view(1, target.size(1)).expand_as(target) # log(input) * target torch.add(buffer, input, self.eps).log_() if weights is not None: buffer.mul_(weights) output = torch.dot(target, buffer) # log(1 - input) * (1 - target) torch.mul(buffer, input, -1).add_(1+self.eps).log_() if weights is not None: buffer.mul_(weights) output = output + torch.sum(buffer) output = output - torch.dot(target, buffer) if self.sizeAverage: output = output / input.nelement() self.output = - output return self.output
def updateOutput(self, input): input1, input2 = input[0], input[1] if not self.buffer: self.buffer = input1.new() torch.mul(self.buffer, input1, input2) torch.sum(self.output, self.buffer, 1) self.output.resize_(input1.size(0)) return self.output
def updateGradInput(self, input, y): v1 = input[0] v2 = input[1] gw1 = self.gradInput[0] gw2 = self.gradInput[1] gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(self.buffer, self.w1, self.w22) gw1.addcmul_(-1, self.buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(self.buffer, self.w1, self.w32) gw2.addcmul_(-1, self.buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) # self._idx = self._outputs <= 0 torch.le(self._idx, self._outputs, 0) self._idx = self._idx.view(-1, 1).expand(gw1.size()) gw1[self._idx] = 0 gw2[self._idx] = 0 torch.eq(self._idx, y, 1) self._idx = self._idx.view(-1, 1).expand(gw2.size()) gw1[self._idx] = gw1[self._idx].mul_(-1) gw2[self._idx] = gw2[self._idx].mul_(-1) if self.sizeAverage: gw1.div_(y.size(0)) gw2.div_(y.size(0)) return self.gradInput
def updateGradInput(self, input, gradOutput): if not self.gradInput: return self._div = self._div or input.new() self._output = self._output or self.output.new() self._gradOutput = self._gradOutput or input.new() self._expand3 = self._expand3 or input.new() if not self.fastBackward: self.updateOutput(input) inputSize, outputSize = self.weight.size(0), self.weight.size(1) """ dy_j -2 * (w_j - x) x - w_j ---- = ---------------- = ------- dx 2 || w_j - x || y_j """ # to prevent div by zero (NaN) bugs self._output.resize_as_(self.output).copy_(self.output).add_(0.0000001) self._view(self._gradOutput, gradOutput, gradOutput.size()) torch.div(self._div, gradOutput, self._output) assert input.dim() == 2 batchSize = input.size(0) self._div.resize_(batchSize, 1, outputSize) self._expand3 = self._div.expand(batchSize, inputSize, outputSize) if torch.typename(input) == 'torch.cuda.FloatTensor': self._repeat2.resize_as_(self._expand3).copy_(self._expand3) self._repeat2.mul_(self._repeat) else: torch.mul(self._repeat2, self._repeat, self._expand3) torch.sum(self.gradInput, self._repeat2, 2) self.gradInput.resize_as_(input) return self.gradInput
def updateGradInput(self, input, y): self.gradInput.resize_as_(input).copy_(y) self.gradInput[torch.mul(torch.eq(y, -1), torch.gt(input, self.margin))] = 0 if self.sizeAverage: self.gradInput.mul_(1. / input.nelement()) return self.gradInput
def updateGradInput(self, input, gradOutput): v1 = input[0] v2 = input[1] v1, v2 = self._makeContiguous(v1, v2) if len(self.gradInput) != 2: self.gradInput[0] = self.gradInput[0] or v1.new() self.gradInput[1] = self.gradInput[1] or v1.new() self.gradInput = self.gradInput[:2] gw1 = self.gradInput[0] gw2 = self.gradInput[1] gw1.resize_as_(v1).copy_(v2) gw2.resize_as_(v1).copy_(v1) torch.mul(self.buffer, self.w1, self.w22) gw1.addcmul_(-1, self.buffer.expand_as(v1), v1) gw1.mul_(self.w.expand_as(v1)) torch.mul(self.buffer, self.w1, self.w32) gw2.addcmul_(-1, self.buffer.expand_as(v1), v2) gw2.mul_(self.w.expand_as(v1)) go = gradOutput.view(-1, 1).expand_as(v1) gw1.mul_(go) gw2.mul_(go) return self.gradInput
def updateGradInput(self, input, gradOutput): return torch.mul(self.gradInput, self.output, gradOutput)
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)
def test_cmul(self): self._test_cop(torch.mul, lambda x, y: x * y)
def test_masked_select(self): num_src = 10 src = torch.randn(num_src) mask = torch.rand(num_src).clamp(0, 1).mul(2).floor().byte() dst = src.masked_select(mask) dst2 = [] for i in range(num_src): if mask[i]: dst2 += [src[i]] self.assertEqual(dst, torch.Tensor(dst2), 0)
def forward(self, pos_u, pos_v, neg_u, neg_v): losses = [] emb_u = self.u_embeddings(Variable(torch.LongTensor(pos_u))) emb_v = self.v_embeddings(Variable(torch.LongTensor(pos_v))) score = torch.mul(emb_u, emb_v) score = torch.sum(score, dim=1) score = F.logsigmoid(score) losses.append(sum(score)) neg_emb_u = self.u_embeddings(Variable(torch.LongTensor(neg_u))) neg_emb_v = self.v_embeddings(Variable(torch.LongTensor(neg_v))) neg_score = torch.mul(neg_emb_u, neg_emb_v) neg_score = torch.sum(neg_score, dim=1) neg_score = F.logsigmoid(-1 * neg_score) losses.append(sum(neg_score)) return -1 * sum(losses)
def forwards(self, pos_u, pos_v, neg_u, neg_v): losses = [] emb_v = [] for i in range(len(pos_v)): emb_v_v = self.u_embeddings(Variable(torch.LongTensor(pos_v[i]))) emb_v_v_numpy = emb_v_v.data.numpy() emb_v_v_numpy = np.sum(emb_v_v_numpy, axis=0) emb_v_v_list = emb_v_v_numpy.tolist() emb_v.append(emb_v_v_list) emb_v = Variable(torch.FloatTensor(emb_v)) emb_u = self.v_embeddings(Variable(torch.LongTensor(pos_u))) score = torch.mul(emb_u, emb_v) score = torch.sum(score, dim=1) score = F.logsigmoid(score) losses.append(sum(score)) neg_emb_v = [] for i in range(len(neg_v)): neg_emb_v_v = self.u_embeddings(Variable(torch.LongTensor(neg_v[i]))) neg_emb_v_v_numpy = neg_emb_v_v.data.numpy() neg_emb_v_v_numpy = np.sum(neg_emb_v_v_numpy, axis=0) neg_emb_v_v_list = neg_emb_v_v_numpy.tolist() neg_emb_v.append(neg_emb_v_v_list) neg_emb_v = Variable(torch.FloatTensor(neg_emb_v)) neg_emb_u = self.v_embeddings(Variable(torch.LongTensor(neg_u))) neg_score = torch.mul(neg_emb_u, neg_emb_v) neg_score = torch.sum(neg_score, dim=1) neg_score = F.logsigmoid(-1 * neg_score) losses.append(sum(neg_score)) return -1 * sum(losses)
def forward(self, pos_u, pos_v, neg_u, neg_v): losses = [] emb_v = [] for i in range(len(pos_v)): emb_v_v = self.u_embeddings(Variable(torch.LongTensor(pos_v[i]))) emb_v_v_numpy = emb_v_v.data.numpy() emb_v_v_numpy = np.sum(emb_v_v_numpy, axis=0) emb_v_v_list = emb_v_v_numpy.tolist() emb_v.append(emb_v_v_list) emb_v = Variable(torch.FloatTensor(emb_v)) emb_u = self.v_embeddings(Variable(torch.LongTensor(pos_u))) score = torch.mul(emb_u, emb_v) score = torch.sum(score, dim=1) score = F.logsigmoid(score) losses.append(sum(score)) neg_emb_v = [] for i in range(len(neg_v)): neg_emb_v_v = self.u_embeddings(Variable(torch.LongTensor(neg_v[i]))) neg_emb_v_v_numpy = neg_emb_v_v.data.numpy() neg_emb_v_v_numpy = np.sum(neg_emb_v_v_numpy, axis=0) neg_emb_v_v_list = neg_emb_v_v_numpy.tolist() neg_emb_v.append(neg_emb_v_v_list) neg_emb_v = Variable(torch.FloatTensor(neg_emb_v)) neg_emb_u = self.v_embeddings(Variable(torch.LongTensor(neg_u))) neg_score = torch.mul(neg_emb_u, neg_emb_v) neg_score = torch.sum(neg_score, dim=1) neg_score = F.logsigmoid(-1 * neg_score) losses.append(sum(neg_score)) return -1 * sum(losses)
def forward(self, Y_hat): # Enhance Source mask_enc_hl = self.relu(self.ffSe_enc(Y_hat)) mask_t2 = self.relu(self.ffSe_dec(mask_enc_hl)) # Apply skip-filtering connections Y_hat_filt = torch.mul(mask_t2, Y_hat) return Y_hat_filt # EOF
def forward(self, x): gate = F.sigmoid(self.gate(x)) return torch.mul(self.active(self.h(x)), gate) + torch.mul(x, (1 - gate))
def _cosine_similarity_mask(self, simi): # [bsz, a_len, q_len] simi = torch.mul(simi, self.q_mask.unsqueeze(1).float()).clamp(min=eps) simi = torch.mul(simi, self.a_mask.unsqueeze(2).float()).clamp(min=eps) return simi
def multi_perspective_expand_for_2D(in_tensor, decompose_params): """ Return: [batch_size, decompse_dim, dim] """ in_tensor = in_tensor.unsqueeze(1) #[batch_size, 'x', dim] decompose_params = decompose_params.unsqueeze(0) # [1, decompse_dim, dim] return torch.mul(in_tensor, decompose_params)
def forward(self, cont_repres, other_cont_first): """ Args: cont_repres - [batch_size, this_len, context_lstm_dim] other_cont_first - [batch_size, context_lstm_dim] Return: size - [batch_size, this_len, mp_dim] """ def expand(context, weight): """ Args: [batch_size, this_len, context_lstm_dim] [mp_dim, context_lstm_dim] Return: [batch_size, this_len, mp_dim, context_lstm_dim] """ # [1, 1, mp_dim, context_lstm_dim] weight = weight.unsqueeze(0) weight = weight.unsqueeze(0) # [batch_size, this_len, 1, context_lstm_dim] context = context.unsqueeze(2) return torch.mul(context, weight) cont_repres = expand(cont_repres, self.weight) other_cont_first = multi_perspective_expand_for_2D(other_cont_first, self.weight) # [batch_size, 1, mp_dim, context_lstm_dim] other_cont_first = other_cont_first.unsqueeze(1) return cosine_similarity(cont_repres, other_cont_first, cont_repres.dim()-1)
def setUp(self): n = 1 self.ps = Variable(torch.Tensor([0.1, 0.6, 0.3])) self.batch_ps = Variable(torch.Tensor([[0.1, 0.6, 0.3], [0.2, 0.4, 0.4]])) self.n = Variable(torch.Tensor([n])) self.test_data = Variable(torch.Tensor([0, 1, 0])) self.test_data_nhot = Variable(torch.Tensor([2])) self.analytic_mean = n * self.ps one = Variable(torch.ones(3)) self.analytic_var = n * torch.mul(self.ps, one.sub(self.ps)) # Discrete Distribution self.d_ps = Variable(torch.Tensor([[0.2, 0.3, 0.5], [0.1, 0.1, 0.8]])) self.d_vs = Variable(torch.Tensor([[0, 1, 2], [3, 4, 5]])) self.d_vs_arr = [['a', 'b', 'c'], ['d', 'e', 'f']] self.d_vs_tup = (('a', 'b', 'c'), ('d', 'e', 'f')) self.d_test_data = Variable(torch.Tensor([[0], [5]])) self.d_v_test_data = [['a'], ['f']] self.n_samples = 50000 self.support_one_hot_non_vec = torch.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) self.support_one_hot = torch.Tensor([[[1, 0, 0], [1, 0, 0]], [[0, 1, 0], [0, 1, 0]], [[0, 0, 1], [0, 0, 1]]]) self.support_non_vec = torch.LongTensor([[0], [1], [2]]) self.support = torch.LongTensor([[[0], [0]], [[1], [1]], [[2], [2]]]) self.discrete_support_non_vec = torch.Tensor([[0], [1], [2]]) self.discrete_support = torch.Tensor([[[0], [3]], [[1], [4]], [[2], [5]]]) self.discrete_arr_support_non_vec = [['a'], ['b'], ['c']] self.discrete_arr_support = [[['a'], ['d']], [['b'], ['e']], [['c'], ['f']]]
def sample(self): """ Ref: :py:meth:`pyro.distributions.distribution.Distribution.sample` """ eps = Variable(torch.rand(self.a.size()).type_as(self.a.data)) return self.a + torch.mul(eps, self.b - self.a)
def batch_log_pdf(self, x): """ Ref: :py:meth:`pyro.distributions.distribution.Distribution.batch_log_pdf` """ a = self.a.expand(self.shape(x)) b = self.b.expand(self.shape(x)) lb = x.ge(a).type_as(a) ub = x.le(b).type_as(b) batch_log_pdf_shape = self.batch_shape(x) + (1,) return torch.sum(torch.log(lb.mul(ub)) - torch.log(b - a), -1).contiguous().view(batch_log_pdf_shape)
def forward(self, x): # print("source x {} ".format(x.size())) x = self.embed(x) # (N,W,D) x = self.dropout(x) x = x.unsqueeze(1) # (N,Ci,W,D) if self.args.batch_normalizations is True: x = [self.convs1_bn(F.tanh(conv(x))).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks) x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] #[(N,Co), ...]*len(Ks) else: # x = [self.dropout(F.relu(conv(x)).squeeze(3)) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks) x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks) # x = [F.tanh(conv(x)).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks) # x = [conv(x).squeeze(3) for conv in self.convs1] #[(N,Co,W), ...]*len(Ks) x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] #[(N,Co), ...]*len(Ks) x = torch.cat(x, 1) # x = self.dropout(x) # (N,len(Ks)*Co) if self.args.batch_normalizations is True: x = self.fc1_bn(self.fc1(x)) fc = self.fc2_bn(self.fc2(F.tanh(x))) else: fc = self.fc1(x) # fc = self.fc2(F.relu(x)) # print("xxx {} ".format(x.size())) gate_layer = F.sigmoid(self.gate_layer(x)) # calculate highway layer values # print(" fc_size {} gate_layer_size {}".format(fc.size(), gate_layer.size())) gate_fc_layer = torch.mul(fc, gate_layer) # print("gate_layer {} ".format(gate_layer)) # print("1 - gate_layer size {} ".format((1 - gate_layer).size())) # if write like follow ,can run,but not equal the HighWay NetWorks formula # gate_input = torch.mul((1 - gate_layer), fc) gate_input = torch.mul((1 - gate_layer), x) highway_output = torch.add(gate_fc_layer, gate_input) logit = self.logit_layer(highway_output) return logit