instruction
stringlengths 13
150
| input
stringlengths 36
29.1k
| output
stringlengths 31
29.5k
| source
stringlengths 45
45
|
---|---|---|---|
"Installing From Source" Within Anaconda Environment | What I would like to do:
I am using macOS and Anaconda 2.
I would like to install a Python package (specifically PyTorch) from source.
I would like to install all the dependencies and the package itself within an Anaconda environment.
I don't want this Anaconda environment to be the default/ root Anaconda environment, but an environment I particularly created for installing this package and its dependencies from source.
What I have done:
First, I created the environment as follows
conda create --name my_env python=3.5
Now, the instructions for installing PyTorch from source are as follows:
export CMAKE_PREFIX_PATH=[anaconda root directory]
conda install numpy pyyaml setuptools cmake cffi
git clone --recursive https://github.com/pytorch/pytorch
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
Now, my questions are:
Following this instructions, requires me to specify anaconda root directory for the CMAKE_PREFIX_PATH. What should that directory be given that I want everything set-up in my_env?
Is it reasonable to create an extra environment for a package installed from source and its dependencies? Why would one do or not do it? My motivation is mainly fear that one day I may screw my system up big time and hence want things to be cleanly separated.
If you can only answer one of the two questions, that is already greatly appreciated. Thanks!
| I received this answer from the Anaconda Google discussion group and re-post it here in case anyone else is interested.
It is the path to my_env. If you created it with -n my_env and you haven't otherwise changed your envs dir, it'll be in <anaconda root>/envs/my_env
Yes, this is definitely good practice. The cleanest way to use conda is to install miniconda, not anaconda, and to install as little as possible into the root environment.
| https://stackoverflow.com/questions/47799803/ |
pytorch loss value not change | I wrote a module based on this article: http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/
The idea is pass the input into multiple streams then concat together and connect to a FC layer. I divided my source code into 3 custom modules: TextClassifyCnnNet >> FlatCnnLayer >> FilterLayer
FilterLayer:
class FilterLayer(nn.Module):
def __init__(self, filter_size, embedding_size, sequence_length, out_channels=128):
super(FilterLayer, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(1, out_channels, (filter_size, embedding_size)),
nn.ReLU(inplace=True),
nn.MaxPool2d((sequence_length - filter_size + 1, 1), stride=1)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
def forward(self, x):
return self.model(x)
FlatCnnLayer:
class FlatCnnLayer(nn.Module):
def __init__(self, embedding_size, sequence_length, filter_sizes=[3, 4, 5], out_channels=128):
super(FlatCnnLayer, self).__init__()
self.filter_layers = nn.ModuleList(
[FilterLayer(filter_size, embedding_size, sequence_length, out_channels=out_channels) for
filter_size in filter_sizes])
def forward(self, x):
pools = []
for filter_layer in self.filter_layers:
out_filter = filter_layer(x)
# reshape from (batch_size, out_channels, h, w) to (batch_size, h, w, out_channels)
pools.append(out_filter.view(out_filter.size()[0], 1, 1, -1))
x = torch.cat(pools, dim=3)
x = x.view(x.size()[0], -1)
x = F.dropout(x, p=dropout_prob, training=True)
return x
TextClassifyCnnNet (main module):
class TextClassifyCnnNet(nn.Module):
def __init__(self, embedding_size, sequence_length, num_classes, filter_sizes=[3, 4, 5], out_channels=128):
super(TextClassifyCnnNet, self).__init__()
self.flat_layer = FlatCnnLayer(embedding_size, sequence_length, filter_sizes=filter_sizes,
out_channels=out_channels)
self.model = nn.Sequential(
self.flat_layer,
nn.Linear(out_channels * len(filter_sizes), num_classes)
)
def forward(self, x):
x = self.model(x)
return x
def fit(net, data, save_path):
if torch.cuda.is_available():
net = net.cuda()
for param in list(net.parameters()):
print(type(param.data), param.size())
optimizer = optim.Adam(net.parameters(), lr=0.01, weight_decay=0.1)
X_train, X_test = data['X_train'], data['X_test']
Y_train, Y_test = data['Y_train'], data['Y_test']
X_valid, Y_valid = data['X_valid'], data['Y_valid']
n_batch = len(X_train) // batch_size
for epoch in range(1, n_epochs + 1): # loop over the dataset multiple times
net.train()
start = 0
end = batch_size
for batch_idx in range(1, n_batch + 1):
# get the inputs
x, y = X_train[start:end], Y_train[start:end]
start = end
end = start + batch_size
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
predicts = _get_predict(net, x)
loss = _get_loss(predicts, y)
loss.backward()
optimizer.step()
if batch_idx % display_step == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(x), len(X_train), 100. * batch_idx / (n_batch + 1), loss.data[0]))
# print statistics
if epoch % display_step == 0 or epoch == 1:
net.eval()
valid_predicts = _get_predict(net, X_valid)
valid_loss = _get_loss(valid_predicts, Y_valid)
valid_accuracy = _get_accuracy(valid_predicts, Y_valid)
print('\r[%d] loss: %.3f - accuracy: %.2f' % (epoch, valid_loss.data[0], valid_accuracy * 100))
print('\rFinished Training\n')
net.eval()
test_predicts = _get_predict(net, X_test)
test_loss = _get_loss(test_predicts, Y_test).data[0]
test_accuracy = _get_accuracy(test_predicts, Y_test)
print('Test loss: %.3f - Test accuracy: %.2f' % (test_loss, test_accuracy * 100))
torch.save(net.flat_layer.state_dict(), save_path)
def _get_accuracy(predicts, labels):
predicts = torch.max(predicts, 1)[1].data[0]
return np.mean(predicts == labels)
def _get_predict(net, x):
# wrap them in Variable
inputs = torch.from_numpy(x).float()
# convert to cuda tensors if cuda flag is true
if torch.cuda.is_available:
inputs = inputs.cuda()
inputs = Variable(inputs)
return net(inputs)
def _get_loss(predicts, labels):
labels = torch.from_numpy(labels).long()
# convert to cuda tensors if cuda flag is true
if torch.cuda.is_available:
labels = labels.cuda()
labels = Variable(labels)
return F.cross_entropy(predicts, labels)
It seems that parameters 're just updated slightly each epoch, the accuracy remains for all the process. While with the same implementation and the same params in Tensorflow, it runs correctly.
I'm new to Pytorch, so maybe my instructions has something wrong, please help me to find out. Thank you!
P.s: I try to use F.nll_loss + F.log_softmax instead of F.cross_entropy. Theoretically, it should return the same, but in fact another result is printed out (but it still be a wrong loss value)
| I realised that L2_loss in Adam Optimizer make loss value remain unchanged (I haven't tried in other Optimizer yet). It works when I remove L2_loss:
# optimizer = optim.Adam(net.parameters(), lr=0.01, weight_decay=0.1)
optimizer = optim.Adam(model.parameters(), lr=0.001)
=== UPDATE (See above answer for more detail!) ===
self.features = nn.Sequential(self.flat_layer)
self.classifier = nn.Linear(out_channels * len(filter_sizes), num_classes)
...
optimizer = optim.Adam([
{'params': model.features.parameters()},
{'params': model.classifier.parameters(), 'weight_decay': 0.1}
], lr=0.001)
| https://stackoverflow.com/questions/47813715/ |
How Pytorch Tensor get the index of specific value | With python lists, we can do:
a = [1, 2, 3]
assert a.index(2) == 1
How can a pytorch tensor find the .index() directly?
| I think there is no direct translation from list.index() to a pytorch function. However, you can achieve similar results using tensor==number and then the nonzero() function. For example:
t = torch.Tensor([1, 2, 3])
print ((t == 2).nonzero(as_tuple=True)[0])
This piece of code returns
1
[torch.LongTensor of size 1x1]
| https://stackoverflow.com/questions/47863001/ |
Why are Embeddings in PyTorch implemented as Sparse Layers? | Embedding Layers in PyTorch are listed under "Sparse Layers" with the limitation:
Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (cuda and cpu), and optim.Adagrad (cpu)
What is the reason for this? For example in Keras I can train an architecture with an Embedding Layer using any optimizer.
| Upon closer inspection sparse gradients on Embeddings are optional and can be turned on or off with the sparse parameter:
class torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2, scale_grad_by_freq=False, sparse=False)
Where:
sparse (boolean, optional) – if True, gradient w.r.t. weight matrix
will be a sparse tensor. See Notes for more details regarding sparse
gradients.
And the "Notes" mentioned are what I quoted in the question about a limited number of optimizers being supported for sparse gradients.
Update:
It is theoretically possible but technically difficult to implement some optimization methods on sparse gradients. There is an open issue in the PyTorch repo to add support for all optimizers.
Regarding the original question, I believe Embeddings can be treated as sparse because it is possible to operate on the input indices directly rather than converting them to one-hot encodings for input into a dense layer. This is explained in @Maxim's answer to my related question.
| https://stackoverflow.com/questions/47868341/ |
How to multiply a matrix by a vector in PyTorch | I'm playing around with PyTorch with the aim of learning it, and I have a very dumb question: how can I multiply a matrix by a single vector?
Here's what I've tried:
>>> import torch
>>> a = torch.rand(4,4)
>>> a
0.3162 0.4434 0.9318 0.8752
0.0129 0.8609 0.6402 0.2396
0.5720 0.7262 0.7443 0.0425
0.4561 0.1725 0.4390 0.8770
[torch.FloatTensor of size 4x4]
>>> b = torch.rand(4)
>>> b
0.1813
0.7090
0.0329
0.7591
[torch.FloatTensor of size 4]
>>> a.mm(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: dimension 1 out of range of 1D tensor at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensor.c:24
>>> a.mm(b.t())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: t() expects a 2D tensor, but self is 1D
>>> b.mm(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: matrices expected, got 1D, 2D tensors at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:1288
>>> b.t().mm(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: t() expects a 2D tensor, but self is 1D
On the other hand, if I do
>>> b = torch.rand(4,2)
then my first attempt, a.mm(b), works fine. So the problem is just that I'm multiplying a vector rather than a matrix --- but how can I do this?
| You're looking for
torch.mv(a,b)
Note that for the future, you may also find torch.matmul() useful. torch.matmul() infers the dimensionality of your arguments and accordingly performs either dot products between vectors, matrix-vector or vector-matrix multiplication, matrix multiplication or batch matrix multiplication for higher order tensors.
| https://stackoverflow.com/questions/47870003/ |
Strange behaviour in PyTorch | I started learning pyTorch recently.I moved from Torch community as I like Python.
I encountered this strange behaviour in pyTorch.Any insights as why this happened would be appreciated.
x=torch.Tensor(10,1).fill_(1)
y=torch.Tensor(10).fill_(2)
print(x.size()) #output is torch.Size([10, 1])
print(y.size()) #output is torch.Size([10])
z=x+y
z.size()
z.size() #output is torch.Size([10, 10])
Output of z is 10 x 10 with value 3 which means it is clearly adding the Tensors(I also checked with other numbers) and then constructing a 10 x 10 Tensor with the values after addition.
Can someone explain me why is this happening . I tried it in Torch(Lua) it did a cumulative addition and returned the Tensor I expected.
| When you're doing the sum between the torch Tensors, broadcasting is happening in the background. It's the same behaviour that you'd also see when you do the addition using NumPy. And, PyTorch simply follows the same broadcasting rules that is followed in NumPy.
You can read and understand broadcasting here: NumPy Broadcasting
| https://stackoverflow.com/questions/47874207/ |
PyTorch Slow Batch matrix multiplication on GPU | I am using Batch Matrix Multiplication on 2 3d tensors of sizes (100 , 128 , 128 ) each.
import torch
a = torch.randn(100,128,128)
b = torch.randn(100,128,128)
import time
t0 = time.time()
torch.bmm(a,b)
print(time.time() - t0)
0.03233695030212402
Now if i do the same thing on GPU it takes a lot longer
a = a.cuda()
b = b.cuda()
t0 = time.time()
torch.bmm(a,b)
print(time.time() - t0)
30.574532985687256
Why does it take so long to solve on GPU?
I have a GTX 1050 Ti 4GB
And processor core i3-6100 3.7Ghz
| GPU:
30.57 secs is the total time taken by following steps:
CPU launches kernels* on the device (GPU)
CPU allocates memory on GPU
CPU copies input data to GPU
CPU launches kernels on GPU to process the input data
CPU copies output results to itself
*Kernel is a serial code which is a small part of the original code.
CPU
Whereas, 0.0323 secs is the total time taken by either:
Intra-CPU communications
CPU and main memory communications
These are very fast and they also have to execute fewer commands to get work done.
Conclusion
Hence it's a trade-off between the complexity of the commands (CPU commands being more complex) and speed-up due to parellelism and simpler nature of commands (GPU can fire more parallel threads and they are much simpler in nature).
If there were more computations to be done, then GPU speed-up would overwhelm the extra time taken for CPU-GPU communications.
| https://stackoverflow.com/questions/47900761/ |
pytorch PIP and CONDA error? | Guys I am new to python and deeplearning world
I tried to install pytorch using conda
I get this Error...
(base) C:\WINDOWS\system32>conda install pytorch
`Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
pytorch
Current channels:
https://repo.continuum.io/pkgs/main/win-64
https://repo.continuum.io/pkgs/main/noarch
https://repo.continuum.io/pkgs/free/win-64
Couldnt post all the channels due to reputation issue on stackoverflow...
Trying Pip for installing Pytorch It just opens pytorch site after this error:
(base) C:\WINDOWS\system32>pip install pytorch
Collecting pytorch
Using cached pytorch-0.1.2.tar.gz
Building wheels for collected packages: pytorch
Running setup.py bdist_wheel for pytorch ... error
Complete output from command C:\ProgramData\Anaconda3\python.exe -u -c "import setuptools, tokenize;file='C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" bdist_wheel -d C:\Users\micha\AppData\Local\Temp\tmpqmo4j08upip-wheel- --python-tag cp36:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\setup.py", line 17, in
raise Exception(message)
Exception: You should install pytorch from http://pytorch.org
Failed building wheel for pytorch
Running setup.py clean for pytorch
Failed to build pytorch
Installing collected packages: pytorch
Running setup.py install for pytorch ... error
Complete output from command C:\ProgramData\Anaconda3\python.exe -u -c "import setuptools, tokenize;file='C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record C:\Users\micha\AppData\Local\Temp\pip-vms7q49e-record\install-record.txt --single-version-externally-managed --compile:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\setup.py", line 13, in
raise Exception(message)
Exception: You should install pytorch from http://pytorch.org
----------------------------------------
Exception:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\commands\install.py", line 342, in run
prefix=options.prefix_path,
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_set.py", line 784, in install
**kwargs
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_install.py", line 878, in install
spinner=spinner,
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\utils__init__.py", line 707, in call_subprocess
% (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command "C:\ProgramData\Anaconda3\python.exe -u -c "import setuptools, tokenize;file='C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record C:\Users\micha\AppData\Local\Temp\pip-vms7q49e-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch\
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\commands\install.py", line 385, in run
requirement_set.cleanup_files()
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_set.py", line 729, in cleanup_files
req.remove_temporary_source()
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\req\req_install.py", line 977, in remove_temporary_source
rmtree(self.source_dir)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip_vendor\retrying.py", line 49, in wrapped_f
return Retrying(*dargs, **dkw).call(f, *args, **kw)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip_vendor\retrying.py", line 212, in call
raise attempt.get()
File "C:\ProgramData\Anaconda3\lib\site-packages\pip_vendor\retrying.py", line 247, in get
six.reraise(self.value[0], self.value[1], self.value[2])
File "C:\ProgramData\Anaconda3\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\ProgramData\Anaconda3\lib\site-packages\pip_vendor\retrying.py", line 200, in call
attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\utils__init__.py", line 102, in rmtree
onerror=rmtree_errorhandler)
File "C:\ProgramData\Anaconda3\lib\shutil.py", line 494, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\ProgramData\Anaconda3\lib\shutil.py", line 393, in _rmtree_unsafe
onerror(os.rmdir, path, sys.exc_info())
File "C:\ProgramData\Anaconda3\lib\site-packages\pip\utils__init__.py", line 114, in rmtree_errorhandler
func(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\micha\AppData\Local\Temp\pip-build-t86penrg\pytorch'
I also tried conda install -c peterjc123 pytorch=0.1.12 and soumith but I get the same error not found
Any Idea where I am going wrong
Tried other forum tips and post also reinstalled Anaconda but still the same issue
| However I found how to use Pytorch on windows here:
[https://www.superdatascience.com/pytorch/]
conda install -c peterjc123 pytorch
Did the trick for me ...
| https://stackoverflow.com/questions/47943081/ |
How to use pytorch DataLoader with a 3-D matrix for LSTM input? | I have a dataset of 3-D(time_stepinputsizetotal_num) matrix which is a .mat file. I want to use DataLoader to get a input dataset for LSTM which batch_size is 5. My code is as following:
file_path = "…/database/frameLength100/notOverlap/a.mat"
mat_data = s.loadmat(file_path)
tensor_data = torch.from_numpy(mat_data[‘a’]) #Tensor
class CustomDataset(Dataset):
def __init__(self, tensor_data):
self.tensor_data = tensor_data
def __getitem__(self, index):
data = self.tensor_data[index]
label = 1;
return data, label
def __len__(self):
return len(self.tensor_data)
custom_dataset = CustomDataset(tensor_data=tensor_data)
train_loader = DataLoader(dataset=custom_dataset, batch_size=5, shuffle=True)
I think the code is wrong but I have no idea how to correct it. What makes me confused is how how can I make DataLoader know which dimension is ‘total_num’ so that I get the dataset which batch size is 5.
| If I understand correctly, you want the batching to happen along the total_num dimension, i. e. dimension 2.
You could simply use that the dimension to index your dataset, i.e. change __getitem__ to data = self.tensor_data[:, :, index], and accordingly in __len__, return self.tensor_data.size(2) instead of len(self.tensor_data). Each batch will then have size [time_step, inputsize, 5].
| https://stackoverflow.com/questions/47943419/ |
Issues installing pytorch for OS X with conda | I used to have pytorch working for python 3 on OS X but now I can't get it to install automatically for some reason (I don't want to do from source).
I did:
conda install pytorch torchvision -c pytorch
as the website suggested... then I got a mkl error so I installed it but it still complains about it:
(FTIR_py3) brandomiranda~/home_simulation_research/FTIR/FTIR_proj $ conda install pytorch torchvision -c pytorch
Fetching package metadata ...........
Solving package specifications:
PackageNotFoundError: Package not found: '' Dependencies missing in current osx-64 channels:
- pytorch -> mkl >=2018
- torchvision -> pytorch >=0.3 -> mkl >=2018
You can search for packages on anaconda.org with
anaconda search -t conda mkl
You may need to install the anaconda-client command line client with
conda install anaconda-client
but I do have mkl:
(FTIR_py3) brandomiranda~/home_simulation_research/FTIR/FTIR_proj $ conda install mkl
Fetching package metadata .........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/brandomiranda/miniconda3/envs/FTIR_py3:
#
mkl 2017.0.3 0
anyone know whats going on? it used to work a few days ago...
longer thread on pytorch forum:
https://discuss.pytorch.org/t/issues-installing-pytorch-for-os-x-with-conda/11496
| As suggested in PyTorch forum, I think you should first install MKL. Your error trace also says that MKL is missing in your system. You can install MKL by doing:
$ conda install -c anaconda mkl
After this, install pytorch and torchvision by
$ conda install -c pytorch pytorch torchvision
| https://stackoverflow.com/questions/47955463/ |
Pytorch: CNN don't learn anything after torch.cat()? | I try to concatenate Variable in the network with code like this
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = x.view(x.size(0), -1)
x= torch.cat((x,angle),1) # from here I concat it.
x = self.dropout1(self.relu1(self.bn1(self.fc1(x))))
x = self.dropout2(self.relu2(self.bn2(self.fc2(x))))
x = self.fc3(x)
And then I find my network learn nothing and give acc always around 50%. So I print param.grad and as I expected, they are all nan. Does anyone encounter this thing before?
I ran the code without concatenation before and it works out well. So I suppose this is where the rub is and the system doesn't throw any error or exception. if any other backup info is needed, please let me know.
Thank you.
| Probably the error is somewhere outside of the code that you provided. Try to check if there are nan's in your input and check if the loss function is not resulting in nan.
| https://stackoverflow.com/questions/47957458/ |
Why do we need to call zero_grad() in PyTorch? | Why does zero_grad() need to be called during training?
| zero_grad(self)
| Sets gradients of all model parameters to zero.
| In PyTorch, for every mini-batch during the training phase, we typically want to explicitly set the gradients to zero before starting to do backpropragation (i.e., updating the Weights and biases) because PyTorch accumulates the gradients on subsequent backward passes. This accumulating behaviour is convenient while training RNNs or when we want to compute the gradient of the loss summed over multiple mini-batches. So, the default action has been set to accumulate (i.e. sum) the gradients on every loss.backward() call.
Because of this, when you start your training loop, ideally you should zero out the gradients so that you do the parameter update correctly. Otherwise, the gradient would be a combination of the old gradient, which you have already used to update your model parameters, and the newly-computed gradient. It would therefore point in some other direction than the intended direction towards the minimum (or maximum, in case of maximization objectives).
Here is a simple example:
import torch
from torch.autograd import Variable
import torch.optim as optim
def linear_model(x, W, b):
return torch.matmul(x, W) + b
data, targets = ...
W = Variable(torch.randn(4, 3), requires_grad=True)
b = Variable(torch.randn(3), requires_grad=True)
optimizer = optim.Adam([W, b])
for sample, target in zip(data, targets):
# clear out the gradients of all Variables
# in this optimizer (i.e. W, b)
optimizer.zero_grad()
output = linear_model(sample, W, b)
loss = (output - target) ** 2
loss.backward()
optimizer.step()
Alternatively, if you're doing a vanilla gradient descent, then:
W = Variable(torch.randn(4, 3), requires_grad=True)
b = Variable(torch.randn(3), requires_grad=True)
for sample, target in zip(data, targets):
# clear out the gradients of Variables
# (i.e. W, b)
W.grad.data.zero_()
b.grad.data.zero_()
output = linear_model(sample, W, b)
loss = (output - target) ** 2
loss.backward()
W -= learning_rate * W.grad.data
b -= learning_rate * b.grad.data
Note:
The accumulation (i.e., sum) of gradients happens when .backward() is called on the loss tensor.
As of v1.7.0, Pytorch offers the option to reset the gradients to None optimizer.zero_grad(set_to_none=True) instead of filling them with a tensor of zeroes. The docs claim that this setting reduces memory requirements and slightly improves performance, but might be error-prone if not handled carefully.
| https://stackoverflow.com/questions/48001598/ |
pytorch: Does variable.long() guarantee 64-bits? | In pytorch, I have a variable that might be IntTensor or cuda.IntTensor. It needs to be changed to 64-bits retaining cpu/gpu. Does variable.long() guarantee 64-bits on all implementations of pytorch? If not, how can variable be converted to 64-bits retaining cpu/gpu for all implementations?
| From PyTorch documentation here, torch.LongTensor or torch.cuda.LongTensor can be used to ensure a 64-bit signed integer.
Similarly, for 64-bit floating points, you can use torch.DoubleTensor or torch.cuda.DoubleTensor.
You can convert them into variables by using Variable(tensor) method where tensor represents the tensor created above.
| https://stackoverflow.com/questions/48033408/ |
Computing gradients of intermediate nodes in PyTorch | I'm trying to learn how autograd works in PyTorch. In the simple program below, I don't understand why gradients of loss w.r.t W1 and W2 are None. As far as I understand from the documentation, W1 and W2 are volatile, therefore gradients cannot be computed. Is that it? I mean, how I cannot take derivative of the loss w.r.t intermediate nodes? Can anyone explain me what I am missing here?
import torch
import torch.autograd as tau
W = tau.Variable(torch.FloatTensor([[0, 1]]), requires_grad=True)
a = tau.Variable(torch.FloatTensor([[2, 2]]), requires_grad=False)
b = tau.Variable(torch.FloatTensor([[3, 3]]), requires_grad=False)
W1 = W + a * a
W2 = W1 - b * b * b
Z = W2 * W2
print 'W:', W
print 'W1:', W1
print 'W2:', W2
print 'Z:', Z
loss = torch.sum((Z - 3) * (Z - 3))
print 'loss:', loss
# free W gradient buffer in case you are running this cell more than 2 times
if W.grad is not None: W.grad.data.zero_()
loss.backward()
print 'W.grad:', W.grad
# all of them are None
print 'W1.grad:', W1.grad
print 'W2.grad:', W2.grad
print 'a.grad:', a.grad
print 'b.grad:', b.grad
print 'Z.grad:', Z.grad
| When required, intermediate gradients are accumulated in a C++ buffer but in order to save memory they are not retained by default (exposed in python object).
Only gradients of leaf Variables set with requires_grad=True will be retained (so Win your example)
One way to retain intermediate gradients is to register a hook. One hook for this job is retain_grad() (see PR)
In your example, if you write W2.retain_grad(), intermediate gradient of W2 will be exposed in W2.grad
W1 and W2 are not volatile (you can check by accessing their volatile attribute (ie: W1.volatile)) and cannot be because they are not leaf variables (such as W, a and b). On the contrary, the computation of their gradients is required, see their requires_grad attribute.
If only one leaf variable is volatile, the whole backward graph is not constructed (You can check by making a volatile and look at the loss gradient function)
a = tau.Variable(torch.FloatTensor([[2, 2]]), volatile=True)
# ...
assert loss.grad_fn is None
To sum up
Volatility implies no gradient computation: Useful in inference mode
Only one leaf variable set volatile disable gradient computation
Requiring gradients implies gradient computation. Intermediate ones are exposed or not
Only one leaf variable requiring grad enable gradient computation
| https://stackoverflow.com/questions/48051434/ |
Unknown Python syntax in PyTorch: a instance can directly receive a parameter | I have a question about Python syntax when I'm learning PyTorch. The following codes are an example from the PyTorch document.
m = nn.Linear(20, 30)
input = autograd.Variable(torch.randn(128, 20))
output = m(input)
print(output.size())
This first line is to create an instance m, but why this instance m can directly receive a parameter like the line 3? I think it should use method to deal with parameter like m.method(input).
| In python, any object can define a __call__ method that allows it to be used as a function like in your example.
Reference: https://docs.python.org/2/reference/datamodel.html#object.call
| https://stackoverflow.com/questions/48055762/ |
Installing a CPU-based library version on a GPU enabled machine | I want to install a CPU version of PyTorch on a server which is equipped with a nVIDIA Tesla GPU. Will it work or can I only install a GPU version (with CUDA) on this server for PyTorch to function properly?
| The version of PyTorch with GPU support also works with CPU (but the cpu training of neural networks is really slow). So you can install the GPU version. Make sure you install PyTorch compiled with the correct cuda version (cuda 7.5, cuda 8.0 or cuda 9.0).
| https://stackoverflow.com/questions/48062076/ |
PyTorch Softmax Dimensions error | I'm attempting to write a simple NN module, with 2 layers, first layer ReLU activation, output softmax with 3 classes (one-hot encoded). It seems theres something wrong with the way I'm using the softmax function, but I'm not sure what's going on.
X is 178x13
Y is 178x3
Dataset I'm using is fairly simple, and can be found here.
I keep getting the error:
RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 3) .
.
import pandas as pd
import numpy as np
import torch
from torch.autograd import Variable
from sklearn.preprocessing import LabelBinarizer
# Read in dataset, specifying that this set didn't come with column headers
x = pd.read_csv('Datasets/wine.data', header=None)
# Rename columns
x.columns = ['Class', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13']
y = x[['Class']].values
#turn class labels into one-hot encoding
one_hot = LabelBinarizer()
y = Variable(torch.from_numpy(one_hot.fit_transform(y)), )
x = Variable(torch.from_numpy(x.iloc[:, 1:14].values).float())
N, D_in, H, D_out = y.shape[0], x.shape[1], 20, 3
# Implement neural net with nn module
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.ReLU(),
torch.nn.Linear(H, D_out),
torch.nn.LogSoftmax(dim=3)
)
loss_fn = torch.nn.NLLLoss
learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for t in range(500):
y_pred = model(x)
loss = loss_fn(y_pred, y)
print("Iteration: %d | Loss: %.3f" % (t, loss))
optimizer.zero_grad()
loss.backward()
optimizer.step()
| This was a problem because for NLLLoss:
The target that this loss expects is a class index (0 to N-1, where N = number of classes)
And I had been trying to give it the one-hot encoded vector. I solved my issue by doing:
loss = loss_fn(y_pred, torch.max(y, 1)[1])
Where torch.max found the maximum values and their respective index.
| https://stackoverflow.com/questions/48070505/ |
Is it possible to implement a multilayered LSTM with LSTMCells modules in PyTorch? | In PyTorch there is a LSTM module which in addition to input sequence, hidden states, and cell states accepts a num_layers argument which specifies how many layers will our LSTM have.
There is however another module LSTMCell which has just input size and number of hidden states as parameters, there is no num_layers since this is a single cell in a multi-layered LSTM.
My question is what is the proper way to connect together the LSTMCell modules to achieve a same effect as a multi layered LSTM with num_layers > 1
| LSTMCell is the basic building block of an LSTM network. You should use the LSTM module (which uses LSTMCell internally). If you want to do this yourself, the best way is to read the source code (https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/rnn.py).
Basically you want to use one LSTMCell for each layer, and you should be careful on how to go from input to output, layer by layer taking into account the hidden states.
I also have basic implementation of a convolutional LSTM but the idea is the same. You can check it here:
https://github.com/rogertrullo/pytorch_convlstm/
| https://stackoverflow.com/questions/48080000/ |
Pytorch: Create an boolean tensor (type: torch.ByteTensor)? | I want to create a tensor only containing boolean values.
In Matlab that would be
a = false(10,1)
| Already found it:
a = torch.zeros(10)
b = a.type(torch.ByteTensor)
| https://stackoverflow.com/questions/48151637/ |
How do I check if PyTorch is using the GPU? | How do I check if PyTorch is using the GPU? The nvidia-smi command can detect GPU activity, but I want to check it directly from inside a Python script.
| These functions should help:
>>> import torch
>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
1
>>> torch.cuda.current_device()
0
>>> torch.cuda.device(0)
<torch.cuda.device at 0x7efce0b03be0>
>>> torch.cuda.get_device_name(0)
'GeForce GTX 950M'
This tells us:
CUDA is available and can be used by one device.
Device 0 refers to the GPU GeForce GTX 950M, and it is currently chosen by PyTorch.
| https://stackoverflow.com/questions/48152674/ |
Pytorch Operation to detect NaNs |
Is there a Pytorch-internal procedure to detect NaNs in Tensors? Tensorflow has the tf.is_nan and the tf.check_numerics operations ... Does Pytorch have something similar, somewhere? I could not find something like this in the docs...
I am looking specifically for a Pytorch internal routine, since I would like this to happen on the GPU as well as on the CPU. This excludes numpy - based solutions (like np.isnan(sometensor.numpy()).any()) ...
| You can always leverage the fact that nan != nan:
>>> x = torch.tensor([1, 2, np.nan])
tensor([ 1., 2., nan.])
>>> x != x
tensor([ 0, 0, 1], dtype=torch.uint8)
With pytorch 0.4 there is also torch.isnan:
>>> torch.isnan(x)
tensor([ 0, 0, 1], dtype=torch.uint8)
| https://stackoverflow.com/questions/48158017/ |
Where can I see the source code for pytorch's MSELoss? | I use the U-NET network to train my data.
But I need to modify its loss function to reduce the loss of pixels below 1 to reduce the impact of negative cases on network weights. But I opened the source code in pycharm MSELOSS, see this:
class MSELoss(_Loss):
r"""Creates a criterion that measures the mean squared error between
`n` elements in the input `x` and target `y`:
:math:`{loss}(x, y) = 1/n \sum |x_i - y_i|^2`
`x` and `y` arbitrary shapes with a total of `n` elements each.
The sum operation still operates over all the elements, and divides by `n`.
The division by `n` can be avoided if one sets the internal variable
`size_average` to `False`.
"""
pass
I can't get anything useful.
| There you go: https://github.com/pytorch/pytorch/blob/master/torch/nn/functional.py#L1423 However, it calls the C api
def mse_loss(input, target, size_average=True, reduce=True):
"""
mse_loss(input, target, size_average=True, reduce=True) -> Variable
Measures the element-wise mean squared error.
See :class:`~torch.nn.MSELoss` for details.
"""
return _pointwise_loss(lambda a, b: (a - b) ** 2, torch._C._nn.mse_loss,
input, target, size_average, reduce)
def own_mse_loss(input, target, size_average=True):
L = (input - target) ** 2
return torch.mean(L) if size_average else torch.sum(L)
| https://stackoverflow.com/questions/48219272/ |
What is the function in TensorFlow that is equivalent to expand() in PyTorch? | Let's say I have a 2 x 3 matrix and I want to create a 6 x 2 x 3 matrix where each element in the first dimension is the original 2 x 3 matrix.
In PyTorch, I can do this:
import torch
from torch.autograd import Variable
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
x = Variable(torch.from_numpy(x))
# y is the desired result
y = x.unsqueeze(0).expand(6, 2, 3)
What is the equivalent way to do this in TensorFlow? I know unsqueeze() is equivalent to tf.expand_dims() but I don't TensorFlow has anything equivalent to expand(). I'm thinking of using tf.concat on a list of the 1 x 2 x 3 tensors but am not sure if this is the best way to do it.
| Tensorflow automatically broadcasts, so in general you don't need to do any of this. Suppose you have a y' of shape 6x2x3 and your x is of shape 2x3, then you can already do y'*x or y'+x will already behave as if you had expanded it. But if for some other reason you really need to do it, then the command in tensorflow is tile:
y = tf.tile(tf.reshape(x, (1,2,3)), multiples=(6,1,1))
Docs: https://www.tensorflow.org/api_docs/python/tf/tile
| https://stackoverflow.com/questions/48226221/ |
Pytorch: Intermediate testing during training | How can I test my pytorch model on validation data during training?
I know that there is the function myNet.eval() which apparantly switches of any dropout layers, but is it also preventing the gradients from being accumulated?
Also how would I undo the myNet.eval() command in order to continue with the training?
If anyone has some code snippet / toy example I would be grateful!
|
How can I test my pytorch model on validation data during training?
There are plenty examples where there are train and test steps for every epoch during training. An easy one would be the official MNIST example. Since pytorch does not offer any high-level training, validation or scoring framework you have to write it yourself. Commonly this consists of
a data loader (commonly based on torch.utils.dataloader.Dataloader)
a main loop over the total number of epochs
a train() function that uses training data to optimize the model
a test() or valid() function to measure the effectiveness of the model given validation data and a metric
This is also what you will find in the linked example.
Alternatively you can use a framework that provides basic looping and validation facilities so you don't have to implement everything by yourself all the time.
tnt is torchnet for pytorch, supplying you with different metrics (such as accuracy) and abstraction of the train loop. See this MNIST example.
inferno and torchsample attempt to model things very similar to Keras and provide some tools for validation
skorch is a scikit-learn wrapper for pytorch that lets you use all the tools and metrics from sklearn
Also how would I undo the myNet.eval() command in order to continue with the training?
myNet.train() or, alternatively, supply a boolean to switch between eval and training: myNet.train(True) for train mode.
| https://stackoverflow.com/questions/48232381/ |
Advantages and Disadvantages of MXNet compared to other Deep Learning APIs | Recently I decided to learn MXNet, as some code I need to use, is written using this API.
However, I would like to know which are the advantages and disadvantages of MXNet compared to the other Deep Learning Libraries out there.
| Perhaps the biggest reason for considering MXNet is its high-performance imperative API. This is one of the most important advantages of MXNet to other platforms. Imperative API with autograd makes it much easier and more intuitive to compose and debug a network. PyTorch also supports imperative API, but MXNet is the only platform AFAIK that supports hybridization, which effectively allows your imperative model to be converted to a symbol for similar performance to symbolic API. Here is a link to tutorials on Gluon, MXNet's imperative API: http://gluon.mxnet.io/
Given that you're using an example code, it is possible that the example was written using symbolic API. You may notice MXNet's advantage in symbolic API when training on many GPUs. Otherwise you won't notice much of a difference (except perhaps in some memory usage).
Tensorflow does have a one year head-start to MXNet and as a result it has a larger user base, but it only supports symbolic API (imperative API is very new and is only meant for experimentation), which is significantly harder to debug a network when you run into issues. However MXNet has quickly caught up in features and with 1.0 release, I don't think there is anything in TF that MXNet doesn't support.
| https://stackoverflow.com/questions/48233780/ |
PyTorch: Relation between Dynamic Computational Graphs - Padding - DataLoader | As far as I understand, the strength of PyTorch is supposed to be that it works with dynamic computational graphs. In the context of NLP, that means that sequences with variable lengths do not necessarily need to be padded to the same length. But, if I want to use PyTorch DataLoader, I need to pad my sequences anyway because the DataLoader only takes tensors - given that me as a total beginner does not want to build some customized collate_fn.
Now this makes me wonder - doesn’t this wash away the whole advantage of dynamic computational graphs in this context?
Also, if I pad my sequences to feed it into the DataLoader as a tensor with many zeros as padding tokens at the end (in the case of word ids), will it have any negative effect on my training since PyTorch may not be optimized for computations with padded sequences (since the whole premise is that it can work with variable sequence lengths in the dynamic graphs), or does it simply not make any difference?
I will also post this question in the PyTorch Forum...
Thanks!
|
In the context of NLP, that means that sequences with variable lengths do not necessarily need to be padded to the same length.
This means that you don't need to pad sequences unless you are doing data batching which is currently the only way to add parallelism in PyTorch. DyNet has a method called autobatching (which is described in detail in this paper) that does batching on the graph operations instead of the data, so this might be what you want to look into.
But, if I want to use PyTorch DataLoader, I need to pad my sequences anyway because the DataLoader only takes tensors - given that me as a total beginner does not want to build some customized collate_fn.
You can use the DataLoader given you write your own Dataset class and you are using batch_size=1. The twist is to use numpy arrays for your variable length sequences (otherwise default_collate will give you a hard time):
from torch.utils.data import Dataset
from torch.utils.data.dataloader import DataLoader
class FooDataset(Dataset):
def __init__(self, data, target):
assert len(data) == len(target)
self.data = data
self.target = target
def __getitem__(self, index):
return self.data[index], self.target[index]
def __len__(self):
return len(self.data)
data = [[1,2,3], [4,5,6,7,8]]
data = [np.array(n) for n in data]
targets = ['a', 'b']
ds = FooDataset(data, targets)
dl = DataLoader(ds, batch_size=1)
print(list(enumerate(dl)))
# [(0, [
# 1 2 3
# [torch.LongTensor of size 1x3]
# , ('a',)]), (1, [
# 4 5 6 7 8
# [torch.LongTensor of size 1x5]
# , ('b',)])]
Now this makes me wonder - doesn’t this wash away the whole advantage of dynamic computational graphs in this context?
Fair point but the main strength of dynamic computational graphs are (at least currently) mainly the possibility of using debugging tools like pdb which rapidly decrease your development time. Debugging is way harder with static computation graphs. There is also no reason why PyTorch would not implement further just-in-time optimizations or a concept similar to DyNet's auto-batching in the future.
Also, if I pad my sequences to feed it into the DataLoader as a tensor with many zeros as padding tokens at the end [...], will it have any negative effect on my training [...]?
Yes, both in runtime and for the gradients. The RNN will iterate over the padding just like normal data which means that you have to deal with it in some way. PyTorch supplies you with tools for dealing with padded sequences and RNNs, namely pad_packed_sequence and pack_padded_sequence. These will let you ignore the padded elements during RNN execution, but beware: this does not work with RNNs that you implement yourself (or at least not if you don't add support for it manually).
| https://stackoverflow.com/questions/48244053/ |
PyTorch's dataloader "too many open files" error when no files should be open | So this is a minimal code which illustrates the issue:
This is the Dataset:
class IceShipDataset(Dataset):
BAND1='band_1'
BAND2='band_2'
IMAGE='image'
@staticmethod
def get_band_img(sample,band):
pic_size=75
img=np.array(sample[band])
img.resize(pic_size,pic_size)
return img
def __init__(self,data,transform=None):
self.data=data
self.transform=transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
sample=self.data[idx]
band1_img=IceShipDataset.get_band_img(sample,self.BAND1)
band2_img=IceShipDataset.get_band_img(sample,self.BAND2)
img=np.stack([band1_img,band2_img],2)
sample[self.IMAGE]=img
if self.transform is not None:
sample=self.transform(sample)
return sample
And this is the code which fails:
PLAY_BATCH_SIZE=4
#load data. There are 1604 examples.
with open('train.json','r') as f:
data=f.read()
data=json.loads(data)
ds=IceShipDataset(data)
playloader = torch.utils.data.DataLoader(ds, batch_size=PLAY_BATCH_SIZE,
shuffle=False, num_workers=4)
for i,data in enumerate(playloader):
print(i)
It gives that weird open files error in the for loop…
My torch version is 0.3.0.post4
If you want the json file, it is available at Kaggle (https://www.kaggle.com/c/statoil-iceberg-classifier-challenge)
I should mention that the error has nothing to do with the state of my laptop:
yoni@yoni-Lenovo-Z710:~$ lsof | wc -l
89114
yoni@yoni-Lenovo-Z710:~$ cat /proc/sys/fs/file-max
791958
What am I doing wrong here?
| I know how to fix the error, but I don't have a complete explanation for why it happens.
First, the solution: you need to make sure that the image data is stored as numpy.arrays, when you call json.loads it loads them as python lists of floats. This causes the torch.utils.data.DataLoader to individually transform each float in the list into a torch.DoubleTensor.
Have a look at default_collate in torch.utils.data.DataLoader - your __getitem__ returns a dict which is a mapping, so default_collate gets called again on each element of the dict. The first couple are ints, but then you get to the image data which is a list, i.e. a collections.Sequence - this is where things get funky as default_collate is called on each element of the list. This is clearly not what you intended. I don't know what the assumption in torch is about the contents of a list versus a numpy.array, but given the error it would appear that that assumption is being violated.
The fix is pretty trivial, just make sure the two image bands are numpy.arrays, for instance in __init__
def __init__(self,data,transform=None):
self.data=[]
for d in data:
d[self.BAND1] = np.asarray(d[self.BAND1])
d[self.BAND2] = np.asarray(d[self.BAND2])
self.data.append(d)
self.transform=transform
or after you load the json, what ever - doesn't really matter where you do it, as long as you do it.
Why does the above results in too many open files?
I don't know, but as the comments pointed out, it is likely to do with interprocess communication and lock files on the two queues data is taken from and added to.
Footnote: the train.json was not available for download from Kaggle due to the competition still being open (??). I made a dummy json file that should have the same structure and tested the fix on that dummy file.
| https://stackoverflow.com/questions/48250053/ |
Pytorch: How to compute IoU (Jaccard Index) for semantic segmentation | Can someone provide a toy example of how to compute IoU (intersection over union) for semantic segmentation in pytorch?
| I found this somewhere and adapted it for me. I'll post the link if I can find it again. Sorry in case this was a dublicate.
The key function here is the function called iou. The wrapping function evaluate_performance is not universal, but it shows that one needs to iterate over all results before computing IoU.
import torch
import pandas as pd # For filelist reading
import myPytorchDatasetClass # Custom dataset class, inherited from torch.utils.data.dataset
def iou(pred, target, n_classes = 12):
ious = []
pred = pred.view(-1)
target = target.view(-1)
# Ignore IoU for background class ("0")
for cls in xrange(1, n_classes): # This goes from 1:n_classes-1 -> class "0" is ignored
pred_inds = pred == cls
target_inds = target == cls
intersection = (pred_inds[target_inds]).long().sum().data.cpu()[0] # Cast to long to prevent overflows
union = pred_inds.long().sum().data.cpu()[0] + target_inds.long().sum().data.cpu()[0] - intersection
if union == 0:
ious.append(float('nan')) # If there is no ground truth, do not include in evaluation
else:
ious.append(float(intersection) / float(max(union, 1)))
return np.array(ious)
def evaluate_performance(net):
# Dataloader for test data
batch_size = 1
filelist_name_test = '/path/to/my/test/filelist.txt'
data_root_test = '/path/to/my/data/'
dset_test = myPytorchDatasetClass.CustomDataset(filelist_name_test, data_root_test)
test_loader = torch.utils.data.DataLoader(dataset=dset_test,
batch_size=batch_size,
shuffle=False,
pin_memory=True)
data_info = pd.read_csv(filelist_name_test, header=None)
num_test_files = data_info.shape[0]
sample_size = num_test_files
# Containers for results
preds = Variable(torch.zeros((sample_size, 60, 36, 60)))
gts = Variable(torch.zeros((sample_size, 60, 36, 60)))
dataiter = iter(test_loader)
for i in xrange(sample_size):
images, labels, filename = dataiter.next()
images = Variable(images).cuda()
labels = Variable(labels)
gts[i:i+batch_size, :, :, :] = labels
outputs = net(images)
outputs = outputs.permute(0, 2, 3, 4, 1).contiguous()
val, pred = torch.max(outputs, 4)
preds[i:i+batch_size, :, :, :] = pred.cpu()
acc = iou(preds, gts)
return acc
| https://stackoverflow.com/questions/48260415/ |
Pytorch - RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed | I keep running into this error:
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
I had searched in Pytorch forum, but still can’t find out what I have done wrong in my custom loss function. My model is nn.GRU, and here is my custom loss function:
def _loss(outputs, session, items): # `items` is a dict() contains embedding of all items
def f(output, target):
pos = torch.from_numpy(np.array([items[target["click"]]])).float()
neg = torch.from_numpy(np.array([items[idx] for idx in target["suggest_list"] if idx != target["click"]])).float()
if USE_CUDA:
pos, neg = pos.cuda(), neg.cuda()
pos, neg = Variable(pos), Variable(neg)
pos = F.cosine_similarity(output, pos)
if neg.size()[0] == 0:
return torch.mean(F.logsigmoid(pos))
neg = F.cosine_similarity(output.expand_as(neg), neg)
return torch.mean(F.logsigmoid(pos - neg))
loss = map(f, outputs, session)
return -torch.mean(torch.cat(loss))
Training code:
# zero the parameter gradients
model.zero_grad()
# forward + backward + optimize
outputs, hidden = model(inputs, hidden)
loss = _loss(outputs, session, items)
acc_loss += loss.data[0]
loss.backward()
# Add parameters' gradients to their values, multiplied by learning rate
for p in model.parameters():
p.data.add_(-learning_rate, p.grad.data)
| The problem is from my training loop: it doesn’t detach or repackage the hidden state in between batches? If so, then loss.backward() is trying to back-propagate all the way through to the start of time, which works for the first batch but not for the second because the graph for the first batch has been discarded.
there are two possible solutions.
1) detach/repackage the hidden state in between batches. There are (at
least) three ways to do this (and I chose this solution):
hidden.detach_()
hidden = hidden.detach()
2) replace loss.backward() with loss.backward(retain_graph=True) but know that each successive batch will take more time than the previous one because it will have to back-propagate all the way through to the start of the first batch.
Example
| https://stackoverflow.com/questions/48274929/ |
What's the difference between "hidden" and "output" in PyTorch LSTM? | I'm having trouble understanding the documentation for PyTorch's LSTM module (and also RNN and GRU, which are similar). Regarding the outputs, it says:
Outputs: output, (h_n, c_n)
output (seq_len, batch, hidden_size * num_directions): tensor containing the output features (h_t) from the last layer of the RNN, for each t. If a torch.nn.utils.rnn.PackedSequence has been given as the input, the output will also be a packed sequence.
h_n (num_layers * num_directions, batch, hidden_size): tensor containing the hidden state for t=seq_len
c_n (num_layers * num_directions, batch, hidden_size): tensor containing the cell state for t=seq_len
It seems that the variables output and h_n both give the values of the hidden state. Does h_n just redundantly provide the last time step that's already included in output, or is there something more to it than that?
| I made a diagram. The names follow the PyTorch docs, although I renamed num_layers to w.
output comprises all the hidden states in the last layer ("last" depth-wise, not time-wise). (h_n, c_n) comprises the hidden states after the last timestep, t = n, so you could potentially feed them into another LSTM.
The batch dimension is not included.
| https://stackoverflow.com/questions/48302810/ |
PyTorch: How to change the learning rate of an optimizer at any given moment (no LR schedule) | Is it possible in PyTorch to change the learning rate of the optimizer in the middle of training dynamically (I don't want to define a learning rate schedule beforehand)?
So let's say I have an optimizer:
optim = torch.optim.SGD(model.parameters(), lr=0.01)
Now due to some tests which I perform during training, I realize my learning rate is too high so I want to change it to say 0.001. There doesn't seem to be a method optim.set_lr(0.001) but is there some way to do this?
| So the learning rate is stored in optim.param_groups[i]['lr'].
optim.param_groups is a list of the different weight groups which can have different learning rates. Thus, simply doing:
for g in optim.param_groups:
g['lr'] = 0.001
will do the trick.
**Alternatively,**
as mentionned in the comments, if your learning rate only depends on the epoch number, you can use a learning rate scheduler.
For example (modified example from the doc):
torch.optim.lr_scheduler import LambdaLR
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
# Assuming optimizer has two groups.
lambda_group1 = lambda epoch: epoch // 30
lambda_group2 = lambda epoch: 0.95 ** epoch
scheduler = LambdaLR(optimizer, lr_lambda=[lambda1, lambda2])
for epoch in range(100):
train(...)
validate(...)
scheduler.step()
Also, there is a prebuilt learning rate scheduler to reduce on plateaus.
| https://stackoverflow.com/questions/48324152/ |
Is location-dependent convolution filter possible in PyTorch or TensorFlow? | Let's pretend that in plus of having an image, I also have a gradient from left to right on the X axis of an image, and another gradient from top to bottom on the Y axis. Those two gradients are of the same size of the image, and could both range from -0.5 to 0.5.
Now, I'd like to make the convolution kernel (a.k.a. convolution filter, or convolution weights) depend on the (x, y) location in the gradient. So the kernel is a function of the gradient as if the kernel was the output of a nested mini-neural net. This would make the weights of the filter to be different in every position, but slightly similar to their neighbors. How do I do that within PyTorch or TensorFlow?
Sure, I could compute a Toeplitz matrix (a.k.a. diagonal-constant matrix) by myself, but the matrix multiplication would take O(n^3) operations if pretending x==y==n, whereas convolutions can be implemented in O(n^2) normally. Or I could maybe iterate on every element myself and do the multiplications in an unvectorized fashion.
Any better ideas? I'd like to see creativity here, thinking about how could this be implemented neatly. I believe coding that would be an interesting way to build a network layer capable of doing things similar to a simplified version of a Spatial Transformer Networks, but which's spatial transformation would be independent of the image.
| Here is a solution I thought for a simplified version of this problem where a linear combination of weights would be used rather than truly using a nested mini neural network:
It may be possible to do 4 different convolutions passes so as to have 4 feature maps, then to multiply those 4 maps with the gradients (2 vertical and 2 horizontal gradients), and add them together so that only 1 map remains. However, that would be a linear combination of the different maps which is simpler than truly using a nested neural network which would alter the kernel in the first place.
| https://stackoverflow.com/questions/48331966/ |
What's the reason of the error ValueError: Expected more than 1 value per channel? | reference fast.ai
github repository of fast.ai
(as the code elevates the library which is built on top of PyTorch)
Please scroll the discussion a bit
I am running the following code, and get an error while trying to pass the data to the predict_array function
The code is failing when i am trying to use it to predict directly on a single image but it run's perfectly when that same image is in a test folder
from fastai.conv_learner import *
from planet import f2
PATH = 'data/shopstyle/'
metrics=[f2]
f_model = resnet34
def get_data(sz):
tfms = tfms_from_model(f_model, sz, aug_tfms=transforms_side_on, max_zoom=1.05)
return ImageClassifierData.from_csv(PATH, 'train', label_csv, tfms=tfms, suffix='.jpg', val_idxs=val_idxs, test_name='test')
def print_list(list_or_iterator):
return "[" + ", ".join( str(x) for x in list_or_iterator) + "]"
label_csv = f'{PATH}prod_train.csv'
n = len(list(open(label_csv)))-1
val_idxs = get_cv_idxs(n)
sz = 64
data = get_data(sz)
print("Loading model...")
learn = ConvLearner.pretrained(f_model, data, metrics=metrics)
learn.load(f'{sz}')
#learn.load("tmp")
print("Predicting...")
learn.precompute=False
trn_tfms, val_tfrms = tfms_from_model(f_model, sz)
#im = val_tfrms(open_image(f'{PATH}valid/4500132.jpg'))
im = val_tfrms(np.array(PIL.Image.open(f'{PATH}valid/4500132.jpg')))
preds = learn.predict_array(im[None])
p=list(zip(data.classes, preds))
print("predictions = " + print_list(p))
Here's the Traceback I am Getting
Traceback (most recent call last):
File "predict.py", line 34, in <module>
preds = learn.predict_array(im[None])
File "/home/ubuntu/fastai/courses/dl1/fastai/learner.py", line 266, in predict_array
def predict_array(self, arr): return to_np(self.model(V(T(arr).cuda())))
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 37, in forward
self.training, self.momentum, self.eps)
File "/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py", line 1011, in batch_norm
raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
Things I have Tried
np.expand_dims(IMG,axis=0) or image = image[..., np.newaxis]
Tried a different way of reading the image
img = cv2.imread(img_path)
img = cv2.resize(img, dsize = (200,200))
img = np.einsum('ijk->kij', img)
img = np.expand_dims(img, axis =0)
img = torch.from_numpy(img)
learn.model(Variable(img.float()).cuda())
BTW the error still remains
ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]
Can't find any reference in The Google search also..
| It will fail on batches of size 1 if we use feature-wise batch normalization.
As Batch normalization computes:
y = (x - mean(x)) / (std(x) + eps)
If we have one sample per batch then mean(x) = x, and the output will be entirely zero (ignoring the bias). We can't use that for learning...
| https://stackoverflow.com/questions/48343857/ |
BatchNorm momentum convention PyTorch | Is the batchnorm momentum convention (default=0.1) correct as in other libraries e.g. Tensorflow it seems to usually be 0.9 or 0.99 by default? Or maybe we are just using a different convention?
| It seems that the parametrization convention is different in pytorch than in tensorflow, so that 0.1 in pytorch is equivalent to 0.9 in tensorflow.
To be more precise:
In Tensorflow:
running_mean = decay*running_mean + (1-decay)*new_value
In PyTorch:
running_mean = (1-decay)*running_mean + decay*new_value
This means that a value of decay in PyTorch is equivalent to a value of (1-decay) in Tensorflow.
| https://stackoverflow.com/questions/48345857/ |
How to model Convolutional recurrent network ( CRNN ) in Keras | I was trying to port CRNN model to Keras.
But, I got stuck while connecting output of Conv2D layer to LSTM layer.
Output from CNN layer will have a shape of ( batch_size, 512, 1, width_dash) where first one depends on batch_size, and last one depends on input width of input ( this model can accept variable width input )
For eg: an input with shape [2, 1, 32, 829] was resulting output with shape of (2, 512, 1, 208)
Now, as per Pytorch model, we have to do squeeze(2) followed by permute(2, 0, 1)
it will result a tensor with shape [208, 2, 512 ]
I was trying to implement this is Keras, but I was not able to do that because, in Keras we can not alter batch_size dimension in a keras.models.Sequential model
Can someone please guide me how to port above part of this model to Keras?
Current state of ported CNN layer
| You don't need to permute the batch axis in Keras. In a pytorch model you need to do it because a pytorch LSTM expects an input shape (seq_len, batch, input_size). However in Keras, the LSTM layer expects (batch, seq_len, input_size).
So after defining the CNN and squeezing out axis 2, you just need to permute the last two axes. As a simple example (in 'channels_first' Keras image format),
model = Sequential()
model.add(Conv2D(512, 3, strides=(32, 4), padding='same', input_shape=(1, 32, None)))
model.add(Reshape((512, -1)))
model.add(Permute((2, 1)))
model.add(LSTM(32))
You can verify the shapes with model.summary():
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_4 (Conv2D) (None, 512, 1, None) 5120
_________________________________________________________________
reshape_3 (Reshape) (None, 512, None) 0
_________________________________________________________________
permute_4 (Permute) (None, None, 512) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 32) 69760
=================================================================
Total params: 74,880
Trainable params: 74,880
Non-trainable params: 0
_________________________________________________________________
| https://stackoverflow.com/questions/48356464/ |
Converting state-parameters of Pytorch LSTM to Keras LSTM | I was trying to port an existing trained PyTorch model into Keras.
During the porting, I got stuck at LSTM layer.
Keras implementation of LSTM network seems to have three state kind of state matrices while Pytorch implementation have four.
For eg, for an Bidirectional LSTM with hidden_layers=64, input_size=512 & output size=128 state parameters where as follows
State params of Keras LSTM
[<tf.Variable 'bidirectional_1/forward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref>,
<tf.Variable 'bidirectional_1/forward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref>,
<tf.Variable 'bidirectional_1/forward_lstm_1/bias:0' shape=(256,) dtype=float32_ref>,
<tf.Variable 'bidirectional_1/backward_lstm_1/kernel:0' shape=(512, 256) dtype=float32_ref>,
<tf.Variable 'bidirectional_1/backward_lstm_1/recurrent_kernel:0' shape=(64, 256) dtype=float32_ref>,
<tf.Variable 'bidirectional_1/backward_lstm_1/bias:0' shape=(256,) dtype=float32_ref>]
State params of PyTorch LSTM
['rnn.0.rnn.weight_ih_l0', torch.Size([256, 512])],
['rnn.0.rnn.weight_hh_l0', torch.Size([256, 64])],
['rnn.0.rnn.bias_ih_l0', torch.Size([256])],
['rnn.0.rnn.bias_hh_l0', torch.Size([256])],
['rnn.0.rnn.weight_ih_l0_reverse', torch.Size([256, 512])],
['rnn.0.rnn.weight_hh_l0_reverse', torch.Size([256, 64])],
['rnn.0.rnn.bias_ih_l0_reverse', torch.Size([256])],
['rnn.0.rnn.bias_hh_l0_reverse', torch.Size([256])],
I tried to look in to the code of both implementation but not able to understand much.
Can someone please help me to transform 4-set of state params from PyTorch into 3-set of state params in Keras
| They are really not that different. If you sum up the two bias vectors in PyTorch, the equations will be the same as what's implemented in Keras.
This is the LSTM formula on PyTorch documentation:
PyTorch uses two separate bias vectors for the input transformation (with a subscript starts with i) and recurrent transformation (with a subscript starts with h).
In Keras LSTMCell:
x_i = K.dot(inputs_i, self.kernel_i)
x_f = K.dot(inputs_f, self.kernel_f)
x_c = K.dot(inputs_c, self.kernel_c)
x_o = K.dot(inputs_o, self.kernel_o)
if self.use_bias:
x_i = K.bias_add(x_i, self.bias_i)
x_f = K.bias_add(x_f, self.bias_f)
x_c = K.bias_add(x_c, self.bias_c)
x_o = K.bias_add(x_o, self.bias_o)
if 0 < self.recurrent_dropout < 1.:
h_tm1_i = h_tm1 * rec_dp_mask[0]
h_tm1_f = h_tm1 * rec_dp_mask[1]
h_tm1_c = h_tm1 * rec_dp_mask[2]
h_tm1_o = h_tm1 * rec_dp_mask[3]
else:
h_tm1_i = h_tm1
h_tm1_f = h_tm1
h_tm1_c = h_tm1
h_tm1_o = h_tm1
i = self.recurrent_activation(x_i + K.dot(h_tm1_i,
self.recurrent_kernel_i))
f = self.recurrent_activation(x_f + K.dot(h_tm1_f,
self.recurrent_kernel_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1_c,
self.recurrent_kernel_c))
o = self.recurrent_activation(x_o + K.dot(h_tm1_o,
self.recurrent_kernel_o))
There's only one bias added in the input transformation. However, the equations would be equivalent if we sum up the two biases in PyTorch.
The two-bias LSTM is what's implemented in cuDNN (see the developer guide). I'm really not that familiar with PyTorch, but I guess that's why they use two bias parameters. In Keras, the CuDNNLSTM layer also has two bias weight vectors.
| https://stackoverflow.com/questions/48361376/ |
Pytorch: Updating numpy array not updating the corresponding tensor | When I run the following code,
import numpy as np
a = np.ones(3)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
Both a and b are 2s.
However, When I run:
import numpy as np
a = np.ones(3)
b = torch.from_numpy(a)
a = a+1
print(a)
print(b)
b remains as 1s while a has been updated to 2s.
Is this an expected behaviour?
| Yes, as @hpaulj pointed out in his comment, the operation
a = a + 1
creates copy of the original array a and adds 1 using broadcasting. And after addition, since we assign it to a, so a gets updated to the result of addition operation. But, b still shares the memory of the original array a (i.e. the array a that was created before updation.)
So, we see result like this:
In [75]: a = np.ones(3)
...: b = torch.from_numpy(a)
...: a = a+1 # <========= creates copy of `a` and modifies it
...: print(a)
...: print(b)
...:
[ 2. 2. 2.]
1
1
1
[torch.DoubleTensor of size 3]
But, see what happens when you rather do:
In [72]: a = np.ones(3)
...: b = torch.from_numpy(a)
...: a += 1 # <========== in-place modification of `a`
...: print(a)
...: print(b)
...:
[ 2. 2. 2.]
2
2
2
[torch.DoubleTensor of size 3]
Observe how the += operation is making modification to the original array in-place whereas somearr = somearr + 1 creates copy of the array somearray and then makes modification to it.
| https://stackoverflow.com/questions/48370286/ |
RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1) | Im using a Pytorch Unet model to which i am feeding in a image as input and along with that i am feeding the label as the input image mask and traning the dataset on it.
The Unet model i have picked up from somewhere else, and i am using the cross-entropy loss as a loss function but i get this dimension out of range error,
RuntimeError
Traceback (most recent call last)
<ipython-input-358-fa0ef49a43ae> in <module>()
16 for epoch in range(0, num_epochs):
17 # train for one epoch
---> 18 curr_loss = train(train_loader, model, criterion, epoch, num_epochs)
19
20 # store best loss and save a model checkpoint
<ipython-input-356-1bd6c6c281fb> in train(train_loader, model, criterion, epoch, num_epochs)
16 # measure loss
17 print (outputs.size(),labels.size())
---> 18 loss = criterion(outputs, labels)
19 losses.update(loss.data[0], images.size(0))
20
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in _ _call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
<ipython-input-355-db66abcdb074> in forward(self, logits, targets)
9 probs_flat = probs.view(-1)
10 targets_flat = targets.view(-1)
---> 11 return self.crossEntropy_loss(probs_flat, targets_flat)
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/loss.py in f orward(self, input, target)
599 _assert_no_grad(target)
600 return F.cross_entropy(input, target, self.weight, self.size_average,
--> 601 self.ignore_index, self.reduce)
602
603
/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce)
1138 >>> loss.backward()
1139 """
-> 1140 return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
1141
1142
/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel)
784 if dim is None:
785 dim = _get_softmax_dim('log_softmax', input.dim(), _stacklevel)
--> 786 return torch._C._nn.log_softmax(input, dim)
787
788
RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1)
Part of my code looks like this
class crossEntropy(nn.Module):
def __init__(self, weight = None, size_average = True):
super(crossEntropy, self).__init__()
self.crossEntropy_loss = nn.CrossEntropyLoss(weight, size_average)
def forward(self, logits, targets):
probs = F.sigmoid(logits)
probs_flat = probs.view(-1)
targets_flat = targets.view(-1)
return self.crossEntropy_loss(probs_flat, targets_flat)
class UNet(nn.Module):
def __init__(self, imsize):
super(UNet, self).__init__()
self.imsize = imsize
self.activation = F.relu
self.pool1 = nn.MaxPool2d(2)
self.pool2 = nn.MaxPool2d(2)
self.pool3 = nn.MaxPool2d(2)
self.pool4 = nn.MaxPool2d(2)
self.conv_block1_64 = UNetConvBlock(4, 64)
self.conv_block64_128 = UNetConvBlock(64, 128)
self.conv_block128_256 = UNetConvBlock(128, 256)
self.conv_block256_512 = UNetConvBlock(256, 512)
self.conv_block512_1024 = UNetConvBlock(512, 1024)
self.up_block1024_512 = UNetUpBlock(1024, 512)
self.up_block512_256 = UNetUpBlock(512, 256)
self.up_block256_128 = UNetUpBlock(256, 128)
self.up_block128_64 = UNetUpBlock(128, 64)
self.last = nn.Conv2d(64, 2, 1)
def forward(self, x):
block1 = self.conv_block1_64(x)
pool1 = self.pool1(block1)
block2 = self.conv_block64_128(pool1)
pool2 = self.pool2(block2)
block3 = self.conv_block128_256(pool2)
pool3 = self.pool3(block3)
block4 = self.conv_block256_512(pool3)
pool4 = self.pool4(block4)
block5 = self.conv_block512_1024(pool4)
up1 = self.up_block1024_512(block5, block4)
up2 = self.up_block512_256(up1, block3)
up3 = self.up_block256_128(up2, block2)
up4 = self.up_block128_64(up3, block1)
return F.log_softmax(self.last(up4))
| According to your code:
probs_flat = probs.view(-1)
targets_flat = targets.view(-1)
return self.crossEntropy_loss(probs_flat, targets_flat)
You are giving two 1d tensor to nn.CrossEntropyLoss but according to documentation, it expects:
Input: (N,C) where C = number of classes
Target: (N) where each value is 0 <= targets[i] <= C-1
Output: scalar. If reduce is False, then (N) instead.
I believe that is the cause of the problem you are encountering.
| https://stackoverflow.com/questions/48377214/ |
pytorch model.cuda() runtime error | I'm building a text classifier using pytorch, and got into some trouble with .cuda() method. I know that .cuda() moves all parameters into gpu so that the training procedure can be faster. However, error occurred in .cuda() method like this:
start_time = time.time()
for model_type in ('lstm',):
hyperparam_combinations = score_util.all_combination(hyperparam_dict[model_type].values())
# for selecting best scoring model
for test_idx, setting in enumerate(hyperparam_combinations):
args = custom_dataset.list_to_args(setting,model_type=model_type)
print(args)
tsv = "test %d\ttrain_loss\ttrain_acc\ttrain_auc\tval_loss\tval_acc\tval_auc\n"%(test_idx) # tsv record
avg_score = [] # cv_mean score
### 4 fold cross validation
for cv_num,(train_iter,val_iter) in enumerate(cv_splits):
### model initiation
model = model_dict[model_type](args)
if args.emb_type is not None: # word embedding init
emb = emb_dict[args.emb_type]
emb = score_util.embedding_init(emb,tr_text_field,args.emb_type)
model.embed.weight.data.copy_(emb)
model.cuda()
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-20-ff6cfce73c10> in <module>()
23 model.embed.weight.data.copy_(emb)
24
---> 25 model.cuda()
26
27 optimizer= torch.optim.Adam(model.parameters(),lr=args.lr)
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in cuda(self, device_id)
145 copied to that device
146 """
--> 147 return self._apply(lambda t: t.cuda(device_id))
148
149 def cpu(self, device_id=None):
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _apply(self, fn)
116 def _apply(self, fn):
117 for module in self.children():
--> 118 module._apply(fn)
119
120 for param in self._parameters.values():
~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _apply(self, fn)
122 # Variables stored in modules are graph leaves, and we don't
123 # want to create copy nodes, so we have to unpack the data.
--> 124 param.data = fn(param.data)
125 if param._grad is not None:
126 param._grad.data = fn(param._grad.data)
RuntimeError: Variable data has to be a tensor, but got torch.cuda.FloatTensor
These are error traceback and I can't see why this happens.
This code worked very well before I set epoch parameter to 1 to run some tests. I set epoch to 1000 again, but the problem lingers on.
Aren't torch.cuda.FloatTensor object also Tensors? Any help would be much appreciated.
my model looks like this :
class TR_LSTM(nn.Module):
def __init__(self,args,
use_hidden_average=False,
pretrained_emb = None):
super(TR_LSTM,self).__init__()
# arguments
self.emb_dim = args.embed_dim
self.emb_num = args.embed_num
self.num_hidden_unit = args.hidden_state_dim
self.num_lstm_layer = args.num_lstm_layer
self.use_hidden_average = use_hidden_average
self.batch_size = args.batch_size
# layers
self.embed = nn.Embedding(self.emb_num, self.emb_dim)
if pretrained_emb is not None:
self.embed.weight.data.copy_(pretrained_emb)
self.lstm_layer = nn.LSTM(self.emb_dim, self.num_hidden_unit, self.num_lstm_layer, batch_first = True)
self.fc_layer = nn.Sequential(nn.Linear(self.num_hidden_unit,self.num_hidden_unit),
nn.Linear(self.num_hidden_unit,2))
def forward(self,x):
x = self.embed(x) # batch * max_seq_len * emb_dim
h_0,c_0 = self.init_hidden(x.size(0))
x, (_, _) = self.lstm_layer(x, (h_0,c_0)) # batch * seq_len * hidden_unit_num
if not self.use_hidden_average:
x = x[:,x.size(1)-1,:]
x = x.squeeze(1)
else:
x = x.mean(1).squeeze(1)
x = self.fc_layer(x)
return x
def init_hidden(self,batch_size):
h_0, c_0 = torch.zeros(self.num_lstm_layer,batch_size , self.num_hidden_unit),\
torch.zeros(self.num_lstm_layer,batch_size , self.num_hidden_unit)
h_0, c_0 = h_0.cuda(), c_0.cuda()
h_0_param, c_0_param = torch.nn.Parameter(h_0), torch.nn.Parameter(c_0)
return h_0_param, c_0_param
| model.cuda() is called inside your training/test loop, which is the problem. As the error message suggests, you repeatedly convert parameters(tensors) in your model to cuda, which is not the right way to convert model into cuda tensor.
model object should be created and cuda-ize outside the loop. Only training/test instances shall be convert to cuda tensor every time you feed your model. I also suggest you read examples code from pytorch document site.
| https://stackoverflow.com/questions/48400225/ |
Given input size: (128x1x1). Calculated output size: (128x0x0). Output size is too small | I am trying to train a U-Net which looks like this
`class UNet(nn.Module):
def __init__(self, imsize):
super(UNet, self).__init__()
self.imsize = imsize
self.activation = F.relu
self.pool1 = nn.MaxPool2d(2)
self.pool2 = nn.MaxPool2d(2)
self.pool3 = nn.MaxPool2d(2)
self.pool4 = nn.MaxPool2d(2)
self.conv_block1_64 = UNetConvBlock(4, 64)
self.conv_block64_128 = UNetConvBlock(64, 128)
self.conv_block128_256 = UNetConvBlock(128, 256)
self.conv_block256_512 = UNetConvBlock(256, 512)
self.conv_block512_1024 = UNetConvBlock(512, 1024)
self.up_block1024_512 = UNetUpBlock(1024, 512)
self.up_block512_256 = UNetUpBlock(512, 256)
self.up_block256_128 = UNetUpBlock(256, 128)
self.up_block128_64 = UNetUpBlock(128, 64)
self.last = nn.Conv2d(64, 1, 1)`
The loss function i am using is
`class BCELoss2d(nn.Module):
def __init__(self, weight=None, size_average=True):
super(BCELoss2d, self).__init__()
self.bce_loss = nn.BCELoss(weight, size_average)
def forward(self, logits, targets):
probs = F.sigmoid(logits)
probs_flat = probs.view(-1)
targets_flat = targets.view(-1)
return self.bce_loss(probs_flat, targets_flat)`
The input image tensor is [1,1,68,68] and labels are also of the same shape
I get this error:
<ipython-input-72-270210759010> in forward(self, x)
75
76 block4 = self.conv_block256_512(pool3)
---> 77 pool4 = self.pool4(block4)
78
79 block5 = self.conv_block512_1024(pool4)
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py in _ _call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/pooling.py in forward(self, input)
141 return F.max_pool2d(input, self.kernel_size, self.stride,
142 self.padding, self.dilation, self.ceil_mode,
--> 143 self.return_indices)
144
145 def __repr__(self):
/usr/local/lib/python3.5/dist-packages/torch/nn/functional.py in max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode, return_indices)
332 See :class:`~torch.nn.MaxPool2d` for details.
333 """
--> 334 ret = torch._C._nn.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
335 return ret if return_indices else ret[0]
336
RuntimeError: Given input size: (128x1x1). Calculated output size: (128x0x0). Output size is too small at /pytorch/torch/lib/THCUNN/generic/SpatialDilatedMaxPooling.cu:69
I'm guessing I'm making a mistake in my channel size or pooling size but i'm not sure where exactly is the mistake.
| Your problem is that before the Pool4 your image has already reduced to a 1x1pixel size image. So you need to either feed an much larger image of size at least around double that (~134x134) or remove a pooling layer in your network.
| https://stackoverflow.com/questions/48402009/ |
pytorch lstm tutorial initializing Variable | I am going through the pytorch tutorial for lstm and here's the code they use:
lstm = nn.LSTM(3, 3) # Input dim is 3, output dim is 3
inputs = [autograd.Variable(torch.randn((1, 3)))
for _ in range(5)] # make a sequence of length 5
# initialize the hidden state.
hidden = (autograd.Variable(torch.randn(1, 1, 3)),
autograd.Variable(torch.randn((1, 1, 3))))
for i in inputs:
# Step through the sequence one element at a time.
# after each step, hidden contains the hidden state.
out, hidden = lstm(i.view(1, 1, -1), hidden)
For variable hidden, it initializes into a tuple and the result is:
(Variable containing:
(0 ,.,.) =
0.4251 -1.2328 -0.6195
[torch.FloatTensor of size 1x1x3]
, Variable containing:
(0 ,.,.) =
1.5133 1.9954 -0.6585
[torch.FloatTensor of size 1x1x3]
)
What I don't understand is
Is (0, ., .) an index? And shouldn't it initialize all three numbers since we said (torch.randn(1,1,3))?
What is the difference between torch.randn(1, 1, 3) and torch.randn((1,1,3))?
| First to quickly answer number 2: They are identical. I don't know why they would do them differently.
Next, to answer question 1:
hidden is a tuple that contains two Variables that are essentially a 1 x 1 x 3 tensor.
Let's focus on what (0 ,.,.). If instead of a 1 x 1 x 3 tensor you had a 2 x 2 tensor, you could simply print out something like:
0.1 0.2
0.3 0.4
But it's kind of hard to represent 3 dimensional things on the screen. Even though it's a bit silly, having the aditional 1 at the beginning changes what would otherwise be a 2 dimensional tensor into a 3 dimensional one. So, instead Pytorch prints out "slices" of the tensor. In this case, you only have one "slice" which happens to be the zeroith slice. Thus you get the additional (0, ,.,.) instead of it just printing out
0.4251 -1.2328 -0.6195
If instead the dimensions were 2 x 1 x 3 you could expect an output like:
(0 ,.,.) =
-0.3027 -1.1077 0.4724
(1 ,.,.) =
1.0063 -0.5936 -1.1589
[torch.FloatTensor of size 2x1x3]
And as you can see every element in the tensor is actually initialized.
| https://stackoverflow.com/questions/48412696/ |
Pytorch transfer learning predictions | I have been following the pytorch transfer learning tutorial,and following the tutorial to my own dataset i have arrived at this
model_conv = train_model(model_conv, criterion, optimizer_conv,
exp_lr_scheduler, num_epochs=25)
Epoch 1/1
......
......
......
Epoch 24/24
train Loss: 0.8674 Acc: 0.5784
val Loss: 1.2930 Acc: 0.4583
Training complete in 43m 24s
Best val Acc: 0.486111
The tutorial ends here,but im not exaclty sure how do i get predictions for my new images?
Any suggestions would be really helpful.
Thanks in advance.
| Simply feed the new image (in the same format as the images in your training dataset were) to the model:
labels = model_conv(new_images)
| https://stackoverflow.com/questions/48414717/ |
How to setup pytorch in google-cloud-ml | I try to throw job with Pytorch code in google-cloud-ml.
so I code the "setup.py" file. And add option "install_requires"
"setup.py"
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['http://download.pytorch.org/whl/cpu/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl','torchvision']
setup(
name='trainer',
version='0.1',
install_requires=REQUIRED_PACKAGES,
packages=find_packages(),
include_package_data=True,
description='My keras trainer application package.'
)
and throw the job to the google-cloud-ml, but it doesn't work
with error message
{
insertId: "3m78xtf9czd0u"
jsonPayload: {
created: 1516845879.49039
levelname: "ERROR"
lineno: 829
message: "Command '['pip', 'install', '--user', '--upgrade', '--force-reinstall', '--no-deps', u'trainer-0.1.tar.gz']' returned non-zero exit status 1"
pathname: "/runcloudml.py"
}
labels: {
compute.googleapis.com/resource_id: "6637909247101536087"
compute.googleapis.com/resource_name: "cmle-training-master-5502b52646-0-ql9ds"
compute.googleapis.com/zone: "us-central1-c"
ml.googleapis.com/job_id: "run_ml_engine_pytorch_test_20180125_015752"
ml.googleapis.com/job_id/log_area: "root"
ml.googleapis.com/task_name: "master-replica-0"
ml.googleapis.com/trial_id: ""
}
logName: "projects/exem-191100/logs/master-replica-0"
receiveTimestamp: "2018-01-25T02:04:55.421517460Z"
resource: {
labels: {…}
type: "ml_job"
}
severity: "ERROR"
timestamp: "2018-01-25T02:04:39.490387916Z"
}
====================================================================
See detailed message here
so how can i use pytorch in google cloud ml engine?
|
i find solution about setting up PYTORCH in google-cloud-ml
first
you have to get a .whl file about pytorch and store it to google storage bucket.
and you will get the link for bucket link.
gs://bucketname/directory/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl
the .whl file is depend on your python version or cuda version....
second
you write the command line and setup.py because you have to set up the google-cloud-ml setting.
related link is this submit_job_to_ml-engine
you write the setup.py file to describe your setup.
the related link is this write_setup.py_file
this is my command code and setup.py file
=====================================================================
"command"
#commandline code
JOB_NAME="run_ml_engine_pytorch_test_$(date +%Y%m%d_%H%M%S)"
REGION=us-central1
OUTPUT_PATH=gs://yourbucket
gcloud ml-engine jobs submit training $JOB_NAME \
--job-dir $OUTPUT_PATH \
--runtime-version 1.4 \
--module-name models.pytorch_test \
--package-path models/ \
--packages gs://yourbucket/directory/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl \
--region $REGION \
-- \
--verbosity DEBUG
=====================================================================
"setup.py"
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['torchvision']
setup(
name='trainer',
version='0.1',
install_requires=REQUIRED_PACKAGES,
packages=find_packages(),
include_package_data=True,
description='My pytorch trainer application package.'
)
=====================================================================
third
if you have experience submitting job to the ml-engine.
you might know the file structure about submitting ml-engine
packaging_training_model.
you have to follow above link and know how to pack files.
| https://stackoverflow.com/questions/48434556/ |
PyTorch: training with GPU gives worse error than training the same thing with CPU | I have a next step prediction model on times series which is simply a GRU with a fully-connected layer on top of it. When I train it using CPU after 50 epochs I get a loss of 0.10 but when I train it with GPU the loss is 0.15 after 50 epochs. Doing more epochs doesnt really lower the losses in either cases.
Why is performance after training on CPU better than GPU?
I have tried changing the random seeds for both data and model, and these results are independent of the random seeds.
I have:
Python 3.6.2
PyTorch 0.3.0
CUDNN_MAJOR 7
CUDNN_MINOR 0
CUDNN_PATCHLEVEL 5
Edit:
I also use PyTorch's weight normalizaton torch.nn.utils.weight_norm on the GRU and on the fully-connected layer.
| After trying many things I think I found the problem. Apparently the CUDNN libraries are sub-optimal in PyTorch. I don't know if it is a bug in PyTorch or a bug in CUDNN but doing
torch.backends.cudnn.enabled = False
solves the problem. With the above line, training with GPU or CPU gives the same loss at the same epoch.
Edit:
It seems that it is the interaction of weight normalization and CUDNN which results in things going wrong. If I remove weight normalization it works. If I remove CUDNN it works. It seems that only in combination they do not work in PyTorch.
| https://stackoverflow.com/questions/48445942/ |
unknown resampling filter error when trying to create my own dataset with pytorch | I am trying to create a CNN implemented with data augmentation in pytorch to classify dogs and cats. The issue that I am having is that when I try to input my dataset and enumerate through it I keep getting this error:
Traceback (most recent call last):
File "<ipython-input-55-6337e0536bae>", line 75, in <module>
for i, (inputs, labels) in enumerate(trainloader):
File "/usr/local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 188, in __next__
batch = self.collate_fn([self.dataset[i] for i in indices])
File "/usr/local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 188, in <listcomp>
batch = self.collate_fn([self.dataset[i] for i in indices])
File "/usr/local/lib/python3.6/site-packages/torchvision/datasets/folder.py", line 124, in __getitem__
img = self.transform(img)
File "/usr/local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 42, in __call__
img = t(img)
File "/usr/local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 147, in __call__
return F.resize(img, self.size, self.interpolation)
File "/usr/local/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 197, in resize
return img.resize((ow, oh), interpolation)
File "/usr/local/lib/python3.6/site-packages/PIL/Image.py", line 1724, in resize
raise ValueError("unknown resampling filter")
ValueError: unknown resampling filter
and I really dont know whats wrong with my code. I have provided the code below:
# Creating the CNN
# Importing the libraries
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import torchvision
from torchvision import transforms
#Creating the CNN Model
class CNN(nn.Module):
def __init__(self, nb_outputs):
super(CNN, self).__init__() #activates the inheritance and allows the use of all the tools in the nn.Module
#making the 3 convolutional layers that will be used in the convolutional neural network
self.convolution1 = nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 5) #kernal_size -> the deminson of the feature detector e.g kernel_size = 5 => feature detector of size 5x5
self.convolution2 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 2)
#making 2 full connections one to connect the inputs of the ANN to the hidden layer and another to connect the hidden layer to the outputs of the ANN
self.fc1 = nn.Linear(in_features = self.count_neurons((1, 64,64)), out_features = 40)
self.fc2 = nn.Linear(in_features = 40, out_features = nb_outputs)
def count_neurons(self, image_dim):
x = Variable(torch.rand(1, *image_dim)) #this variable repersents a fake image to allow us to compute the number of neruons
#in order to pass the elements of the tuple image_dim into our function as a list of arguments we need to add a * before image_dim
#since x will be going into our neural network we need to convert it into a torch variable using the Variable() function
x = F.relu(F.max_pool2d(self.convolution1(x), 3, 2)) #first we apply the convolution to x then apply max_pooling to the convolutional fake images and then activate all the neurons in the pooling layer
x = F.relu(F.max_pool2d(self.convolution2(x), 3, 2)) #the signals are now propragated up to the thrid convoulational layer
#Now to flatten x to obtain the number of neurons in the flattening layer
return x.data.view(1, -1).size(1) #this will flatten x into a huge vector and returns the size of the vector, that size repersents the number of neurons that will be inputted into the ANN
#even though x is not a real image from the game since the size of the flattened vector only depends on the dimention of the inputted image we can just set x to have the same dimentions as the image
def forward(self, x):
x = F.relu(F.max_pool2d(self.convolution1(x), 3, 2)) #first we apply the convolution to x then apply max_pooling to the convolutional fake images and then activate all the neurons in the pooling layer
x = F.relu(F.max_pool2d(self.convolution2(x), 3, 2))
#flattening layer of the CNN
x = x.view(x.size(0), -1)
#x is now the inputs to the ANN
x = F.relu(self.fc1(x)) #we propagte the signals from the flatten layer to the full connected layer and activate the neruons by breaking the linearilty with the relu function
x = F.sigmoid(self.fc2(x))
#x is now the output neurons of the ANN
return x
train_tf = transforms.Compose([transforms.RandomHorizontalFlip(),
transforms.Resize(64,64),
transforms.RandomRotation(20),
transforms.RandomGrayscale(.2),
transforms.ToTensor()])
test_tf = transforms.Compose([transforms.Resize(64,64),
transforms.ToTensor()])
training_set = torchvision.datasets.ImageFolder(root = './dataset/training_set',
transform = train_tf)
test_set = torchvision.datasets.ImageFolder(root = './dataset/test_set',
transform = transforms.Compose([transforms.Resize(64,64),
transforms.ToTensor()]) )
trainloader = torch.utils.data.DataLoader(training_set, batch_size=32,
shuffle=True, num_workers=0)
testloader = torch.utils.data.DataLoader(test_set, batch_size= 32,
shuffle=False, num_workers=0)
#training the model
cnn = CNN(1)
cnn.train()
loss = nn.BCELoss()
optimizer = optim.Adam(cnn.parameters(), lr = 0.001) #the optimizer => Adam optimizer
nb_epochs = 25
for epoch in range(nb_epochs):
train_loss = 0.0
train_acc = 0.0
total = 0.0
for i, (inputs, labels) in enumerate(trainloader):
inputs, labels = Variable(inputs), Variable(labels)
cnn.zero_grad()
outputs = cnn(inputs)
loss_error = loss(outputs, labels)
optimizer.step()
_, pred = torch.max(outputs.data, 1)
total += labels.size(0)
train_loss += loss_error.data[0]
train_acc += (pred == labels).sum()
train_loss = train_loss/len(training_loader)
train_acc = train_acc/total
print('Epoch: %d, loss: %.4f, accuracy: %.4f' %(epoch+1, train_loss, train_acc))
The folder arrangement for the code is /dataset/training_set and inside the training_set folder are two more folders one for all the cat images and the other for all the dog images. Each image is name either dog.xxxx.jpg or cat.xxxx.jpg, where the xxxx represents the number so for the first cat image it would be cat.1.jpg up to cat.4000.jpg. This is the same format for the test_set folder. The number of training images is 8000 and the number of test images is 2000. If anyone can point out my error I would greatly appreciate it.
Thank you
| Try to set the desired size in transforms.Resize as a tuple:
transforms.Resize((64, 64))
PIL is using the second argument (in your case 64) as the interpolation method.
| https://stackoverflow.com/questions/48446898/ |
Problems with PyTorch MLP when training the MNIST dataset retrieved from Keras | I have finished a PyTorch MLP model for the MNIST dataset, but got two different results: 0.90+ accuracy when using MNIST dataset from PyTorch, but ~0.10 accuracy when using MNIST dataset from Keras.
Below is my code with dependency: PyTorch 0.3.0.post4, keras 2.1.3, tensorflow backend 1.4.1 gpu version.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import torch as pt
import torchvision as ptv
from keras.datasets import mnist
from torch.nn import functional as F
from torch.utils.data import Dataset, DataLoader
# training data from PyTorch
train_set = ptv.datasets.MNIST("./data/mnist/train", train=True, transform=ptv.transforms.ToTensor(), download=True)
test_set = ptv.datasets.MNIST("./data/mnist/test", train=False, transform=ptv.transforms.ToTensor(), download=True)
train_dataset = DataLoader(train_set, batch_size=100, shuffle=True)
test_dataset = DataLoader(test_set, batch_size=10000, shuffle=True)
class MLP(pt.nn.Module):
"""The Multi-layer perceptron"""
def __init__(self):
super(MLP, self).__init__()
self.fc1 = pt.nn.Linear(784, 512)
self.fc2 = pt.nn.Linear(512, 128)
self.fc3 = pt.nn.Linear(128, 10)
self.use_gpu = True
def forward(self, din):
din = din.view(-1, 28 * 28)
dout = F.relu(self.fc1(din))
dout = F.relu(self.fc2(dout))
# return F.softmax(self.fc3(dout))
return self.fc3(dout)
model = MLP().cuda()
print(model)
# loss func and optim
optimizer = pt.optim.SGD(model.parameters(), lr=1)
criterion = pt.nn.CrossEntropyLoss().cuda()
def evaluate_acc(pred, label):
pred = pred.cpu().data.numpy()
label = label.cpu().data.numpy()
test_np = (np.argmax(pred, 1) == label)
test_np = np.float32(test_np)
return np.mean(test_np)
def evaluate_loader(loader):
print("evaluating ...")
accurarcy_list = []
for i, (inputs, labels) in enumerate(loader):
inputs = pt.autograd.Variable(inputs).cuda()
labels = pt.autograd.Variable(labels).cuda()
outputs = model(inputs)
accurarcy_list.append(evaluate_acc(outputs, labels))
print(sum(accurarcy_list) / len(accurarcy_list))
def training(d, epochs):
for x in range(epochs):
for i, data in enumerate(d):
optimizer.zero_grad()
(inputs, labels) = data
inputs = pt.autograd.Variable(inputs).cuda()
labels = pt.autograd.Variable(labels).cuda()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if i % 200 == 0:
print(i, ":", evaluate_acc(outputs, labels))
# Training MLP for 4 epochs with MNIST dataset from PyTorch
training(train_dataset, 4)
# The accuracy is ~0.96.
evaluate_loader(test_dataset)
print("###########################################################")
def load_mnist():
(x, y), (x_test, y_test) = mnist.load_data()
x = x.reshape((-1, 1, 28, 28)).astype(np.float32)
x_test = x_test.reshape((-1, 1, 28, 28)).astype(np.float32)
y = y.astype(np.int64)
y_test = y_test.astype(np.int64)
print("x.shape", x.shape, "y.shape", y.shape,
"\nx_test.shape", x_test.shape, "y_test.shape", y_test.shape,
)
return x, y, x_test, y_test
class TMPDataset(Dataset):
"""Dateset for loading Keras MNIST dataset."""
def __init__(self, a, b):
self.x = a
self.y = b
def __getitem__(self, item):
return self.x[item], self.y[item]
def __len__(self):
return len(self.y)
x_train, y_train, x_test, y_test = load_mnist()
# Create dataloader for MNIST dataset from Keras.
test_loader = DataLoader(TMPDataset(x_test, y_test), num_workers=1, batch_size=10000)
train_loader = DataLoader(TMPDataset(x_train, y_train), shuffle=True, batch_size=100)
# Evaluate the performance of MLP trained on PyTorch dataset and the accurach is ~0.96.
evaluate_loader(test_loader)
evaluate_loader(train_loader)
model = MLP().cuda()
print(model)
optimizer = pt.optim.SGD(model.parameters(), lr=1)
criterion = pt.nn.CrossEntropyLoss().cuda()
# Train now on MNIST dataset from Keras.
training(train_loader, 4)
# Evaluate the trianed model on MNIST dataset from Keras and result in performance ~0.10...
evaluate_loader(test_loader)
evaluate_loader(train_loader)
I had checked some samples from Keras MNIST dataset and found no error.
I am wondering what is wrong with the datasets?
The code can run without error, run it to see the results.
| The MNIST data coming from Keras are not normalized; following the Keras MNIST MLP example, you should do it manually, i.e. you should include the following in your load_data() function:
x /= 255
x_test /= 255
Not sure about PyTorch, but it would seem that the MNIST data from their own utility functions come already normalized (as is the case with Tensorflow - see the third point in my answer here).
A 10% accuracy (i.e. equivalent to random guessing) in case of not-normalized input data is perfectly consistent.
| https://stackoverflow.com/questions/48477198/ |
How to find the index of a tensor in a list? | I want to find the index of the smallest tensor (by some key function) in a list li.
So I did min and afterwards li.index(min_el). My MWE suggests that somehow tensors don't work with index.
import torch
li=[torch.ones(1,1), torch.zeros(2,2)]
li.index(li[0])
0
li.index(li[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../local/lib/python2.7/site-packages/torch/tensor.py", line 330, in __eq__
return self.eq(other)
RuntimeError: inconsistent tensor size at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:2679
I can of course make my own index function that first checks for size and then element wise. E.g.
def index(list, element):
for i,el in enumerate(list):
if el.size() == element.size():
diff = el - element
if (1- diff.byte()).all():
return i
return -1
I was just wondering, why doesn't index work?
Is there maybe a smart way of doing this not by hand that I'm missing?
| You could directly find the index by using enumerate and a key function that operates on the second element of each tuple. For instance, if your key function compares the first element of the each tensor, you could use
ix, _ = min(enumerate(li), key=lambda x: x[1][0, 0])
I think the reason why index succeeds for the first element is that Python probably does something that is equivalent to x is value or x == value, where x is the current element in the list. Since value is value, the failing equality comparison never happens. This is why li.index(li[0]) works but this fails:
y = torch.ones(1,1)
li.index(y)
| https://stackoverflow.com/questions/48527722/ |
Loss is increasing from first epoch itself | I am training my siamese network for nlp. I have used lstm in it. and BCELoss. My loss is increasing from the first epoch. The first 36 epoch loss is
error after 0 is
272.4357
[torch.FloatTensor of size 1]
error after 1 is
271.8972
[torch.FloatTensor of size 1]
error after 2 is
271.5598
[torch.FloatTensor of size 1]
error after 3 is
271.6979
[torch.FloatTensor of size 1]
error after 4 is
271.7315
[torch.FloatTensor of size 1]
error after 5 is
272.3965
[torch.FloatTensor of size 1]
error after 6 is
273.3982
[torch.FloatTensor of size 1]
error after 7 is
275.1197
[torch.FloatTensor of size 1]
error after 8 is
275.8228
[torch.FloatTensor of size 1]
error after 9 is
278.3311
[torch.FloatTensor of size 1]
error after 10 is
277.1054
[torch.FloatTensor of size 1]
error after 11 is
277.8418
[torch.FloatTensor of size 1]
error after 12 is
279.0189
[torch.FloatTensor of size 1]
error after 13 is
278.4090
[torch.FloatTensor of size 1]
error after 14 is
281.8813
[torch.FloatTensor of size 1]
error after 15 is
283.4077
[torch.FloatTensor of size 1]
error after 16 is
286.3093
[torch.FloatTensor of size 1]
error after 17 is
287.6292
[torch.FloatTensor of size 1]
error after 18 is
297.2318
[torch.FloatTensor of size 1]
error after 19 is
307.4176
[torch.FloatTensor of size 1]
error after 20 is
304.6649
[torch.FloatTensor of size 1]
error after 21 is
328.9772
[torch.FloatTensor of size 1]
error after 22 is
300.0669
[torch.FloatTensor of size 1]
error after 23 is
292.3902
[torch.FloatTensor of size 1]
error after 24 is
300.8633
[torch.FloatTensor of size 1]
error after 25 is
305.1822
[torch.FloatTensor of size 1]
error after 26 is
333.9984
[torch.FloatTensor of size 1]
error after 27 is
346.2062
[torch.FloatTensor of size 1]
error after 28 is
354.6148
[torch.FloatTensor of size 1]
error after 29 is
341.3568
[torch.FloatTensor of size 1]
error after 30 is
369.7580
[torch.FloatTensor of size 1]
error after 31 is
366.1615
[torch.FloatTensor of size 1]
error after 32 is
368.2455
[torch.FloatTensor of size 1]
error after 33 is
391.4102
[torch.FloatTensor of size 1]
error after 34 is
394.3190
[torch.FloatTensor of size 1]
error after 35 is
401.0990
[torch.FloatTensor of size 1]
error after 36 is
422.3723
[torch.FloatTensor of size 1]
| Probably your learning rate is too high. Try decreasing your learning rate. A too large learning rate is the most common reason for loss increasing from the first epoch.
Also your loss is very high. It is unusual to have such a high lost. You probably have a sum in your loss function, it might be wiser to replace that sum with a mean. While this makes no difference if you use the Adam optimizer, if you use simple SGD with or without momentum using a sum instead of a mean, means that you will need to tune your learning rate differently if the dimensions (or the length of your sequence processed by your lstm) of your system changes.
| https://stackoverflow.com/questions/48527808/ |
Pytorch Test Loss increases while accuracy increases | I am trying to implement
End to End Memory Network using Pytorch and BabI dataset.
The network architecture is :
MemN2N (
(embedding_A): Embedding(85, 120, padding_idx=0)
(embedding_B): Embedding(85, 120, padding_idx=0)
(embedding_C): Embedding(85, 120, padding_idx=0)
(match): Softmax ()
)
85 is the vocabulary size and 120 is embedding size.
Loss function is cross entropy and optimizer is RmsProp.
The results is
Epoch Train Loss Test Loss TrainAcc TestAcc
10 0.608 11.213 1.0 0.99
20 0.027 11.193 1.0 0.99
30 0.0017 11.740 1.0 0.99
40 0.0006 12.190 1.0 0.99
50 5.597e-05 12.319 1.0 0.99
60 3.366-05 12.379 1.0 0.99
70 2.72e-05 12.361 1.0 0.99
80 2.64e-05 12.333 1.0 0.99
90 2.63e-05 12.329 1.0 0.99
100 2.63e-05 12.329 1.0 0.99
110 2.63e-05 12.329 1.0 0.99
120 2.63e-05 12.329 1.0 0.99
Final TrainAcc TestAcc
1.0 0.999
I know the accuracy is good, but I wonder the behaviour of the test loss.
Since training loss decreases, the test loss increases.
The calculation is the same for each loss value. Shouldn't it decrease too? I used Task 1 to display, but the behaviour is the same with other tasks.
Do you have any idea about this behaviour?
| When training loss continues to decrease but test loss starts to increase, that is the moment you are starting to overfit, that means that your network weights are fitting the data you are training on better and better, but this extra fitting will not generalize to new unseen data. This means that that is the moment you should stop training.
You are embedding 80 words in 120 dimensions, so you have no information bottle neck at all, you have much too many dimensions for only 80 words. You have so many free parameters you can fit anything, even noise. Try changing 120 for 10 and probably you will not overfit anymore. If you try using 2 dimensions instead of 120, then you will probably underfit.
Overfitting: When your model has enough capacity to fit particularities of your training data which doesn't generalize to new data from the same distribution.
Underfitting: When your model does not have enough capacity to fit even your training data (you cannot bring your training loss "close" to zero).
In your case, I am guessing that your model becomes over-confident on your training data (output probabilities too close to 1 or 0) which is justified in the case of the training data but which is too confident for your test data (or any other data you didn't train on).
| https://stackoverflow.com/questions/48541336/ |
How to calculate cost for softmax regression with pytorch | I would to calculate the cost for the softmax regression.
The cost function to calculate is given at the bottom of the page.
For numpy I can get the cost as follows:
"""
X.shape = 2,300 # floats
y.shape = 300, # integers
W.shape = 2,3
b.shape = 3,1
"""
import numpy as np
np.random.seed(100)
# Data and labels
X = np.random.randn(300,2)
y = np.ones(300)
y[0:100] = 0
y[200:300] = 2
y = y.astype(np.int)
# weights and bias
W = np.random.randn(2,3)
b = np.random.randn(3)
N = X.shape[0]
scores = np.dot(X, W) + b
hyp = np.exp(scores-np.max(scores, axis=0, keepdims=True))
probs = hyp / np.sum(hyp, axis = 0)
logprobs = np.log(probs[range(N),y])
cost_data = -1/N * np.sum(logprobs)
print("hyp.shape = {}".format(hyp.shape)) # hyp.shape = (300, 3)
print(cost_data)
But, when I tried torch I could not get this.
So far I have got this:
"""
X.shape = 2,300 # floats
y.shape = 300, # integers
W.shape = 2,3
b.shape = 3,1
"""
import numpy as np
import torch
from torch.autograd import Variable
np.random.seed(100)
# Data and labels
X = np.random.randn(300,2)
y = np.ones(300)
y[0:100] = 0
y[200:300] = 2
y = y.astype(np.int)
X = Variable(torch.from_numpy(X),requires_grad=True).type(torch.FloatTensor)
y = Variable(torch.from_numpy(y),requires_grad=True).type(torch.LongTensor)
# weights and bias
W = Variable(torch.randn(2,3),requires_grad=True)
b = Variable(torch.randn(3),requires_grad=True)
N = X.shape[0]
scores = torch.mm(X, W) + b
hyp = torch.exp(scores - torch.max(scores))
probs = hyp / torch.sum(hyp)
correct_probs = probs[range(N),y] # got problem HERE
# logprobs = np.log(correct_probs)
# cost_data = -1/N * torch.sum(logprobs)
# print(cost_data)
I got problem calculating the correct probabilities for the classes.
How can we solve this problem and get the correct cost value.
The cost function to calculate is given below:
| Your problem is that you cannot use range(N) with pytorch, use the slice 0:N instead:
hyp = torch.exp(scores - torch.max(scores))
probs = hyp / torch.sum(hyp)
correct_probs = probs[0:N,y] # problem solved
logprobs = torch.log(correct_probs)
cost_data = -1/N * torch.sum(logprobs)
Another point is that your labels y do not require gradients, you would better have:
y = Variable(torch.from_numpy(y),requires_grad=False).type(torch.LongTensor)
| https://stackoverflow.com/questions/48549101/ |
How to take the average of the weights of two networks? | Suppose in PyTorch I have model1 and model2 which have the same architecture. They were further trained on same data or one model is an earlier version of the othter, but it is not technically relevant for the question. Now I want to set the weights of model to be the average of the weights of model1 and model2. How would I do that in PyTorch?
| beta = 0.5 #The interpolation parameter
params1 = model1.named_parameters()
params2 = model2.named_parameters()
dict_params2 = dict(params2)
for name1, param1 in params1:
if name1 in dict_params2:
dict_params2[name1].data.copy_(beta*param1.data + (1-beta)*dict_params2[name1].data)
model.load_state_dict(dict_params2)
Taken from pytorch forums. You could grab the parameters, transform and load them back but make sure the dimensions match.
Also I would be really interested in knowing about your findings with these..
| https://stackoverflow.com/questions/48560227/ |
ImportError when importing pytorch | When I try importing pytorch inside a jupyter notebook i get the following error:
ImportError: dlopen: cannot load any more object with static TLS
pytorch
import torch
No error when i import torch from command line (not jupyter notebook)
| For reasons i cannot yet explain, i found out that this is happening only when i install Keras too in the same virtual environment, and it goes away when i uninstall Keras.
So the solution was
pip uninstall keras
| https://stackoverflow.com/questions/48564959/ |
pytorch variable index lost one dimension | I am going to get every horizontal tensor in a variable but I got one dimension lost.
This is my code:
import torch
from torch.autograd import Variable
t = torch.rand((2,2,4))
x = Variable(t)
print(x)
shape = x.size()
for i in range(shape[0]):
for j in range(shape[1]):
print(x[i,j])
and the output is :
Variable containing:
(0 ,.,.) =
0.6717 0.8216 0.5100 0.9106
0.3280 0.8182 0.5781 0.3919
(1 ,.,.) =
0.8823 0.4237 0.6620 0.0817
0.5781 0.4187 0.3769 0.0498
[torch.FloatTensor of size 2x2x4]
Variable containing:
0.6717
0.8216
0.5100
0.9106
[torch.FloatTensor of size 4]
Variable containing:
0.3280
0.8182
0.5781
0.3919
[torch.FloatTensor of size 4]
Variable containing:
0.8823
0.4237
0.6620
0.0817
[torch.FloatTensor of size 4]
Variable containing:
0.5781
0.4187
0.3769
0.0498
[torch.FloatTensor of size 4]
and how can I get [torch.FloatTensor of size 1x4]?
| In your case, x is a 2x2x4 tensor. So when you do x[0] you obtain the 2x4 tensor which is in the first row. And if you do x[i,j] you obtain the 4 dimensional vector in position (i,j). If you want to retain one of the dimensions you can either use a slice: x[i,j:j+1] OR reshape the tensor: x[i,j].view(1,4). Thus your code would look like:
import torch
from torch.autograd import Variable
t = torch.rand((2,2,4))
x = Variable(t)
print(x)
shape = x.size()
for i in range(shape[0]):
for j in range(shape[1]):
print(x[i,j:j+1])
or
import torch
from torch.autograd import Variable
t = torch.rand((2,2,4))
x = Variable(t)
print(x)
shape = x.size()
for i in range(shape[0]):
for j in range(shape[1]):
print(x[i,j].view(1,4)
Will give you the desired result.
Edit:
Yes, or as mentioned in the answer by nnnmmm, torch.unsqueeze(x[i, j], 0) also works as it adds a dimension of size 1 in the 0th position.
| https://stackoverflow.com/questions/48577014/ |
How can I find the equivalent 'batch_size' used in Keras from this Pytorch code? | I am using Pytorch code from github
I am trying to port this over to Keras.
In particular, Keras uses model.fit for training the Neural net and has a batch_size parameter. I am trying to set this but cannot determine it in the Pytorch script linked above.
In the script, there is a function called sliding_window in box 4 where there is a parameter called step. I am not sure if this is the equivalent setting of batch_size.
Also, I am looking into how to set the learning rate scheduler from the box 11 in the Pytorch code:
base_lr = 0.01
params_dict = dict(net.named_parameters())
params = []
for key, value in params_dict.items():
if '_D' in key:
# Decoder weights are trained at the nominal learning rate
params += [{'params':[value],'lr': base_lr}]
else:
# Encoder weights are trained at lr / 2 (we have VGG-16 weights as initialization)
params += [{'params':[value],'lr': base_lr / 2}]
optimizer = optim.SGD(net.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0005)
# We define the scheduler
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [25, 35, 45], gamma=0.1)
I have always used the default learning rate from Keras. Any insight on how to go about converting this scheduler into Keras code would also be appreciated.
| Below find an example on how to write a Learning Rate scheduler in Keras:
from keras.callbacks import Callback
from keras import backed as K
class LRSchedule(Callback):
def __init__(self, schedule):
super(LRSchedule, self).__init__()
self.schedule = schedule
def on_train_begin(self, logs = {}):
self.epoch_counter = 0
self.schedule_index = 0
def on_epoch_end(self, epoch, logs = {}):
self.epoch_counter += 1
if len(self.schedule) > self.schedule_index + 1:
next_epoch = self.schedule[self.schedule_index + 1]
if self.epoch_counter == next_epoch:
K.set_value(self.model.optimizer.lr, self.model.optimizer.lr / 2.0)
self.schedule_index += 1
In your code, you will call the callback in this way:
lr_scheduler = LRSchedule([25, 35, 45])
model.fit_generator(..., callbacks = [lr_scheduler])
Notice that this scheduler, when an epoch is reached, sets the learning to a lower value dividing by 2. Modifying it to more fancy schedule policies is trivial.
| https://stackoverflow.com/questions/48586429/ |
Kaggle Pytorch run length encoding | Im working on the DSB problem in pytorch,I have my predictions but im not sure how to get those into the run length encoding format that is required for the submission,In short this is what it is
===================================================================
In order to reduce the submission file size, our metric uses run-length encoding on the pixel values. Instead of submitting an exhaustive list of indices for your segmentation, you will submit pairs of values that contain a start position and a run length. E.g. '1 3' implies starting at pixel 1 and running a total of 3 pixels (1,2,3).
The competition format requires a space delimited list of pairs. For example, '1 3 10 5' implies pixels 1,2,3,10,11,12,13,14 are to be included in the mask. The pixels are one-indexed and numbered from top to bottom, then left to right: 1 is pixel (1,1), 2 is pixel (2,1), etc.
===================================================================
I get the predictions like this
model = model.eval()
for data in testdataloader:
data = t.autograd.Variable(data.cuda(device = 1))
pred = model(data)
but now that i have my prediction im not sure how to move forward,
I found this script online,but im not sure how to modify this for my use case
def rle_encoding(x):
dots = np.where(x.T.flatten() == 1)[0]
run_lengths = []
prev = -2
for b in dots:
if (b>prev+1): run_lengths.extend((b + 1, 0))
run_lengths[-1] += 1
prev = b
return run_lengths
def prob_to_rles(x, cutoff=0.5):
lab_img = label(x > cutoff)
for i in range(1, lab_img.max() + 1):
yield rle_encoding(lab_img == i)
Any suggestions on how may i get started or how do i modify this would be really helpfull!
| Try if something like that works
def rle_encode(image):
"""
receives a masked image and encodes it to RLE
:param mask_image:
:return: string corresponding to the rle of the input image
"""
pixels = image.flatten()
# We avoid issues with '1' at the start or end (at the corners of
# the original image) by setting those pixels to '0' explicitly.
# We do not expect these to be non-zero for an accurate mask,
# so this should not harm the score.
pixels[0] = 0
pixels[-1] = 0
runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
runs[1::2] = runs[1::2] - runs[:-1:2]
return ' '.join(str(x) for x in runs)
# create the file path
f = open(args.submit_dir + args.arch + '.csv', 'w')
# add the header of the csv file
f.write('img,rle_mask\n')
# NOTE: put this part in the test loop to generate the output file
f.write(image_name ',' + data_utils.mask_to_RLEstring(net_output.numpy()) + '\n')
| https://stackoverflow.com/questions/48599440/ |
Where is the len function used in PyTorch Dataset? | I am looking to use the code from here .
However, I am looking at box 5, where there is the following function;
def __len__(self):
# Default epoch size is 10 000 samples
return 10000
I do not see anywhere throughout this script where this function is being used.
Clarification on this would be appreciated.
Also, I want to determine the number of image patches used for training this convolutional neural network. Is this len function linked to the number of patches?
| This is a function of the Dataset class. The __len__() function specifies the size of the dataset. In your referenced code, in box 10, a dataset is initialized and passed to a DataLoader object:
train_set = ISPRS_dataset(train_ids, cache=CACHE)
train_loader = torch.utils.data.DataLoader(train_set,batch_size=BATCH_SIZE)
You see that in the DataLoader the dataset object is passed as well as the batch size. The DataLoader object then uses the __len__ function of the Dataset to create the batches. This happens in box 13, where it is iterated over the DataLoader.
| https://stackoverflow.com/questions/48608585/ |
Result of slicing in an empty tensor | Can anyone help me to understand why this works:
lens = list(range(170,1,-1))
xs = Variable(torch.randn(169, 200, 1))
packed = torch.nn.utils.rnn.pack_padded_sequence(xs, lens, batch_first=True)
and this does not:
lens = [294, 289, 288, 282, 273, 270, 261, 260, 240, 235, 231, 228, 228, 227, 226, 226, 199, 195, 194, 192, 190, 189, 177, 176, 165, 165, 161, 156, 153, 149, 149, 142, 142, 137, 136, 136, 135, 134, 134, 132, 131, 129, 122, 121, 121, 114, 113, 113, 112, 110, 109, 108, 107, 107, 106, 105, 105, 103, 102, 100, 99, 99, 98, 96, 95, 93, 92, 91, 91, 90, 88, 88, 87, 79, 78, 78, 77, 76, 75, 74, 73, 72, 72, 71, 71, 71, 71, 69, 69, 69, 68, 68, 68, 68, 68, 68, 67, 67, 66, 66, 65, 65, 64, 64, 64, 63, 63, 61, 61, 61, 61, 60, 60, 59, 59, 59, 59, 57, 57, 57, 57, 57, 56, 56, 55, 55, 54, 54, 54, 54, 54, 53, 53, 52, 52, 52, 51, 51, 51, 51, 51, 50, 50, 50, 49, 49, 49, 48, 47, 47, 47, 47, 46, 46, 46, 45, 44, 44, 44, 44, 43, 42, 39, 38, 36, 30, 30, 25, 23]
xs = Variable(torch.randn(169, 200, 1))
packed = torch.nn.utils.rnn.pack_padded_sequence(xs, lens, batch_first=True)
I am getting a "ValueError: result of slicing is an empty tensor"
| Answering my own question.
In fact it was answered by SimomW at the Pytorch forum: https://discuss.pytorch.org/t/result-of-slicing-is-an-empty-tensor/13306
It happens because the first len has values all <= 200, but the second has many > 200, which is the maximum seq_len a tensor of shape 169,200,1 can have.
| https://stackoverflow.com/questions/48626992/ |
Simple Pytorch Example - Loss on training doesnt decrease | I am just starting to try and learn pytorch and am finding it frustrating regardless of how it is advertised :)
Here I am running a simple regression as an experiment but since the loss doesn't seem to be decreasing with each epoch (on the training) I must be doing something wrong -- either in training or how I am collecting the MSE?
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.utils.data as utils_data
from torch.autograd import Variable
from torch import optim, nn
from torch.utils.data import Dataset
import torch.nn.functional as F
from sklearn.datasets import load_boston
cuda=True
#regular old numpy
boston = load_boston()
x=boston.data
y=boston.target
x.shape
training_samples = utils_data.TensorDataset(x, y)
data_loader = utils_data.DataLoader(training_samples, batch_size=10)
len(data_loader) #number of batches in an epoch
#override this
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
#all the layers
self.fc1 = nn.Linear(x.shape[1], 50)
self.drop = nn.Dropout(p=0.2)
self.fc2 = nn.Linear(50, 1)
#
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.drop(x)
x = self.fc2(x)
return x
net=Net()
if cuda:
net.cuda()
print(net)
# create a stochastic gradient descent optimizer
optimizer = optim.Adam(net.parameters())
# create a loss function (mse)
loss = nn.MSELoss()
# create a stochastic gradient descent optimizer
optimizer = optim.Adam(net.parameters())
# create a loss function (mse)
loss = nn.MSELoss()
# run the main training loop
epochs =20
hold_loss=[]
for epoch in range(epochs):
cum_loss=0.
for batch_idx, (data, target) in enumerate(data_loader):
tr_x, tr_y = Variable(data.float()), Variable(target.float())
if cuda:
tr_x, tr_y = tr_x.cuda(), tr_y.cuda()
# Reset gradient
optimizer.zero_grad()
# Forward pass
fx = net(tr_x)
output = loss(fx, tr_y) #loss for this batch
cum_loss += output.data[0]
# Backward
output.backward()
# Update parameters based on backprop
optimizer.step()
hold_loss.append(cum_loss)
#print(epoch+1, cum_loss) #
plt.plot(np.array(hold_loss))
| Well I just changed the line:
training_samples = utils_data.TensorDataset(torch.from_numpy(x), torch.from_numpy(y))
Adding the torch.from_numpy (otherwise, it was throwing an error, thus nor running) and I get a learning curve that looks something like this:
| https://stackoverflow.com/questions/48675563/ |
Installing Pytorch on Windows 10 | I am trying to install Pytorch on Windows 10 anaconda environment with Python 3.6 using the following command:
conda install -c peterjc123 pytorch
But it gives the following error:
UnsatisfiableError: The following specifications were found to be in conflict:
- curl -> krb5=1.14 -> *[track_features=vc14]
- curl -> libssh2=1.8 -> vc==14
- pytorch
Use "conda info " to see the dependencies for each package.
The same goes for the new updated December code
conda install -c peterjc123 pytorch cuda80
Is there a way which will help me install Pytorch on Windows ?
| I solved the issue by side loading the pytorch's tar.bz2 file
| https://stackoverflow.com/questions/48675722/ |
reshaping a tensor with padding in pytorch | How do I reshape a tensor with dimensions (30, 35, 49) to (30, 35, 512) by padding it?
|
While @nemo's solution works fine, there is a pytorch internal routine, torch.nn.functional.pad, that does the same - and which has a couple of properties that a torch.ones(*sizes)*pad_value solution does not (namely other forms of padding, like reflection padding or replicate padding ... it also checks some gradient-related properties):
import torch.nn.functional as F
source = torch.rand((5,10))
# now we expand to size (7, 11) by appending a row of 0s at pos 0 and pos 6,
# and a column of 0s at pos 10
result = F.pad(input=source, pad=(0, 1, 1, 1), mode='constant', value=0)
The semantics of the arguments are:
input: the source tensor,
pad: a list of length 2 * len(source.shape) of the form (begin last axis, end last axis, begin 2nd to last axis, end 2nd to last axis, begin 3rd to last axis, etc.) that states how many dimensions should be added to the beginning and end of each axis,
mode: 'constant', 'reflect' or 'replicate'. Default: 'constant' for the different kinds of padding
value for constant padding.
| https://stackoverflow.com/questions/48686945/ |
How can I download and skip VGG weights that have no counterpart with my CNN in Keras? | I would like to follow the Convolutional Neural Net (CNN) approach here. However, this code in github uses Pytorch, whereas I am using Keras.
I want to reproduce boxes 6,7 and 8 where pre-trained weights from VGG-16 on ImageNet is downloaded and is used to make the CNN converge faster.
In particular, there is a portion (box 8) where weights are downloaded and skipped from VGG-16 that have no counterpart in SegNet (the CNN model). In my work, I am using a CNN model called U-Net instead of Segnet. The U-Net Keras code that I am using can be found here.
I am new to Keras and would appreciate any insight in Keras code on how I can go about downloading and skipping the VGG weights that have no counterpart with my U-Netmodel.
| The technique you are addressing is called "Transfer Learning" - when a pre-trained model on a different dataset is used as part of the model as a starting point for better convergence. The intuition behind it is simple: we assume that after training on such a large and rich dataset as ImageNet, the convolution kernels of the model will learn useful representations.
In your specific case, you want to stack VGG16 weights in the bottom and deconvolution blocks on the top. I will go step-by-step, as you pointed out that you are new to Keras. This answer is organized as a step-by-step tutorial and will provide small snippets for you to use in your own code.
Loading weights
In the PyTorch code you linked to above, the model was first defined, and only then the weights are copied. I found this approach abundant, as it contains lots of not necessary code. Here, we will load VGG16 first, and then stack the other layers on the top.
from keras import applications
from keras.layers import Input
# Loading without top layers, since you only need convolution. Note that by not
# specifying the shape of top layers, the input tensor shape is (None, None, 3),
# so you can use them for any size of images.
vgg_model = applications.VGG16(weights='imagenet', include_top=False)
# If you want to specify input tensor shape, e.g. 256x256 with 3 channels:
input_tensor = Input(shape=(256, 256, 3))
vgg_model = applications.VGG16(weights='imagenet',
include_top=False,
input_tensor=input_tensor)
# To see the models' architecture and layer names, run the following
vgg_model.summary()
Defining the U-Net computation graph with VGG16 on the bottom
As pointed in previous paragraph, you do not need to define a model and copy the weights over. Just stack other layers on top of vgg_model:
# Import the layers to be used in U-Net
from keras.layers import ...
# From the U-Net code you provided
def make_conv_block(nb_filters, input_tensor, block):
...
# Creating dictionary that maps layer names to the layers
layers = dict([(layer.name, layer) for layer in vgg_model.layers])
# Getting output tensor of the last VGG layer that we want to include.
# I don't know much about U-Net, but according to the code you provided,
# you don't need the last pooling layer, right?
vgg_top = layers['block5_conv3'].output
# Now getting bottom layers for multi-scale skip-layers
block1_conv2 = layers['block1_conv2'].output
block2_conv2 = layers['block2_conv2'].output
block3_conv3 = layers['block3_conv3'].output
block4_conv3 = layers['block4_conv3'].output
# Stacking the remaining layers of U-Net on top of it (modified from
# the U-Net code you provided)
up6 = Concatenate()([UpSampling2D(size=(2, 2))(vgg_top), block4_conv3])
conv6 = make_conv_block(256, up6, 6)
up7 = Concatenate()([UpSampling2D(size=(2, 2))(conv6), block3_conv3])
conv7 = make_conv_block(128, up7, 7)
up8 = Concatenate()([UpSampling2D(size=(2, 2))(conv7), block2_conv2])
conv8 = make_conv_block(64, up8, 8)
up9 = Concatenate()([UpSampling2D(size=(2, 2))(conv8), block1_conv2])
conv9 = make_conv_block(32, up9, 9)
conv10 = Conv2D(nb_labels, (1, 1), name='conv_10_1')(conv9)
x = Reshape((nb_rows * nb_cols, nb_labels))(conv10)
x = Activation('softmax')(x)
outputs = Reshape((nb_rows, nb_cols, nb_labels))(x)
I want to emphasize that what we've done in this paragraph is just defining the computation graph for U-Net. This code is written specifically for VGG16, but you can modify it for other architectures as you wish.
Creating a model
After the previous step, we've got a computational graph (I assume that you use Tensorflow backend for Keras. If you're using Theano, I recommend you to switch to Tensorflow since this framework has achieved a state of maturity now). Now, we need to do the following things:
Create a model on top of this computation graph
Freeze the bottom layers, since you don't want to wreck your pre-trained weights
# Creating new model. Please note that this is NOT a Sequential() model
# as in commonly found tutorials on the internet.
from keras.models import Model
custom_model = Model(inputs=vgg_model.input, outputs=outputs)
# Make sure that the pre-trained bottom layers are not trainable.
# Here, I freeze all the layers of VGG16 (layers 0-18, including the
# pooling ones.
for layer in custom_model.layers[:19]:
layer.trainable = False
# Do not forget to compile it before training
custom_model.compile(loss='your_loss',
optimizer='your_optimizer',
metrics=['your_metrics'])
"I got confused"
Assuming that you're new to Keras and to Deep Learning in general (as you admitted in your question), I recommend the following articles to read to further understand the process of Fine Tuning and Transfer Learning on Keras:
How CNNs see the world - a great short article that will give you intuitive understanding of the dirty magic behind Transfer Learning.
Building powerful image classification models using very little data - This one will give you more insight on how to adjust learning rates and "release" the frozen layers.
When you're learning a framework, documentation is your best friend. Fortunately, Keras has an incredible documentation.
Q&A
The deconvolution blocks we put on top of VGG are from the UNET achitecture (i.e. up6 to conv10)? Please confirm.
Yes, it's the same as here, just with different names of the skip-connection layers (e.g. block1_conv2 instead of conv1)
We leave out the conv layers (i.e., conv1 to conv5). Can you please share with me as to why this is so?
We don't leave or throw any layers from the VGG network. The VGG16 network architecture and the bottom architecture of U-Net (up to conv5) is very similar. In fact, they are made of 5 blocks of the following format:
+-----------------+-------------------+
| VGG conv blocks | U-Net conv blocks |
+-----------------+-------------------+
| blockX_conv1 | convN |
| ... | poolN |
| blockX_convN | |
| blockX_pool | |
+-----------------+-------------------+
Here is a better visualization. So, the only difference between VGG16 and bottom part of U-Net is that each block of VGG16 contains multiple convolution layers instead of one. That's why, the alternative of connecting conv3 to conv6 is connecting block3_conv3 to conv6. The U-Net architecture remains the same, just with more convolution layers on the bottom.
Is there anyway to incorporate the Max pooling in the conv layers (in your opinion what are we doing here by leaving them out, and would you say it is insignificant?)
We don't leave them out. The only pooling layer that I threw away is block5_pool (which is the last layer in bottom part of VGG16) - because in the original U-Net (refer to the code) it seems like the last convolution block in the bottom part is not followed by a pooling layer (we have conv5 but don't have pool5). I kept all the layers of VGG16.
We see Maxpooling being used on the convolution blocks. Would we also just simply drop these pooling layers (as we are doing here with Unet) if we wanted to combine Segnet with VGG?
As I explained in the question above, we are not dropping any pooling layers.
However, you would need to stack a different type of pooling layers instead of the simple MaxPooling2D that is used in the default VGG16, because SegNet preserves max-indexes. This can be achieved with tf.nn.max_pool_with_argmax and using the trick of replacing middle layers of Keras model (I won't cover the detailed information in this answer to keep it clean). The replacement is harmless and doesn't require re-training because pooling layers don't contain any trained weights.
The U-NET from here is different from what I am using, can you tell what is the impact of such a difference between the two?
It is a more shallow U-Net. The one in your original question has 5 convolution blocks on the bottom (conv1 - conv5), while the later only has 3. Choose how many blocks you need depending on the data (e.g. for simple data as cells you might want to use only 2-3 blocks, while gray matter or tissue segmentation might require 5 blocks for better quality. See this link to have an insight of what convolution kernels "see".
Also, what do you think about the VGGSegnet from here. Does it use the trick of the middle layers you mentioned in Q&A? And is it the equivalent of the Pytorch code I initially posted?
Interesting. It is an incorrect implementation, and is not equivalent to the Pytorch code you posted. I have opened an issue in that repository.
Final question....is it always a rule in Transfer Learning to put the pretrained model (i.e., the model w/ pretrained weights) at the bottom?
Generally it is. Think of the convolution kernels as "features": the first layer detects small edges, colors. The following layers combines those edges and colors into more complicated detections, like "yellow lines" or "blue circle". Then the upper convolution layers detects more abstract shapes as "eyes", "nose", etc. based on detections of lower layers. So replacing the bottom layers (while the upper layers depends on the bottom representation) is illogic.
| https://stackoverflow.com/questions/48716184/ |
Error when implementing RBF kernel bandwidth differentiation in Pytorch | I'm implementing an RBF network by using some beginer examples from Pytorch Website. I have a problem when implementing the kernel bandwidth differentiation for the network. Also, Iwould like to know whether my attempt ti implement the idea is fine. This is a code sample to reproduce the issue. Thanks
# -*- coding: utf-8 -*-
import torch
from torch.autograd import Variable
def kernel_product(x,y, mode = "gaussian", s = 1.):
x_i = x.unsqueeze(1)
y_j = y.unsqueeze(0)
xmy = ((x_i-y_j)**2).sum(2)
if mode == "gaussian" : K = torch.exp( - xmy/s**2) )
elif mode == "laplace" : K = torch.exp( - torch.sqrt(xmy + (s**2)))
elif mode == "energy" : K = torch.pow( xmy + (s**2), -.25 )
return torch.t(K)
class MyReLU(torch.autograd.Function):
"""
We can implement our own custom autograd Functions by subclassing
torch.autograd.Function and implementing the forward and backward passes
which operate on Tensors.
"""
@staticmethod
def forward(ctx, input):
"""
In the forward pass we receive a Tensor containing the input and return
a Tensor containing the output. ctx is a context object that can be used
to stash information for backward computation. You can cache arbitrary
objects for use in the backward pass using the ctx.save_for_backward method.
"""
ctx.save_for_backward(input)
return input.clamp(min=0)
@staticmethod
def backward(ctx, grad_output):
"""
In the backward pass we receive a Tensor containing the gradient of the loss
with respect to the output, and we need to compute the gradient of the loss
with respect to the input.
"""
input, = ctx.saved_tensors
grad_input = grad_output.clone()
grad_input[input < 0] = 0
return grad_input
dtype = torch.cuda.FloatTensor
N, D_in, H, D_out = 64, 1000, 100, 10
# Create random Tensors to hold input and outputs, and wrap them in Variables.
x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)
# Create random Tensors for weights, and wrap them in Variables.
w1 = Variable(torch.randn(H, D_in).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)
# I've created this scalar variable (the kernel bandwidth)
s = Variable(torch.randn(1).type(dtype), requires_grad=True)
learning_rate = 1e-6
for t in range(500):
# To apply our Function, we use Function.apply method. We alias this as 'relu'.
relu = MyReLU.apply
# Forward pass: compute predicted y using operations on Variables; we compute
# ReLU using our custom autograd operation.
# y_pred = relu(x.mm(w1)).mm(w2)
y_pred = relu(kernel_product(w1, x, s)).mm(w2)
# Compute and print loss
loss = (y_pred - y).pow(2).sum()
print(t, loss.data[0])
# Use autograd to compute the backward pass.
loss.backward()
# Update weights using gradient descent
w1.data -= learning_rate * w1.grad.data
w2.data -= learning_rate * w2.grad.data
# Manually zero the gradients after updating weights
w1.grad.data.zero_()
w2.grad.data.zero_()
However I get this error, which dissapears when I simply use a fixed scalar in the default input parameter of kernel_product():
RuntimeError: eq() received an invalid combination of arguments - got (str), but expected one of:
* (float other)
didn't match because some of the arguments have invalid types: (str)
* (Variable other)
didn't match because some of the arguments have invalid types: (str)
| Well, you are calling kernel_product(w1, x, s) where w1, x and s are torch Variable while the definition of the function is: kernel_product(x,y, mode = "gaussian", s = 1.). Seems like s should be a string specifying the mode.
| https://stackoverflow.com/questions/48729473/ |
setuptools: installing pytorch from download link: 403 Forbidden | I am trying to include pytorch in the requirements list for setuptools:
install_requires=[
'torch'
],
dependency_links=[
'http://download.pytorch.org/whl/cpu/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl' '@develop#egg=torch'
],
But after running python setup.py develop I receive:
error: Can't download http://download.pytorch.org/whl/cpu/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl@develop#egg=torch: 403 Forbidden
I also tried using the source install link rather than the one for pip:
install_requires=[
'torch'
],
dependency_links=[
'https://github.com/pytorch/pytorch#from-source' '@develop#egg=torch'
],
But then:
RuntimeError: PyTorch does not currently provide packages for PyPI (see status at https://github.com/pytorch/pytorch/issues/566).
What is the correct way to install pytorch through setuptools? (Note: using anaconda is not an option in my case)
Thank you.
EDIT:
As suggested in the answer, I also tried:
dependency_links=[
'https://github.com/pytorch/pytorch'
],
dependency_links=[
'https://github.com/pytorch/pytorch#egg=torch'
],
dependency_links=[
'http://download.pytorch.org/whl/cpu/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl'
],
And received:
RuntimeError: PyTorch does not currently provide packages for PyPI (see status at https://github.com/pytorch/pytorch/issues/566).
| First error: if you use direct URL to a wheel file:
http://download.pytorch.org/whl/cpu/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl
You must not use @develop#egg=torch. That part is for installing from VCS like git.
The second URL
https://github.com/pytorch/pytorch#from-source
is also wrong. It should be
https://github.com/pytorch/pytorch
and here @develop#egg=torch is exactly right if you want to install develop branch.
| https://stackoverflow.com/questions/48743554/ |
Mini batch training for inputs of variable sizes | I have a list of LongTensors, and another list of labels. I'm new to PyTorch and RNN's so I'm quite confused as to how to implement minibatch training for the data I have. There is much more to this data, but I want to keep it simple, so I can understand only how to implement the minibatch training part. I'm doing multiclass classification based on the final hidden state of an LSTM/GRU trained on variable length inputs. I managed to get it working with batch size 1(basically SGD) but I'm struggling with implementing minibatches.
Do I have to pad the sequences to the maximum size and create a new tensor matrix of larger size which holds all the elements? I mean like this:
inputs = pad(sequences)
train = DataLoader(inputs, batch_size=batch_size, shuffle=True)
for i, data in train:
#do stuff using LSTM and/or GRU models
Is this the accepted way of doing minibatch training on custom data? I couldn't find any tutorials on loading custom data using DataLoader(but I assume that's the way to create batches using pyTorch?)
Another doubt I have is with regards to padding. The reason I'm using LSTM/GRU is because of the variable length of the input. Doesn't padding defeat the purpose? Is padding necessary for minibatch training?
| Yes. The issue with minibatch training on sequences which have different lengths is that you can't stack sequences of different lengths together.
Normally one would do.
for e in range(epochs):
sequences = shuffle(sequences)
for mb in range(len(sequences)/mb_size):
batch = torch.stack(sequences[mb*mb_size:(mb+1)*mb_size])
and then you apply your neural network on your batch. But because your sequences are of different lengths, the torch.stack will fail. So indeed what you have to do is to pad your sequences with zeros so that they all have the same length (at least in a minibatch). So you have 2 options:
1) At the very very beginning, pad all your sequences with initial zeros so that they all have the same length as your longest sequence of all your data.
OR
2) On the fly, for each minibatch, before stacking the sequences together, pad all the sequences that will go into the minibatch with initial zeros so that they all have the same length as the longest sequence of the minibatch.
| https://stackoverflow.com/questions/48796469/ |
Reading multiple images as custom dataset for PyTorch? | I want to read in multiple images for the main_image set and blur_image set. For example, 5 main images and 5 blurred images. The goal is determine what values for the kernel in the convolutional layer convert the main images to the blurred images. The assumption is that the same kernel is used to blur each of the 5 original images to produce the 5 blurred images
My code is available at: https://pastebin.com/PWf7rjd4 and https://pastebin.com/VxryDb7g
However, it seems to only be processing the first image, that is "1.png" for the main and blurred images. It is not processing images 2.png, 3.png, 4.png, and 5.png
How can I fix this?
| In your class BlurDataset you only return one image in the __getitem__ method. In your main method you call
for batch_idx, (main, blur) in enumerate(train_loader)
The torch.utils.data.Dataset class that you inherit from then calls __getitem__ with the index given by enumerate. It will give you one pair of pictures in each iteration of the loop. If you want to get all 5 pairs of pictures in the first iteration you have to change your __getitem__ method to loop over images and return them all.
| https://stackoverflow.com/questions/48815203/ |
PyTorch: How do the means and stds get calculated in the Transfer Learning tutorial? | I'm going through the PyTorch Transfer Learning tutorial at: link
In the data augmentation stage, there is the following step to normalize images:
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
I can understand why it's doing this but I can't find how the mean and std values get calculated? I tried to calculate the mean on the train data set and the mean values are:
array([ 0.11727478, 0.04542569, -0.28624609], dtype=float32)
| Your numbers don't seem right to me; since the ToTensor transform has output in the range [0.0, 1.0] it shouldn't be possible to get a negative mean.
If I calculate the mean with
traindata = datasets.ImageFolder(data_dir + '/train', transforms.ToTensor())
image_means = torch.stack([t.mean(1).mean(1) for t, c in traindata])
image_means.mean(0)
I get (0.5143, 0.4760, 0.3487) and for the validation set (0.5224, 0.4799, 0.3564). These are closer to the numbers in the tutorial. Searching for the specific numbers, you'll see that they appear in the Imagenet example, so my guess is that they are the means of the Imagenet dataset, of which the tutorial dataset is a subset.
| https://stackoverflow.com/questions/48818619/ |
How to use PyTorch multiprocessing? | I'm trying to use python's multiprocessing Pool method in pytorch to process a image. Here's the code:
from multiprocessing import Process, Pool
from torch.autograd import Variable
import numpy as np
from scipy.ndimage import zoom
def get_pred(args):
img = args[0]
scale = args[1]
scales = args[2]
img_scale = zoom(img.numpy(),
(1., 1., scale, scale),
order=1,
prefilter=False,
mode='nearest')
# feed input data
input_img = Variable(torch.from_numpy(img_scale),
volatile=True).cuda()
return input_img
scales = [1,2,3,4,5]
scale_list = []
for scale in scales:
scale_list.append([img,scale,scales])
multi_pool = Pool(processes=5)
predictions = multi_pool.map(get_pred,scale_list)
multi_pool.close()
multi_pool.join()
I'm getting this error:
`RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method
`
In this line:
predictions = multi_pool.map(get_pred,scale_list)
Can anyone tell me what I'm doing wrong ?
| As stated in pytorch documentation the best practice to handle multiprocessing is to use torch.multiprocessing instead of multiprocessing.
Be aware that sharing CUDA tensors between processes is supported only in Python 3, either with spawn or forkserver as start method.
Without touching your code, a workaround for the error you got is replacing
from multiprocessing import Process, Pool
with:
from torch.multiprocessing import Pool, Process, set_start_method
try:
set_start_method('spawn')
except RuntimeError:
pass
| https://stackoverflow.com/questions/48822463/ |
Google Ngram Viewer - English One Million | I'm training a language model in PyTorch and I'd need the most common one million words in English to serve as dictionary.
From what I've understood, the Google Ngram English One Million (1-grams) might suit to this task, but after downloading every part (0-9) of this dataset and using tail on them to check if they were what I supposed, I found out that no part of this dataset contains words beyond the F letter.
As far as I understood, any Version 1 file has its ngrams alphabetically and cronologically sorted and I'm concerned if it might be possible that the most common one million words do not go beyond the F?
Or am I missing the point of this dataset and it isn't the most commond one million words?
| Try shuf <file> to get a random sorting and you will see the data covers all letters. What you see at the end of the files is not an f but the ligature fl.
| https://stackoverflow.com/questions/48830857/ |
LSTM in Pytorch | I'm new to PyTorch. I came across some this GitHub repository (link to full code example) containing various different examples.
There is also an example about LSTMs, this is the Network class:
# RNN Model (Many-to-One)
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# Set initial states
h0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
# Forward propagate RNN
out, _ = self.lstm(x, (h0, c0))
# Decode hidden state of last time step
out = self.fc(out[:, -1, :])
return out
So my question is about the following lines:
h0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
As far as I understand it, forward() is called for every training example. But this would mean, that the hidden state and cell state would be resettet i.e. replaced with a matrix of zeros on every training example.
The names h0 and c0 indicate that this is only the hidden/cell state at t=0, but why then are theses zeros matrices handed over to the lstm with every training example?
Even if they are just ignored after the first call, it would not be a very nice solution.
When testing the code it states an accuracy of 97% on the MNIST set, so it seems to work this way, but it doesn't make sense to me.
Hope someone can help me out with this.
Thanks in advance!
| Obviously I was on the wrong track with this. I was confusing hidden units and hidden/cell state.
Only the hidden units in the LSTM are trained during the training step. Cell state and hidden state are resetet at the beginning of every sequence. So it just makes sense that it is programmed this way.
Sorry for this..
| https://stackoverflow.com/questions/48831585/ |
ouputs in semantic segmentation task | Im working on the Kaggle semantic segmentation task,
In the testing part of my code,
model = model.eval()
predictions =[]
for data in testdataloader:
data = t.autograd.Variable(data, volatile=True).cuda()
output = model.forward(data)
_,preds = t.max(output, 1, keepdim = True)
when i do the preds part,the array is only filled with ,i was hoping it to be an array of maximum locations im not sure what is going wrong.
The output part works well,I have attached a screenshot for visualization of output
Any sugestions on what is going wrong would be really helpful.
thanks
| Assuming your data is of the form MiniBatch x Dim what you are doing now is looking at which minibatch has the highest value. If you are testing it with a single sample (MB = 1) then you will always get 0 as your answer. Thus, you might want to try:
_,preds = t.max(output, 0, keepdim = False)
| https://stackoverflow.com/questions/48849715/ |
Why unintialized tensor in Pytorch have initial values? | The torch command
x = torch.Tensor(4, 3)
is supposed to create an uninitialized tensor (based on documentations).
But when we try to print the content of x, there are values there.
>>>from __future__ import print_function
>>>print(x)
0.0000e+00 -8.5899e+09 6.1021e-38
8.5920e+09 1.7470e-21 4.5806e-41
0.0000e+00 0.0000e+00 0.0000e+00
0.0000e+00 0.0000e+00 0.0000e+00
[torch.FloatTensor of size 4x3]
So what is the meaning of uninitialized here?
| It means that PyTorch just reserves a certain area within the memory for the tensor, without changing its content.
This part of the memory was before occupied by something else (an other tensor or or maybe something completely different like Browser, Code Editor .. if you use CPU memory). The values inside are not cleared afterwards for performance reasons.
The content (what previously might be something entirely different) is just interpreted as values for tensor.
Writing zeros or some other initialization requires computational power, so just reserving the area in memory is much faster.
But the values are also completely uncontrolled, values can grow very high, so in many cases you might do additional initialization.
| https://stackoverflow.com/questions/48869836/ |
Package Cuda 8 not found for PyTorch | I am trying to follow the tutorial for chatbots at facebook (https://github.com/facebookresearch/end-to-end-negotiator) and I am stuck here :
conda install pytorch torchvision cuda80 -c soumith
I managed to get all packages except for cuda80 which is not found. I also tried
conda install magma-cuda80 -c soumith
conda install cuda80 -c soumith
conda install magma-cuda80 -c pytorch
with no success.
PackagesNotFoundError: The following packages are not available from current channels:
- magma-cuda80
Where can I find the package ? I am on Windows10.
| Open Anaconda Prompt and run -
conda install -c peterjc123 pytorch cuda80
OR
conda install -c peterjc123 pytorch cuda90
| https://stackoverflow.com/questions/48874285/ |
Install Pytorch on Windows | I am trying to install Pytorch on Windows8.1. I am using Python 3.6.4 and no GPU. I've tried already the Anaconda package provided by peterjc123 by running conda install -c peterjc123 pytorch_legacy cuda80 using a virtual environment. While the installation goes smooth (without errors), after import torch I get the following error.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\myuser\AppData\Local\conda\conda\envs\myenv\lib\site-packages\torch\__init__.py", line 76, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
Can somebody help me to install it?
| You are installing the GPU version with the command.
Check the link for the github repo.
In short, you should run something like
conda install -c peterjc123 pytorch.
Be sure to install the required dependencies before attempting to install the main framework. For more details, check the link.
| https://stackoverflow.com/questions/48895910/ |
What does .contiguous() do in PyTorch? | What does x.contiguous() do for a tensor x?
| There are a few operations on Tensors in PyTorch that do not change the contents of a tensor, but change the way the data is organized. These operations include:
narrow(), view(), expand() and transpose()
For example: when you call transpose(), PyTorch doesn't generate a new tensor with a new layout, it just modifies meta information in the Tensor object so that the offset and stride describe the desired new shape. In this example, the transposed tensor and original tensor share the same memory:
x = torch.randn(3,2)
y = torch.transpose(x, 0, 1)
x[0, 0] = 42
print(y[0,0])
# prints 42
This is where the concept of contiguous comes in. In the example above, x is contiguous but y is not because its memory layout is different to that of a tensor of same shape made from scratch. Note that the word "contiguous" is a bit misleading because it's not that the content of the tensor is spread out around disconnected blocks of memory. Here bytes are still allocated in one block of memory but the order of the elements is different!
When you call contiguous(), it actually makes a copy of the tensor such that the order of its elements in memory is the same as if it had been created from scratch with the same data.
Normally you don't need to worry about this. You're generally safe to assume everything will work, and wait until you get a RuntimeError: input is not contiguous where PyTorch expects a contiguous tensor to add a call to contiguous().
| https://stackoverflow.com/questions/48915810/ |
PyTorch Sparse Tensors number of dimensions must be nDimI + nDimV | I'm trying to insert the value in gd to coordinate [1,0]. Below are the matrices. When I try this, I get a RuntimeError.
>>> import torch
>>> cd = [[1, 0]]
>>> gd = [0.39613232016563416]
>>> i = torch.LongTensor(cd)
>>> v = torch.FloatTensor(gd)
>>> p = torch.rand(2)
>>> i
1 0
[torch.LongTensor of size 1x2]
>>> v
0.3961
[torch.FloatTensor of size 1]
>>> p
0.4678
0.0996
[torch.FloatTensor of size 2]
>>> torch.sparse.FloatTensor(i.t(), v, torch.Size(list(p.size()))).to_dense()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: number of dimensions must be nDimI + nDimV at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/THS/generic/THSTensor.c:169
| Two things.
1) Right now p is a Tensor of rank 1. To insert something in position [1,0] it needs to be a Tensor of rank 2.
2) You don't need to do complicated things with sparse tensors. Simply p[cd[0], cd[1]] = v[0] should work. Where cd = torch.LongTensor([row_idx, col_idx])
So:
>>> cd = torch.LongTensor([1,0])
>>> gd = [0.39613232016563416]
>>> v = torch.FloatTensor(gd)
>>> p = torch.rand((2,2))
>>> p
0.9342 0.8539
0.7044 0.0823
[torch.FloatTensor of size 2x2]
>>> p[cd[0], cd[1]] = v[0]
>>> p
0.9342 0.8539
0.3961 0.0823
[torch.FloatTensor of size 2x2]
That simple.
| https://stackoverflow.com/questions/48941208/ |
multiprocessing error with pytorch on windows 10 | i get the following error when i try to execute my code, which clearly shows its an mulitprocessing error:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
On Linux, the following code runs fine, but Im wondering why i cant get it running on Windows 10. However, here is the code:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
kwargs = {'num_workers': 1, 'pin_memory': True}
train_data = torch.utils.data.DataLoader(datasets.MNIST('data', train=True, download=True,
transform=transforms.Compose([transforms.ToTensor,
transforms.Normalize((0.1307,), (0.3081,))])),
batch_size=64, shuffle=True, **kwargs)
test_data = torch.utils.data.DataLoader(datasets.MNIST('data', train=False,
transform=transforms.Compose([transforms.ToTensor,
transforms.Normalize((0.1307,), (0.3081,))])),
batch_size=64, shuffle=True, **kwargs)
class Netz(nn.Module):
def __init__(self):
super(Netz, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv_dropout = nn.Dropout2d()
self.fc1 = nn.Linear(320, 60)
self.fc2 = nn.Linear(60, 10)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, 2)
x = F.relu(x)
x = self.conv2(x)
x = self.conv_dropout(x)
x = F.max_pool2d(x, 2)
x = F.relu(x)
print(x.size())
exit()
model = Netz()
model.cuda()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.8)
def train(epoch):
model.train()
for batch_id, (data, target) in enumerate(train_data):
data = data.cuda()
target = target.cuda()
data = Variable(data)
target = Variable(target)
optimizer.zero_grad()
out = model(data)
criterion = F.nll_loss
loss = criterion(out, target)
loss.backward()
optimizer.step()
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch, batch_id * len(data), len(train_data.dataset),
100. * batch_id / len(train_data), loss.data[0]))
for epoch in range(1, 30):
print(epoch)
train(epoch)
I tried to fix it with:
if __name__ == '__main__':
for epoch in range(1, 30):
train(epoch)
But also without success.
Anyone has an idea how i can fix this multiprocessing error?
Any help would be appreciated.
(Yes, i know pytorch is not officially released for windows, but i dont think this is causing the error here.)
Thanks!
| I found it out myself.
I had to put the whole code into if name == 'main':
Also i forgot the brakets in the end at the transforms.ToTensor part.
| https://stackoverflow.com/questions/49013664/ |
PyTorch - How to use "toPILImage" correctly | I would like to know, whether I used toPILImage from torchvision correctly. I want to use it, to see how the images look after initial image transformations are applied to the dataset.
When I use it like in the code below, the image that comes up has weird colors like this one. The original image is a regular RGB image.
This is my code:
import os
import torch
from PIL import Image, ImageFont, ImageDraw
import torch.utils.data as data
import torchvision
from torchvision import transforms
import matplotlib.pyplot as plt
# Image transformations
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
transform_img = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor(),
normalize ])
train_data = torchvision.datasets.ImageFolder(
root='./train_cl/',
transform=transform_img
)
test_data = torchvision.datasets.ImageFolder(
root='./test_named_cl/',
transform=transform_img
)
train_data_loader = data.DataLoader(train_data,
batch_size=4,
shuffle=True,
num_workers=4) #num_workers=args.nThreads)
test_data_loader = data.DataLoader(test_data,
batch_size=32,
shuffle=False,
num_workers=4)
# Open Image from dataset:
to_pil_image = transforms.ToPILImage()
my_img, _ = train_data[248]
results = to_pil_image(my_img)
results.show()
Edit:
I had to use .data on the Torch Variable to get the tensor.
Also I needed to rescale the numpy array before transposing. I found a working solution here, but it doesn't always work well. How can I do this better?
for i, data in enumerate(train_data_loader, 0):
img, labels = data
img = Variable(img)
break
image = img.data.cpu().numpy()[0]
# This worked for rescaling:
image = (1/(2*2.25)) * image + 0.5
# Both of these didn't work:
# image /= (image.max()/255.0)
# image *= (255.0/image.max())
image = np.transpose(image, (1,2,0))
plt.imshow(image)
plt.show()
| You can use PIL image but you're not actually loading the data as you would normally.
Try something like this instead:
import numpy as np
import matplotlib.pyplot as plt
for img,labels in train_data_loader:
# load a batch from train data
break
# this converts it from GPU to CPU and selects first image
img = img.cpu().numpy()[0]
#convert image back to Height,Width,Channels
img = np.transpose(img, (1,2,0))
#show the image
plt.imshow(img)
plt.show()
As an update (02-10-2021):
import torchvision.transforms.functional as F
# load the image (creating a random image as an example)
img_data = torch.ByteTensor(4, 4, 3).random_(0, 255).numpy()
pil_image = F.to_pil_image(img_data)
Alternatively
import torchvision.transforms as transforms
img_data = torch.ByteTensor(4, 4, 3).random_(0, 255).numpy()
pil_image = transforms.ToPILImage()(img_data)
The second form can be integrated with dataset loader in pytorch or called directly as so.
I added a modified to_pil_image here
essentially it does what I suggested back in 2018 but it is integrated into pytorch now.
| https://stackoverflow.com/questions/49035156/ |
Pytorch softmax: What dimension to use? | The function torch.nn.functional.softmax takes two parameters: input and dim. According to its documentation, the softmax operation is applied to all slices of input along the specified dim, and will rescale them so that the elements lie in the range (0, 1) and sum to 1.
Let input be:
input = torch.randn((3, 4, 5, 6))
Suppose I want the following, so that every entry in that array is 1:
sum = torch.sum(input, dim = 3) # sum's size is (3, 4, 5, 1)
How should I apply softmax?
softmax(input, dim = 0) # Way Number 0
softmax(input, dim = 1) # Way Number 1
softmax(input, dim = 2) # Way Number 2
softmax(input, dim = 3) # Way Number 3
My intuition tells me that is the last one, but I am not sure. English is not my first language and the use of the word along seemed confusing to me because of that.
I am not very clear on what "along" means, so I will use an example that could clarify things. Suppose we have a tensor of size (s1, s2, s3, s4), and I want this to happen
| The easiest way I can think of to make you understand is: say you are given a tensor of shape (s1, s2, s3, s4) and as you mentioned you want to have the sum of all the entries along the last axis to be 1.
sum = torch.sum(input, dim = 3) # input is of shape (s1, s2, s3, s4)
Then you should call the softmax as:
softmax(input, dim = 3)
To understand easily, you can consider a 4d tensor of shape (s1, s2, s3, s4) as a 2d tensor or matrix of shape (s1*s2*s3, s4). Now if you want the matrix to contain values in each row (axis=0) or column (axis=1) that sum to 1, then, you can simply call the softmax function on the 2d tensor as follows:
softmax(input, dim = 0) # normalizes values along axis 0
softmax(input, dim = 1) # normalizes values along axis 1
You can see the example that Steven mentioned in his answer.
| https://stackoverflow.com/questions/49036993/ |
Change Tanh activation in LSTM to ReLU | The default non-linear activation function in LSTM class is tanh. I wish to use ReLU for my project. Browsing through the documentation and other resources, I'm unable to find a way to do this in a simple manner. The only way I could find was to define my own custom LSTMCell, but here the author says that custom LSTMCells don't support GPU acceleration capabilities(or has that changed since the article was published?). I need to use CUDA to speed up my training. Any help would be appreciated.
| Custom LSTMCells don't support GPU acceleration capabilities - this statement probably means GPU acceleration capabilities become limited if you use LSTMCells. And definitely, you can write your own implementation of LSTM but you need to sacrifice runtime.
For example, once I implemented an LSTM (based on linear layers) as follows which used to take 2~3 times more time than LSTM (provided in PyTorch) when used as a part of a deep neural model.
class LSTMCell(nn.Module):
def __init__(self, input_size, hidden_size, nlayers, dropout):
""""Constructor of the class"""
super(LSTMCell, self).__init__()
self.nlayers = nlayers
self.dropout = nn.Dropout(p=dropout)
ih, hh = [], []
for i in range(nlayers):
ih.append(nn.Linear(input_size, 4 * hidden_size))
hh.append(nn.Linear(hidden_size, 4 * hidden_size))
self.w_ih = nn.ModuleList(ih)
self.w_hh = nn.ModuleList(hh)
def forward(self, input, hidden):
""""Defines the forward computation of the LSTMCell"""
hy, cy = [], []
for i in range(self.nlayers):
hx, cx = hidden[0][i], hidden[1][i]
gates = self.w_ih[i](input) + self.w_hh[i](hx)
i_gate, f_gate, c_gate, o_gate = gates.chunk(4, 1)
i_gate = F.sigmoid(i_gate)
f_gate = F.sigmoid(f_gate)
c_gate = F.tanh(c_gate)
o_gate = F.sigmoid(o_gate)
ncx = (f_gate * cx) + (i_gate * c_gate)
nhx = o_gate * F.tanh(ncx)
cy.append(ncx)
hy.append(nhx)
input = self.dropout(nhx)
hy, cy = torch.stack(hy, 0), torch.stack(cy, 0)
return hy, cy
I would be happy to know if the runtime of custom implementation of LSTM can be improved!
| https://stackoverflow.com/questions/49040180/ |
How could I feed a custom image into this model? | I've been following a course online and one of the exercises was to create a simple image detection model (using MNIST data) to detect written numbers. I've been trying to load a custom image I drew in (128x128 jpg) but I can't seem to figure it out. I'm really close, but I think I'm just confused about what parameters the model takes in. Any help would be appreciated!!
Here is my code
| Simply convert your image to an 128x128 numpy array with values between 0 and 1.
Then:
image = Variable(torch.from_numpy(image))[None, :, :]
classification = model(image)
classification is then a pytorch Variable containing probabilities of belonging to each class.
| https://stackoverflow.com/questions/49044980/ |
Implementing RNN and LSTM into DQN Pytorch code | I have some troubles finding some example on the great www to how i implement a recurrent neural network with LSTM layer into my current Deep q-network in Pytorch so it become a DRQN.. Bear with me i am just getting started..
Futhermore, I am NOT working with images processing, thereby CNN so do not worry about this. My states are purely temperatures values.
Here is my code that i am currently train my DQN with:
# Importing the libraries
import numpy as np
import random # random samples from different batches (experience replay)
import os # For loading and saving brain
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim # for using stochastic gradient descent
import torch.autograd as autograd # Conversion from tensor (advanced arrays) to avoid all that contains a gradient
# We want to put the tensor into a varaible taht will also contain a
# gradient and to this we need:
from torch.autograd import Variable
# to convert this tensor into a variable containing the tensor and the gradient
# Creating the architecture of the Neural Network
class Network(nn.Module): #inherinting from nn.Module
#Self - refers to the object that will be created from this class
# - self here to specify that we're referring to the object
def __init__(self, input_size, nb_action): #[self,input neuroner, output neuroner]
super(Network, self).__init__() #inorder to use modules in torch.nn
# Input and output neurons
self.input_size = input_size
self.nb_action = nb_action
# Full connection between different layers of NN
# In this example its one input layer, one hidden layer and one output layer
# Using self here to specify that fc1 is a variable of my object
self.fc1 = nn.Linear(input_size, 40)
self.fc2 = nn.Linear(40, 30)
#Example of adding a hiddenlayer
# self.fcX = nn.Linear(30,30)
self.fc3 = nn.Linear(30, nb_action) # 30 neurons in hidden layer
# For function that will activate neurons and perform forward propagation
def forward(self, state):
# rectifier function
x = F.relu(self.fc1(state))
x = F.relu(self.fc2(x))
q_values = self.fc3(x)
return q_values
# Implementing Experience Replay
# We know that RL is based on MDP
# So going from one state(s_t) to the next state(s_t+1)
# We gonna put 100 transition between state into what we call the memory
# So we can use the distribution of experience to make a decision
class ReplayMemory(object):
def __init__(self, capacity):
self.capacity = capacity #100 transitions
self.memory = [] #memory to save transitions
# pushing transitions into memory with append
#event=transition
def push(self, event):
self.memory.append(event)
if len(self.memory) > self.capacity: #memory only contain 100 events
del self.memory[0] #delete first transition from memory if there is more that 100
# taking random sample
def sample(self, batch_size):
#Creating variable that will contain the samples of memory
#zip =reshape function if list = ((1,2,3),(4,5,6)) zip(*list)= (1,4),(2,5),(3,6)
# (state,action,reward),(state,action,reward)
samples = zip(*random.sample(self.memory, batch_size))
#This is to be able to differentiate with respect to a tensor
#and this will then contain the tensor and gradient
#so for state,action and reward we will store the seperately into some
#bytes which each one will get a gradient
#so that eventually we'll be able to differentiate each one of them
return map(lambda x: Variable(torch.cat(x, 0)), samples)
# Implementing Deep Q Learning
class Dqn():
def __init__(self, input_size, nb_action, gamma, lrate, T):
self.gamma = gamma #self.gamma gets assigned to input argument
self.T = T
# Sliding window of the evolving mean of the last 100 events/transitions
self.reward_window = []
#Creating network with network class
self.model = Network(input_size, nb_action)
#creating memory with memory class
#We gonna take 100000 samples into memory and then we will sample from this memory to
#to get a snakk number of random transitions
self.memory = ReplayMemory(100000)
#creating optimizer (stochastic gradient descent)
self.optimizer = optim.Adam(self.model.parameters(), lr = lrate) #learning rate
#input vector which is batch of input observations
#by unsqeeze we create a fake dimension to this is
#what the network expect for its inputs
#have to be the first dimension of the last_state
self.last_state = torch.Tensor(input_size).unsqueeze(0)
#Inilizing
self.last_action = 0
self.last_reward = 0
def select_action(self, state):
#Q value depends on state
#Temperature parameter T will be a positive number and the closer
#it is to ze the less sure the NN will when taking an action
#forexample
#softmax((1,2,3))={0.04,0.11,0.85} ==> softmax((1,2,3)*3)={0,0.02,0.98}
#to deactivate brain then set T=0, thereby it is full random
probs = F.softmax((self.model(Variable(state, volatile = True))*self.T),dim=1) # T=100
#create a random draw from the probability distribution created from softmax
action = probs.multinomial()
print(probs.multinomial())
return action.data[0,0]
# See section 5.3 in AI handbook
def learn(self, batch_state, batch_next_state, batch_reward, batch_action):
outputs = self.model(batch_state).gather(1, batch_action.unsqueeze(1)).squeeze(1)
#next input for target see page 7 in attached AI handbook
next_outputs = self.model(batch_next_state).detach().max(1)[0]
target = self.gamma*next_outputs + batch_reward
#Using hubble loss inorder to obtain loss
td_loss = F.smooth_l1_loss(outputs, target)
#using lass loss/error to perform stochastic gradient descent and update weights
self.optimizer.zero_grad() #reintialize the optimizer at each iteration of the loop
#This line of code that backward propagates the error into the NN
#td_loss.backward(retain_variables = True) #userwarning
td_loss.backward(retain_graph = True)
#And this line of code uses the optimizer to update the weights
self.optimizer.step()
def update(self, reward, new_signal):
#Updated one transition and we have dated the last element of the transition
#which is the new state
new_state = torch.Tensor(new_signal).float().unsqueeze(0)
self.memory.push((self.last_state, new_state, torch.LongTensor([int(self.last_action)]), torch.Tensor([self.last_reward])))
#After ending in a state its time to play a action
action = self.select_action(new_state)
if len(self.memory.memory) > 100:
batch_state, batch_next_state, batch_action, batch_reward = self.memory.sample(100)
self.learn(batch_state, batch_next_state, batch_reward, batch_action)
self.last_action = action
self.last_state = new_state
self.last_reward = reward
self.reward_window.append(reward)
if len(self.reward_window) > 1000:
del self.reward_window[0]
return action
def score(self):
return sum(self.reward_window)/(len(self.reward_window)+1.)
def save(self):
torch.save({'state_dict': self.model.state_dict(),
'optimizer' : self.optimizer.state_dict(),
}, 'last_brain.pth')
def load(self):
if os.path.isfile('last_brain.pth'):
print("=> loading checkpoint... ")
checkpoint = torch.load('last_brain.pth')
self.model.load_state_dict(checkpoint['state_dict'])
self.optimizer.load_state_dict(checkpoint['optimizer'])
print("done !")
else:
print("no checkpoint found...")
I hope there is someone out there that can help me and could implement a RNN and a LSTM layer into my code! I believe in you stackflow!
Best regards Søren Koch
| From my point of view, I think you could add RNN, LSTM layer to the Network#__init__,Network#forward; shape of data should be reshaped into sequences...
For more detail, I think you should read these two following articles; after that implementing RNN, LSTM not hard as it seem to be.
http://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html#sphx-glr-beginner-nlp-sequence-models-tutorial-py
http://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
| https://stackoverflow.com/questions/49065222/ |
An analog of weighted_cross_entropy_with_logits in PyTorch | I'm trying to train a model with PyTorch. Is there any simple way to create a loss like weighted_cross_entropy_with_logits from Tensorflow?
There are pos_weight argument in weighted_cross_entropy_with_logits that can help with balancing. But there are only weights for labels in the list of arguments in BCEWithLogitsLoss.
| You can write your own custom loss function as you want. For example, you can write:
def weighted_cross_entropy_with_logits(logits, target, pos_weight):
return targets * -logits.sigmoid().log() * pos_weight +
(1 - targets) * -(1 - logits.sigmoid()).log()
This is a basic implementation. You should follow the steps mentioned here to ensure stability and avoid overflow. Just use the final formulation that they derived.
| https://stackoverflow.com/questions/49069502/ |
PyTorch: Testing with torchvision.datasets.ImageFolder and DataLoader | I'm a newbie trying to make this PyTorch CNN work with the Cats&Dogs dataset from kaggle. As there are no targets for the test images, I manually classified some of the test images and put the class in the filename, to be able to test (maybe should have just used some of the train images).
I used the torchvision.datasets.ImageFolder class to load the train and test images. The training seems to work.
But what do I need to do to make the test-routine work? I don't know, how to connect my test_data_loader with the test loop at the bottom, via test_x and test_y.
The Code is based on this MNIST example CNN. There, something like this is used right after the loaders are created. But I failed to rewrite it for my dataset:
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1), volatile=True).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labels[:2000]
The Code:
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.utils.data as data
import torchvision
from torchvision import transforms
EPOCHS = 2
BATCH_SIZE = 10
LEARNING_RATE = 0.003
TRAIN_DATA_PATH = "./train_cl/"
TEST_DATA_PATH = "./test_named_cl/"
TRANSFORM_IMG = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] )
])
train_data = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=TRANSFORM_IMG)
train_data_loader = data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
test_data = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=TRANSFORM_IMG)
test_data_loader = data.DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
class CNN(nn.Module):
# omitted...
if __name__ == '__main__':
print("Number of train samples: ", len(train_data))
print("Number of test samples: ", len(test_data))
print("Detected Classes are: ", train_data.class_to_idx) # classes are detected by folder structure
model = CNN()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
loss_func = nn.CrossEntropyLoss()
# Training and Testing
for epoch in range(EPOCHS):
for step, (x, y) in enumerate(train_data_loader):
b_x = Variable(x) # batch x (image)
b_y = Variable(y) # batch y (target)
output = model(b_x)[0]
loss = loss_func(output, b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Test -> this is where I have no clue
if step % 50 == 0:
test_x = Variable(test_data_loader)
test_output, last_layer = model(test_x)
pred_y = torch.max(test_output, 1)[1].data.squeeze()
accuracy = sum(pred_y == test_y) / float(test_y.size(0))
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data[0], '| test accuracy: %.2f' % accuracy)
| Looking at the data from Kaggle and your code, it seems that there are problems in your data loading, both train and test set. First of all, the data should be in a different folder per label for the default PyTorch ImageFolder to load it correctly. In your case, since all the training data is in the same folder, PyTorch is loading it as one class and hence learning seems to be working. You can correct this by using a folder structure like - train/dog, - train/cat, - test/dog, - test/cat and then passing the train and the test folder to the train and test ImageFolder respectively. The training code seems fine, just change the folder structure and you should be good. Take a look at the official documentation of ImageFolder which has a similar example.
| https://stackoverflow.com/questions/49073799/ |
Image Captioning Example input size of Decoder LSTM Pytorch | I'm new to Pytorch, there is a doubt that am having in the Image Captioning example code . In DcoderRNN class the lstm is defined as ,
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
in the forward function ,
embeddings = self.embed(captions)
embeddings = torch.cat((features.unsqueeze(1), embeddings), 1)
we first embed the captions and then concat the embeddings with the context feature from the EncoderCNN, but the concat increases the size from embed size how we can forward that to the lstm? as the input size of lstm is already defined as embed_size.
Am I missing something here? Thanks in advance .
| You can analyze the shape of all input and output tensors and then it will become easier for you to understand what changes you need to make.
Let's say: captions = B x S where S = sentence (caption) length.
embeddings = self.embed(captions)
Now, embeddings = B x S x E where E = embed_size.
embeddings = torch.cat((features.unsqueeze(1), embeddings), 1)
Here, embeddings = B x (S + 1) X E.
My understanding says you are doing wrong here. I guess you should concatenate features along axis=2. Because probably you want to concatenate the image features along with the word embeddings for each word in the caption. So, if you do:
embeddings = torch.cat((features.unsqueeze(1), embeddings), 2)
It results in, embeddings = B X S X (E + F) where E + F = embed_size + img_feat_size
Then you need to revise your LSTM definition as follows.
self.lstm = nn.LSTM(embed_size+img_feat_size, hidden_size, num_layers, batch_first=True)
My experience says, usually, people concatenate image features with word features and pass it to the LSTM layer.
| https://stackoverflow.com/questions/49085370/ |
Is a torch.FloatTensor not a Tensor? | Although this example has no training, this is an adapted part of a larger program where training does occur. I simply want the generator network to spew out a random image in this case:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
class Generator(nn.Module):
def __init__(self):
"""
Generator component of GAN. requires an input slightly bigger
than 300 x 300 (precisely 308 x 308)
"""
super(Generator, self).__init__()
# 5 x 5 square convolution.
self.conv1 = nn.Conv2d(3, 6, 5)
self.conv2 = nn.Conv2d(6, 4, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
return x
def main():
# Generate example image.
generator = Generator()
img = generator(Variable(torch.randn(1, 3, 308, 308))).data
img_pil = transforms.ToPILImage()(img)
img_pil.save("test.png")
if __name__ == "__main__":
main()
Running this program gives the following:
(mgan-Csuh5VLx) ➜ mgan git:(broken) ✗ python test.py
Traceback (most recent call last):
File "test.py", line 34, in <module>
main()
File "test.py", line 30, in main
img_pil = transforms.ToPILImage()(img)
File "/home/christopher/.local/share/virtualenvs/mgan-Csuh5VLx/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 92, in __call__
return F.to_pil_image(pic, self.mode)
File "/home/christopher/.local/share/virtualenvs/mgan-Csuh5VLx/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 96, in to_pil_image
raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
TypeError: pic should be Tensor or ndarray. Got <class 'torch.FloatTensor'>.
I thought FloatTensors were essentially Tensors. Is there a way to get around this error?
(Note: I have four output channels for RGBA, but even switching to 3 output channels gives the same error.)
| Simply changing the second last line of your main function solves the problem:
img_pil = transforms.ToPILImage()(img.squeeze())
img.squeeze() makes the tensor shape (1, 4, 300, 300) to (4, 300, 300).
| https://stackoverflow.com/questions/49088670/ |
where is the definition of _cudnn_convolution_full_forward in pytorch | as the title mentioned, I want to find the definition of _cudnn_convolution_full_forward, but I search through all the project
in pytorch and failed. And I cannot find and doc about this function.
any one can help me?
| All the cudnn convolution functions are defined here:
https://github.com/pytorch/pytorch/blob/1848cad10802db9fa0aa066d9de195958120d863/aten/src/ATen/native/cudnn/Conv.cpp
This function doesn't exist anymore in the latest versions of pytorch. The closest thing that there is there is cudnn_convolution_forward. In version 0.1.12, the function is in the same file:
https://github.com/pytorch/pytorch/blob/v0.1.12/torch/csrc/cudnn/Conv.cpp
I would recommend against using using an unpublic api (one starting with _) and use a public method instead, but you probably already know that.
In otherwords you should be using
torch.backends.cudnn.enabled = True
and then conv2d or conv3d depending on your use.
| https://stackoverflow.com/questions/49103096/ |
Indexing on axis by list in PyTorch | I have Variables lengths_X of size (10L,) and A of size (10L, 16L, 5L).
I want to use lengths_X to index along the second axis of A. In other words, I want to get a new tensor predicted_Y of size (10L, 5L) that indexes axis 1 at i for all entries with index i in axis 0.
What is the best way to do this in PyTorch?
| What you are looking for is actually called batched_index_select and I looked for such functionality before but couldn't find any native function in PyTorch that can do the job. But we can simply use:
A = torch.randn(10, 16, 5)
index = torch.from_numpy(numpy.random.randint(0, 16, size=10))
B = torch.stack([a[i] for a, i in zip(A, index)])
You can see the discussion here. You can also check out the function batched_index_select provided in the AllenNLP library. I would be happy to know if there is a better solution.
| https://stackoverflow.com/questions/49104307/ |
Issue training RNN model with pytorch with trivial goal | I'm trying to train a simple RNN model with a trivial goal where the output matches a fixed vector regardless of the input
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
print "i2h WEIGHT size ", list(self.i2h.weight.size())
print "i2h bias size ", list(self.i2h.bias.size())
self.i2o = nn.Linear(hidden_size, output_size)
print "i2o WEIGHT size ", list(self.i2o.weight.size())
print "i2o bias size ", list(self.i2o.bias.size())
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
combined = torch.cat((input, hidden), 1)
hidden = self.i2h(combined)
output = self.i2o(hidden)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return Variable(torch.zeros(1, self.hidden_size))
n_hidden = 20
rnn = RNN(10, n_hidden, 3)
learning_rate = 1e-3
loss_fn = torch.nn.MSELoss(size_average=False)
out_target = Variable( torch.FloatTensor([[0.0 , 1.0, 0.0]] ) , requires_grad=False)
print "target output::: ", out_target
def train(category_tensor, line_tensor):
hidden = rnn.initHidden()
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
#print "train iteration ", i, ": input data: ", line_tensor[i]
output, hidden = rnn(line_tensor[i], hidden)
loss = loss_fn(output, out_target)
loss.backward()
# Add parameters' gradients to their values, multiplied by learning rate
for p in rnn.parameters():
#print "parameter: ", p, " gradient: ", p.grad.data
p.data.add_(-learning_rate, p.grad.data)
return output, loss.data[0]
current_loss = 0
n_iters = 500
for iter in range(1, n_iters + 1):
inp = Variable(torch.randn(100,1,10) + 5)
output, loss = train(out_target, inp)
current_loss += loss
if iter % 1 == 0:
print "weights: ",rnn.i2h.weight
print "LOSS: ", loss
print output
As it shows, the loss stays above 6 and never goes down. Notice also that I am biasing all the random inputs normal distributions by 5, so they are mostly positive numbers, so there should exist a weight distribution that approaches the goal output
What am I doing wrong in this example that is failing to output to approach the goal?
| Your fixed output is:
torch.FloatTensor([[0.0, 1.0, 0.0]])
But you are using the following as the final layer in your RNN:
self.softmax = nn.LogSoftmax(dim=1)
Does LogSoftmax returns value in [0, 1]? Althouhgh, you can use the Softmax but I would recommend you to use the sign function and transform -1 to 0.
| https://stackoverflow.com/questions/49115560/ |
how to apply gradients manually in pytorch | Starting to learn pytorch and was trying to do something very simple, trying to move a randomly initialized vector of size 5 to a target vector of value [1,2,3,4,5].
But my distance is not decreasing!! And my vector x just goes crazy. No idea what I am missing.
import torch
import numpy as np
from torch.autograd import Variable
# regress a vector to the goal vector [1,2,3,4,5]
dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU
x = Variable(torch.rand(5).type(dtype), requires_grad=True)
target = Variable(torch.FloatTensor([1,2,3,4,5]).type(dtype),
requires_grad=False)
distance = torch.mean(torch.pow((x - target), 2))
for i in range(100):
distance.backward(retain_graph=True)
x_grad = x.grad
x.data.sub_(x_grad.data * 0.01)
| There are two errors in your code that prevents you from getting the desired results.
The first error is that you should put the distance calculation in the loop. Because the distance is the loss in this case. So we have to monitor its change in each iteration.
The second error is that you should manually zero out the x.grad because pytorch won't zero out the grad in variable by default.
The following is an example code which works as expected:
import torch
import numpy as np
from torch.autograd import Variable
import matplotlib.pyplot as plt
# regress a vector to the goal vector [1,2,3,4,5]
dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU
x = Variable(torch.rand(5).type(dtype), requires_grad=True)
target = Variable(torch.FloatTensor([1,2,3,4,5]).type(dtype),
requires_grad=False)
lr = 0.01 # the learning rate
d = []
for i in range(1000):
distance = torch.mean(torch.pow((x - target), 2))
d.append(distance.data)
distance.backward(retain_graph=True)
x.data.sub_(lr * x.grad.data)
x.grad.data.zero_()
print(x.data)
fig, ax = plt.subplots()
ax.plot(d)
ax.set_xlabel("iteration")
ax.set_ylabel("distance")
plt.show()
The following is the graph of distance w.r.t iteration
We can see that the model converges at about 600 iterations. If we set the learning rate to be higher (e.g, lr=0.1), the model will converge much faster (it takes about 60 iterations, see image below)
Now, x becomes something like the following
0.9878
1.9749
2.9624
3.9429
4.9292
which is pretty close to your target of [1, 2, 3, 4, 5].
| https://stackoverflow.com/questions/49154514/ |
How to get around in place operation error if index leaf variable for gradient update? | I am encountering In place operation error when I am trying to index a leaf variable to update gradients with customized Shrink function. I cannot work around it. Any help is highly appreciated!
import torch.nn as nn
import torch
import numpy as np
from torch.autograd import Variable, Function
# hyper parameters
batch_size = 100 # batch size of images
ld = 0.2 # sparse penalty
lr = 0.1 # learning rate
x = Variable(torch.from_numpy(np.random.normal(0,1,(batch_size,10,10))), requires_grad=False) # original
# depends on size of the dictionary, number of atoms.
D = Variable(torch.from_numpy(np.random.normal(0,1,(500,10,10))), requires_grad=True)
# hx sparse representation
ht = Variable(torch.from_numpy(np.random.normal(0,1,(batch_size,500,1,1))), requires_grad=True)
# Dictionary loss function
loss = nn.MSELoss()
# customized shrink function to update gradient
shrink_ht = lambda x: torch.stack([torch.sign(i)*torch.max(torch.abs(i)-lr*ld,0)[0] for i in x])
### sparse reprsentation optimizer_ht single image.
optimizer_ht = torch.optim.SGD([ht], lr=lr, momentum=0.9) # optimizer for sparse representation
## update for the batch
for idx in range(len(x)):
optimizer_ht.zero_grad() # clear up gradients
loss_ht = 0.5*torch.norm((x[idx]-(D*ht[idx]).sum(dim=0)),p=2)**2
loss_ht.backward() # back propogation and calculate gradients
optimizer_ht.step() # update parameters with gradients
ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
RuntimeError Traceback (most recent call last) in ()
15 loss_ht.backward() # back propogation and calculate gradients
16 optimizer_ht.step() # update parameters with gradients
—> 17 ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
18
19
/home/miniconda3/lib/python3.6/site-packages/torch/autograd/variable.py in setitem(self, key, value)
85 return MaskedFill.apply(self, key, value, True)
86 else:
—> 87 return SetItem.apply(self, key, value)
88
89 def deepcopy(self, memo):
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
Specifically, this line of code below seems give error as it index and update leaf variable at the same time.
ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
Thanks.
W.S.
| I just found: In order to update the variable, it needs to be ht.data[idx] instead of ht[idx]. We can use .data to access the tensor directly.
| https://stackoverflow.com/questions/49161652/ |
Copying a PyTorch Variable to a Numpy array | Suppose I have a PyTorch Variable in GPU:
var = Variable(torch.rand((100,100,100))).cuda()
What's the best way to copy (not bridge) this variable to a NumPy array?
var.clone().data.cpu().numpy()
or
var.data.cpu().numpy().copy()
By running a quick benchmark, .clone() was slightly faster than .copy(). However, .clone() + .numpy() will create a PyTorch Variable plus a NumPy bridge, while .copy() will create a NumPy bridge + a NumPy array.
| This is a very interesting question. According to me, the question is little bit opinion-based and I would like to share my opinion on this.
From the above two approaches, I would prefer the first one (use clone()). Since your goal is to copy information, essentially you need to invest extra memory. clone() and copy() should take a similar amount of storage since creating numpy bridge doesn't cause extra memory. Also, I didn't understand what you meant by, copy() will create two numPy arrays. And as you mentioned, clone() is faster than copy(), I don't see any other problem with using clone().
I would love to give a second thought on this if anyone can provide some counter arguments.
| https://stackoverflow.com/questions/49178967/ |
Check the total number of parameters in a PyTorch model | How do I count the total number of parameters in a PyTorch model? Something similar to model.count_params() in Keras.
| PyTorch doesn't have a function to calculate the total number of parameters as Keras does, but it's possible to sum the number of elements for every parameter group:
pytorch_total_params = sum(p.numel() for p in model.parameters())
If you want to calculate only the trainable parameters:
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
Answer inspired by this answer on PyTorch Forums.
Note: I'm answering my own question. If anyone has a better solution, please share with us.
| https://stackoverflow.com/questions/49201236/ |
How to use pack_padded_sequence with multiple variable-length input with the same label in pytorch | I have a model which takes three variable-length inputs with the same label. Is there a way I could use pack_padded_sequence somehow? If so, how should I sort my sequences?
For example,
a = (([0,1,2], [3,4], [5,6,7,8]), 1) # training data is in length 3,2,4; label is 1
b = (([0,1], [2], [6,7,8,9,10]), 1)
Both a and b will be fed into three separated LSTMs and the result will be merged to predict the target.
| Let's do it step by step.
Input Data Processing
a = (([0,1,2], [3,4], [5,6,7,8]), 1)
# store length of each element in an array
len_a = np.array([len(a) for a in a[0]])
variable_a = np.zeros((len(len_a), np.amax(len_a)))
for i, a in enumerate(a[0]):
variable_a[i, 0:len(a)] = a
vocab_size = len(np.unique(variable_a))
Variable(torch.from_numpy(variable_a).long())
print(variable_a)
It prints:
Variable containing:
0 1 2 0
3 4 0 0
5 6 7 8
[torch.DoubleTensor of size 3x4]
Defining embedding and RNN layer
Now, let's say, we have an Embedding and RNN layer class as follows.
class EmbeddingLayer(nn.Module):
def __init__(self, input_size, emsize):
super(EmbeddingLayer, self).__init__()
self.embedding = nn.Embedding(input_size, emsize)
def forward(self, input_variable):
return self.embedding(input_variable)
class Encoder(nn.Module):
def __init__(self, input_size, hidden_size, bidirection):
super(Encoder, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.bidirection = bidirection
self.rnn = nn.LSTM(self.input_size, self.hidden_size, batch_first=True,
bidirectional=self.bidirection)
def forward(self, sent_variable, sent_len):
# Sort by length (keep idx)
sent_len, idx_sort = np.sort(sent_len)[::-1], np.argsort(-sent_len)
idx_unsort = np.argsort(idx_sort)
idx_sort = torch.from_numpy(idx_sort)
sent_variable = sent_variable.index_select(0, Variable(idx_sort))
# Handling padding in Recurrent Networks
sent_packed = nn.utils.rnn.pack_padded_sequence(sent_variable, sent_len, batch_first=True)
sent_output = self.rnn(sent_packed)[0]
sent_output = nn.utils.rnn.pad_packed_sequence(sent_output, batch_first=True)[0]
# Un-sort by length
idx_unsort = torch.from_numpy(idx_unsort)
sent_output = sent_output.index_select(0, Variable(idx_unsort))
return sent_output
Embed and encode the processed input data
We can embed and encode our input as follows.
emb = EmbeddingLayer(vocab_size, 50)
enc = Encoder(50, 100, False, 'LSTM')
emb_a = emb(variable_a)
enc_a = enc(emb_a, len_a)
If you print the size of enc_a, you will get torch.Size([3, 4, 100]). I hope you understand the meaning of this shape.
Please note, the above code runs only on CPU.
| https://stackoverflow.com/questions/49203019/ |
pytorch error: multi-target not supported in CrossEntropyLoss() | I am on a project using acceleration data to predict some activities.
But I have problems on the loss calculation. I am using CrossEntropyLoss for it.
Data is used for it like below
I use the first 4 data of each rows to predict the index like the last one of each rows.
1 84 84 81 4
81 85 85 80 1
81 82 84 80 1
1 85 84 2 0
81 85 82 80 1
81 82 84 80 1
81 25 84 80 5
The error messages are like below.
minoh@minoh-VirtualBox:~/cow$ python lec5.py
Traceback (most recent call last):
File "lec5.py", line 97, in <module>
train(epoch)
File "lec5.py", line 74, in train
loss = criterion(y_pred, labels)
File "/home/minoh/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
result = self.forward(*input, **kwargs)
File "/home/minoh/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 679, in forward
self.ignore_index, self.reduce)
File "/home/minoh/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1161, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
File "/home/minoh/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1052, in nll_loss
return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
RuntimeError: multi-target not supported at /opt/conda/conda-bld/pytorch_1518243271935/work/torch/lib/THNN/generic/ClassNLLCriterion.c:22
My code is based on Sung Kim's pytorch
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
class CowDataset(Dataset):
def __init__(self):
xy_str = np.loadtxt('cow_test', delimiter = ' ', dtype = np.str)
xy = xy_str.astype(np.float32)
xy_int = xy_str.astype(np.int)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 0:4])
self.y_data = torch.from_numpy(xy_int[:, [4]])
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
dataset = CowDataset()
train_loader = DataLoader(dataset = dataset, batch_size = 32, shuffle = True)
class CowTestset(Dataset):
def __init__(self):
xy_str = np.loadtxt('cow_test2', delimiter = ' ', dtype =np.str)
xy = xy_str.astype(np.float32)
xy_int = xy_str.astype(np.int)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 0:4])
self.y_data = torch.from_numpy(xy_int[:, [4]])
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
testset = CowTestset()
test_loader = DataLoader(dataset = testset, batch_size = 32, shuffle = True)
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.l1 = torch.nn.Linear(4,5)
self.l2 = torch.nn.Linear(5,7)
self.l3 = torch.nn.Linear(7,6)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
out1 = self.sigmoid(self.l1(x))
out2 = self.sigmoid(self.l2(out1))
y_pred = self.sigmoid(self.l3(out2))
return y_pred
model = Model()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr = 0.1, momentum = 0.5)
def train(epoch):
model.train()
for batch_idx, (inputs, labels) in enumerate(train_loader):
inputs, labels = Variable(inputs), Variable(labels)
optimizer.zero_grad()
y_pred = model(inputs)
loss = criterion(y_pred, labels)
loss.backward()
optimizer.step()
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
def test():
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data, volatile = True), Variable(target)
print(target)
output = model(data)
test_loss += criterion(output, target).data[0]
pred = output.data.max(1, keepdim = True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(test_loss, correct, len(test_loader.dataset), 100.* correct / len(test_loader.dataset)))
for epoch in range(1,7):
train(epoch)
test()
| Ok. So I reproduced your problem and after some search and reading the API of CrossEntropyLoss(), I have found it's because you have a wrong label dimension.
Offical docs of CrossEntropyLoss here. And you can see
Input: (N,C) where C = number of classes
Target: (N) where each value is 0≤targets[i]≤C−1
While here, in your criterion() function, you have a batchSize x 7 input and batchSize x 1 label. The confusing point is, say your batchSize is 10, a 10x1 tensor can not be regarded as a size-10 tensor, which is what the loss function expectes. You must explictly do the size conversion.
Solution:
Add labels = labels.squeeze_() before you call loss = criterion(y_pred, labels) and do the same thing in your test code. The squeeze_() funciton removes size-1 dimensions inplace. So you have a batchSize-size label now.
| https://stackoverflow.com/questions/49206550/ |
Is there an efficient way to create a random bit mask in Pytorch? | I want to have a random bit mask that has some specified percent of 0s. The function I devised is:
def create_mask(shape, rate):
"""
The idea is, you take a random permutations of numbers. You then mod then
mod it by the [number of entries in the bitmask] / [percent of 0s you
want]. The number of zeros will be exactly the rate of zeros need. You
can clamp the values for a bitmask.
"""
mask = torch.randperm(reduce(operator.mul, shape, 1)).float().cuda()
# Mod it by the percent to get an even dist of 0s.
mask = torch.fmod(mask, reduce(operator.mul, shape, 1) / rate)
# Anything not zero should be put to 1
mask = torch.clamp(mask, 0, 1)
return mask.view(shape)
To illustrate:
>>> x = create_mask((10, 10), 10)
>>> x
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1 1
0 1 1 1 1 0 1 1 1 1
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 0 1 1
0 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1
[torch.cuda.FloatTensor of size 10x10 (GPU 0)]
The main issue I have with this method is it requires the rate to divide the shape. I want a function that accepts an arbitrary decimal and gives approximately rate percent of 0s in the bitmask. Furthermore, I am trying to find a relatively efficient way of doing so. Hence, I would rather not move a numpy array from the CPU to the GPU. Is there an effiecient way of doing so that allows for a decimal rate?
| For anyone running into this, this will create a bitmask with approximately 80% zero's directly on GPU. (PyTorch 0.3)
torch.cuda.FloatTensor(10, 10).uniform_() > 0.8
| https://stackoverflow.com/questions/49216615/ |
Difference between 1 LSTM with num_layers = 2 and 2 LSTMs in pytorch | I am new to deep learning and currently working on using LSTMs for language modeling. I was looking at the pytorch documentation and was confused by it.
If I create a
nn.LSTM(input_size, hidden_size, num_layers)
where hidden_size = 4 and num_layers = 2, I think I will have an architecture something like:
op0 op1 ....
LSTM -> LSTM -> h3
LSTM -> LSTM -> h2
LSTM -> LSTM -> h1
LSTM -> LSTM -> h0
x0 x1 .....
If I do something like
nn.LSTM(input_size, hidden_size, 1)
nn.LSTM(input_size, hidden_size, 1)
I think the network architecture will look exactly like above. Am I wrong? And if yes, what is the difference between these two?
| The multi-layer LSTM is better known as stacked LSTM where multiple layers of LSTM are stacked on top of each other.
Your understanding is correct. The following two definitions of stacked LSTM are same.
nn.LSTM(input_size, hidden_size, 2)
and
nn.Sequential(OrderedDict([
('LSTM1', nn.LSTM(input_size, hidden_size, 1),
('LSTM2', nn.LSTM(hidden_size, hidden_size, 1)
]))
Here, the input is feed into the lowest layer of LSTM and then the output of the lowest layer is forwarded to the next layer and so on so forth. Please note, the output size of the lowest LSTM layer and the rest of the LSTM layer's input size is hidden_size.
However, you may have seen people defined stacked LSTM in the following way:
rnns = nn.ModuleList()
for i in range(nlayers):
input_size = input_size if i == 0 else hidden_size
rnns.append(nn.LSTM(input_size, hidden_size, 1))
The reason people sometimes use the above approach is that if you create a stacked LSTM using the first two approaches, you can't get the hidden states of each individual layer. Check out what LSTM returns in PyTorch.
So, if you want to have the intermedia layer's hidden states, you have to declare each individual LSTM layer as a single LSTM and run through a loop to mimic the multi-layer LSTM operations. For example:
outputs = []
for i in range(nlayers):
if i != 0:
sent_variable = F.dropout(sent_variable, p=0.2, training=True)
output, hidden = rnns[i](sent_variable)
outputs.append(output)
sent_variable = output
In the end, outputs will contain all the hidden states of each individual LSTM layer.
| https://stackoverflow.com/questions/49224413/ |
What is the difference between softmax and log-softmax? | The difference between these two functions that has been described in this pytorch post: What is the difference between log_softmax and softmax?
is: exp(x_i) / exp(x).sum()
and log softmax is: log(exp(x_i) / exp(x).sum()).
But for the Pytorch code below why am I getting different output:
>>> it = autograd.Variable(torch.FloatTensor([0.6229,0.3771]))
>>> op = autograd.Variable(torch.LongTensor([0]))
>>> m = nn.Softmax()
>>> log = nn.LogSoftmax()
>>> m(it)
Variable containing:
`0.5611 0.4389`
[torch.FloatTensor of size 1x2]
>>>log(it)
Variable containing:
-0.5778 -0.8236
[torch.FloatTensor of size 1x2]
However, the value log(0.5611) is -0.25095973129 and log(0.4389) is -0.35763441915
Why is there such discrepancy?
| By default, torch.log provides the natural logarithm of the input, so the output of PyTorch is correct:
ln([0.5611,0.4389])=[-0.5778,-0.8236]
Your last results are obtained using the logarithm with base 10.
| https://stackoverflow.com/questions/49236571/ |
Pytorch beginner : tensor.new method | everyone, I have a small question.
What is the purpose of the method tensor.new(..) in Pytorch, I didn't find anything in the documentation. It looks like it creates a new Tensor (like the name suggests), but why we don't just use torch.Tensor constructors instead of using this new method that requires an existing tensor.
Thank you in advance.
| As the documentation of tensor.new() says:
Constructs a new tensor of the same data type as self tensor.
Also note:
For CUDA tensors, this method will create new tensor on the same device as this tensor.
| https://stackoverflow.com/questions/49263588/ |
How does batching work in a seq2seq model in pytorch? | I am trying to implement a seq2seq model in Pytorch and I am having some problem with the batching.
For example I have a batch of data whose dimensions are
[batch_size, sequence_lengths, encoding_dimension]
where the sequence lengths are different for each example in the batch.
Now, I managed to do the encoding part by padding each element in the batch to the length of the longest sequence.
This way if I give as input to my net a batch with the same shape as said, I get the following outputs:
output, of shape [batch_size, sequence_lengths, hidden_layer_dimension]
hidden state, of shape [batch_size, hidden_layer_dimension]
cell state, of shape [batch_size, hidden_layer_dimension]
Now, from the output, I take for each sequence the last relevant element, that is the element along the sequence_lengths dimension corresponding to the last non padded element of the sequence. Thus the final output I get is of shape [batch_size, hidden_layer_dimension].
But now I have the problem of decoding it from this vector. How do I handle a decoding of sequences of different lengths in the same batch? I tried to google it and found this, but they don't seem to address the problem. I thought of doing element by element for the whole batch, but then I have the problem to pass the initial hidden states, given that the ones from the encoder will be of shape [batch_size, hidden_layer_dimension], while the ones from the decoder will be of shape [1, hidden_layer_dimension].
Am I missing something? Thanks for the help!
| You are not missing anything. I can help you since I have worked on several sequence-to-sequence application using PyTorch. I am giving you a simple example below.
class Seq2Seq(nn.Module):
"""A Seq2seq network trained on predicting the next query."""
def __init__(self, dictionary, embedding_index, args):
super(Seq2Seq, self).__init__()
self.config = args
self.num_directions = 2 if self.config.bidirection else 1
self.embedding = EmbeddingLayer(len(dictionary), self.config)
self.embedding.init_embedding_weights(dictionary, embedding_index, self.config.emsize)
self.encoder = Encoder(self.config.emsize, self.config.nhid_enc, self.config.bidirection, self.config)
self.decoder = Decoder(self.config.emsize, self.config.nhid_enc * self.num_directions, len(dictionary),
self.config)
@staticmethod
def compute_decoding_loss(logits, target, seq_idx, length):
losses = -torch.gather(logits, dim=1, index=target.unsqueeze(1)).squeeze()
mask = helper.mask(length, seq_idx) # mask: batch x 1
losses = losses * mask.float()
num_non_zero_elem = torch.nonzero(mask.data).size()
if not num_non_zero_elem:
return losses.sum(), 0 if not num_non_zero_elem else losses.sum(), num_non_zero_elem[0]
def forward(self, q1_var, q1_len, q2_var, q2_len):
# encode the query
embedded_q1 = self.embedding(q1_var)
encoded_q1, hidden = self.encoder(embedded_q1, q1_len)
if self.config.bidirection:
if self.config.model == 'LSTM':
h_t, c_t = hidden[0][-2:], hidden[1][-2:]
decoder_hidden = torch.cat((h_t[0].unsqueeze(0), h_t[1].unsqueeze(0)), 2), torch.cat(
(c_t[0].unsqueeze(0), c_t[1].unsqueeze(0)), 2)
else:
h_t = hidden[0][-2:]
decoder_hidden = torch.cat((h_t[0].unsqueeze(0), h_t[1].unsqueeze(0)), 2)
else:
if self.config.model == 'LSTM':
decoder_hidden = hidden[0][-1], hidden[1][-1]
else:
decoder_hidden = hidden[-1]
decoding_loss, total_local_decoding_loss_element = 0, 0
for idx in range(q2_var.size(1) - 1):
input_variable = q2_var[:, idx]
embedded_decoder_input = self.embedding(input_variable).unsqueeze(1)
decoder_output, decoder_hidden = self.decoder(embedded_decoder_input, decoder_hidden)
local_loss, num_local_loss = self.compute_decoding_loss(decoder_output, q2_var[:, idx + 1], idx, q2_len)
decoding_loss += local_loss
total_local_decoding_loss_element += num_local_loss
if total_local_decoding_loss_element > 0:
decoding_loss = decoding_loss / total_local_decoding_loss_element
return decoding_loss
You can see the complete source code here. This application is about predicting users' next web-search query given the current web-search query.
The answerer to your question:
How do I handle a decoding of sequences of different lengths in the same batch?
You have padded sequences, so you can consider as all the sequences are of the same length. But when you are computing loss, you need to ignore loss for those padded terms using masking.
I have used a masking technique to achieve the same in the above example.
Also, you are absolutely correct on: you need to decode element by element for the mini-batches. The initial decoder state [batch_size, hidden_layer_dimension] is also fine. You just need to unsqueeze it at dimension 0, to make it [1, batch_size, hidden_layer_dimension].
Please note, you do not need to loop over each example in the batch, you can execute the whole batch at a time, but you need to loop over the elements of the sequences.
| https://stackoverflow.com/questions/49283435/ |
Tying weights in neural machine translation | I want to tie weights of the embedding layer and the next_word prediction layer of the decoder. The embedding dimension is set to 300 and the hidden size of the decoder is set to 600. Vocabulary size of the target language in NMT is 50000, so embedding weight dimension is 50000 x 300 and weight of the linear layer which predicts the next word is 50000 x 600.
So, how can I tie them? What will be the best approach to achieve weight tying in this scenario?
| You could use linear layer to project the 600 dimensional space down to 300 before you apply the shared projection. This way you still get the advantage that the entire embedding (possibly) has a non-zero gradient for each mini-batch but at the risk of increasing the capacity of the network slightly.
| https://stackoverflow.com/questions/49299609/ |