Posts

Showing posts with the label pytorch

How to compute Pairwise L1 Distance matrix on very large images in neighborhood only?

Image
How to compute Pairwise L1 Distance matrix on very large images in neighborhood only? I am working on Deep learning approach for my project. And I need to calculate Distance Matrix on 4D Tensor which will be of size N x 128 x 64 x 64 (Batch Size x Channels x Height x Width) . The distance matrix for this type of tensor will of size N x 128 x 4096 x 4096 and it will be impossible to fit this type of tensor in GPU, even on CPU it will require lot of memory. So, I would like to calculate the distance matrix only in some neighborhood pixels (say within radius of 5) and consider this rectangular matrix for further processing in neural network. With this approach my distance matrix will be of size N x 128 x 4096 x 61 . It will take less memory in comparison to full distance matrix. Precisely, I am trying to implement the Convolution Random Walk Networks for Semantic Segmentation. This network needs to calculate the Pairwise L1 Distance for features. Architecture Just to add this type of Di...

How to format TSV files to use with torchtext?

How to format TSV files to use with torchtext? The way i'm formatting is like: Jersei N atinge V média N . PU Programe V ... First string in each line is the lexical item, the other is a pos tag. But the empty-line (that i'm using to indicate the end of a sentence) gives me the error AttributeError: 'Example' object has no attribute 'text' when running the given code: AttributeError: 'Example' object has no attribute 'text' src = data.Field() trg = data.Field(sequential=False) mt_train = datasets.TabularDataset( path='/path/to/file.tsv', fields=(src, trg)) src.build_vocab(train) How the proper way to indicate EOS to torchtext? @kmario23 done! – Bledson Jul 3 at 17:43 You could replace the empty line with 2 TAB s. – Danny_ds ...

Custom weight initialization in PyTorch

Custom weight initialization in PyTorch What would be the right way to implement a custom weight initialization method in PyTorch ? custom weight initialization 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. 2 Answers 2 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.02) m.bias.data.fill_(0) And then just apply it to your network: model = create_your_model() model.apply(weights_init) See https://discuss.pytorch.org/t/how-to-initialize-weights-bias-of-rnn-lstm-gru/2879/2 for reference. You can do weight_dict = net.state_dict() n...

How to train Actor-Critic (A2C) reinforcement learning

How to train Actor-Critic (A2C) reinforcement learning I am currently been able to train a system using Q-Learning. I will to move it to Actor_Critic (A2C) method. Please don't ask me why for this move, I have to. I am currently borrowing the implementation from https://github.com/higgsfield/RL-Adventure-2/blob/master/1.actor-critic.ipynb The thing is, I am keep getting a success rate of approx ~ 50% (which is basically random behavior). My game is a long episode (50 steps). Should I print out the reward, the value, or what? How should I debug this? Here are some log: simulation episode 2: Success, turn_count =20 loss = tensor(1763.7875) simulation episode 3: Fail, turn_count= 42 loss = tensor(44.6923) simulation episode 4: Fail, turn_count= 42 loss = tensor(173.5872) simulation episode 5: Fail, turn_count= 42 loss = tensor(4034.0889) simulation episode 6: Fail, turn_count= 42 loss = tensor(132.7567) loss = simulation episode 7: Success, turn_count =22 loss = tensor(2099.5344) ...

PyTorch Autograd automatic differentiation feature

Image
PyTorch Autograd automatic differentiation feature I am just curious to know, how does PyTorch track operations on tensors (after the .requires_grad is set as True and how does it later calculate the gradients automatically. Please help me understand the idea behind autograd . Thanks. .requires_grad True autograd 1 Answer 1 That's a great question! Generally, the idea of automatic differentiation ( AutoDiff ) is based on the multivariable chain rule, i.e. . What this means is that you can express the derivative of x with respect to z via a "proxy" variable y; in fact, that allows you to break up almost any operation in a bunch of simpler (or atomic) operations that can then be "chained" together. Now, what AutoDiff packages like Autograd do, is simply to store the derivative of such an atomic operation block, e.g., a division, multiplication, etc. Then, at runtime, your p...