pytorch

`*** RuntimeError: mat1 dim 1 must match mat2 dim 0` whenever I run model(images)

梦想的初衷 提交于 2021-02-19 05:02:47
问题 def __init__(self): super().__init__() self.conv = nn.Sequential( nn.Conv2d(1, 64, kernel_size=5, stride=2, bias=False), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 64, kernel_size=3, stride=2, bias=False), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 64, kernel_size=3, stride=2, bias=False), nn.BatchNorm2d(64), ) How can I deal with this error? I think the error is with self.fc, but I can't say how to fix it. 回答1: The output from self.conv(x) is of shape torch.Size([32, 64, 2, 2]) : 32*64*2

Pytorch Batchnorm layer different from Keras Batchnorm

核能气质少年 提交于 2021-02-19 03:38:08
问题 I'm trying to copy pre-trained BN weights from a pytorch model to its equivalent Keras model but I keep getting different outputs. I read Keras and Pytorch BN documentation and I think that the difference lies in the way they calculate the "mean" and "var". Pytorch: The mean and standard-deviation are calculated per-dimension over the mini-batches source: Pytorch BatchNorm Thus, they average over samples. Keras: axis: Integer, the axis that should be normalized (typically the features axis).

How to resize a PyTorch tensor?

ぃ、小莉子 提交于 2021-02-18 23:01:34
问题 Now I have a torch.Tensor of size (5, 1, 44, 44) in Pytorch. 5 = batch size 1 = channel 44= image height 44= image width and I want to 'resize' it to shape (5, 1, 224, 224) How can I do that? What functions should I use? 回答1: It seems like you are looking for interpolate (a function in nn.functional ): import torch.nn.functional as nnf x = torch.rand(5, 1, 44, 44) out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False) If you really care about the accuracy of the

How to resize a PyTorch tensor?

一笑奈何 提交于 2021-02-18 23:00:20
问题 Now I have a torch.Tensor of size (5, 1, 44, 44) in Pytorch. 5 = batch size 1 = channel 44= image height 44= image width and I want to 'resize' it to shape (5, 1, 224, 224) How can I do that? What functions should I use? 回答1: It seems like you are looking for interpolate (a function in nn.functional ): import torch.nn.functional as nnf x = torch.rand(5, 1, 44, 44) out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False) If you really care about the accuracy of the

PyTorch LSTM - using word embeddings instead of nn.Embedding()

左心房为你撑大大i 提交于 2021-02-18 18:15:16
问题 Is the nn.Embedding() essential for learning for an LSTM? I am using an LSTM in PyTorch to predict NER - example of a similar task is here - https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html Code wise, I am using code almost identical to the code in the tutorial above. The only detail is - I am using word2Vec instead of nn.Embedding(). So I remove the nn.Embedding() layer and provide the forward function the features from the word2Vec directly. The RNN does not learn.

How to use multiprocessing in PyTorch?

喜你入骨 提交于 2021-02-18 11:30:15
问题 The bounty expires in 5 days . Answers to this question are eligible for a +50 reputation bounty. Charlie Parker is looking for an answer from a reputable source . I'm trying to use PyTorch with complex loss function. In order to accelerate the code, I hope that I can use the PyTorch multiprocessing package. The first trial, I put 10x1 features into the NN and get 10x4 output. After that, I want to pass 10x4 parameters into a function to do some calculation. (The calculation will be complex

How can I generate and display a grid of images in PyTorch with plt.imshow and torchvision.utils.make_grid?

一曲冷凌霜 提交于 2021-02-18 10:49:52
问题 I am trying to understand how torchvision interacts with mathplotlib to produce a grid of images. It's easy to generate images and display them iteratively: import torch import torchvision import matplotlib.pyplot as plt w = torch.randn(10,3,640,640) for i in range (0,10): z = w[i] plt.imshow(z.permute(1,2,0)) plt.show() However, displaying these images in a grid does not seem to be as straightforward. w = torch.randn(10,3,640,640) grid = torchvision.utils.make_grid(w, nrow=5) plt.imshow(grid

How to get the output from a specific layer from a PyTorch model?

两盒软妹~` 提交于 2021-02-18 07:25:25
问题 How to extract the features from a specific layer from a pre-trained PyTorch model (such as ResNet or VGG), without doing a forward pass again? 回答1: You can register a forward hook on the specific layer you want. Something like: def some_specific_layer_hook(module, input_, output): pass # the value is in 'output' model.some_specific_layer.register_forward_hook(some_specific_layer_hook) model(some_input) For example, to obtain res5c output in ResNet, you may want to use a nonlocal variable (or

How to get the output from a specific layer from a PyTorch model?

本秂侑毒 提交于 2021-02-18 07:22:18
问题 How to extract the features from a specific layer from a pre-trained PyTorch model (such as ResNet or VGG), without doing a forward pass again? 回答1: You can register a forward hook on the specific layer you want. Something like: def some_specific_layer_hook(module, input_, output): pass # the value is in 'output' model.some_specific_layer.register_forward_hook(some_specific_layer_hook) model(some_input) For example, to obtain res5c output in ResNet, you may want to use a nonlocal variable (or

Custom weight initialization in PyTorch

╄→гoц情女王★ 提交于 2021-02-18 06:35:03
问题 What would be the right way to implement a custom weight initialization method in PyTorch ? I believe I can't directly add any method to 'torch.nn.init` but wish to initialize my model's weights with my own proprietary method. 回答1: You can define a method to initialize the weights according to each layer: def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv2d') != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0