Python torch.flatten() Examples

The following are 30 code examples of torch.flatten(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: pytorch_ts.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def forward(self, x):
        x = self.model(x)

        x = torch.flatten(x, start_dim=1)  # Flattens layers without losing batches

        x = self.full_conn1(x)
        x = self.norm1(x)
        x = F.relu(x)
        x = F.dropout(x)

        x = self.full_conn2(x)
        x = F.relu(x)
        x = F.dropout(x)

        x = self.full_conn3(x)

        return x 
Example #2
Source File: pre_word_list.py    From Unsupervised-Sentence-Summarization with MIT License 6 votes vote down vote up
def findwordlist(template, closewordind, vocab, numwords=10, addeos=False):
    """
    Based on a template sentence, find the candidate word list.
    
    Input:
        template: source sentence.
        closewordind: precalculated 100 closest word indices (using character embeddings). torch.LongTensor.
        vocab: full vocabulary.
        numwords: number of closest words per word in the template.
        addeos: whether to include '<eos>' in the candidate word list.
    """
    if isinstance(template, str):
        template = template.split()
    templateind = closewordind.new_tensor([vocab.stoi[w] for w in template])
    # subvocab = closewordind[templateind, :numwords].flatten().cpu()  # torch.flatten() only exists from PyTorch 0.4.1
    subvocab = closewordind[templateind, :numwords].view(-1).cpu()
    if addeos:
        subvocab = torch.cat([subvocab, torch.LongTensor([vocab.stoi['<eos>']])])
    subvocab = subvocab.unique(sorted=True)
    word_list = [vocab.itos[i] for i in subvocab]
    
    return word_list, subvocab 
Example #3
Source File: mnist.py    From chainer-compiler with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, start_dim=1) # EDIT(momohatt): Add 'start_dim='
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output


# Example input 
Example #4
Source File: resnet.py    From optimized-models with Apache License 2.0 6 votes vote down vote up
def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x 
Example #5
Source File: resnet.py    From chainer-compiler with MIT License 6 votes vote down vote up
def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        # EDIT(momohatt): Add 'start_dim='
        x = torch.flatten(x, start_dim=1)
        x = self.fc(x)

        return x 
Example #6
Source File: torch_serde.py    From PySyft with Apache License 2.0 6 votes vote down vote up
def protobuf_tensor_serializer(worker: AbstractWorker, tensor: torch.Tensor) -> TensorDataPB:
    """Strategy to serialize a tensor using Protobuf"""
    dtype = TORCH_DTYPE_STR[tensor.dtype]

    protobuf_tensor = TensorDataPB()

    if tensor.is_quantized:
        protobuf_tensor.is_quantized = True
        protobuf_tensor.scale = tensor.q_scale()
        protobuf_tensor.zero_point = tensor.q_zero_point()
        data = torch.flatten(tensor).int_repr().tolist()
    else:
        data = torch.flatten(tensor).tolist()

    protobuf_tensor.dtype = dtype
    protobuf_tensor.shape.dims.extend(tensor.size())
    getattr(protobuf_tensor, "contents_" + dtype).extend(data)

    return protobuf_tensor 
Example #7
Source File: torchvision_resnet.py    From fastMRI with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x 
Example #8
Source File: inception_v3.py    From pytorch-image-models with Apache License 2.0 6 votes vote down vote up
def forward(self, x):
        # N x 768 x 17 x 17
        x = F.avg_pool2d(x, kernel_size=5, stride=3)
        # N x 768 x 5 x 5
        x = self.conv0(x)
        # N x 128 x 5 x 5
        x = self.conv1(x)
        # N x 768 x 1 x 1
        # Adaptive average pooling
        x = F.adaptive_avg_pool2d(x, (1, 1))
        # N x 768 x 1 x 1
        x = torch.flatten(x, 1)
        # N x 768
        x = self.fc(x)
        # N x 1000
        return x 
Example #9
Source File: resnet.py    From PyTorch-Encoding with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        #x = x.view(x.size(0), -1)
        x = torch.flatten(x, 1)
        if self.drop:
            x = self.drop(x)
        x = self.fc(x)

        return x 
Example #10
Source File: unpooled_resnet.py    From fastMRI with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        #x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x 
Example #11
Source File: rpc_parameter_server.py    From examples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.max_pool2d(x, 2)

        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        # Move tensor to next device if necessary
        next_device = next(self.fc1.parameters()).device
        x = x.to(next_device)

        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output


# --------- Helper Methods --------------------

# On the local node, call a method with first arg as the value held by the
# RRef. Other args are passed in as arguments to the function called.
# Useful for calling instance methods. 
Example #12
Source File: cnn.py    From PKUAutoElective with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)       # batch*32*20*20
        x = self.bn1(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)  # batch*32*10*10
        x = self.conv2(x)       # batch*64*8*8
        x = self.bn2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)  # batch*64*4*4
        x = self.conv3(x)       # batch*128*2*2
        x = self.bn3(x)
        x = F.relu(x)
        x = torch.flatten(x, 1) # batch*512
        x = self.fc1(x)         # batch*128
        x = F.relu(x)
        x = self.fc2(x)         # batch*55
        x = F.log_softmax(x, dim=1)
        return x 
Example #13
Source File: resnet.py    From lsq-net with MIT License 6 votes vote down vote up
def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x 
Example #14
Source File: g_resnext.py    From dgconv.pytorch with MIT License 6 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x, layer1_sum = self.layer1(x)
        x, layer2_sum = self.layer2(x)
        x, layer3_sum = self.layer3(x)
        x, layer4_sum = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x, layer1_sum + layer2_sum + layer3_sum + layer4_sum 
Example #15
Source File: inference_mnist_model.py    From pytorch-to-javascript-with-onnx-js with MIT License 6 votes vote down vote up
def forward(self, x):
    x = x.reshape(280, 280, 4)
    x = torch.narrow(x, dim=2, start=3, length=1)
    x = x.reshape(1, 1, 280, 280)
    x = F.avg_pool2d(x, 10, stride=10)
    x = x / 255
    x = (x - MEAN) / STANDARD_DEVIATION

    x = self.conv1(x)
    x = F.relu(x)
    x = self.conv2(x)
    x = F.max_pool2d(x, 2)
    x = self.dropout1(x)
    x = torch.flatten(x, 1)
    x = self.fc1(x)
    x = F.relu(x)
    x = self.dropout2(x)
    x = self.fc2(x)
    output = F.softmax(x, dim=1)
    return output 
Example #16
Source File: resnet_preact_bin.py    From cv-tricks.com with MIT License 6 votes vote down vote up
def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.bn5(x)
        x = self.relu(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        #x = self.fc(x)

        return x 
Example #17
Source File: resnet_preact.py    From cv-tricks.com with MIT License 6 votes vote down vote up
def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        # x = self.bn1(x)
        # x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.bn5(x)
        x = self.relu(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        #x = self.fc(x)

        return x 
Example #18
Source File: densenet.py    From pytorch-tools with MIT License 5 votes vote down vote up
def logits(self, x):
        x = F.relu(x, inplace=True)
        x = self.global_pool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x 
Example #19
Source File: vgg.py    From pytorch-tools with MIT License 5 votes vote down vote up
def logits(self, x):
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x 
Example #20
Source File: efficientnet.py    From pytorch-tools with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.features(x)
        x = self.global_pool(x)
        x = torch.flatten(x, 1)
        x = self.dropout(x)
        x = self.classifier(x)
        return x 
Example #21
Source File: resnet.py    From pytorch-tools with MIT License 5 votes vote down vote up
def logits(self, x):
        x = self.global_pool(x)
        x = torch.flatten(x, 1)
        x = self.dropout(x)
        x = self.last_linear(x)
        return x 
Example #22
Source File: logger.py    From GCA-Matting with MIT License 5 votes vote down vote up
def normalize_image(image):
    """
    normalize image array to 0~1
    """
    image_flat = torch.flatten(image, start_dim=1)
    return (image - image_flat.min(dim=1, keepdim=False)[0].view(3,1,1)) / (
                image_flat.max(dim=1, keepdim=False)[0].view(3,1,1) - image_flat.min(dim=1, keepdim=False)[0].view(3,1,1) + 1e-8) 
Example #23
Source File: pytorch-overfeat.py    From FlexTensor with MIT License 5 votes vote down vote up
def forward(self, inputs):
        return torch.flatten(inputs) 
Example #24
Source File: pytorch_converter_example.py    From petastorm with Apache License 2.0 5 votes vote down vote up
def forward(self, x):  # pylint: disable=arguments-differ
        x = x.view((-1, 1, 28, 28))
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output 
Example #25
Source File: vtrace.py    From torchbeast with Apache License 2.0 5 votes vote down vote up
def action_log_probs(policy_logits, actions):
    return -F.nll_loss(
        F.log_softmax(torch.flatten(policy_logits, 0, -2), dim=-1),
        torch.flatten(actions),
        reduction="none",
    ).view_as(actions) 
Example #26
Source File: polybeast.py    From torchbeast with Apache License 2.0 5 votes vote down vote up
def compute_policy_gradient_loss(logits, actions, advantages):
    cross_entropy = F.nll_loss(
        F.log_softmax(torch.flatten(logits, 0, 1), dim=-1),
        target=torch.flatten(actions, 0, 1),
        reduction="none",
    )
    cross_entropy = cross_entropy.view_as(advantages)
    return torch.sum(cross_entropy * advantages.detach()) 
Example #27
Source File: monobeast.py    From torchbeast with Apache License 2.0 5 votes vote down vote up
def compute_policy_gradient_loss(logits, actions, advantages):
    cross_entropy = F.nll_loss(
        F.log_softmax(torch.flatten(logits, 0, 1), dim=-1),
        target=torch.flatten(actions, 0, 1),
        reduction="none",
    )
    cross_entropy = cross_entropy.view_as(advantages)
    return torch.sum(cross_entropy * advantages.detach()) 
Example #28
Source File: cnn_network.py    From sparktorch with MIT License 5 votes vote down vote up
def forward(self, x):
        x = x.view(-1, 1, 28, 28)
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        return x 
Example #29
Source File: train_mnist_model.py    From pytorch-to-javascript-with-onnx-js with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output 
Example #30
Source File: seq2seq_utils.py    From simpletransformers with Apache License 2.0 5 votes vote down vote up
def preprocess_data(data):
    input_text, target_text, encoder_tokenizer, decoder_tokenizer, args = data

    input_text = encoder_tokenizer.encode(
        input_text, max_length=args.max_seq_length, pad_to_max_length=True, return_tensors="pt",
    )

    target_text = decoder_tokenizer.encode(
        target_text, max_length=args.max_seq_length, pad_to_max_length=True, return_tensors="pt"
    )
    return (torch.flatten(input_text), torch.flatten(target_text))