title: PyTorch: Why does validation accuracy change once calling it inside or outside training epochs loop?

body:
I am learning deep learning and I am trying to use the RNN with train, test and validation sets on time series finance data. Below is my code :

def get_lr(optimizer):
    for param_group in optimizer.param_groups:
        return param_group['lr']

# In[63]:

def train_model(epoch, model, optimizer, train_loader):
    model.train()
    t0 = time.time()
    correct = 0
    total = 0
    final_loss = 0
    for batch_idx, (X,labels) in enumerate(train_loader):
        data,labels = map(lambda x: Variable(x), [X,labels])
        optimizer.zero_grad()
        output = model(data)
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        ##print('device : ', device)
        final_output=output.to(device)
        loss = F.cross_entropy(final_output, labels)
        final_loss += loss.item()
        loss.backward()
        optimizer.step()
        print('predicted labels',final_output.squeeze())
        #print('Actual labels',labels.squeeze())
        print('Train Epoch: {} Batch: {} [{}/{} ({:.2f}%, time:{:.2f}s)]\tBatch Loss: {:.6f}'.format(
                epoch, batch_idx, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), time.time() - t0,
                final_loss))
                ##avg_loss))
        _, predicted = torch.max(output.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
        t0 = time.time()
    final_loss /= (batch_idx+1)
    accuracy =  100*correct/total
    lr = get_lr(optimizer)
    learning_rates.append(lr)
    print('Training Accuracy : ',accuracy)
    print('Training Loss : ',final_loss)
    print('Learning Rate : ',lr)
    if epoch%epoch_interval == 0 or epoch ==1 or epoch == epochs:
        path = base_path + 'models/RNN/rnn_'
        torch.save(model,path+str(epoch)+'.pth')
        ##torch.save(model,path)
        print('model saved')
    if epoch%plot_epoch_interval == 0 or epoch ==1 or epoch == epochs:
        epochs_list.append(epoch)
        train_loss.append(final_loss)
        train_accuracies.append(accuracy)
    return lr,final_loss,accuracy

# In[166]:

def validate(epoch,model, val_loader,optimizer):
    model.eval()
    val_loss = 0
    correct = 0
    total = 0
    loss = 0
    ypred,ytrue,scores = [],[],[]
    for batch_idx,(X,labels) in enumerate(val_loader):
        data,labels = map(lambda x: Variable(x), [X,labels])
        output = model(data)
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        final_val_output=output.to(device)
        val_loss += F.cross_entropy(final_val_output, labels)  # sum up batch loss
        _, predicted = torch.max(output.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
        ypred.extend(predicted.tolist())
        ytrue.extend(labels.tolist())
        scores.extend(output.tolist())
    val_loss /= (batch_idx+1)
    accuracy =  100*correct/total
    if epoch%plot_epoch_interval == 0 or epoch ==1 or epoch == epochs:
        validation_loss.append(val_loss.item())
        val_accuracies.append(accuracy)

    print('Accuracy : ',accuracy)
    print('\nVal set: Average loss: {:.4f}, Accuracy: {}/{} ({:.4f}%)\n'.format(
        val_loss, correct,total,accuracy))
    print("==============================================")
    return "{:.4f}%".format(100.* correct / total), accuracy,loss,ypred,ytrue,scores

# In[276]:

def test(data_loader,model):
    torch.manual_seed(1)
    np.random.seed(1)
    #data_loader = DataLoader(FinancialData(xtest,ytest), batch_size = batch_size, shuffle = False)
    model = torch.load(path)
    model.eval()
    for params in model.parameters():
         print(params)
    val_loss = 0
    correct = 0
    total = 0
    loss = 0
    ypred,ytrue,scores = [],[],[]
    with torch.no_grad():
        for batch_idx,(X,labels) in enumerate(data_loader):
            data,labels = map(lambda x: Variable(x), [X,labels])
            output = model(data)
            _, predicted = torch.max(output.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            ypred.extend(predicted.tolist())
            ytrue.extend(labels.tolist())
            scores.extend(output.tolist())
            accuracy =  100*correct/total

    print('Test Accuracy : ',accuracy)

# In[288]:

def train_on_batch(lr,epochs,momentum,X_train,Y_train,X_val,Y_val,batch_size):
    cuda=False
    seed=1
    torch.manual_seed(seed)

    train_loader = DataLoader(FinancialData(X_train,Y_train),batch_size=batch_size,shuffle=True)
    val_loader = DataLoader(FinancialData(X_val,Y_val),batch_size=batch_size,shuffle=False)
    test_loader = DataLoader(FinancialData(X_test_new,Y_test), batch_size = batch_size, shuffle = False)
    input_size = 1
    hid_size = 10
    num_layers = 2
    num_classes = len(np.unique(Y_train))
    dropRate = 0.0
    bidirection = True
    model = Network(input_size=input_size,hid_size =hid_size,window_size = window_size,num_layers=num_layers,
                num_classes=num_classes,dropRate = dropRate,bidirection=bidirection)
    ypred,ytrue, scores = [],[],[]
    for params in model.parameters():
        print(params)
    optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-4, amsgrad=False)

    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,'max', factor=0.25, patience=6, verbose=True,
                                                            threshold_mode='abs', threshold=0.01, min_lr=1e-6)

    #scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', factor=0.5, patience=5,
    #                                                       verbose=True,threshold_mode='abs', threshold=0.01,
    #                                                       min_lr=1e-6)
    path = base_path + 'models/RNN/rnn_best_model.pth'
    best_val_loss = 0
    best_val_acc = 0
    best_epoch = 0
    best_lr = lr
    best_tr_acc = 0
    for epoch in range(1, epochs + 1):
        tuned_lr,tr_loss,tr_acc = train_model(epoch, model, optimizer, train_loader)
        acc_str, val_acc, val_loss, ypred, ytrue, scores = validate(epoch,model,val_loader,optimizer)
        if val_acc >= best_val_acc:
                torch.save(model,path)
                #best_val_loss = val_loss
                best_val_acc = val_acc
                best_epoch = epoch
                best_lr = tuned_lr
                best_tr_acc = tr_acc
        scheduler.step(val_acc)
        #scheduler.step(tr_acc)
        print('='*100)
#         for params in model.parameters():
#             print(params)
#         print('='*100)
    test(val_loader,model)
    test(test_loader,model)
    #validate(epoch,model,val_loader,optimizer)
    #validate(epoch,model,test_loader,optimizer)
    print('best epoch : {}, best_lr : {}, best_tr_acc : {}, best val_acc : {:.4f}\n'.format(best_epoch,best_lr,best_tr_acc,best_val_acc))
    scores = np.asarray(scores)
    return tr_acc,val_acc, ypred, ytrue, scores

# In[289]:

cuda=torch.cuda.is_available()

X_train,Y_train,X_val,Y_val,X_test,Y_test = splitDataWithVal(feat_wise_data,labels_new,test_size=0.2,val_size=0.25)

X_train_new, X_val_new, X_test_new = standardizeDataVal(X_train, X_test, X_val, mode = 'Normalizer')

# # Check for Class Imbalance

# In[292]:

Ytrain_df= pd.DataFrame(Y_train,columns=[0])
print(Ytrain_df.shape)
print(Ytrain_df.columns)
print(Ytrain_df.groupby(0).size())

train_loss = []
validation_loss = []
epochs_list = []
train_accuracies = []
val_accuracies = []
learning_rates = []
epoch_interval = 1#10
plot_epoch_interval = 5

lr = 0.01
momentum = 0.9
epochs = 3
batch_size = 4
print('batch_size : ',batch_size)
tr_acc,val_acc, ypred, ytrue, scores = train_on_batch(lr,epochs,momentum,X_train_new,Y_train,X_val_new,Y_val,batch_size)

I tested it for 3 epochs and saved models after every epoch. However, after 3rd epoch i.e. complete 3 epochs of training, when I test my model by calling test() function of my code, it gives 49.7% validation accuracy and 59.3% test accuracy.

Whereas if I use validate() function of my code, it gives 51.146% validation accuracy when called after 3rd epoch of training within training loop. Using validate() function after complete training of 3 epochs ie. outside for loop, I get 49.12% validation accuracy and 54.0697% test accuracy.

Why does validation accuracy change calling the same validate function twice i.e. once inside training epochs loop and other time after training epochs loop ? Also, which function is correct way of testing and validating, validate() or test()?

I even loaded all the models which I am saving after every epoch and checked their weights which are same as what they were seen during training. I am new to this domain.


title: TypeError although same shape: if not (target.size() == input.size()): 'int' object is not callable

body:
This is the error message I get. In the first line, I output the shapes of predicted and  target. From my understanding, the error arises from those shapes not being the same but here they clearly are.

torch.Size([6890, 3]) torch.Size([6890, 3])

Traceback (most recent call last):
  File "train.py", line 251, in <module>
    main()
  File "train.py", line 230, in main
    train(net, training_dataset, targets, device, criterion, optimizer, epoch, args.epochs)
  File "train.py", line 101, in train
    loss = criterion(predicted, target.detach().cpu().numpy())
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 443, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2244, in mse_loss
    if not (target.size() == input.size()):
TypeError: 'int' object is not callable

I hope all the relevant context information is provided and if not, please let me know. Thanks for any suggestions!

EDIT: This is the part of the code where this error occurs:

        target = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(i))).to(device)
        rv = torch.zeros(12 * outputs.shape[0])

        for j in [x for x in range(10) if x != i]:
            source = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(j))).to(device)
            rv = factor.ransac(source, target, prob, n_iter, tol, device) # some self-written RANSAC-like method

        predicted = factor.predict(source, rv, outputs)
        print(target.shape, predicted.shape)
        loss = criterion(predicted, target.detach().cpu().numpy()) ## error occurs here

criterion is nn.MSELoss().


title: Pytorch CPU and GPU run in parallel

body:
Is there a way to do something with CPU (compute mean and variance of current mini-batch loss) while GPU is doing back-propagation?

Something like this:

for input, label in dataloader:
  output = model(input)
  losses = some_loss_function(output, label) # size = (batch_size,)
  loss = losses.sum() / batch_size
  # =========== do on CPU ============
  mean = loss.item()
  var = losses.pow(2).sum().item() / batch_size - mean**2
  # ============ BP ================
  loss.backward()
  #gradient update

will the backward() on GPU wait for CPU computation to finish? Is there a way to do backward() and CPU computation in parallel?


title: BERT training with character embeddings

body: Does it make sense to change the tokenization paradigm in the BERT model, to something else? Maybe just a simple word tokenization or character level tokenization?


title: Creating a pytorch tensor binary mask using specific values

body:
I am given a pytorch 2-D tensor with integers, and 2 integers that always appear in each row of the tensor.
I want to create a binary mask that will contain 1 between the two appearances of these 2 integers, otherwise 0. For example, if the integers are 4 and 2 and the 1-D array is [1,1,9,4,6,5,1,2,9,9,11,4,3,6,5,2,3,4], the returned mask will be: [0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0].
Is there any efficient and quick way to compute this mask without iterations?


title: How does one manually compute the error of the whole data set in pytorch?

body:
I was trying to track the error of the whole data set and compute the error of the whole data set in pytorch. I wrote the following (reproducible example and fully contained) in cifar10 pytorch 0.3.1:

import torch
from torch.autograd import Variable
import torch.optim as optim

import torchvision
import torchvision.transforms as transforms

from math import inf

from pdb import set_trace as st

def error_criterion(outputs,labels):
    max_vals, max_indices = torch.max(outputs,1)
    train_error = (max_indices != labels).sum().data[0]/max_indices.size()[0]
    return train_error

def evalaute_mdl_data_set(loss,error,net,dataloader,enable_cuda,iterations=inf):
    '''
    Evaluate the error of the model under some loss and error with a specific data set.
    '''
    running_loss,running_error = 0,0
    for i,data in enumerate(dataloader):
        if i >= iterations:
            break
        inputs, labels = extract_data(enable_cuda,data,wrap_in_variable=True)
        outputs = net(inputs)
        running_loss += loss(outputs,labels).data[0]
        running_error += error(outputs,labels)
    return running_loss/(i+1),running_error/(i+1)

def extract_data(enable_cuda,data,wrap_in_variable=False):
    inputs, labels = data
    if enable_cuda:
        inputs, labels = inputs.cuda(), labels.cuda() #TODO potential speed up?
    if wrap_in_variable:
        inputs, labels = Variable(inputs), Variable(labels)
    return inputs, labels

def train_and_track_stats(enable_cuda, nb_epochs, trainloader,testloader, net,optimizer,criterion,error_criterion, iterations=inf):
    ''' Add stats before training '''
    train_loss_epoch, train_error_epoch = evalaute_mdl_data_set(criterion, error_criterion, net, trainloader, enable_cuda, iterations)
    test_loss_epoch, test_error_epoch = evalaute_mdl_data_set(criterion, error_criterion, net, testloader, enable_cuda, iterations)
    print(f'[-1, -1], (train_loss: {train_loss_epoch}, train error: {train_error_epoch}) , (test loss: {test_loss_epoch}, test error: {test_error_epoch})')
    ##
    ''' Start training '''
    print('about to start training')
    for epoch in range(nb_epochs):  # loop over the dataset multiple times
        running_train_loss,running_train_error = 0.0,0.0
        for i,data_train in enumerate(trainloader):
            ''' zero the parameter gradients '''
            optimizer.zero_grad()
            ''' train step = forward + backward + optimize '''
            inputs, labels = extract_data(enable_cuda,data_train,wrap_in_variable=True)
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            running_train_loss += loss.data[0]
            running_train_error += error_criterion(outputs,labels)
        ''' End of Epoch: collect stats'''
        train_loss_epoch, train_error_epoch = running_train_loss/(i+1), running_train_error/(i+1)
        #train_loss_epoch, train_error_epoch = evalaute_mdl_data_set(criterion,error_criterion,net,trainloader,enable_cuda,iterations)
        test_loss_epoch, test_error_epoch = evalaute_mdl_data_set(criterion,error_criterion,net,testloader,enable_cuda,iterations)
        print(f'[{epoch}, {i+1}], (train_loss: {train_loss_epoch}, train error: {train_error_epoch}) , (test loss: {test_loss_epoch}, test error: {test_error_epoch})')
    return train_loss_epoch, train_error_epoch, test_loss_epoch, test_error_epoch

class Flatten(torch.nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)

def main():
    enable_cuda = True
    print('running main')
    num_workers = 0
    ''' Get Data set '''
    batch_size_test = 10000
    batch_size_train = 10000
    data_path = './data'
    transform = [transforms.ToTensor(),transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5) )]
    transform = transforms.Compose(transform)
    trainset = torchvision.datasets.CIFAR10(root=data_path, train=True,download=False, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size_train,shuffle=True, num_workers=num_workers)
    testset = torchvision.datasets.CIFAR10(root=data_path, train=False,download=False, transform=transform)
    testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size_test,shuffle=False, num_workers=num_workers)
    ''' Get model '''
    net = torch.nn.Sequential(
        torch.nn.Conv2d(3,13,5), #(in_channels, out_channels, kernel_size),
        Flatten(),
        torch.nn.Linear(28*28*13, 13),
        torch.nn.Linear(13, 10)
    )
    net.cuda()
    ''' Train '''
    nb_epochs = 10
    lr = 0.1
    err_criterion = error_criterion
    criterion = torch.nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.0)
    train_and_track_stats(enable_cuda, nb_epochs, trainloader,testloader, net,optimizer,criterion,err_criterion, iterations=inf)
    ''' Done '''
    print('Done')

if __name__ == '__main__':
    main()

When I run it I get the following error:

python my_cifar10.py
running main
[-1, -1], (train_loss: 2.3172860145568848, train error: 0.0054) , (test loss: 2.317185878753662, test error: 0.0038)
about to start training
[0, 5], (train_loss: 2.22599835395813, train error: 0.015160000000000002) , (test loss: 2.0623881816864014, test error: 0.0066)
[1, 5], (train_loss: 2.014406657218933, train error: 0.00896) , (test loss: 1.9619578123092651, test error: 0.0195)
[2, 5], (train_loss: 1.9428715705871582, train error: 0.01402) , (test loss: 1.918603539466858, test error: 0.0047)
[3, 5], (train_loss: 1.9434458494186402, train error: 0.01192) , (test loss: 1.9194672107696533, test error: 0.0125)
[4, 5], (train_loss: 1.8804980754852294, train error: 0.00794) , (test loss: 1.8549214601516724, test error: 0.004)
[5, 5], (train_loss: 1.8573726177215577, train error: 0.010159999999999999) , (test loss: 1.8625996112823486, test error: 0.0158)
[6, 5], (train_loss: 1.8454653739929199, train error: 0.01524) , (test loss: 1.8155865669250488, test error: 0.0122)
[7, 5], (train_loss: 1.8140610456466675, train error: 0.01066) , (test loss: 1.808283805847168, test error: 0.0101)
[8, 5], (train_loss: 1.8036894083023072, train error: 0.00832) , (test loss: 1.799634575843811, test error: 0.007)
[9, 5], (train_loss: 1.8023016452789307, train error: 0.0077399999999999995) , (test loss: 1.8030155897140503, test error: 0.0114)
Done

Clearly it has to be wrong cuz the test error is nearly zero with a model that is ridiculously small and simple (1 conv 2 fcs).

the code seems so simple that I can't figure out what is going wrong. I've been doing stuff and changing things for a few days now. Any new suggestions what to try?


title: How to use Cross Entropy loss in pytorch for binary prediction

body:
In the pytorch docs, it says for cross entropy loss:

  input has to be a Tensor of size (minibatch, C)

Does this mean that for binary (0,1) prediction, the input must be converted into an (N,2) tensor where the second dimension is equal to (1-p)?

So for instance if I predict 0.75 for a class with target 1 (true), would I have to stack two values (0.75; 0.25) on top of each other as input?


title: What is the time-complexity of the pseudo-inverse in pytorch (i.e. torch.pinverse)?

body:
Let's say I have a matrix X with n, m == X.shape in PyTorch.
What is the time complexity of calculating the pseudo-inverse with torch.pinverse?

In other words, what is the time complexity of

X_p = torch.pinverse(X)

?

Here is the documentation


title: Loading ResNet50 on RTX2070 - Out of Memory

body:
I'm trying to load ResNext50, and on top of it CenterNet, I'm able to do it with Google Colab or Kaggle's GPU. But,

Would love to know how much GPU Memory (VRAM) does this network need?
When using RTX 2070 with free 5.5GB VRAM left on it (out of 8GB), I'm not able to load it.

Batch size is 1, #of workers is 1, everything is set to minimum values. OS: Ubuntu 18.04 (Using PyTorch)

In TensorFlow, I know that I can restrict the amount of VRAM (which enables me to load and run networks although I don't have enough VRAM), but in PyTorch I didn't find this functionality yet.

Any ideas how to solve this?


title: How to fix "ERROR: Failed building wheel for pytorch3d" error on Colab?

body:
I'm trying to use a very cool machine learning Colab.  https://colab.research.google.com/drive/1eQLZrNYRZMo9zdnGGccE0hFswGiinO-Z?usp=sharing Running their steps as is, I keep getting ERROR: Failed building wheel for pytorch3d.
After much Googling, I've tried for instance replacing the install line with
!pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable'
and also
!pip install pytorch3d
The former doesn't work. The latter makes another issue arise:
"ImportError: /usr/local/lib/python3.6/dist-packages/pytorch3d/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZNK2at6Tensor7is_cudaEv"
I also tried doing !pip install torch==1.6.0 which causes
Traceback (most recent call last):
  File "./meshrcnn/demo/demo.py", line 11, in <module>
    from detectron2.data import MetadataCatalog
  File "/usr/local/lib/python3.6/dist-packages/detectron2/data/__init__.py", line 4, in <module>
    from .build import (
  File "/usr/local/lib/python3.6/dist-packages/detectron2/data/build.py", line 14, in <module>
    from detectron2.structures import BoxMode
  File "/usr/local/lib/python3.6/dist-packages/detectron2/structures/__init__.py", line 6, in <module>
    from .keypoints import Keypoints, heatmaps_to_keypoints
  File "/usr/local/lib/python3.6/dist-packages/detectron2/structures/keypoints.py", line 6, in <module>
    from detectron2.layers import interpolate
  File "/usr/local/lib/python3.6/dist-packages/detectron2/layers/__init__.py", line 3, in <module>
    from .deform_conv import DeformConv, ModulatedDeformConv
  File "/usr/local/lib/python3.6/dist-packages/detectron2/layers/deform_conv.py", line 10, in <module>
    from detectron2 import _C
ImportError: /usr/local/lib/python3.6/dist-packages/detectron2/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN6caffe28TypeMeta21_typeMetaDataInstanceISt7complexIdEEEPKNS_6detail12TypeMetaDataEv

Have done !pip install mmcv-full===1.2.1 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.0/index.html to try fixing that and the error persists.
Does anyone have ideas for how to make the Colab environment work?


title: Input 3 and 1 channel input to the network in pytorch?

body:
My dataset consists mostly of 3 channel images, but i also have a few 1 channel images,Is it possible to train a network that takes in both 3 channels and 1 channels as inputs?

Any suggestions are welcome,Thanks in advance,


title: Pytorch 0.4.1 invalid gradient at index 0 - expected shape[] but got [1]

body:
I've been around this problem for the whole day.

torch.autograd.backward(loss_seq, grad_seq) will get an error.

Output:

Traceback (most recent call last):
  File "train_vgg.py", line 272, in <module>
    torch.autograd.backward(loss_seq, grad_seq)
  File "/root/anaconda3/lib/python3.6/site-
packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: invalid gradient at index 0 - expected shape [] but got [1]

Input:

loss_seq:[tensor(7.3761, device='cuda:1', grad_fn=<ThAddBackward>), tensor(4.3005, device='cuda:1', grad_fn=<ThAddBackward>), tensor(4.2209, device='cuda:1', grad_fn=<ThAddBackward>)]
grad_seq:[tensor([1.], device='cuda:1'), tensor([1.], device='cuda:1'), tensor([1.], device='cuda:1')]

```

Can someone tell how to fix it?

input code:

images = Variable(images).cuda(gpu)

label_yaw = Variable(labels[:,0]).cuda(gpu)
label_pitch = Variable(labels[:,1]).cuda(gpu)
label_roll = Variable(labels[:,2]).cuda(gpu)

pre_yaw, pre_pitch, pre_roll = model(images)

# Cross entropy loss
loss_yaw = criterion(pre_yaw, label_yaw)
loss_pitch = criterion(pre_pitch, label_pitch)
loss_roll = criterion(pre_roll, label_roll)

loss_yaw += 0.005 * loss_reg_yaw
loss_pitch += 0.005 * loss_reg_pitch
loss_roll += 0.005 * loss_reg_roll

loss_seq = [loss_yaw, loss_pitch, loss_roll]
grad_seq = [torch.ones(1).cuda(gpu) for _ in range(len(loss_seq))]

# crash here
torch.autograd.backward(loss_seq, grad_seq)


title: PyTorch: apply mapping over singleton dimension of tensor

body:
I'm afraid the title is not very descriptive but I could not think of a better one. Essentially my problem is the following:

I have a pytorch tensor of shape (n, 1, h, w) for arbitrary integers n, h and w (in my specific case this array represents a batch of grayscale images of dimension h x w).

I also have another tensor of shape (m, 2) which maps every possible value in the first array (i.e. the first array can contain values from 0 to m - 1) to some tuple of values. I would like to "apply" this mapping to the first array so that I obtain an array of shape (n, 2, h, w).

I hope this is somewhat clear, I find this hard to express in words, here's a code example (but note that that is not super intuitive either due to the four dimensional arrays involved):

import torch

m = 18

# could also be arbitrary tensor with this shape with values between 0 and m - 1
a = torch.arange(m).reshape(2, 1, 3, 3)

# could also be arbitrary tensor with this shape
b = torch.LongTensor(
    [[11, 17, 9, 6, 5, 4, 2, 10, 3, 13, 14, 12, 7, 1, 15, 16, 8, 0],
     [11, 8, 4, 14, 13, 12, 16, 1, 5, 17, 0, 10, 7, 15, 9, 6, 2, 3]]).t()

# I probably have to do this and the permute/reshape, but how?
c = b.index_select(0, a.flatten())

# ...

# another approach that I think works (but I'm not really sure why, I found this
# more or less by trial and error). I would ideally like to find a 'nicer' way
# of doing this
c = torch.stack([
    b.index_select(0, a_.flatten()).reshape(3, 3, 2).permute(2, 0, 1)
    for a_ in a
])

# the end result should be:
#[[[[11, 17,  9],
#   [ 6,  5,  4],
#   [ 2, 10,  3]],
#
#  [[11,  8,  4],
#   [14, 13, 12],
#   [16,  1,  5]]],
#
#
# [[[13, 14, 12],
#   [ 7,  1, 15],
#   [16,  8,  0]],
#
#  [[17,  0, 10],
#   [ 7, 15,  9],
#   [ 6,  2,  3]]]]

How can I perform this transformation in an efficient manner? (Ideally not using any additional memory). In numpy this could easily be achieved with np.apply_along_axis but there seems to be no pytorch equivalent to that.


title: what is the difference between unsqueez_ in pytorch and epxand_dim in keras and what will be the shape of output after using it?

body:
I am a beginner in keras and I have a pytorch code that I need to change it to keras, but I could not understand some part of it. specially I have problems in the size of the output shape.  the shape of image is (:, 3,32,32) and the first dimension of image is the size of the batch. now, my question is: what this line do and what is the output shape:

    image_yuv_ch = image[:, channel, :, :].unsqueeze_(1)

it adds a dimension in position 1? what is the output shape?:(
the size of filters was (64,8,8) and then we have filters.unsqueez_(1), is this means the new shape of filters is (64,1,8,8)?
what does this line do?  image_conv = F.conv2d(image_yuv_ch, filters, stride=8) is it the same as conv2d in keras what is the shape of output tensor from it? I also could not understand what view do? I know it tries to show tensor in new shape but in the below code I could not understand the output shape after each unsqueez_, permute or view. could you please tell me what is the output shape of each line? Thank you in advance.

import torch.nn.functional as F
def apply_conv(self, image, filter_type: str):

        if filter_type == 'dct':
            filters = self.dct_conv_weights
        elif filter_type == 'idct':
            filters = self.idct_conv_weights
        else:
            raise('Unknown filter_type value.')

        image_conv_channels = []
        for channel in range(image.shape[1]):
            image_yuv_ch = image[:, channel, :, :].unsqueeze_(1)
            image_conv = F.conv2d(image_yuv_ch, filters, stride=8)
            image_conv = image_conv.permute(0, 2, 3, 1)
            image_conv = image_conv.view(image_conv.shape[0], image_conv.shape[1], image_conv.shape[2], 8, 8)
            image_conv = image_conv.permute(0, 1, 3, 2, 4)
            image_conv = image_conv.contiguous().view(image_conv.shape[0],
                                                  image_conv.shape[1]*image_conv.shape[2],
                                                  image_conv.shape[3]*image_conv.shape[4])

            image_conv.unsqueeze_(1)

            # image_conv = F.conv2d()
            image_conv_channels.append(image_conv)

        image_conv_stacked = torch.cat(image_conv_channels, dim=1)

        return image_conv_stacked


title: Pytorch Autograd gives different gradients when using .clamp instead of torch.relu

body:
I'm still working on my understanding of the PyTorch autograd system. One thing I'm struggling at is to understand why .clamp(min=0) and nn.functional.relu() seem to have different backward passes.

It's especially confusing as .clamp is used equivalently to relu in PyTorch tutorials, such as https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-nn.

I found this when analysing the gradients of a simple fully connected net with one hidden layer and a relu activation (linear in the outputlayer).

to my understanding the output of the following code should be just zeros. I hope someone can show me what I am missing.

import torch
dtype = torch.float

x = torch.tensor([[3,2,1],
                  [1,0,2],
                  [4,1,2],
                  [0,0,1]], dtype=dtype)

y = torch.ones(4,4)

w1_a = torch.tensor([[1,2],
                     [0,1],
                     [4,0]], dtype=dtype, requires_grad=True)
w1_b = w1_a.clone().detach()
w1_b.requires_grad = True

w2_a = torch.tensor([[-1, 1],
                     [-2, 3]], dtype=dtype, requires_grad=True)
w2_b = w2_a.clone().detach()
w2_b.requires_grad = True

y_hat_a = torch.nn.functional.relu(x.mm(w1_a)).mm(w2_a)
y_a = torch.ones_like(y_hat_a)
y_hat_b = x.mm(w1_b).clamp(min=0).mm(w2_b)
y_b = torch.ones_like(y_hat_b)

loss_a = (y_hat_a - y_a).pow(2).sum()
loss_b = (y_hat_b - y_b).pow(2).sum()

loss_a.backward()
loss_b.backward()

print(w1_a.grad - w1_b.grad)
print(w2_a.grad - w2_b.grad)

# OUT:
# tensor([[  0.,   0.],
#         [  0.,   0.],
#         [  0., -38.]])
# tensor([[0., 0.],
#         [0., 0.]])
#


title: PyTorch autocomplete (code completion) in Sublime Text 3

body:
I'm a fan of Sublime Text 3 and would like to get code autocompletion for PyTorch. However, I can't get this working yet. Any suggestions or starting points where I can begin to get this working?

I have searched in the packages repository of Sublime Text but unfortunately there's none.

Note: I have looked at a related question here IDE autocomplete for pytorch but that's only for VS Code.


I create the following simple linear class:

class Decoder(nn.Module):
    def __init__(self, K, h=()):
        super().__init__()
        h = (K,)+h+(K,)
        self.layers = [nn.Linear(h1,h2) for h1,h2 in zip(h, h[1:])]

    def forward(self, x):
        for layer in self.layers[:-1]:
            x = F.relu(layer(x))
        return self.layers[-1](x)

However, when I try to put the parameters in a optimizer class I get the error ValueError: optimizer got an empty parameter list.

decoder = Decoder(4)
LR = 1e-3
opt = optim.Adam(decoder.parameters(), lr=LR)

Is there something I'm doing obviously wrong with the class definition?


title: Transformers PreTrainedTokenizer add_tokens Functionality

body:
Referring to the documentation of the awesome Transformers library from Huggingface, I came across the add_tokens functions.

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
num_added_toks = tokenizer.add_tokens(['new_tok1', 'my_new-tok2'])
model.resize_token_embeddings(len(tokenizer))

I tried the above by adding previously absent words in the default vocabulary. However, keeping all else constant, I noticed a decrease in accuracy of the fine tuned classifier making use of this updated tokenizer. I was able to replicate similar behavior even when just 10% of the previously absent words were added.

My questions

Am I missing something?
Instead of whole words, is the add_tokens function expecting masked tokens, for example :  '##ah', '##red', '##ik', '##si', etc.? If yes, is there a procedure to generate such masked tokens?

Any help would be appreciated.

Thanks in advance.


title: Pytorch UnpicklingError: A load persistent id instruction was encountered

body:
My dataloader raises this error when loading its files:
UnpicklingError                           Traceback (most recent call last)
<ipython-input-14-cb081a68afbe> in <module>
----> 1 torch.load("/network/tmp1/ccai/data/labelbox_2020/imgs/AB_304.png")

~/.conda/envs/omnienv/lib/python3.8/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    593                     return torch.jit.load(opened_file)
    594                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 595         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    596
    597

~/.conda/envs/omnienv/lib/python3.8/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    762             "functionality.")
    763
--> 764     magic_number = pickle_module.load(f, **pickle_load_args)
    765     if magic_number != MAGIC_NUMBER:
    766         raise RuntimeError("Invalid magic number; corrupt file?")

UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

What's bugging me is that I'm not doing multiple loads from the same process as pointed out here.
Could it be that this error is triggered by multiple python processes reading the same file?
pytorch 1.7 on ubuntu 18 with python 3.8


title: Unable to get cuda available in Jupyter Notebook or Spyder for PyTorch

body:
I've installed pytorch cuda with pip and conda

when i run this command in IDLE:

>> import torch

>> torch.cuda.is_available()

I get "True",
but in Spyder or Jupyter Notebook it gives as "False" even after updating the package and conda.
The conda update of pytorch cuda was from 10.1 to 10.2


title: Using Transformer for Text-Summarization

body:
I am using huggingface transformer models for text-summarization.
Currently I am testing different models such as T5 and Pegasus.
Now these models were trained for summarizing Big Texts into very short like a maximum of two sentences. Now I have the task, that I want summarizations, that are about half the size of the text, ergo the generated summaries are too small for my purpose.
My question now is, if there is a way to tell the model that another sentence came before?
Kind of similar to the logic inside stateful RNNs (although I know they work completly different).
If yes, I could summarize small windows over the sentences always with the information which content came before.
Is that just a thing of my mind? I cant believe that I am the only one, who wants to create shorter summaries, but not only 1 or two sentence long ones.
Thank you


title: PyTorch: Loss remains constant

body:
I've written a code in PyTorch with my own implemented loss function focal_loss_fixed. But my loss value stays fixed after every epoch. Looks like weights are not being updated. Here is my code snippet:

optimizer = optim.SGD(net.parameters(),
                          lr=lr,
                          momentum=0.9,
                          weight_decay=0.0005)

for epoch in T(range(20)):
    net.train()
    epoch_loss = 0
    for n in range(len(x_train)//batch_size):
        (imgs, true_masks) = data_gen_small(x_train, y_train, iter_num=n, batch_size=batch_size)
        temp = []
        for tt in true_masks:
            temp.append(tt.reshape(128, 128, 1))
        true_masks = np.copy(np.array(temp))
        del temp
        imgs = np.swapaxes(imgs, 1,3)
        imgs = torch.from_numpy(imgs).float().cuda()
        true_masks = torch.from_numpy(true_masks).float().cuda()
        masks_pred = net(imgs)
        masks_probs = F.sigmoid(masks_pred)
        masks_probs_flat = masks_probs.view(-1)
        true_masks_flat = true_masks.view(-1)
        print((focal_loss_fixed(tf.convert_to_tensor(true_masks_flat.data.cpu().numpy()), tf.convert_to_tensor(masks_probs_flat.data.cpu().numpy()))))
        loss = torch.from_numpy(np.array(focal_loss_fixed(tf.convert_to_tensor(true_masks_flat.data.cpu().numpy()), tf.convert_to_tensor(masks_probs_flat.data.cpu().numpy())))).float().cuda()
        loss = Variable(loss.data, requires_grad=True)
        epoch_loss *= (n/(n+1))
        epoch_loss += loss.item()*(1/(n+1))
        print('Step: {0:.2f}% --- loss: {1:.6f}'.format(n * batch_size* 100.0 / len(x_train), epoch_loss), end='\r')
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print('Epoch finished ! Loss: {}'.format(epoch_loss))

And this is my `focal_loss_fixed' function:

def focal_loss_fixed(true_data, pred_data):
    gamma=2.
    alpha=.25
    eps = 1e-7
    # print(type(y_true), type(y_pred))
    pred_data = K.clip(pred_data,eps,1-eps)
    pt_1 = tf.where(tf.equal(true_data, 1), pred_data, tf.ones_like(pred_data))
    pt_0 = tf.where(tf.equal(true_data, 0), pred_data, tf.zeros_like(pred_data))
    with tf.Session() as sess:
        return sess.run(-K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1))-K.sum((1-alpha) * K.pow( pt_0, gamma) * K.log(1. - pt_0)))

After each epoch the loss value stays constant(5589.60328). What's wrong with it?


I wanna generate some random number with python and transform it to tensor with pytorch. Here is my code for generating the random number and transform it into tensor.

import numpy as np
import torch

P = np.random.uniform(0.5, 1, size=[20, 1])
k = np.random.randint(1, 20, size=[20, 1])
d_k = np.random.uniform(0, np.sqrt(80000), size=[20, 1])

P = torch.from_numpy(P).float()
k = torch.from_numpy(k).int()
d_k = torch.from_numpy(d_k).float()

torch.cat((P, k, d_k), dim=-1)

Afterward, I got some error which showed:

RuntimeError: Expected a Tensor of type torch.FloatTensor but found a type torch.IntTensor for sequence element 1 in sequence argument at position #1 'tensors'


title: Pytorch Dataloader for Image GT dataset

body:
I am new to pytorch. I am trying to create a DataLoader for a dataset of images where each image got a corresponding ground truth (same name):

root:
--->RGB:
------>img1.png
------>img2.png
------>...
------>imgN.png
--->GT:
------>img1.png
------>img2.png
------>...
------>imgN.png

When I use the path for root folder (that contains RGB and GT folders) as input for the torchvision.datasets.ImageFolder it reads all of the images as if they were all intended for input (classified as RGB and GT), and it seems like there is no way to pair the RGB-GT images. I would like to pair the RGB-GT images, shuffle, and divide it to batches of defined size. How can it be done? Any advice will be appreciated.
Thanks.


title: Net does not change weights during training, pytorch

body:
I'm implementing DDPG and got stuck training my two nets.

All in all I've got 4 nets called: actor, actor_target, critic and critic_target.
I'm training the actor and critic in the training loop and do soft updates for the other two nets with:

def update_weights(self, source, tau):
        for target, source in zip(self.parameters(), source.parameters()):
            target.data.copy_(tau * source.data + (1 - tau) * target.data)

My training loop looks like:

tensor_next_states = torch.tensor(next_states).view(-1, 1)
prediction_target = self.actor_target(tensor_next_states).data.numpy()
target_critic_output = self.critic_target(
    construct_tensor(next_states, prediction_target))
y = torch.tensor(rewards).view(-1,1) + \
    self.gamma * target_critic_output
output_critic = self.critic(
    torch.tensor(construct_tensor(states, actions), dtype=torch.float))

# compute loss and update critic
self.critic.zero_grad()
loss_critic = self.criterion_critic(y, output_critic)
loss_critic.backward()
self.critic_optim.step()

# compute loss and update actor
tensor_states = torch.tensor(states).view(-1, 1)
ouput_actor = self.actor(tensor_states).data.numpy()
self.actor.zero_grad()
loss_actor = (-1.) * \
             self.critic(construct_tensor(states, ouput_actor)).mean()
loss_actor.backward()
self.actor_optim.step()

# update target
self.actor_target.update_weights(self.actor, self.tau)
self.critic_target.update_weights(self.critic, self.tau)

using SGD as optimizer and self.criterion_critic = F.mse_loss.

construct_tensor(a,b) constructs a tensor like [a[0], b[0], a[1], b[1], ...].

I've noticed, that the RMSE on the test set before and after training is the same.
So I debugged a lot and noticed in update_weights that the weights of the trained net and the target net are the same - so I concluded, that the training does not have any affect on the weights of the trained net.
I already checked that the computed losses are not zero but still a float, checked replacing the zero_grad() calls and moving the computed losses to self, which hadn't got any impact.

Does anyone already met this behavior and/or has any tips or know how to fix this?

Update: Full code:

import datetime
import random
from collections import namedtuple

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

def combine_tensors(s, a):
    """
    Combines the two given tensors
    :param s: tensor1
    :param a: tensor2
    :return: combined tensor
    """
    target = []
    if not len(a[0].shape) == 0:
        for i in range(len(s)):
            target.append(torch.cat((s[i], a[i])).data.numpy())
    else:
        for i in range(len(s)):
            target.append(torch.cat((s[i], a[i].float().view(-1))) \
                          .data.numpy())
    return torch.tensor(target, device=device)

class actor(nn.Module):
    """
    Actor - gets a state (2-dim) and returns probabilities about which
    action to take (4 actions -> 4 outputs)
    """

    def __init__(self):
        super(actor, self).__init__()

        # define net structure
        self.input_layer = nn.Linear(2, 4)
        self.hidden_layer_1 = nn.Linear(4, 8)
        self.hidden_layer_2 = nn.Linear(8, 16)
        self.hidden_layer_3 = nn.Linear(16, 32)
        self.output_layer = nn.Linear(32, 4)

        # initialize them
        nn.init.xavier_uniform_(self.input_layer.weight)
        nn.init.xavier_uniform_(self.hidden_layer_1.weight)
        nn.init.xavier_uniform_(self.hidden_layer_2.weight)
        nn.init.xavier_uniform_(self.hidden_layer_3.weight)
        nn.init.xavier_uniform_(self.output_layer.weight)

        nn.init.constant_(self.input_layer.bias, 0.1)
        nn.init.constant_(self.hidden_layer_1.bias, 0.1)
        nn.init.constant_(self.hidden_layer_2.bias, 0.1)
        nn.init.constant_(self.hidden_layer_3.bias, 0.1)
        nn.init.constant_(self.output_layer.bias, 0.1)

    def forward(self, state):
        state = F.relu(self.input_layer(state))
        state = F.relu(self.hidden_layer_1(state))
        state = F.relu(self.hidden_layer_2(state))
        state = F.relu(self.hidden_layer_3(state))
        state = F.softmax(self.output_layer(state), dim=0)
        return state

class critic(nn.Module):
    """
    Critic - gets a state (2-dim) and an action and returns value
    """

    def __init__(self):
        super(critic, self).__init__()
        # define net structure
        self.input_layer = nn.Linear(3, 8)
        self.hidden_layer_1 = nn.Linear(8, 16)
        self.hidden_layer_2 = nn.Linear(16, 32)
        self.hidden_layer_3 = nn.Linear(32, 16)
        self.output_layer = nn.Linear(16, 1)

        # initialize them
        nn.init.xavier_uniform_(self.input_layer.weight)
        nn.init.xavier_uniform_(self.hidden_layer_1.weight)
        nn.init.xavier_uniform_(self.hidden_layer_2.weight)
        nn.init.xavier_uniform_(self.hidden_layer_3.weight)
        nn.init.xavier_uniform_(self.output_layer.weight)

        nn.init.constant_(self.input_layer.bias, 0.1)
        nn.init.constant_(self.hidden_layer_1.bias, 0.1)
        nn.init.constant_(self.hidden_layer_2.bias, 0.1)
        nn.init.constant_(self.hidden_layer_3.bias, 0.1)
        nn.init.constant_(self.output_layer.bias, 0.1)

    def forward(self, state_, action_):
        state_ = combine_tensors(state_, action_)
        state_ = F.relu(self.input_layer(state_))
        state_ = F.relu(self.hidden_layer_1(state_))
        state_ = F.relu(self.hidden_layer_2(state_))
        state_ = F.relu(self.hidden_layer_3(state_))
        state_ = self.output_layer(state_)
        return state_

Transition = namedtuple('Transition',
                        ('state', 'action', 'next_state', 'reward'))

class ReplayMemory(object):
    """
    Memory
    """

    def __init__(self, capacity):
        self.capacity = capacity
        self.memory = []
        self.position = 0

    def push(self, *args):
        if len(self.memory) < self.capacity:
            self.memory.append(None)
        self.memory[self.position] = Transition(*args)
        self.position = (self.position + 1) % self.capacity

    def sample(self, batch_size):
        return random.sample(self.memory, batch_size)

    def __len__(self):
        return len(self.memory)

def compute_action(actor_trainined, state, eps=0.1):
    """
    Computes an action given the actual policy, the state and an eps.
    Eps is resposible for the amount of exploring
    :param actor_trainined: actual policy
    :param state:
    :param eps: float in [0,1]
    :return:
    """
    denoise = random.random()

    if denoise > eps:
        action_probs = actor_trainined(state.float())
        return torch.argmax(action_probs).view(1).int()
    else:
        return torch.randint(0, 4, (1,)).view(1).int()

def compute_next_state(_action, _state):
    """
    Computes the next state given an action and a state
    :param _action:
    :param _state:
    :return:
    """
    state_ = _state.clone()
    if _action.item() == 0:
        state_[1] += 1
    elif _action.item() == 1:
        state_[1] -= 1
    elif _action.item() == 2:
        state_[0] -= 1
    elif _action.item() == 3:
        state_[0] += 1

    return state_

def update_weights(target, source, tau):
    """
    Soft-Update of weights
    :param target:
    :param source:
    :param tau:
    :return:
    """
    for target, source in zip(target.parameters(), source.parameters()):
        target.data.copy_(tau * source.data + (1 - tau) * target.data)

def update(transition__, replay_memory, batch_size_, gamma_):
    """
    Performs one update step
    :param transition__:
    :param replay_memory:
    :param batch_size_:
    :param gamma_:
    :return:
    """
    replay_memory.push(*transition__)

    if replay_memory.__len__() < batch_size_:
        return

    transitions = replay_memory_.sample(batch_size_)
    batch = Transition(*zip(*transitions))

    states = torch.stack(batch.state)
    actions = torch.stack(batch.action)
    rewards = torch.stack(batch.reward)
    next_states = torch.stack(batch.next_state)

    action_target = torch.argmax(actor_target(next_states.float()), 1).int()
    y = (
            rewards.float().view(-1, 1) +
            gamma_ * critic_target(next_states.float(), action_target.float())
            .float()
    )

    critic_trained.zero_grad()
    crit_ = critic_trained(states.float(), actions.float())
    # nn stuff does not work here! -> doing mse myself..
    # loss_critic = (torch.sum((y.float() - crit_.float()) ** 2.)
    #                / y.data.nelement())
    loss_critic = F.l1_loss(y.float(), crit_.float())
    loss_critic.backward()
    optimizer_critic.step()

    actor_trained.zero_grad()
    loss_actor = ((-1.) * critic_trained(states.float(),
                                         torch.argmax(
                                             actor_trained(states.float()), 1
                                         ).int().float())).mean()
    loss_actor.backward()
    optimizer_actor.step()

def get_eps(epoch):
    """
    Computes the eps for action choosing dependant on the epoch
    :param epoch: number of epoch
    :return:
    """
    if epoch <= 10:
        eps_ = 1.
    elif epoch <= 20:
        eps_ = 0.8
    elif epoch <= 40:
        eps_ = 0.6
    elif epoch <= 60:
        eps_ = 0.4
    elif epoch <= 80:
        eps_ = 0.2
    else:
        eps_ = 0.1
    return eps_

def compute_reward_2(state_, next_state_, terminal_state_):
    """
    Better (?) reward function that "compute_reward"
    If next_state == terminal_state -> reward = 100
    If next_state illegal           -> reward = -100
    if next_state is further away from terminal_state than state_ -> -2
    else 1
    :param state_:
    :param next_state_:
    :param terminal_state_:
    :return:
    """
    if torch.eq(next_state_, terminal_state_).all():
        reward_ = 100
    elif torch.eq(next_state_.abs(), 15).any():
        reward_ = -100
    else:
        if (state_.abs() > next_state_.abs()).any():
            reward_ = 1.
        else:
            reward_ = -2
    return torch.tensor(reward_, device=device, dtype=torch.float)

def compute_reward(next_state_, terminal_state_):
    """
    Computes some reward
    :param next_state_:
    :param terminal_state_:
    :return:
    """
    if torch.eq(next_state_, terminal_state_).all():
        return torch.tensor(100., device=device, dtype=torch.float)
    elif next_state_[0] == 15 or next_state_[1] == 15:
        return torch.tensor(-100., device=device, dtype=torch.float)
    else:
        return (-1.) * next_state_.abs().sum().float()

def fill_memory_2():
    """
    Fills the memory with random transitions which got a "good" action chosen
    """
    terminal_state_ = torch.tensor([0, 0], device=device, dtype=torch.int)
    while replay_memory_.__len__() < batch_size:
        state_ = torch.randint(-4, 4, (2,)).to(device).int()
        if state_[0].item() == 0 and state_[1].item == 0:
            continue

        # try to find a "good" action
        if state_[0].item() == 0:
            if state_[1].item() > 0:
                action_ = torch.tensor(1, device=device, dtype=torch.int)
            else:
                action_ = torch.tensor(0, device=device, dtype=torch.int)
        elif state_[1].item() == 0:
            if state_[0].item() > 0:
                action_ = torch.tensor(2, device=device, dtype=torch.int)
            else:
                action_ = torch.tensor(3, device=device, dtype=torch.int)
        else:
            random_bit = random.random()
            if random_bit > 0.5:
                if state_[1].item() > 0:
                    action_ = torch.tensor(1, device=device, dtype=torch.int)
                else:
                    action_ = torch.tensor(0, device=device, dtype=torch.int)
            else:
                if state_[0].item() > 0:
                    action_ = torch.tensor(2, device=device, dtype=torch.int)
                else:
                    action_ = torch.tensor(3, device=device, dtype=torch.int)

        action_ = action_.view(1).int()
        next_state_ = compute_next_state(action_, state_)
        reward_ = compute_reward_2(state_, next_state_, terminal_state_)

        transition__ = Transition(state=state_, action=action_,
                                  reward=reward_, next_state=next_state_)
        replay_memory_.push(*transition__)

def fill_memory():
    """
    Fills the memory with random transitions
    """
    while replay_memory_.__len__() < batch_size:
        state_ = torch.randint(-14, 15, (2,)).to(device).int()
        if state_[0].item() == 0 and state_[1].item == 0:
            continue
        terminal_state_ = torch.tensor([0, 0], device=device, dtype=torch.int)
        action_ = torch.randint(0, 4, (1,)).view(1).int()
        next_state_ = compute_next_state(action_, state_)
        reward_ = compute_reward_2(state_, next_state_, terminal_state_)

        transition__ = Transition(state=state_, action=action_,
                                  reward=reward_, next_state=next_state_)
        replay_memory_.push(*transition__)

if __name__ == '__main__':
    # get device if possible
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # set seed
    seed_ = 0
    random.seed(seed_)  # seed of python
    if device == "cuda":
        # cuda seed
        torch.cuda.manual_seed(seed_)
    else:
        # cpu seed
        torch.manual_seed(seed_)

    # initialize the nets
    actor_trained = actor().to(device)
    actor_target = actor().to(device)
    # copy -> _trained eqaul _target
    actor_target.load_state_dict(actor_trained.state_dict())
    optimizer_actor = optim.RMSprop(actor_trained.parameters())
    # move them to the device
    critic_trained = critic().to(device)
    critic_target = critic().to(device)
    critic_target.load_state_dict((critic_trained.state_dict()))
    actor_target.load_state_dict((actor_trained.state_dict()))
    # used optimizer
    optimizer_critic = optim.RMSprop(critic_trained.parameters(),
                                     momentum=0.9, weight_decay=0.001)

    # replay memory
    capacity_replay_memory = 16384
    replay_memory_ = ReplayMemory(capacity_replay_memory)

    # hyperparams
    batch_size = 1024
    gamma = 0.7
    tau = 0.01
    num_epochs = 256

    # fill replay memory such that batching is possible
    fill_memory_2()

    # Print params
    printing_while_training = True
    printing_while_testing = False

    print('######################## Training ########################')
    starting_time = datetime.datetime.now()
    for i in range(num_epochs):
        # random state
        starting_state = torch.randint(-14, 15, (2,)).to(device).int()
        # skip if terminal state
        if starting_state[0].item() == 0 and starting_state[0].item() == 0:
            continue
        state = starting_state.clone()
        # terminal state
        terminal_state = torch.tensor([0, 0], device=device, dtype=torch.int)
        iteration = 0

        # get eps for exploring
        eps = get_eps(i)

        running_reward = 0.

        # training loos
        while True:
            # compute action and next state
            action = compute_action(actor_trained, state, eps)
            next_state = compute_next_state(action, state)

            # finished if next state is terminal state
            if torch.eq(next_state, terminal_state).all():
                reward = compute_reward_2(state, next_state, terminal_state)
                running_reward += reward.item()
                transition_ = Transition(state=state, action=action,
                                         reward=reward, next_state=next_state)
                replay_memory_.push(*transition_)
                if printing_while_training:
                    print('{}: Finished after {} iterations with reward {} '
                          'in state {} starting from {}'
                          .format(i + 1, iteration + 1, running_reward,
                                  next_state.data.numpy(),
                                  starting_state.data.numpy()))
                break
            # abort if illegal state
            elif torch.eq(next_state.abs(), 15).any() or iteration == 99:
                reward = compute_reward_2(state, next_state, terminal_state)
                running_reward += reward
                transition_ = Transition(state=state, action=action,
                                         reward=reward, next_state=next_state)
                replay_memory_.push(*transition_)
                if printing_while_training:
                    print('{}: Aborted after {} iterations with reward {} '
                          'in state {} starting from {}'
                          .format(i + 1, iteration + 1, running_reward,
                                  next_state.data.numpy(),
                                  starting_state.data.numpy()))
                break

            # compute immediate reward
            reward = compute_reward_2(state, next_state, terminal_state)
            # save it - only for logging purposes
            running_reward += reward.item()

            # construct transition
            transition_ = Transition(state=state, action=action, reward=reward,
                                     next_state=next_state)

            # update model
            update(transition_, replay_memory_, batch_size, gamma)
            # perform soft updates
            update_weights(actor_target, actor_trained, tau)
            update_weights(critic_target, critic_trained, tau)

            state = next_state
            iteration += 1
    print('Ended after: {}'.format(datetime.datetime.now() - starting_time))

    print('######################## Testing ########################')
    starting_time = datetime.datetime.now()
    test_states = [torch.tensor([i, j], device=device, dtype=torch.int)
                   for i in range(-15, 14) for j in range(-15, 14)]
    finished = 0
    aborted = 0
    aborted_reward = []
    finished_reward = []

    for starting_state in test_states:
        state = starting_state.clone()
        terminal_state = torch.tensor([0, 0], device=device, dtype=torch.int)
        iteration = 0
        reward = 0.

        while True:
            action = torch.argmax(actor_target(state.float())).view(1).int()
            next_state = compute_next_state(action, state)

            if torch.eq(next_state, terminal_state).all():
                reward += compute_reward_2(state, next_state,
                                           terminal_state)
                finished_reward.append(reward.item())
                if printing_while_testing:
                    print('{}: Finished after {} iterations with reward {} '
                          'in state {} starting from {}'
                          .format(starting_state.data.numpy(), iteration + 1,
                                  reward.item(), next_state.data.numpy(),
                                  starting_state.data.numpy()))
                finished += 1
                break
            elif torch.eq(next_state.abs(), 15).any():
                reward += compute_reward_2(state, next_state,
                                           terminal_state)
                aborted_reward.append(reward.item())
                if printing_while_testing:
                    print('{}: Aborted after {} iterations with reward {} '
                          'in state {} starting from {}'
                          .format(starting_state.data.numpy(), iteration + 1,
                                  reward.item(), next_state.data.numpy(),
                                  starting_state.data.numpy()))
                aborted += 1
                break
            elif iteration > 500:
                if printing_while_testing:
                    print('Aborting due to more than 500 iterations! '
                          'Started from {}'.format(
                        starting_state.data.numpy()))
                aborted += 1
                break
            reward += compute_reward_2(state, next_state, terminal_state)
            state = next_state
            iteration += 1

    print('Ended after: {}'.format(datetime.datetime.now() - starting_time))
    print('Finished: {}, aborted: {}'.format(finished, aborted))
    print('Reward mean finished: {}, aborted: {}'
          .format(np.mean(finished_reward), np.mean(aborted_reward)))

I already tried using an other reward function but it did not have any effect.. .

Additionally I tried using some less aggressive exploration and optim.SGD instead of optim.RMSprop - both had no effect.


title: PyTorch Getting Started example not working

body:
I followed this tutorial in the Getting Started section on the PyTorch website: "Deep Learning with PyTorch: A 60 Minute Blitz" and I downloaded the code for "Training a Classifier" on the bottom of the page and I ran it, and it's not working for me. I'm using the CPU version of PyTorch if that makes a difference. I'm new to Python and basically learning it for Pytorch. Here's the error message, Control + K isn't working for me because I think the editing interface is different for the first few posts and Stack Overflow needs to fix it. Or it could just be my browser:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
  File "C:\Users\Anonymous\PycharmProjects\pytorchHelloWorld\train_network.py", line 100, in <module>
dataiter = iter(trainloader)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 819, in __iter__
return _DataLoaderIter(self)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 560, in __init__
w.start()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
_check_not_importing_main()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError:
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.
Traceback (most recent call last):
 File "C:/Users/Anonymous/PycharmProjects/pytorchHelloWorld/train_network.py", line 100, in <module>
dataiter = iter(trainloader)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 819, in __iter__
return _DataLoaderIter(self)
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 560, in __init__
w.start()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe


title: How to do reinforcement learning with an LSTM in PyTorch?

body:
Due to observations not revealing the entire state, I need to do reinforcement with a recurrent neural network so that the network has some sort of memory of what has happened in the past. For simplicity let's assume that we use an LSTM.

Now the in-built PyTorch LSTM requires you to feed it a an input of shape Time x MiniBatch x Input D and it outputs a tensor of shape Time x MiniBatch x Output D.

In reinforcement learning however, to know the input at time t+1, I need to know the output at time t, because I am doing actions in an environment.

So is it possible to use the in-built PyTorch LSTM to do BPTT in a reinforcement learning setting? And if it is, how could I do it?


Reference github: fast-bert
I used to run the following notebook to predict the multi label classification using Bert model that means I don't need GPU driver instead I can use CPU memory,
here is the reference for jupyter notebook multilabel
It's not the memory issue, How to resolve this error?
While increasing CPU with RAM size, target size also getting increasing,

I choose n1-standard-4 (6 vCPUs, 26 GB memory) machine type.

Sample code:
I have removed this peace of code I use 'cpu' instead of 'cuda'
device = torch.device('cuda')
if torch.cuda.device_count() > 1:
    args.multi_gpu = True
else:
    args.multi_gpu = False

to
torch.device('cpu')

Error Logs:
Traceback (most recent call last):-------------------------------------------------------------| 0.00% [0/63 00:00<00:00]
  File "bert/run.py", line 146, in <module>
    learner.fit(args.num_train_epochs, args.learning_rate, validate=True)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/fast_bert/learner_cls.py", line 397, in fit
    outputs = self.model(**inputs)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/fast_bert/modeling.py", line 205, in forward
    logits.view(-1, self.num_labels), labels.view(-1, self.num_labels)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 617, in forward
    reduction=self.reduction)
  File "/home/pt4_gcp/.local/lib/python3.7/site-packages/torch/nn/functional.py", line 2433, in binary_cross_entropy_with_logits
    raise ValueError("Target size ({}) must be the same as input size ({})".format(target.size(), input.size()))
ValueError: Target size (torch.Size([64, 3])) must be the same as input size (torch.Size([32, 3]))


title: FileNotFoundError in ...\.cache\ using SentenceTransformer

body:
Does anyone have experience working with SentenceTransformer (Bert)?

My Code:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('roberta-large-nli-stsb-mean-tokens')

My Error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ga2943/.cache\\torch\\sentence_transformers\\public.ukp.informatik.tu-darmstadt.de_reimers_sentence-transformers_v0.2_roberta-large-nli-stsb-mean-tokens.zip\\modules.json'

Once in a while I get a different error running the same Code with another Bert-model:

PermissionError: [WinError 32] Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird: 'C:\\Users\\ga2943/.cache\\torch\\sentence_transformers\\public.ukp.informatik.tu-darmstadt.de_reimers_sentence-transformers_v0.2_bert-large-nli-cls-token.zip\\model.zip'

(Translates to: Data is beeing used in a different Process at the time, which cant be the case. I restarted my machine to ensure all programs are closed in the background but no difference)

The first time I ran my code it worked. It downloaded the Sentenceencoder I wanted (different Bert model)  Now that I want to test a different model, it does not download it but gives me the errors.

Funny thing is, on a different Computer I was able to load a different model but it resulted in the same case. Only the first ever downloaded model works on the machine.

Any ideas? Still getting the error, no one knows how to fix it?


Help me please. Is there any way to start a project with 2.1 cuda capability?
Trying to launch a project https://github.com/CorentinJ/Real-Time-Voice-Cloning/

Found GPU0 GeForce GT 630M which is of cuda capability 2.1.
    PyTorch no longer supports this GPU because it is too old.
    The minimum cuda capability that we support is 3.5.

  warnings.warn(old_gpu_warn % (d, name, major, capability[1]))
Found 1 GPUs available. Using GPU 0 (GeForce GT 630M) of compute capability 2.1 with 2.1Gb total memory.

Preparing the encoder, the synthesizer and the vocoder...
Traceback (most recent call last):
  File "demo_cli.py", line 61, in <module>
    encoder.load_model(args.enc_model_fpath)
  File "C:\Users\kisel\Desktop\Real-Time-Voice-Cloning-master\encoder\inference.py", line 32, in load_model
    _model = SpeakerEncoder(_device, torch.device("cpu"))
  File "C:\Users\kisel\Desktop\Real-Time-Voice-Cloning-master\encoder\model.py", line 21, in __init__
    batch_first=True).to(device)
  File "C:\Users\kisel\anaconda3\envs\TestVoice\lib\site-packages\torch\nn\modules\module.py", line 386, in to
    return self._apply(convert)
  File "C:\Users\kisel\anaconda3\envs\TestVoice\lib\site-packages\torch\nn\modules\rnn.py", line 127, in _apply
    self.flatten_parameters()
  File "C:\Users\kisel\anaconda3\envs\TestVoice\lib\site-packages\torch\nn\modules\rnn.py", line 123, in flatten_parameters
    self.batch_first, bool(self.bidirectional))
RuntimeError: cuDNN error: CUDNN_STATUS_ARCH_MISMATCH


title: TypeError: a float is required

body:
l have a pytorch variable :

preds[4,4]
Out[305]:
Variable containing:
-96.7809
[torch.cuda.FloatTensor of size 1 (GPU 0)]

l want to do the following :

 import math
 x=preds[4,4]
 y=maths.exp(x)
 z= y / (y+1)

However when l do :

y=maths.exp(x)

l get the following error :

   math.exp(preds[4,4])
TypeError: a float is required

How can l transform a torch variable to a float in order to be able to do these operations ?

Thanks


title: How to check if model in on CUDA?

body:
I would like to check if model is on CUDA. How to do that?
import torch
import torchvision
model = torchvision.models.resnet18()
model.to('cuda')

Seams that model.is_cuda() is not working.


title: In pytorch, how to debug the JIT error information "RuntimeError: Only tensors or tuples of tensors can be output from traced functions"?

body:
I wrote a simple pytorch Sequential module for which I plan to convert to ONNX for visualisation & mobile deployment. The first step is to convert it into JIT version:

        input = torch.rand(1, 3, 300, 300, dtype=torch.float32, device=device)
        mod = FPNImproved(raw)
        torch.jit.trace(mod, input)

This step gave me the following error:

        for method_name, example_inputs in inputs.items():
            # this is needed since Module.__call__ sets up some extra tracing
            func = mod if method_name == "forward" else getattr(mod, method_name)
            example_inputs = make_tuple(example_inputs)
>           module._c._create_method_from_trace(method_name, func, example_inputs, var_lookup_fn, _force_outplace)
E           RuntimeError: Only tensors or tuples of tensors can be output from traced functions (getOutput at /opt/conda/conda-bld/pytorch_1570711556059/work/torch/csrc/jit/tracer.cpp:209)
E           frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x47 (0x7f35dca71687 in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libc10.so)
E           frame #1: torch::jit::tracer::TracingState::getOutput(c10::IValue const&) + 0x365 (0x7f35aa607105 in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libtorch.so)
E           frame #2: torch::jit::tracer::exit(std::vector<c10::IValue, std::allocator<c10::IValue> > const&) + 0x3c (0x7f35aa60740c in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libtorch.so)
E           frame #3: <unknown function> + 0x57472a (0x7f35e62fd72a in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
E           frame #4: <unknown function> + 0x58bbb4 (0x7f35e6314bb4 in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
E           frame #5: <unknown function> + 0x206506 (0x7f35e5f8f506 in /home/shared/conda3/envs/cv-torch/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
E           frame #6: _PyMethodDef_RawFastCallKeywords + 0x254 (0x56498498e744 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #7: _PyCFunction_FastCallKeywords + 0x21 (0x56498498e861 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #8: _PyEval_EvalFrameDefault + 0x52f8 (0x5649849fa6e8 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #9: _PyEval_EvalCodeWithName + 0x2f9 (0x56498493e539 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #10: _PyFunction_FastCallKeywords + 0x325 (0x56498498def5 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #11: _PyEval_EvalFrameDefault + 0x416 (0x5649849f5806 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #12: _PyEval_EvalCodeWithName + 0x2f9 (0x56498493e539 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #13: _PyFunction_FastCallKeywords + 0x325 (0x56498498def5 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #14: _PyEval_EvalFrameDefault + 0x4b39 (0x5649849f9f29 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #15: _PyFunction_FastCallKeywords + 0xfb (0x56498498dccb in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #16: _PyEval_EvalFrameDefault + 0x416 (0x5649849f5806 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #17: _PyEval_EvalCodeWithName + 0x2f9 (0x56498493e539 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #18: _PyFunction_FastCallDict + 0x400 (0x56498493f860 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #19: _PyObject_Call_Prepend + 0x63 (0x56498495de53 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #20: PyObject_Call + 0x6e (0x564984950dbe in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #21: _PyEval_EvalFrameDefault + 0x1e42 (0x5649849f7232 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #22: _PyEval_EvalCodeWithName + 0x2f9 (0x56498493e539 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #23: _PyFunction_FastCallDict + 0x400 (0x56498493f860 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #24: _PyObject_Call_Prepend + 0x63 (0x56498495de53 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #25: <unknown function> + 0x16ba3a (0x564984995a3a in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #26: _PyObject_FastCallKeywords + 0x49b (0x5649849968fb in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #27: _PyEval_EvalFrameDefault + 0x569f (0x5649849faa8f in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #28: _PyFunction_FastCallKeywords + 0xfb (0x56498498dccb in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #29: _PyEval_EvalFrameDefault + 0x6a3 (0x5649849f5a93 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #30: _PyFunction_FastCallDict + 0x10b (0x56498493f56b in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #31: _PyEval_EvalFrameDefault + 0x1e42 (0x5649849f7232 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #32: _PyEval_EvalCodeWithName + 0xac9 (0x56498493ed09 in /home/shared/conda3/envs/cv-torch/bin/python)
E           frame #33: _PyFunction_FastCallKeywords + 0x387 (0x56498498df57 in /home/shared/conda3/envs/cv-torch/bin/python)

Unfortunately this message doesn't help, as I'm under the impression that all the layers in my modules already have tensors or tuples output. Even worse the error message doesn't tell me which layer is the culprit.

What is the recommended way to ask pytorch JIT to explain it? I already enabled PYTORCH_JIT=0.

I'm using pytorch 1.3.0 with only CPU support


title: Understanding backward hooks

body:
I wrote this snippet below to try and understand what's going on with these hooks.
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.fc1 = nn.Linear(10,5)
        self.fc2 = nn.Linear(5,1)
        self.fc1.register_forward_hook(self._forward_hook)
        self.fc1.register_backward_hook(self._backward_hook)

    def forward(self, inp):
        return self.fc2(self.fc1(inp))

    def _forward_hook(self, module, input, output):
        print(type(input))
        print(len(input))
        print(type(output))
        print(input[0].shape)
        print(output.shape)
        print()

    def _backward_hook(self, module, grad_input, grad_output):
        print(type(grad_input))
        print(len(grad_input))
        print(type(grad_output))
        print(len(grad_output))
        print(grad_input[0].shape)
        print(grad_input[1].shape)
        print(grad_output[0].shape)
        print()

model = Model()
out = model(torch.tensor(np.arange(10).reshape(1,1,10), dtype=torch.float32))
out.backward()

Produces output
<class 'tuple'>
1
<class 'torch.Tensor'>
torch.Size([1, 1, 10])
torch.Size([1, 1, 5])

<class 'tuple'>
2
<class 'tuple'>
1
torch.Size([1, 1, 5])
torch.Size([5])
torch.Size([1, 1, 5])

You can also follow the CNN example here. In fact, it's needed to understand the rest of my question.
I have a few questions:

I would normally think that grad_input (backward hook) should be the  same shape as output (forward hook) because when we go backwards, the direction is reversed. But the CNN example seems to indicate otherwise. I'm still a bit confused. Which way around is it?

Why are grad_input[0] and grad_output[0] the same shape on my Linear layer here? Regardless of the answer to my question 1, at least one of them should be torch.Size([1, 1, 10]) right?

What's with the second element of the tuple grad_input? In the CNN case I copy pasted the example and did print(grad_input[1].size()) with output torch.Size([20, 10, 5, 5]). So I presume it's the gradients of the weights. I also ran print(grad_input[2].size()) and got torch.Size([20]). So it seemed clear I was looking at the gradients of the biases. But then in my Linear example grad_input is length 2, so I can only access up to grad_input[1], which seems to be giving me the gradients of the biases. So then where are the gradients of the weights?

In summary, there are two apparent contradictions between the behaviour of the backwards hook in the cases of Conv2d and `Linear' modules. This has left me totally confused about what to expect with this hook.
Thanks for your help!


title: PyTorch is installed but not imported

body:
I am trying to build PyTorch.
Reference site：https://github.com/hughperkins/pytorch

but, When we performed unit test, The following error occur.

ImportError while importing test module '/home/usr2/pytorch/test/testByteTensor.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test/testByteTensor.py:2: in <module>
    import PyTorch
E   ImportError: No module named 'PyTorch'
__________________ ERROR collecting test/testDoubleTensor.py ___________________

I understand that PyTorch is not imported.
but It is confirmed that pytorch is installed.
Is there a way to solve this problem?

environment

ubuntu 16.04
python3.5
cuda9.2


title: Pytorch: How to create a random int tensor where a certain percent are of a certain value? For example, 25% are 1s, and rest 0s

body:
In pytorch I can create a random zero and one tensor with around %50 distribution of each
import torch
torch.randint(low=0, high=2, size=(2, 5))

I am wondering how I can make a tensor where only 25% of the values are 1s, and the rest are zeros?


title: Why does gpytorch seem to be less accurate than scikit-learn?

body:
I current found gpytorch (https://github.com/cornellius-gp/gpytorch). It seems to be a great package for integrating GPR into pytorch. First tests were also positive. Using gpytorch the GPU-Power as well as intelligent algorithms can used in order to improve performance in comparison to other packages such as scikit-learn.
However, I found that it is much harder to estimate the hyperparameters that are needed. In scikit-learn that happens in the background and is very robust. I would like get some feed from the community about the reasons and to discuss if there might be a better way to estimatethese parameter than provided by the example in the documentation of gpytorch.
For comparisson, I took the code of a provided example on the offcial page of gpytorch (https://github.com/cornellius-gp/gpytorch/blob/master/examples/03_Multitask_GP_Regression/Multitask_GP_Regression.ipynb) and modified it in two parts:

I use a different kernel (gpytorch.kernels.MaternKernel(nu=2.5) in stead of gpytorch.kernels.RBFKernel())
I used a different output function

In the following, I provide first the code using gpytorch. Subsequently, I provide the code for scikit-learn. Finally, I compare the results
Importing (for gpytorch and scikit-learn):
import math
import torch
import numpy as np
import gpytorch

Generating data (for gpytorch and scikit-learn):
n = 20
train_x = torch.zeros(pow(n, 2), 2)
for i in range(n):
    for j in range(n):
        # Each coordinate varies from 0 to 1 in n=100 steps
        train_x[i * n + j][0] = float(i) / (n-1)
        train_x[i * n + j][1] = float(j) / (n-1)

train_y_1 = (torch.sin(train_x[:, 0]) + torch.cos(train_x[:, 1]) * (2 * math.pi) + torch.randn_like(train_x[:, 0]).mul(0.01))/4
train_y_2 = torch.sin(train_x[:, 0]) + torch.cos(train_x[:, 1]) * (2 * math.pi) + torch.randn_like(train_x[:, 0]).mul(0.01)

train_y = torch.stack([train_y_1, train_y_2], -1)

test_x = torch.rand((n, len(train_x.shape)))
test_y_1 = (torch.sin(test_x[:, 0]) + torch.cos(test_x[:, 1]) * (2 * math.pi) + torch.randn_like(test_x[:, 0]).mul(0.01))/4
test_y_2 = torch.sin(test_x[:, 0]) + torch.cos(test_x[:, 1]) * (2 * math.pi) + torch.randn_like(test_x[:, 0]).mul(0.01)
test_y = torch.stack([test_y_1, test_y_2], -1)

Now comes the estimation as described in the provided example from the cited documentation:
torch.manual_seed(2) # For a more robust comparison
class MultitaskGPModel(gpytorch.models.ExactGP):
    def __init__(self, train_x, train_y, likelihood):
        super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
        self.mean_module = gpytorch.means.MultitaskMean(
            gpytorch.means.ConstantMean(), num_tasks=2
        )
        self.covar_module = gpytorch.kernels.MultitaskKernel(
            gpytorch.kernels.MaternKernel(nu=2.5), num_tasks=2, rank=1
        )

    def forward(self, x):
        mean_x = self.mean_module(x)
        covar_x = self.covar_module(x)
        return gpytorch.distributions.MultitaskMultivariateNormal(mean_x, covar_x)

likelihood = gpytorch.likelihoods.MultitaskGaussianLikelihood(num_tasks=2)
model = MultitaskGPModel(train_x, train_y, likelihood)

# Find optimal model hyperparameters
model.train()
likelihood.train()

# Use the adam optimizer
optimizer = torch.optim.Adam([
    {'params': model.parameters()},  # Includes GaussianLikelihood parameters
], lr=0.1)

# "Loss" for GPs - the marginal log likelihood
mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)

n_iter = 50
for i in range(n_iter):
    optimizer.zero_grad()
    output = model(train_x)
    loss = -mll(output, train_y)
    loss.backward()
    # print('Iter %d/%d - Loss: %.3f' % (i + 1, n_iter, loss.item()))
    optimizer.step()

# Set into eval mode
model.eval()
likelihood.eval()

# Make predictions
with torch.no_grad(), gpytorch.settings.fast_pred_var():
    predictions = likelihood(model(test_x))
    mean = predictions.mean
    lower, upper = predictions.confidence_region()

test_results_gpytorch = np.median((test_y - mean) / test_y, axis=0)

In the following, I provide the code for scikit-learn. Which is a little bit more convenient^^:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import WhiteKernel, Matern
kernel = 1.0 * Matern(length_scale=0.1, length_scale_bounds=(1e-5, 1e5), nu=2.5) \
         + WhiteKernel()
gp = GaussianProcessRegressor(kernel=kernel, alpha=0.0).fit(train_x.numpy(),
                                                            train_y.numpy())
# x_interpolation = test_x.detach().numpy()[np.newaxis, :].transpose()
y_mean_interpol, y_std_norm = gp.predict(test_x.numpy(), return_std=True)

test_results_scitlearn = np.median((test_y.numpy() - y_mean_interpol) / test_y.numpy(), axis=0)

Finally I compare the results:
comparisson = (test_results_scitlearn - test_results_gpytorch)/test_results_scitlearn
print('Variable 1: scitkit learn is more accurate my factor: ' + str(abs(comparisson[0]))
print('Variable 2: scitkit learn is more accurate my factor: ' + str(comparisson[1]))

Unfortunatelly, I did not find an easy way to fix the seed for scikit-learn. The last time I have run the code, it returned:

Variable 1: scitkit learn is more accurate my factor: 11.362540360431087
Variable 2: scitkit learn is more accurate my factor: 29.64760087022618

In case of gpytorch, I assume that the optimizer runs in some local optima. But I cannot think of any more robust optimization algorithm that still uses pytorch.
I am looking forward for suggestions!
Lazloo


title: Mahalanobis distance returns NaN in Pytorch

body:
I'm testing new metrics to measure distance between weight matrices in Pytorch, right now I'm trying to use Mahalanobis. For that I reshape every matrix into a vector and concat then into one matrix and then use this matrix to calculate the mahalanobis distance between any two rows of this matrix. Problem is some of those are getting me negative results and the square root of a negative throws me NaN.

I know the covariance matrix has to be Positive Defined, I guess I'm messing up there, or maybe my ideia of using mahalanobis in this case is not possible?

Here's the code I'm using, where I'm passing to it a X with shape (64,121), that being 64 (11x11) matrices

def _mahalanobis(X):
    VI = torch.inverse(cov(X)) #covariance matrix
    total_dist = 0
    for i,v in enumerate(X):
        dist = 0
        for j,u in enumerate(X):
            if i == j:
                continue
            x = (v-u).unsqueeze(0).t()
            y = (v - u).unsqueeze(0)

            dist = torch.sqrt(torch.mm(torch.mm(y,VI),x))

            total_dist +=dist
            print(dist)
    return total_dist

## Returns the covariance matrix of m
    def cov(m, rowvar=False):

        if m.dim() > 2:
            raise ValueError('m has more than 2 dimensions')
        if m.dim() < 2:
            m = m.view(1, -1)
        if not rowvar and m.size(0) != 1:
            m = m.t()
        # m = m.type(torch.double)  # uncomment this line if desired
        fact = 1.0 / (m.size(1) - 1)
        m -= torch.mean(m, dim=1, keepdim=True)
        mt = m.t()  # if complex: mt = m.t().conj()
        return fact * m.matmul(mt).squeeze()


title: Why pytorch isn't minimizing x*x for me?

body:
I expect x to converge to 0, which is minimum of x*x. But this doesn't happen. What am I doing wrong in this small sample code:

import torch
from torch.autograd import Variable
tns = torch.FloatTensor([3])
x = Variable(tns, requires_grad=True)
z = x*x
opt = torch.optim.Adam([x], lr=.01, betas=(0.5, 0.999))
for i in range(3000):
    z.backward(retain_graph=True) # Calculate gradients
    opt.step()
    print(x)


title: can't import 'torchtext' module in jupyter notebook while using pytorch

body:
I installed pytorch using anaconda3 and my created virtual conda environment named 'torchTest'.
I installed all the modules needed but, codes doesn't work in jupyter python.

I installed torchtext using
1.pip install https://github.com/pytorch/text/archive/master.zip
2.and also pip install torchtext too.
all I mentioned successfully downloaded in my MAC OS X, but can't get what's wrong with my Jupyter notebook..


title: Pytorch Convolutional Layer returning Nan

body: so I am using a convolutional layer as the first layer of a neural network for deep reinforcement learning to get the spatial features out of a simulation I built. The simulation gives different maps that are of different lengths and heights to process. If I understand convolutional networks, this should not matter since the channel size is kept constant. In between the convolutional network and the fully connected layers there is a spatial pyramid pooling layer so that the varying image sizes does not matter. Also the spatial data is pretty sparse. Usually it is able to go through a few states and sometimes a few episodes before the first convolutional layer spits out all Nans. Even when I fix the map size this happens. I do not know where the problem lies, where can the problem lie?


title: Resize RGB Tensor pytorch

body:
I want to resize a 3-D RBG tensor in pytorch. I know how to resize a 4-D tensor, but unfortunalty this method does not work for 3-D.

The input is:

#input shape: [3, 100, 200]   ---> desired output shape: [3, 80, 120]

if I have a 4-D vector it works fine.

#input shape: [2, 3, 100, 200]
out = torch.nn.functional.interpolate(T,size=(100,80), mode='bilinear')

Any suggestions? Thanks in advance!


title: How to Use Class Weights with Focal Loss in PyTorch for Imbalanced dataset for MultiClass Classification

body:
I am working on Multiclass Classification (4 classes) for Language Task and I am using the BERT model for classification task. I am following this blog as reference. My BERT Fine Tuned model returns nn.LogSoftmax(dim=1).
My data is pretty imbalanced so I used sklearn.utils.class_weight.compute_class_weight to compute weights of the classes and used the weights inside the Loss.
class_weights = compute_class_weight('balanced', np.unique(train_labels), train_labels)
weights= torch.tensor(class_weights,dtype=torch.float)
cross_entropy  = nn.NLLLoss(weight=weights)

My results were not so good so I thought of Experementing with  Focal Loss and have a code for Focal Loss.
class FocalLoss(nn.Module):
  def __init__(self, alpha=1, gamma=2, logits=False, reduce=True):
    super(FocalLoss, self).__init__()
    self.alpha = alpha
    self.gamma = gamma
    self.logits = logits
    self.reduce = reduce

  def forward(self, inputs, targets):
    BCE_loss = nn.CrossEntropyLoss()(inputs, targets)

    pt = torch.exp(-BCE_loss)
    F_loss = self.alpha * (1-pt)**self.gamma * BCE_loss

    if self.reduce:
      return torch.mean(F_loss)
    else:
      return F_loss

I have 3 questions now. First and the Most important is

Should I use Class Weight with Focal Loss?
If I have to Implement weights inside this Focal Loss, can I use weights parameters inside  nn.CrossEntropyLoss()
If this implement is incorrect, what should be the proper code for this one including the weights (if possible)


title: Multiple threads accessing same model on GPU for inference

body: I have a cnn model that is loaded onto the GPU and for every image, a new thread has to be created and detached to run the model on this image. Is this possible and if so, Is it safe?


title: How SLURM and Pytorch handle multi-node multi-gpu training together

body:
I would like to ask how the gradients aggregate when being trained with multi-node multi-gpu in a cluster using Slurm to manage workload. The code is written using Pytorch.

For example when launching a script train.py in Slurm to train a model on 4 nodes with 4GPUs per node as below, what do the srun command do exactly?

srun python train.py

It is written in the documentation that srun is used to run a parallel job on cluster managed by Slurm. However, I would like to know what steps it performs. I imagine that it copies the script to 16 GPUs and each GPU will run the script at the same time. Is that correct?

If I use torch.distributed.init_process_group() in my script to handle multi-gpu training, how Slurm will handle the gradients collected from each GPU together with Pytorch? I assume that there is a master port (which is a GPU device in a node assigned by Slurm) that gathers the gradients. And all 16 GPUs in 4 nodes will transfer the gradients to this master port thanks to the torch.distributed package? Slurm helps to allocate resources but it does not have any role in the aggregation of the gradients?

I'm also not sure if I should launch the script using just srun as above or should I specify the torch.distributed.launch in my command as below. I want to make sure the gradients are collected correctly.

# NGPU equals to number of GPUs/node
export NGPU=4
srun python -m torch.distributed.launch --nproc_per_node=$NGPU train.py

Thank you in advance for your help!


title: how to duplicate the input channel in a tensor?

body: I have a tensor with the shape torch.Size([39, 1, 20, 256, 256]) how do I duplicate the channel to make the shape torch.Size([39, 3, 20, 256, 256]).


title: How to install torch 0.4.1 on Windows 10?

body:
I have windows 10 on a Lenovo Thinkpad P72 with a Nvidia Quadro P5200, and I absolutely need to install (py)torch v0.4.1 to use a 3D Mask R-CNN. So I tried the following link:
https://github.com/pytorch/pytorch/issues/19457

However, when I finish with "python setup.py install", I obtain:

C:\Users\...\pytorch-0.4.1\build>msbuild INSTALL.vcxproj /p:Configuration=Release
Microsoft (R) Build Engine, version 4.8.3752.0
[Microsoft .NET Framework, Version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. Tous droits réservés.

La génération a démarré 31/03/2020 07:03:00.
Projet "C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj" sur le noud 1 (cibles par défaut).
C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj(32,3): error MSB4019: Le projet importé     "C:\Microsoft.Cpp.Default.props" est introuvable. Vérifiez que le chemin d'accès dans la     déclaration <Import> est correct et que le fichier existe sur le disque.
Génération du projet "C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj" terminée (cibles par défaut) -- ÉCHEC.

ÉCHEC de la build.

"C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj" (cible par défaut) (1) -> C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj(32,3): error MSB4019: Le projet importé     "C:\Microsoft.Cpp.Default.props" est introuvable. Vérifiez que le chemin d'accès dans la déclaration <Import> est correct et que le fichier existe sur le disque.

0 Avertissement(s)
1 Erreur(s)

Temps écoulé 00:00:00.28

C:\Users\...\pytorch-0.4.1\build>IF ERRORLEVEL 1 exit 1
Failed to run 'tools\build_pytorch_libs.bat --use-cuda --use-nnpack caffe2 nanopb libshm_windows'

Since I wasn't able to solve this issue, I copied all the missing files there, and then I obtained (even if C:\Microsoft.Build.CppTasks.Common.dll exists):

ÉCHEC de la build.

"C:\Users\...\pytorch-0.4.1\build\INSTALL.vcxproj" (cible par défaut) (1) ->
"C:\Users\...\pytorch-0.4.1\build\ZERO_CHECK.vcxproj" (cible par défaut) (2) ->
(SetBuildDefaultEnvironmentVariables cible) -> C:\Microsoft.Cpp.Current.targets(64,5): error MSB4062: Impossible de charger la tâche "SetEnv" à partir de l'assembly
C:\Microsoft.Build.CppTasks.Common.dll. Impossible de charger le fichier ou l'assembly         'Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=...' ou une     de ses dépendances. Le fichier spécifié est introuvable. Assurez-vous que la déclaration     <UsingTask> est correcte, que l'assembly et toutes ses dépendances sont
disponibles et que la tâche contient une classe publique qui implémente     Microsoft.Build.Framework.ITask. [C:\Users\...\pytorch-0.4.1\build\ZERO_CHECK.vcxproj]

Someone has an idea?