How to use the torch.nn.functional function in torch

To help you get started, we’ve selected a few torch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pokbe / SequenceTagging / LstmModel.py View on Github external
def forward(self, sentence):
        embeds = self.word_embeddings(sentence)
        lstm_out, self.hidden = self.lstm(embeds.view(len(sentence), 1, -1))
        tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
        tag_scores = F.log_softmax(tag_space)
        return tag_scores
github ZhixiuYe / HSCRF-pytorch / model / highway_layer.py View on Github external
"""
        update statics for f1 score

        args: 
            x (ins_num, hidden_dim): input tensor

        """
        
        t = self.gate[0](x)
        g = nn.functional.sigmoid(t)
        h = nn.functional.relu(self.trans[0](x))
        x = g * h + (1 - g) * x

        for i in range(1, self.num_layers):
            x = self.dropout(x)
            g = nn.functional.sigmoid(self.gate[i](x))
            h = nn.functional.relu(self.trans[i](x))
            x = g * h + (1 - g) * x

        return x
github MaestroGraph / sparse-hyper / experiments / bias.py View on Github external
# Train for a fixed nr of instances (with the true gradient)
        for e in range(arg.epochs):
            print('epoch', e)

            for i, (inputs, _) in enumerate(trainloader):

                b, c, h, w = inputs.size()

                if arg.cuda:
                    inputs = inputs.cuda()

                # compute actual gradient
                opt.zero_grad()

                latent = encoder(inputs)
                latent = F.softmax(latent, dim=1)

                dinp = torch.eye(l, device=d(arg.cuda))[None, :, :].expand(b, l, l).reshape(b*l, l)
                dout = decoder(dinp)

                assert dout.size() == (b*l, c, h, w)

                target = inputs.detach()[:, None, :, :, :].expand(b, l, c, h, w).reshape(b*l, c, h, w)

                loss = F.binary_cross_entropy(dout, target, reduction='none')
                loss = loss.sum(dim=1).sum(dim=1).sum(dim=1).view(b, l)

                loss = (loss * latent).sum(dim=1).mean()

                loss.backward()

                true_gradient = gradient([encoder, decoder])
github zuoxingdong / lagom / baselines / ddpg / logs / default / source_files / agent.py View on Github external
def learn(self, D, **kwargs):
        replay = kwargs['replay']
        episode_length = kwargs['episode_length']
        out = {}
        out['actor_loss'] = []
        out['critic_loss'] = []
        Q_vals = []
        for i in range(episode_length):
            observations, actions, rewards, next_observations, masks = replay.sample(self.config['replay.batch_size'])
            
            Qs = self.critic(observations, actions)
            with torch.no_grad():
                next_Qs = self.critic_target(next_observations, self.actor_target(next_observations))
                targets = rewards + self.config['agent.gamma']*masks*next_Qs
            critic_loss = F.mse_loss(Qs, targets.detach())
            self.actor_optimizer.zero_grad()
            self.critic_optimizer.zero_grad()
            critic_loss.backward()
            critic_grad_norm = nn.utils.clip_grad_norm_(self.critic.parameters(), self.config['agent.max_grad_norm'])
            self.critic_optimizer.step()
            
            actor_loss = -self.critic(observations, self.actor(observations)).mean()
            self.actor_optimizer.zero_grad()
            self.critic_optimizer.zero_grad()
            actor_loss.backward()
            actor_grad_norm = nn.utils.clip_grad_norm_(self.actor.parameters(), self.config['agent.max_grad_norm'])
            self.actor_optimizer.step()
            
            self.polyak_update_target()
            
            out['actor_loss'].append(actor_loss)
github reinforcement-learning-kr / alpha_omok / 2-omok / 3-alphazero / local_version / main.py View on Github external
logging.warning('=' * 58)
    logging.warning('current memory size: {}'.format(len(cur_memory)))
    logging.warning('augment memory size: {}'.format(len(cur_augment)))
    logging.warning('replay memory size: {}'.format(len(rep_memory)))
    logging.warning('train memory size: {}'.format(len(train_memory)))
    logging.warning('optimizer: {}'.format(optimizer))

    for epoch in range(n_epochs):
        for i, (s, pi, z) in enumerate(dataloader):
            s_batch = s.to(device).float()
            pi_batch = pi.to(device).float()
            z_batch = z.to(device).float()

            p_batch, v_batch = Agent.model(s_batch)

            v_loss = F.mse_loss(v_batch, z_batch)
            p_loss = -(pi_batch * (p_batch + 1e-8).log()).sum(dim=1).mean()
            loss = v_loss + p_loss

            loss_v.append(v_loss.item())
            loss_p.append(p_loss.item())
            loss_all.append(loss.item())

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            step += 1

            # tensorboad & print loss
            if USE_TENSORBOARD:
                Writer.add_scalar('Loss', loss.item(), step)
github AllenInstitute / aics-ml-segmentation / aicsmlsegment / Net3D / uNet_original.py View on Github external
self.ec3 = self.encoder(128, 128, batchnorm=batchnorm_flag)
        self.ec4 = self.encoder(256, 256, batchnorm=batchnorm_flag)

        self.pool1 = nn.MaxPool3d(2)
        self.pool2 = nn.MaxPool3d(2)
        self.pool3 = nn.MaxPool3d(2)

        self.up3 = nn.ConvTranspose3d(512, 512, kernel_size=2, stride=2, padding=0, output_padding=0, bias=True)
        self.dc3 = self.decoder(256 + 512, 256, batchnorm=batchnorm_flag)
        self.up2 = nn.ConvTranspose3d(256, 256, kernel_size=2, stride=2, padding=0, output_padding=0, bias=True)
        self.dc2 = self.decoder(128 + 256, 128, batchnorm=batchnorm_flag)
        self.up1 = nn.ConvTranspose3d(128, 128, kernel_size=2, stride=2, padding=0, output_padding=0, bias=True)
        self.dc1 = self.decoder(64 + 128, 64, batchnorm=batchnorm_flag)

        self.dc0 = nn.Conv3d(64, n_classes, 1)
        self.softmax = F.log_softmax

        self.numClass = n_classes
github huaweicloud / dls-example / moxing_pytorch_mnist / mnist_standalone.py View on Github external
def forward(self, x):
    x = F.relu(F.max_pool2d(self.conv1(x), 2))
    x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
    x = x.view(-1, 320)
    x = F.relu(self.fc1(x))
    x = F.dropout(x, training=self.training)
    x = self.fc2(x)
    return F.log_softmax(x, dim=1)
github tribhuvanesh / knockoffnets / knockoff / models / cifar / wrn.py View on Github external
def forward(self, x):
        out = self.conv1(x)
        out = self.block1(out)
        out = self.block2(out)
        out = self.block3(out)
        out = self.relu(self.bn1(out))
        out = F.avg_pool2d(out, 8)
        out = out.view(-1, self.nChannels)
        return self.fc(out)
github outcastofmusic / quick-nlp / src / quicknlp / data / learners.py View on Github external
def cvae_loss_tchebycheff(input, target, step=0, **kwargs):
        predictions, recog_mu, recog_log_var, prior_mu, prior_log_var, bow_logits = input
        sl, bs, vocab = predictions.size()
        # dims are sq-1 times bs times vocab
        dec_input = predictions[:target.size(0)].view(-1, vocab).contiguous()
        slt = target.size(0)
        bow_targets = bow_logits.unsqueeze_(0).repeat(slt, 1, 1)
        target = target.view(-1).contiguous()
        bow_loss = F.cross_entropy(input=bow_targets.view(-1, vocab), target=target, ignore_index=pad_idx,
                                   reduce=False).view(-1, bs)
        bow_loss = bow_loss.mean()
        # targets are sq-1 times bs (one label for every word)
        kld_loss = gaussian_kld(recog_mu, recog_log_var, prior_mu, prior_log_var)
        decoder_loss = F.cross_entropy(input=dec_input,
                                       target=target,
                                       ignore_index=pad_idx,
                                       )
        # kld_weight = 1.0 if max_kld_step is None else min((step + 1) / max_kld_step, 1)
        nonlocal STEP
        if step > STEP:
            print(f"\nlosses: decoder {decoder_loss}, bow: {bow_loss}, kld x weight: {kld_loss}")
            STEP += 1
        losses = torch.cat([decoder_loss.view(1), bow_loss.view(1), kld_loss.view(1)])
        loss = tchebycheff_objective(losses, weights=weights, norm=tbc_norm, optimal_point=optimal_point)
        return loss
github Abhipanda4 / Sparse-Autoencoders / model.py View on Github external
def forward(self, x):
        encoded = F.sigmoid(self.encoder(x))
        decoded = F.sigmoid(self.decoder(encoded))
        return encoded, decoded