Skip to content

Latest commit

 

History

History
166 lines (115 loc) · 7.79 KB

PyTorch.md

File metadata and controls

166 lines (115 loc) · 7.79 KB

PyTorch

Installation

pip3 install torch torchvision

Important Feature

Broadcast Semantics: the 1D operation can also apply on higher dimension (In short, if a PyTorch operation supports broadcast, then its Tensor arguments can be automatically expanded to be of equal sizes (without making copies of the data).)

Compare with TensorFlow

  • PyTorch use dynamic computational graph while TensorFlow use static one.

Dataset and DataLoader

Customized

Ready-made

  • from torchtext.datasets import text_classification

Embedding

Tips

Debug

  • Use ipdb. (import ipdb; ipdb.set_trace())
  • Usually print tensor shape can find the problem easier

Debug in the nn.Sequential

Use a PrintLayer

class PrintLayer(nn.Module):
    def __init__(self):
        super(PrintLayer, self).__init__()

    def forward(self, x):
        # Do your print / debug stuff here
        print(x)
        return x

model = nn.Sequential(
        nn.Linear(1, 5),
        PrintLayer(), # Add Print layer for debug
        nn.ReLU(),
        nn.Linear(5,1),
        nn.LogSigmoid(),
        )

x = Variable(torch.randn(10, 1))
output = model(x)

Resources

Tutorial

Article

Example/Approach

BiRNN

Embedding

Attention

Related Project

NLP

CV

Others

Others

Appendix

  • torch.bmm vs. torch.mm vs. torch.matmul
  • torch.permute vs. torch.transpose vs. torch.view

Why optimizer.zero_grad()?

nn.Module in list can't be auto assign .to(device), put it in nn.ModuleList and use it as normal list will solve the problem