title: How to run a proper Bayesian Logistic Regression

body:
I'm trying to run  a bayesian logistic regression on the wine dataset provided from the sklearn package. As variables, I decided to use alcohol, color_intensity, flavanoids, hue and magnesium where alcohol is my response variable and the rest the predictors. To do so, I'm using pyro and torch packages:
import pyro
import torch
import pyro.distributions as dist
import pyro.optim as optim
from pyro.infer import SVI, Trace_ELBO
import pandas as pd
import numpy as np
from pyro.infer import Predictive
import torch.distributions.constraints as constraints
from sklearn import datasets
pyro.set_rng_seed(0)

#loading data and prepearing dataframe
 wine = datasets.load_wine()
 data = pd.DataFrame(columns = wine['feature_names'], data=wine['data'] )

#choosiing variables: response and predictors
 variables = data[['alcohol', 'color_intensity', 'flavanoids', 'hue', 'magnesium']]

#standardization
 variables = (variables-variables.min())/(variables.max()-variables.min())

#tensorizing
 alcohol = torch.tensor(variables['alcohol'].values, dtype=torch.float)
 predictors = torch.stack([torch.tensor(variables[column].values, dtype=torch.float)
               for column in ['alcohol', 'color_intensity', 'flavanoids', 'hue', 'magnesium']], 1)

#splitting data
 k = int(0.8 * len(variables))
 x_train, y_train = predictors[:k], alcohol[:k]
 x_test, y_test = predictors[k:], alcohol[k:]

#modelling

def model_alcohol(predictors, alcohol):
    n_observations, n_predictors = predictors.shape

 #weights
    w = pyro.sample('w', dist.Normal(torch.zeros(n_predictors), torch.ones(n_predictors)))
    epsilon = pyro.sample('epsilon', dist.Normal(0.,1.))

#non-linearity
    y_hat = torch.sigmoid((w*predictors).sum(dim=1) + epsilon)
    sigma = pyro.sample("sigma", dist.Uniform(0.,3.))
    with pyro.plate('alcohol', len(alcohol)):
       y=pyro.sample('y', dist.Normal(y_hat, sigma), obs=alcohol)

def guide_alcohol(predictors, alcohol=None):
    n_observations, n_predictors = predictors.shape

    w_loc = pyro.param('w_loc', torch.rand(n_predictors))
    w_scale = pyro.param('w_scale', torch.rand(n_predictors), constraint=constraints.positive)
    w = pyro.sample('w', dist.Normal(w_loc, w_scale))

    epsilon_loc = pyro.param('b_loc', torch.rand(1))
    epsilon_scale = pyro.param('b_scale', torch.rand(1), constraint=constraints.positive)
    epsilon = pyro.sample('epsilon', dist.Normal(epsilon_loc, epsilon_scale))

    sigma_loc = pyro.param('sigma_loc', torch.rand(n_predictors))
    sigma_scale = pyro.param('sigma_scale', torch.rand(n_predictors),
                   constraint=constraints.positive)
    sigma = pyro.sample('sigma', dist.Normal(sigma_loc, sigma_scale))

alcohol_svi = SVI(model=model_alcohol, guide=guide_alcohol, optim=optim.ClippedAdam({'lr' : 0.0002}),
                loss=Trace_ELBO())

losses = []
for step in range(10000):
   loss = alcohol_svi.step(x_train, y_train)/len(x_train)
   losses.append(loss)

As I have to use Stochastic Variational Inference, I have defined both the model and the guide. My problem is now at matching tensor sizes, as I now I get the error:
RuntimeError: The size of tensor a (142) must match the size of tensor b (5) at non-singleton
dimension 0
Trace Shapes:
Param Sites:
Sample Sites:
   w dist   5 |
    value   5 |
 epsilon dist |
    value   1 |
 sigma dist   |
    value   5 |
 alcohol dist |
    value 142 |

I'm kinda new to the idea of modelling on my own, so clearly there are mistakes around the code (hopefully not on the theory behind it). Still, I see I should adjust dimension on the guide maybe? I'm not entirely sure on how to honestly.


title: Vectorize code and remove nested loops from pytorch code

body:
Is it possible to remove the nested loop for iterating over all images and then row-wise iteration over all pixels of these images? I am finding it difficult because I need to use the location of the pixel(h,w)  from another variable label to choose exactly which channel's h, w value needs to be zeroed.
Colab Link
Overall what I need to do is given output of segmentation ([2,2,2,2]) (n, channels, height, width), and corresponding labels([2,2,2]) (n,height,width). I need to add another tensor M same size as output, which has a constant value m at all channels except the ground truth channel(from labels) for all the pixels.


title: Split string into sentences with punctuation marks

body:
I have a string of text from live speech (only lowercase letters, no full stops, commas etc.)
Is there any language model to transform this string into sentences with punctuation marks and capital letters?


title: How to use all vCPUs with `pytorch`?

body: I'm training 2 pytorch models on a Google cloud instance n1-standard-8 (8 vCPUs, 30 GB memory). In the first case I have only about 50% utilization (400% in the picture below) but in the second case it can be much higher (around 600-700%). In both cases I'm training NLP models with LSTMs. How can I increase utilization in the 1st case?


There are 46 benign (B) patients and 44 malignant (M) patients. Each patient has 4 images. I want to take 60% patient as train, 20% as valid and 20% for test. The split should always have been done patient-level, meaning images of the same patient should either belong to the train or test set but not be shared among them.

So

  Train – B: 28 patients  + M: 26 patients ;
      Valid- B: 9 patients  + M: 9 patients ;
      Test- B: 9 patients  + M: 9 patients;

How to split the datasets in folder wise in pytorch?


title: How can I output some data during a model.fit() run in tensorflow?

body:
I would like to print the value and/or the shape of a tensor during a model.fit() run and not before.
In PyTorch I can just put a print(input.shape) statement into the model.forward() function. Is there something similar in tensorflow?

Thanks


title: pytorch copying weights only worked with colon

body:
resnet18 = models.resnet18(pretrained=True)
    reg_resnet = resnet_model()
    for each_param in resnet18.state_dict().keys():
        reg_resnet.state_dict()[each_param][:] = resnet18.state_dict()[each_param]

The above code worked for me in updating the weights of my reg_resnet model (which is initialized from scratch) with pretrained weights of resnet 18. But it ddidn't work when I had the below:

resnet18 = models.resnet18(pretrained=True)
reg_resnet = resnet_model()
for each_param in resnet18.state_dict().keys():
    reg_resnet.state_dict()[each_param] = resnet18.state_dict()[each_param]

Why is it that adding the '[:]' made it work? What does that do in pytorch?


title: code to print information about only one batch

body:
I want to view information about my variables (type, min,max,std etc). While I write code below, it shows me information about all images in all batches. Please, tell me code, that will show me information about only one batch (something like "for batch [0], print(type(input))"

My code:

for t,(input, image_id) in enumerate(loader):
    print(image_id)

    input = input.cuda()
    with torch.no_grad():
        logit = net(input)
        probabilityy= torch.sigmoid(logit)

    probability = probabilityy.data.cpu().numpy()
    batch_size = len(image_id)
    for b in range(batch_size):
        p = probability[b]
        for c in range(4):
            predict, num = post_process(p[c], threshold, min_size)

            rle = run_length_encode(predict)

    print('input shape: ',input.shape)
    print('input ndim qty osi: ',input.ndim)
    print('input type: ',input.type)
    print('input mean std max min: ',input.mean(),input.std(),input.max(),input.min(),'\n')
    print('input: ',input[0,0:3,100:105,100:105])`


title: Choosing grad_norm() value

body: Is there's any technique to choose the best grad_norm() value by looking at the hidden weights? As trying different value takes a lot of time, I was wondering if anyone knows it and share their knowledge here.


title: Customize Dataset in Federated Learning

body:
i'm trying to modify the code of the tutorial (in the link below) to work on a customized dataset of images created by me.
Here you can find the tutorial i'm following
Here is the code that I have to go to modify:
    datasets.MNIST('../data', train=True, download=True,
                   transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ]))
    .federate((bob, alice)), # <-- NEW: we distribute the dataset across all the workers, it's now a FederatedDataset
    batch_size=args.batch_size, shuffle=True, **kwargs)

test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=False, transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ])),
    batch_size=args.test_batch_size, shuffle=True, **kwargs) ```


title: Saving weights of PyTorch VGG model after every iteration

body:
The same weights of VGG classifier get saved after every iteration. But the dictionary classifier_weights contains same weights at every iteration.
Below is the code snippet:
classifier_weights = {}
task_model.train()

for iter_count in range(self.args.train_iterations):
    if iter_count != 0 and iter_count % lr_change == 0:
        for param in optim_task_model.param_groups:
            param['lr'] = param['lr'] / 10

     # task_model step
        preds = task_model(labeled_imgs)
        task_loss = self.ce_loss(preds, labels)
        optim_task_model.zero_grad()
        task_loss.backward()
        optim_task_model.step()

        if iter_count > self.args.weight_save_iter:
            classifier_weights[iter_count - self.args.weight_save_iter - 1] = {}
            classifier_weights[iter_count - self.args.weight_save_iter - 1][0] = task_model.classifier[0].weight.data
            classifier_weights[iter_count - self.args.weight_save_iter - 1][1] = task_model.classifier[3].weight.data
            classifier_weights[iter_count - self.args.weight_save_iter - 1][2] = task_model.classifier[6].weight.data


title: Weights not updating with A3C

body:
I am trying to implement A3C algorithm. I have tried to train the dataset for 3 processes.However on all the 3 processes the output reward is comming same on all 3 iteration. From this I assume that the updates to the model is not taking place.
Below is my code..can someone please help to check whether there is some issue with my code.
Also my environment is not asynchronous capable.i.e it is handling 1 agent at a time and therefore 3 environments interact with shared agent. However the reward function in all 3 times is exactly the same.
Can someone please help a check if something is wrong with my code.
import os
import torch
import torch.multiprocessing as mp
from model import ActorCritic
import my_optim
from ENV import ENV
import pandas as pd
import atr_bb as ab
import numpy as np
import traceback
import torch.nn.functional as F
from torch.autograd import Variable
import torch
import torch.nn.functional as F
from ENV import ENV
import multiprocessing

os.chdir(os.path.dirname(os.path.abspath(__file__)))
state_dim = 5
action_dim = 4

PATH = "E:\\ML\\Breakout_a3c\\Code_With_Comments\\model.pth"
# Implementing a function to make sure the models share the same gradient
def ensure_shared_grads(model, shared_model):
    for param, shared_param in zip(model.parameters(), shared_model.parameters()):
        if shared_param.grad is not None:
            return
        shared_param._grad = param.grad

# Gathering all the parameters (that we can modify to explore)
class Params():
    def __init__(self):
        self.lr = 0.0001
        self.gamma = 0.99
        self.tau = 1.
        self.seed = 1
        self.num_processes = 3
        self.num_steps = 20
        self.max_episode_length = 10000
        self.env_name = 'Breakout-v0'

def train(rank, params, shared_model, optimizer,ticker):
    torch.manual_seed(params.seed + rank) # shifting the seed with rank to asynchronize each training agent
    # try:
    ohlcv = pd.read_csv(ticker + '.csv')
    data = ohlcv.copy()
    data['rsi'] = ab.RSI(data['Close'],14)
    data['adx'] = ab.ADX(data,20)
    data = ab.BollBnd(data,20)
    data['BBr'] = data['Close']/data['BB_dn']
    data['macd_signal'] = ab.MACD(data)
    data['macd_r'] = data['macd_signal']/data['Close']
    data['ema20'] = ab.EMA(np.asarray(data['Close']), 20)
    data['ema20_r'] = data['ema20']/data['Close']
    data['Close'] = data['Close']/data['Close'].max()
    data = data.iloc[:,[4,7,8,13,15,17]]
    data = data.dropna()
    data = torch.DoubleTensor(np.asarray(data))
    env = ENV(state_dim, action_dim, data)
    print("env created shape:",env.num_steps)
    model = ActorCritic(env.observation_space, env.action_space)
    state = env.reset()
    done = True
    episode_length = 0
    try:
        for idx in range(1):

            episode_length += 1
            model.load_state_dict(shared_model.state_dict())
            if done:
                cx = Variable(torch.zeros(1, state_dim)) # the cell states of the LSTM are reinitialized to zero
                hx = Variable(torch.zeros(1, state_dim)) # the hidden states of the LSTM are reinitialized to zero
            else:
                cx = Variable(cx.data)
                hx = Variable(hx.data)
            values = []
            log_probs = []
            rewards = []
            entropies = []
            for step in range(env.num_steps-2):
                value, action_values, (hx, cx) = model((Variable(state.unsqueeze(0)), (hx, cx)))
                prob = F.softmax(action_values,-1)
                log_prob = F.log_softmax(action_values,-1)
                entropy = -(log_prob * prob).sum(1)
                entropies.append(entropy)
                action = prob.multinomial(num_samples = 1).data
                log_prob = log_prob.gather(1, Variable(action))
                values.append(value)
                log_probs.append(log_prob)

                state, reward, done = env.step(action)
                print(ticker," reward ",reward)
                done = (done or episode_length >= env.num_steps)
                reward = max(min(reward, 1), -1) # clamping the reward between -1 and +1
                if done:
                    episode_length = 0
                    state = env.reset()
                rewards.append(reward)
                if done:
                    break
            R = torch.zeros(1, 1)
            if not done: # if we are not done:
                value, _, _ = model((Variable(state.unsqueeze(0)), (hx, cx)))
                R = value.data
            values.append(Variable(R))
            policy_loss = torch.zeros(1, 1)
            value_loss = torch.zeros(1, 1)
            R = Variable(R)
            gae = torch.zeros(1, 1)

            for i in reversed(range(len(rewards))):
                R = params.gamma * R + rewards[i]
                advantage = R - values[i]
                value_loss = value_loss + 0.5 * advantage.pow(2) # computing the value loss
                TD = rewards[i] + params.gamma * values[i + 1].data - values[i].data # computing the temporal difference
                gae = gae * params.gamma * params.tau + TD # gae = sum_i (gamma*tau)^i * TD(i) with gae_i = gae_(i+1)*gamma*tau + (r_i + gamma*V(state_i+1) - V(state_i))
                policy_loss = policy_loss - log_probs[i] * Variable(gae) - 0.01 * entropies[i] # computing the policy loss
            optimizer.zero_grad() # initializing the optimizer
            los = policy_loss + 0.5 * value_loss
            (policy_loss + 0.5 * value_loss).backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 40) # clamping the values
            ensure_shared_grads(model, shared_model)
            optimizer.step() # running the optimization
        torch.save(model.state_dict(), PATH)
    except Exception as e:
        print(e)
        traceback.print_exc()

def worker(i):
    """worker function"""
    print('Worker:',i)
    # sys.stdout.flush()
    return

params = Params()
shared_model = ActorCritic(state_dim, action_dim) # shared_model is the model shared by the different agents (different threads in different cores)
shared_model.share_memory() # storing the model in the shared memory of the computer, which allows the threads to have access to this shared memory even if they are in different cores
optimizer = my_optim.SharedAdam(shared_model.parameters(), lr=params.lr) # the optimizer is also shared because it acts on the shared model
optimizer.share_memory()

tickers = pd.read_csv("tickers.csv")
indices = tickers['Symbol']
jobs = []
for ticker in indices:

    try:

        processes = [] # initializing the processes with an empty list
        for rank in range(0, params.num_processes): # making a loop to run all the other processes that will be trained by updating the shared model
            # if __name__ == '__main__':
            #     p = mp.Process(target=train, args=(rank, params, shared_model, optimizer,ticker))
            #     p.start()
            #     processes.append(p)

            if __name__ == '__main__':
                p = multiprocessing.Process(target=train, args=(rank, params, shared_model, optimizer,ticker))
                jobs.append(p)
                p.start()
                for p in jobs: # creating a pointer that will allow to kill all the threads when at least one of the threads, or main.py will be killed, allowing to stop the program safely
                    p.join()

    except Exception as e:
        print(e)
        traceback.print_exc()

# if __name__ == '__main__':
#     jobs = []
#     for i in range(5):
#         p = multiprocessing.Process(target=worker,args=(i,))
#         jobs.append(p)
#         p.start()


title: GAN's training is not working, loss value is unchanged

body:
I am working on building a Dual Video Discriminator - GAN which uses a Temporal Discriminator and a Spatial Discriminator that need to cooperate.
I have set the classes and optimizers as such:
disS_optim = optim.Adam(ds.parameters(),lr=0.0005)
disT_optim = optim.Adam(dt.parameters(),lr=0.0005)
gen_optim = optim.Adam(g.parameters(),lr=0.0001)
loss = nn.BCELoss()
g = GeneratorNet()
ds = SpatialDiscriminatorNet()
dt = TemporalDiscriminatorNet()

And the training is:
lossDs_values = []
lossG_values = []
lossDt_values = []
num_epochs = 3
batch_size = 16
for epoch in trange(num_epochs):
    for i, (video, label) in enumerate(tqdm(data_loader)):
        #video shape ([batch_size, n_frames, 3, 64, 64])

        # 1) train discriminators
        disS_optim.zero_grad()
        disT_optim.zero_grad()

        real_data = video.permute(1,0,2,3,4)
        fake_data = g(_generator_noise_input(bs=batch_size, seqLen=n_frames)).detach()
        fake_data2 = g(_generator_noise_input(bs=batch_size, seqLen=n_frames)).detach()

        prediction_real_Ds = ds(real_data)
        error_real_Ds = loss(prediction_real_Ds, _ones_target(prediction_real_Ds.shape[0]))

        prediction_real_Dt = dt(real_data)
        error_real_Dt = loss(prediction_real_Dt, _ones_target(prediction_real_Dt.shape[0]))

        prediction_fake_Ds = ds(fake_data)
        error_fake_Ds = loss(prediction_fake_Ds, _zeros_target(prediction_fake_Ds.shape[0]))

        prediction_fake_Dt = dt(fake_data)
        error_fake_Dt = loss(prediction_fake_Dt, _zeros_target(prediction_fake_Dt.shape[0]))

        error_Ds = error_real_Ds + error_fake_Ds
        error_Dt = error_real_Dt + error_fake_Dt
        print(error_Ds, error_Dt)

        lossDs_values.append(error_Ds)
        lossDt_values.append(error_Dt)
        error_Ds.backward()
        error_Dt.backward()

        disS_optim.step()
        disT_optim.step()

        # 2) train generator
        gen_optim.zero_grad()

        prediction_G_Ds = ds(fake_data2)
        prediction_G_Dt = dt(fake_data2)

        error_G_Ds = loss(prediction_G_Ds, _ones_target(prediction_G_Ds.shape[0]))
        error_G_Dt = loss(prediction_G_Dt, _ones_target(prediction_G_Dt.shape[0]))
        error_G = error_G_Ds + error_G_Dt
        lossG_values.append(error_G)
        error_G.backward()
        print(error_G)

        gen_optim.step()

plt.plot(lossG_values,label='G error')
plt.plot(lossDs_values,label='Ds error')
plt.plot(lossDt_values,label='Dt error')
plt.legend(['G error','Ds error','Dt error'])
plt.show()

The problem is that the values of the graph remain basically still, or they change very slightly


title: How to represent this in the Tensorflow?

body:
object_for_each_prior[prior_for_each_object] = torch.LongTensor(range(n_objects)).to(device)

object_for_each_prior-> tensor([0, 0, 0,  ..., 0, 0, 0], device='cuda:0') 8732
prior_for_each_object.-> tensor([8709], device='cuda:0')

I'm not sure what does object_for_each_prior[prior_for_each_object]  mean in torch
but I want to know how to represent this in Tensorflow?


title: how can i fix pytorch predict problem with deep learning

body:
Given groups=1, weight of size [48, 3, 3, 3], expected input [5, 128, 129, 4] to have 3 channels, but got 128 channels instead.
This is my code:
    **model_ft.eval()
    for image in test_loader:
        image = image.cuda()
        output = model_ft(image)
        output = output.cpu().detach().numpy()
        for i, (e, n) in enumerate(list(zip(output, name))):
            sub.loc[sub['id_code'] == n.split('/')[-1].split('.')[0], 'diagnosis'] = le.inverse_transform([np.argmax(e)])

    sub.to_csv('submission.csv', index=False)**

    print(X_test.shape)
    (3071, 128, 128, 3)
    from torch.utils.data import DataLoader
    test_loader = DataLoader(X_test, batch_size=5, shuffle=True)
    print(train_data)

i don't know how to fix this problem to predict  my compete


title: action values goes to nan for all the actions while training A3C

body:
I am using pytorch to make an A3C with 4 processes as in below code.
But to my surprise while training the A3C action values goes to nan for all actions.
Initially it was not the case that action values were gone to nan.
But after an overnight training it goes to nan.
Can someone please help to let me know what issue is there.
class SharedAdam(torch.optim.Adam):
    def __init__(self, params, lr=1e-3, betas=(0.9, 0.99), eps=1e-8,
                 weight_decay=0):
        super(SharedAdam, self).__init__(params, lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
        # State initialization
        for group in self.param_groups:
            for p in group['params']:
                state = self.state[p]
                state['step'] = 0
                state['exp_avg'] = torch.zeros_like(p.data)
                state['exp_avg_sq'] = torch.zeros_like(p.data)

                # share in memory
                state['exp_avg'].share_memory_()
                state['exp_avg_sq'].share_memory_()

class ActorCritic(torch.nn.Module):

    def __init__(self, num_inputs, action_space):
        super(ActorCritic, self).__init__()

        self.num_inputs = num_inputs
        self.action_space = action_space
        self.lstm = nn.LSTMCell(num_inputs, num_inputs)
        num_outputs = action_space
        self.fc1 = nn.Linear(num_inputs, 256)
        self.fc1.apply(init_weights)
        self.fc2 = nn.Linear(256, 256)
        self.fc2.apply(init_weights)
        self.critic_linear = nn.Linear(256, 1)
        self.critic_linear.apply(init_weights)
        self.actor_linear = nn.Linear(256, num_outputs)
        self.actor_linear.apply(init_weights)
        self.lstm.bias_ih.data.fill_(0)
        self.lstm.bias_hh.data.fill_(0)
        self.sig1 = nn.Sigmoid()
        self.train()

    def forward(self, inputs):
        inputs, (hx, cx) = inputs
        hx, cx = self.lstm(inputs, (hx, cx))
        x = self.sig1(self.fc1(hx))
        x = torch.tanh(self.fc2(x))
        return self.critic_linear(x), self.actor_linear(x), (hx, cx)

    def save(self, filename, directory):
        torch.save(self.state_dict(), '%s/%s_actor.pth' % (directory, filename))

    def load(self, filename, directory):
            self.load_state_dict(torch.load('%s/%s_actor.pth' % (directory, filename)))

and below is code for training
def train(rank,params, model, optimizer,data):
    try:
        data = data.dropna()

        count = 0

        data = torch.DoubleTensor(np.asarray(data))

        env = ENV(params.state_dim, params.action_dim, data)
        print("env created\n")
        # init training variables
        max_timesteps = data.shape[0] - 1
        state = env.reset()
        done = True
        episode_length = 0
        count = 0
        while count<max_timesteps-1:
            episode_length += 1
            if done:
                cx = Variable(torch.zeros(1, params.state_dim))
                hx = Variable(torch.zeros(1, params.state_dim))
            else:
                cx = Variable(cx.data)
                hx = Variable(hx.data)

            values = []
            log_probs = []
            rewards = []
            entropies = []
            while count<max_timesteps-1:
                value, action_values, (hx, cx) = model((Variable(state.unsqueeze(0)), (hx, cx)))
                prob = F.softmax(action_values,dim = -1)
                log_prob = F.log_softmax(action_values, dim=-1).reshape(-1,)
                entropy = -(log_prob * prob).sum(1, keepdim=True)
                entropies.append(entropy)

                action = sample(prob)

                log_prob = log_prob.gather(0, Variable(action))

                state, reward, done = env.step(action)
                done = (done or count == max_timesteps-2)
                reward = max(min(reward, 1), -1)

                count +=1

                if done:
                    episode_length = 0
                    state = env.reset()

                values.append(value)
                log_probs.append(log_prob)
                rewards.append(reward)
                print(ticker, "rank ",rank," action:",action, "reward ",reward)

                if done:
                    break

            R = torch.zeros(1, 1)
            if not done:
                value, _, _ = model((Variable(state.unsqueeze(0)), (hx, cx)))
                R = value.data
            values.append(Variable(R))
            policy_loss = 0
            value_loss = 0
            R = Variable(R)
            gae = torch.zeros(1, 1)
            for i in reversed(range(len(rewards))):
                R = params.gamma * R + rewards[i]
                advantage = R - values[i]
                value_loss = value_loss + 0.5 * advantage.pow(2)
                TD = rewards[i] + params.gamma * values[i + 1].data - values[i].data
                gae = gae * params.gamma * params.tau + TD
                policy_loss = policy_loss - log_probs[i] * Variable(gae) - 0.01 * entropies[i]

            optimizer.zero_grad()
            (policy_loss + 0.5 * value_loss).backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 40)
            optimizer.step()

    except:
        traceback.print_exc()

below is code for sampling an action
def sample(logits):
    noise = torch.rand(logits.shape)
    return torch.argmax(logits - torch.log(-torch.log(noise)), 1)


title: flip 2 tensor mask with Pythorch

body:
I created 2 masks of 2 images by pytorch,
self.img_m = self.mask * self.img_truth
x = self.mask * self.img_m
y = (1 - self.mask) * self.img_g[-1].detach()

The masks are the same size
I want to flip the 2 masks, to put the first mask on the second image, and to put the second mask on the first image?
Can I do that?


title: How to cluster a score / probability map and get the modes (with variable numbers) in pytorch or numpy

body:
I have a 2d probability map (please correct me if I use any term wrong). Something like this:

Here yellow is a high value and violet is zero. Please ignore the red cross. It is represented as a matrix in numpy/pytorch.
You can see, this examples has two clusters. How can I find those clusters including the mode coordinates (matrix indices) and accumulated probability mass corresponding to these clusters. The number of clusters can vary in each probability map and needs to be determined automatically.
I believe something like mean-shift should work, but I am new to this field so I don't know the best way to do it. I found a code from sklearn.cluster.MeanShift but it needs as input sampled points, which seems very expensive to do for images with size of roughly 512 x 512. Can I do it without sampling first?


title: IPython Widgets Separated by Character Rather Than Word

body:
As you can tell in the picture below, the table is separated by characters rather than full words.
def apply(f):
    text = f
    text = re.sub(r'\W+', ' ', text)
    res = LM().check_probabilities(text, topk=20)
    l = str(res)
    paraphrase_widget = widgets.SelectMultiple(
    options=l,
    description='Paraphrases',
    disabled=False,
    layout= widgets.Layout(width='100%')
   )
    display(paraphrase_widget)
    return {"result": res}

apply("In order to")


title: differences between two 1-dim tensor

body:
I have two tensors like this:
a = tensor([1,3,4,5]).cuda()

b = tensor([0,1,2,3,4,5,6]).cuda()

I want the result tensor to be like this:
tensor([0,2,6])

differences between 1st tensor and 2nd tensor
is there simple solution that works on CPU and GPU and is significantly faster than np, set?


title: Neural network doesn't converge for a single input

body:
I am trying to reproduce a neural network for shape and appearance disentangling. The network was written in Tensorflow and I want to write it in PyTorch. The Model looks as follows:
class Model(nn.Module):
    def __init__(self, parts=16, n_features=32):
        super(Model, self).__init__()
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.E_sigma = E(3, parts, residual_dim=64, sigma=True)
        self.E_alpha = E(1, n_features, residual_dim=64, sigma=False)
        self.decoder = Decoder(parts, n_features)

    def forward(self, x):
        sig, stack = self.E_sigma(x)
        f_xs = self.E_alpha(stack)
        alpha = get_local_part_appearances(f_xs, sig)
        mu, L_inv = get_mu_and_prec(sig, self.device)
        encoding = feat_mu_to_enc(alpha, mu, L_inv, self.device)
        reconstruction = self.decoder(encoding)
        return reconstruction

The model consists of three nn.modules, which are E_sigma, E_alpha and Decoder. As an example, it looks as follows:
class E(nn.Module):
    def __init__(self, depth, n_out, residual_dim, sigma=True):
        super(E, self).__init__()
        self.sigma = sigma
        self.hg = Hourglass(depth, residual_dim)  # depth 4 has bottleneck of 4x4
        self.n_out = Conv(residual_dim, n_out, kernel_size=1, stride=1, bn=True, relu=True)
        if self.sigma:
            self.preprocess_1 = Conv(3, 64, kernel_size=6, stride=2, bn=True, relu=True)  # transform to 64 x 64 for sigma
            self.preprocess_2 = Residual(64, residual_dim)
            self.map_transform = Conv(n_out, residual_dim, 1, 1)    # channels for addition must be increased

    def forward(self, x):
        if self.sigma:
            x = self.preprocess_1(x)
            x = self.preprocess_2(x)
        out = self.hg(x)
        map = self.n_out(out)
        if self.sigma:
            map_normalized = F.softmax(map.reshape(map.size(0), map.size(1), -1), dim=2).view_as(map)
            map_transform = self.map_transform(map_normalized)
            stack = map_transform + x   # Why not stack? x is much larger than map_transform, so it is almost no impact
            return map_normalized, stack
        else:
            return map

Also there are three functions, which transform the data. I am trying to test, whether the model is converging if I just feed it with a single input and let it train for some time:
def train():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net = Model().to(device)
    net.train()
    optimizer = torch.optim.Adam(net.parameters(), lr=1e-4)
    criterion = nn.MSELoss().to(device)
    img = torch.randn(1, 3, 128, 128).to(device)
    for epoch in range(1000):
        optimizer.zero_grad()
        prediction = net(img)
        loss = criterion(prediction, img)
        loss.backward()
        optimizer.step()
        if epoch % 10 == 0:
            print(loss)

The model has about 12 million parameters and unfortunately for that single input, the loss always converges at about 0.5. Is that a sign, that there is a problem with my architecture? What could be the reason for that?


title: can mceps be used to classfication audio's kind?

body: recently, im using mceps features extracted from audio data, but i found when using it to conuct classfication, experiment's result is bad. Wanna know if mcep can be used for classfication.


title: Can't see shared files in Docker container

body:
I'm trying to build a docker image that contains pytorch and cv2.
docker build -t test .\Desktop\Docker\Docker_image

Dockerfile:
FROM pytorch/pytorch:latest

RUN conda install -c conda-forge opencv

WORKDIR ./work

COPY . .

CMD [ "python", "./my_script.py" ]

my_script.py:
import cv2
import os
import sys
import shutil

print('we start')
print(os.listdir(os.getcwd()))
print('what is in?')
print(os.listdir(os.getcwd() + '/Volume/'))

Then I run the container:
docker run --name test-container -v C:\Users\user\Desktop\Docker\Volume\.:/work/Volume/ test

And I can't see any files when use os.listdir(path) in the volume directory, but I can copy files using the cp command...
docker cp test-container:/work/Results/. C:\Users\user\Desktop\Results

So what should I do to see files in the volume directory (my main code uses these files) and be able to use pytorch and cv2?


title: Passing 3 dimensional and one dimensional features to neural network with PyTorch Dataloader

body:
I have examples with size 2x8x8 as tensor and I'm using PyTorch Dataloader for them. But now I want to add an additional 1 dim tensor with size 1 (a single number) as input too.
So I have two input parameters for the neural network, a multidimensional for convolutional layers and an additional one that I will concatenate later.
Probably I could use two dataloader, for every tensor shape one, but than I could not shuffle them.
How can I use a single PyTorch Dataloader for this two different input tensors?


title: How to handle samples with multiple images in a pytorch image processing model?

body:
My model training involves encoding multiple variants of a same image then summing the produced representation over all variants for the image.
The data loader produces tensor batches of the shape: [batch_size,num_variants,1,height,width].
The 1 corresponds to image color channels.
How can I train my model with minibatches in pytorch?
I am looking for a proper way to forward all the batch_size×num_variant images through the network and summing the results over all groups of variants.
My current solution involves flattening the first two dimensions and doing a for loop to sum the representations, but I feel like there should be a better way an d I am not sure the gradients will remember everything.


title: Reshape 5-dimensional tiled image to 3-dimensional normal image

body:
I'm creating a program that takes use of an RGB image that is tiled of the shape (n, n, 3, l, l). n is the number of each tile on each side of the image, and l is the length of each tile in pixels. I am trying to reshape this into a (3, l * n, l * n) shape. An example would be shape (7, 7, 3, 224, 224) to (3, 224, 224).
I want to keep the positions of the pixels in the new matrix, so I can visualise this image later. If I start with an image of a checkerboard pattern (every other tile has all pixel values set to 1, see example below), and use .reshape((3, 224, 224)) the result is the following:

Checkerboard (wanted result)

Wrong way of reshaping
I have made this for loop method of merging the tiles, which works, but is quite slow:
# l: the pixel length of each tile
img_reshaped = torch.zeros((3, 224, 224))
for i in range(len(img)):
    for j in range(len(img[i])):
        img_reshaped[:, i * l:(i + 1) * l, j * 32:(j + 1) * l] = noise[i, j]

I've also tried using .fold(), but this only works with 3D matrices, and not 5D.
Any tips on how to solve this? I feel it should be relatively simple, but just can't wrap my head around it just now.
PS: The code I used to generate the checkerboard:
noise = torch.zeros((7, 7, 3, 32, 32))

for i in range(len(noise)):
    for j in range(len(noise[i])):
        if (i % 2 == 0 and j % 2 != 0) or (i % 2 != 0 and j % 2 == 0):
            noise[i][j] = torch.ones((3, 32, 32))


title: What is the .optimizer package in Pytorch?

body:
I am trying to write my own optimizer for pytorch and am looking at the source code https://pytorch.org/docs/stable/_modules/torch/optim/sgd.html#SGD
to get started. When I try to run the code for the SGD, I get an error on the line
from .optimizer import Optimizer, required. I've searched everywhere but I'm not sure where to obtain the .optimizer package. Any help here is greatly appreciated.


title: Multi-Layer Neural Network Errors

body:
I have posted before, but I am posting a different neural network that I am trying to run on the same dataset. I am trying to run on a sklearn dataset.
This is the code that I have written thus far:
X, y = make_moons(200, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=1, stratify = y)
class MultiLayerPerceptron(nn.Module):
    def __init__(self):
        super(MultiLayerPerceptron, self).__init__()
        self.fc1 = nn.Sigmoid()
        self.fc2 = nn.Sigmoid()
        self.fc3 = nn.Sigmoid()

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = MultiLayerPerceptron)
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)
loss_fn = nn.CrossEntropyLoss()
epochs = 1000

def print_(loss):
    print("Loss", loss)

x, y = Variable (torch.from_numpy(X_train)).float(), Variable(torch.from_numpy(y_train)).long()

for epoch in range(1, epochs + 1):
    print("Epoch #", epoch)
    y_pred = model(x)
    loss = loss_fn(y_pred, y)
    print_(loss.item())

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

x_test = Variable(torch.from_numpy(X_test)).float()
model.eval()
pred = model(x_test)
pred = pred.detach().numpy()

print("Accuracy", accuracy_score(y_test, np.argmax(pred, axis = 1)))

But when I run this I get this error:
line 47, in __init__
    raise ValueError("optimizer got an empty parameter list")
ValueError: optimizer got an empty parameter list

I am trying to run a 3-layer neural network to classify the dataset and I do not know what is stopping it from running.


title: Does model.train() put every thing in train mode in pytorch even sub-networks?

body:
I wrote a neural network in PyTorch, which uses ResNet as feature generator, and it is fined tuned with the whole network.
My model consists of Resnet and several layers that I added to it. My question is:
When I call model.train(), Does it put ResNet in train mode or I should call train on it separately?


title: Still confused about model.train()

body:
I read all the posts here regarding model.train() and still didn't understand what is up with it. Specifically, when I use a pre-trained model like DenseNet or VGG with all parameters frozen beside the last layer not using drop-out nor Batch Normalization, the training loss starts off a lot smaller when using model.train(), but then decreases at about the same rate as when without it.
Why?


title: Convolve a small neural network across an image?

body:
I did a quick search about this that didn't turn up anything, so it may or may not be an off-the-wall idea.
Suppose I have a (relatively) tiny CNN (say, spatial input shape of (7, 7)) with several hidden layers and a spatial output size of (1, 1). I want to generate a feature map or an output by sliding this network across an image as you would a convolution filter.
Is this supported in pytorch? Is this a feature deep learning libraries in general support? Or would you have to do a custom implementation looping over the image manually? I'm curious if it's implementable, or whether it's already common and I'm not aware of it.


title: pytorch loss value fluctuating and not decreasing -

body:
I am trying to extract (opencv) humoment features from MNIST dataset and train the neural network. Below is the complete code. The problem here is the loss value fluctuating between 7 and 8 and not decreasing.
Can someone suggest what is the problem and how to debug?
# Imports
import torch
import torchvision
from torchvision.datasets import MNIST
import cv2
import numpy as np
import torch.nn as nn
import numpy
import matplotlib.pyplot as plt
%matplotlib inline

# Download training dataset
dataset = MNIST(root='data/', download=True)

input_array = []
target_array = []

for image,lbl in dataset:
  image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR)
  image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  _,image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)
  feature = cv2.HuMoments(cv2.moments(image)).flatten()
  input_array.append(feature)
  target_array.append([lbl])

inputs = torch.from_numpy(np.array(input_array,dtype="float32"))
targets = torch.from_numpy(np.array(target_array,dtype="float32"))

from torch.utils.data import TensorDataset
# Define dataset
train_ds = TensorDataset(inputs, targets)

from torch.utils.data import DataLoader
# Define data loader
batch_size = 500
train_dl = DataLoader(train_ds, batch_size, shuffle=True)

# Define model
model = nn.Linear(7, 1)

# Import nn.functional
import torch.nn.functional as F
# Define loss function
loss_fn = F.mse_loss
loss = loss_fn(model(inputs), targets)
print(loss)
# Define optimizer
opt = torch.optim.SGD(model.parameters(), lr=0.0001)

# Utility function to train the model
def fit(num_epochs, model, loss_fn, opt, train_dl):

    # Repeat for given number of epochs
    for epoch in range(num_epochs):

        # Train with batches of data
        for xb,yb in train_dl:

            pred = model(xb)  # 1. Generate predictions
            loss = loss_fn(pred, yb) # 2. Calculate loss
            loss.backward() # 3. Compute gradients
            opt.step() # 4. Update parameters using gradients
            opt.zero_grad() # 5. Reset the gradients to zero

        # Print the progress
        if (epoch+1) % 100 == 0:
            print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))

fit(10000, model, loss_fn, opt, train_dl)


title: How to emphasize selected output in neural network

body: I'm training a data set with 17 features and 5 output using pytorch. But I'm most interested in two of them, let say output 2 and 3 out of 0-4. What's a good strategy to get as high accuracy as possible on 2 and 3, while the rest can have lower accuracy?


title: MCMC saving samples of intermediate values with the posterior?

body:
I'm using Pyro for MCMC inference of parameters of some complicated calculation. Is there a way to keep the values of calculations made with the inferred variables?
A boiled down version of the code is:
def model(X_data, Y_data):
    #X_data, and Y_data are column vectors
    A = pyro.sample('A', dist.Normal(loc = 100.0, scale = 10.0)).double()
    B = pyro.sample('B', dist.Normal(loc = 25.0, scale = 2.0)).double()
    C = pyro.sample('C', dist.Normal(loc = 10.0, scale = 1.0)).double()
    D = pyro.sample('D', dist.Normal(loc = 400.0, scale = 50.0)).double()

    #Calculate Z for every value of X_data, return as column vector
    Z = some_complicated_calulation(A, B, C, D, X_data)

    #Create a distribution around Z
    Y_predicted = dist.Normal(loc = Z, scale = 10.0)

    #Sample from that distribution, and compare to observed data in Y_data
    pyro.sample('obs', Y_predicted, obs = Y_data)

kernel = NUTS(model, max_tree_depth=3, adapt_step_size=True)
posterior = MCMC(kernel, num_samples=200, warmup_steps=20)
posterior.run(X_grid, Y_grid)

Where X_data and Y_data are the training dataset, and both torch tensors column vectors (i.e. something with a shape like (8,1)).
When I run this and call posterior.get_samples() it returns the posterior of A, B, C, and D - each of which is shape(200). But I'd also like the posterior of Z, which should have a shape(8,200).
The NUTS sampler does a lot more than the 200 (or 220) calculations of Z in the background, so using an external list, tensor, or dictionary doesn't seem to work.
My thought is that this could be done with pyro.param, but it's unclear to me how to do that.
My end goal is to get the confidence interval of some_complicated_calculation for a the test set of unmeasured X values.


title: trying to use tacotron code but get 'indices' to have scalar type long & no module named hparams

body:
I am getting the following  error   while trying to use the below code .
This code works fine on google colab but when i download all the resources and run it within my own jupyter notebook i get error as below
modulenotfounderror traceback (most recent call last) <ipython-input-3-11ec0309c0d0> in <module>() 12
from ipython.display
import audio 13 --->
14 from hparams import hparams as hp 15 from audio import save_to_wav 16 from models import text2mel,
ssrn modulenotfounderror: no module named 'hparams'

expected tensor for argument #1 'indices' to have scalar type long; but got torch.inttensor instead
(while checking arguments for embedding)

Traceback (most recent call last):
  File "englishtts.py", line 107, in <module>
    _, Y_t, A = text2mel(L, Y, monotonic_attention=True)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Users\prilc\Desktop\speech\models\text2mel.py", line 165, in forward
    K, V = self.text_enc(L)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Users\prilc\Desktop\speech\models\text2mel.py", line 78, in forward
    out = self.embedding(x)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Users\prilc\Desktop\speech\models\layers.py", line 98, in forward
    return self.embedding(x)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\sparse.py", line 126, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\functional.py", line 1852, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.IntTensor instead (while checking arguments for embedding)

I am trying to run the below  inside a Jupyter notebook on windows ,
the below snippet is where the error is occuring but i dont know how to fix it .
"""### Synthetize on CPU"""

for i in range(len(SENTENCES)):
    sentence = SENTENCES[i]
    normalized_sentence = "".join([c if c.lower() in vocab else '' for c in sentence])
    print(normalized_sentence)

    sentences = [normalized_sentence]
    max_N = len(normalized_sentence)
    L = torch.from_numpy(get_test_data(sentences, max_N))
    zeros = torch.from_numpy(np.zeros((1, hp.n_mels, 1), np.float32))
    Y = zeros
    A = None

    for t in range(hp.max_T):
      _, Y_t, A = text2mel(L, Y, monotonic_attention=True)
      Y = torch.cat((zeros, Y_t), -1)
      _, attention = torch.max(A[0, :, -1], 0)
      attention = attention.item()
      if L[0, attention] == vocab.index('E'):  # EOS
          break

    _, Z = ssrn(Y)

    Z = Z.cpu().detach().numpy()
    save_to_wav(Z[0, :, :].T, '%d.wav' % (i + 1))
    IPython.display.display(Audio('%d.wav' % (i + 1), rate=hp.sr))


title: when I run this code in Pycharm i didn't get any outputs simply it shows------ process finished with exit code 0. please help me with this issue

body:
import torch
torch.cuda.is_available()
torch.cuda.current_device()
torch.cuda.get_device_name(0)
torch.cuda.memory_reserved()
torch.cuda.memory_allocated()
torch.cuda.memory_allocated()
var1=torch.FloatTensor([1.0,2.0,3.0]).cuda()
var1
var1.device
import pandas as pd
df=pd.read_csv('diabetes.csv')
df.head()
df.isnull().sum()
import seaborn as sns
import numpy as np
df['Outcome']=np.where(df['Outcome']==1,"Diabetic","No Diabetic")
df.head()
sns.pairplot(df,hue="Outcome")

The output shows:
C:\Users\vinot.conda\envs\python21\python.exe D:/python/python_work/pythonProject/diabetes.py
Process finished with exit code 0


title: MLModel style transfer prediction. Scale effect(brushstrokes)

body:
I have style transfer model which is trained by pytorch and converted by onnx to mlmodel. The style-image was 1500x2000. By using coremltools I set two sizes: 256x256 and 1500x2000.
Now I can pass two image sizes to prediction process. Here are results:

On the left side it is 1500x2000 image, and on the right side is 256x256 (scaled up after processing)
Is it possible to pass big image but have bigger size of brushstrokes as you can see on image on the right? So I want to keep image size and quality (1500x2000) but change the size of style(brushstrokes). Or it is not possible and it is totally depend of image-style size I was using to train model.


title: How to vectorize JSMA for fast computation on Google Colab GPU?

body:
I've implemented on Google Colab the following algorithm

from the paper Maximal Jacobian-based Saliency Map Attack
However, even a single iteration of the attack is taking hours. So I need to adapt the algorithm so that it works efficiently on GPU.
I don't know anything about adapting code for GPU.
Here's my code:
def clip_eps(x,eps):
  return min(1,x+eps,max(0,x-eps,x))

def jsma_attack(image, y, f, Imax, th, eps):
  x_ = image
  n = 784
  x_ = x_.view(1,1,28,28)
  i = 0
  eta = np.zeros((1,n))
  Gamma = dict()
  for k in range(0, n):
    Gamma[k] = 1
  #############################################################
  while i < Imax and len(Gamma) > 1:# and f(x_) == y:
    y_pred = f(x_.cuda()).detach().max(1,keepdims=True)[1].item()
    if y_pred != y.item():
      break
    gamma = 0
    J = torch.autograd.functional.jacobian(f,x_.cuda()).cpu()
    for k in Gamma:
      wk = k//28
      hk = k%28
      for l in Gamma:
        alpha = 0
        beta = 0
        wl = l//28
        hl = l%28
        if k != l:
          for t in range(10):
            print("k:{} l:{} t:{}".format(k,l,t))
            alpha += J[0,t,0,0,wk,hk]
            alpha += J[0,t,0,0,wl,hl]
            for m in range(10):
              if m != t:
                beta += J[0,m,0,0,wk,hk]
                beta += J[0,m,0,0,wl,hl]
            if -alpha*beta > gamma :
              p_star, q_star, gamma = k, l, -alpha*beta
              if t == y.item():
                theta_prime = -torch.sign(alpha) * th
              else:
                theta_prime = torch.sign(alpha) * th
        else:
          continue
    if gamma == 0:
      break
    x_[0,0,p_star//28,p_star%28] = clip_eps(x_[0,0,p_star//28,p_star%28] + theta_prime, eps)
    x_[0,0,q_star//28,q_star%28] = clip_eps(x_[0,0,q_star//28,q_star%28] + theta_prime, eps)
    if (not (x_[0,0,p_star//28,p_star%28] > 0 and x_[0,0,p_star//28,p_star%28] < 1)) or eta[0,p_star] == -theta_prime:
      Gamma.pop(p_star,'Key not found')
    if (not (x_[0,0,q_star//28,q_star%28] > 0 and x_[0,0,q_star//28,q_star%28] < 1)) or eta[0,q_star] == -theta_prime:
      Gamma.pop(q_star, 'Key not found')
    eta[0,p_star] = theta_prime
    eta[0,q_star] = theta_prime
    i = i + 1
  return x_.view(28,28)


title: How can I use torch Dataloader to get Picture having same class?

body:
in my dataset, there are 6 classes and 23 pictures per class
I used torchvision.dataset to make ImageFolder and it work well.
dataset = vision_dataset.ImageFolder(root = DATA_ROOT,
                                     transform = vision_trans.Compose([
                                                    vision_trans.Resize(256),
                                                    vision_trans.CenterCrop(256),
                                                    vision_trans.ToTensor()
                                     ]))

dataloader = torch.utils.data.DataLoader(dataset = dataset, batch_size = SHOT_K,
                                         shuffle = False, num_workers = 2, )

but I want to get batch images with same class.
...
tensor([2, 2, 2, 2, 2])
tensor([2, 2])
tensor([3, 3, 3, 3, 3])
...

This is what I wanted label(batch data's class) form
but actually DataLoader will work like that
...
tensor([2, 2, 2, 2, 2])
tensor([2, 2, 3, 3, 3])
tensor([3, 3, 3, 3, 3])
...

how can I get batch data per label?


title: How to split json dataset and save it?

body:
I have taken a json dataset. Dataset name is v2_OpenEnded_mscoco_train2014_questions.json How can I split some of the data from the dataset and save the split data into another json file?
This is the example of my dataset:

{"image_id": 426004, "question": "How many buns are on the plate?",
"question_id": 426004002}, {"image_id": 92846, "question": "What is
the color of the vase without flowers?", "question_id": 92846000},
{"image_id": 92846, "question": "Is there anything red in this
photo?", "question_id": 92846002}, {"image_id": 92846, "question":
"What does that vase represent?", "question_id": 92846003},
{"image_id": 262166, "question": "What color is the couch?",
"question_id": 262166002}, {"image_id": 262166, "question": "How many
seats are available?", "question_id": 262166003}

I have about 443,757 data in my dataset. I want to split the dataset into 400 different dataset each having 100 data. How can I automate this using python?
It will be a great help if this can be done with pytorch.


title: how can i reshape these images as a 2d images tensors?

body:
I am currently working with rgb images loaded as tensors and i would like to reshape them to be 2d tensors to implement deep neural networks on them
the shape which I am currently working on is :
images.shape

torch.Size([32, 3, 244, 244])

I dont know how to deal with the last two fields and also how to flatten the 3 channels of colors


title: CUDA_OUT_OF_MEMORY in PyTorch head2head model

body:
I am executing the head2head model presented in the Github repo here.
When I am running the code using the following command:
./scripts/train/train_on_target.sh Obama head2headDataset

with contents of the train_on_target.sh file as:
target_name=$1
dataset_name=$2

python train.py --checkpoints_dir checkpoints/$dataset_name \
                --target_name $target_name \
                --name head2head_$target_name \
                --dataroot datasets/$dataset_name/dataset \
                --serial_batches

Then I am getting the following error:
Traceback (most recent call last):
  File "train.py", line 108, in <module>
    flow_ref, conf_ref, t_scales, n_frames_D)
  File "/home/nitin/head2head/util/util.py", line 48, in get_skipped_flows
    flow_ref_skipped[s], conf_ref_skipped[s] = flowNet(real_B[s][:,1:], real_B[s][:,:-1])
  File "/home/nitin/anaconda3/envs/head2head/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/nitin/anaconda3/envs/head2head/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py", line 150, in forward
    return self.module(*inputs[0], **kwargs[0])
  File "/home/nitin/anaconda3/envs/head2head/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/nitin/head2head/models/flownet.py", line 38, in forward
    flow, conf = self.compute_flow_and_conf(input_A, input_B)
  File "/home/nitin/head2head/models/flownet.py", line 55, in compute_flow_and_conf
    flow1 = self.flowNet(data1)
  File "/home/nitin/anaconda3/envs/head2head/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/nitin/head2head/models/flownet2_pytorch/models.py", line 156, in forward
    flownetfusion_flow = self.flownetfusion(concat3)
  File "/home/nitin/anaconda3/envs/head2head/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/nitin/head2head/models/flownet2_pytorch/networks/FlowNetFusion.py", line 62, in forward
    concat0 = torch.cat((out_conv0,out_deconv0,flow1_up),1)
RuntimeError: CUDA out of memory. Tried to allocate 82.00 MiB (GPU 0; 5.80 GiB total capacity; 4.77 GiB already allocated; 73.56 MiB free; 4.88 GiB reserved in total by PyTorch)

I have checked the batch size in the file options/base_options.py. It is already set to 1. How can I solve the above mentioned exception. My system has 6 GB NVIDIA GTX 1660 Super GPU.


title: How to make DataLoader load correct batch_size data while using pytorch_geometric Data object?

body:
I am new to pytorch_geometric.
Here is my code to create a DataLoader which contains 5 samples, and each sample was represented by a 1 * 10 matrix.
import torch
import numpy as np
from torch_geometric.data import Data, DataLoader

x = torch.tensor(np.random((5, 10))
y = torch.tensor(np.random((5, 1))
data = Data(x = x, y = y)
loader = DataLoader([data], batch_size=2)

When I set batch_size to 2, I'd like the loader to load my 5 samples by 2 2 1, but now it pushes out all data in one batch, below code is the print result.
for batch in loader:
    print(batch)

# Batch(batch=[5], x=[5, 10], y=[5, 1])

Please let me know how can I achieve the result I want if you are familiar with that.
By the way, I learned this from pytorch_geometric official repository dataset like this.


title: What is the difference between storing PyTorch model file with extension .model and doing the same with .pkl extension?

body:
I wanted to understand the fundamental difference between models with extensions: ".model" & ".pkl"
Are they just two optional formats which we can choose to persist the PyTorch models?
As per my understanding,
".model" -> persist model weights and just an extension.
".pkl" -> a way of serializing objects in Python.
Just want to understand how are we using these extensions (use cases or the need) when it comes to persisting torch models.


title: How to handle "Memory quota exceeded" in heroku app developed using streamlit and PyTorch?

body:
I have been developing a style transfer image application using streamlit and PyTorch. The app is facing memory quota exceeded whenever high resolution image is stylized(using the app). How do I reduce the app size(its around 200+ mb)?
App code : https://github.com/pr2tik1/deep-stylize-image
App deployment : https://deep-style-images.herokuapp.com


title: Unable to import torchvision despite successful installation

body:
I am not able to import torchvision despite successful installation.
Environment OS : Windows

runfile('C:/Users/kundan/Desktop/Computer_Vision/Computer_Vision_A_Z_Template_Folder/Module
2 - Object Detection/data/untitled1.py',
wdir='C:/Users/kundan/Desktop/Computer_Vision/Computer_Vision_A_Z_Template_Folder/Module
2 - Object Detection/data') Traceback (most recent call last):
File
"C:\Users\kundan\Desktop\Computer_Vision\Computer_Vision_A_Z_Template_Folder\Module
2 - Object Detection\data\untitled1.py", line 8, in
import torchvision
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision_init_.py",
line 1, in
from torchvision import models
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision\models_init_.py",
line 11, in
from . import detection
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision\models\detection_init_.py",
line 1, in
from .faster_rcnn import *
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision\models\detection\faster_rcnn.py",
line 7, in
from torchvision.ops import misc as misc_nn_ops
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision\ops_init_.py",
line 1, in
from .boxes import nms, box_iou
File
"C:\Users\kundan.conda\envs\mynewflaskenv\lib\site-packages\torchvision\ops\boxes.py",
line 2, in
from torchvision import _C
ImportError: DLL load failed: The specified module could not be found.


title: Q: R Torch memory usage on GPU for convolutional LSTM tutorial

body:
The convolution LSTM tutorial linked from the Torch for R frontpage is written to run on cpu by default -- no calls to a gpu device. I can set up and run it just fine.
When I make a GPU modification, as follows in the code below,
model <- convlstm(input_dim = 1, hidden_dims = c(64, 1), kernel_sizes = c(3,3), n_layers = 2)
#---CUDA modification
device <- if (cuda_is_available()) torch_device("cuda:0") else "cpu"
model <- model$to(device = device)
#---
optimizer <- optim_adam(model$parameters)
num_epochs <- 100

for (epoch in 1:num_epochs) {
  model$train()
  batch_losses <- c()
  for (b in enumerate(dl)) {
    optimizer$zero_grad()
    preds <- model(b$x$to(device = device))[[2]][[2]][[1]] #last time step output from the last layer
    loss <- nnf_mse_loss(preds, b$y$to(dtype = torch_float(), device = device))
    batch_losses <- c(batch_losses, loss$item())
    loss$backward()
    optimizer$step()
  }
  if(epoch %%10 ==0)
    cat(sprintf("\nEpoch %d, training loss: %3f\n", epoch, mean(batch_losses)))
}

I get 40-50 epochs in before encountering an obvious "your GPU outta memory," error:
Error in (function (self, gradient, retain_graph, create_graph)  :
  CUDA out of memory. Tried to allocate 72.00 MiB (GPU 0; 6.00 GiB total capacity; 4.40 GiB already allocated; 10.19 MiB free; 4.74 GiB reserved in total by PyTorch)
Exception raised from malloc at ..\..\c10\cuda\CUDACachingAllocator.cpp:272 (most recent call first):
00007FFF51AFA7B200007FFF51AFA750 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>]

My GPU is pretty dated, a 2014-2015 GeForce GTX 970M, 3GB of memory native. But the tensors in this example are not particularly large. They're dim(100 6 1 24 24) synthetic tensors, although admittedly this structure is preserving all of the hidden and cell states. I don't have the background to calculate what this 'should' be using, but my intuition is that something about this setup (or the current R torch implementation) isn't cleaning up after itself, particularly through training epochs.
Is anyone able to either reproduce or easily run a GPU modification of my example, and is there a straightforward solution here or is there simply a fundamental limit to the capacity of my GPU in this case?


title: how to use torch.argmax and get only index value

body:
Using torch==1.7.1
MRE:
lst = [torch.rand(4).reshape(1,-1) for _ in range(5)]

for each tensor, I want to get index of largest value.
max_indexes = [torch.argmax(tensor) for tensor in lst]

which outputting
[tensor(0), tensor(0), tensor(2), tensor(2), tensor(2)]

I want to use its positions to be able to grab appropriate value from list of actions. How can I remove tensor and just get max_index?
desired output looks like this:
[0, 0, 2, 2, 2]

Thanks!


title: Pass an arbitrary image size to cnn in pytorch

body:
I'm trying to train a lenet model in pytorch, The ideia is to put images of any size in it, so I started doing with nn.AdaptiveAvgPool2d but the error comes as

mat1 dim 1 must match mat2 dim 0

Here is my code
class LeNet5(nn.Module):
  def __init__(self, num_classes=10):
    super(LeNet5, self).__init__()

    self.conv_1 = nn.Conv2d(
        in_channels=1, out_channels=32, kernel_size=5, bias=False
    )
    self.relu_1 = nn.ReLU(inplace=True)
    self.maxpool_1 = nn.MaxPool2d(kernel_size=2, stride=2)
    self.conv_2 = nn.Conv2d(
        in_channels=32, out_channels=256, kernel_size=5, bias=False
    )
    self.relu_2 = nn.ReLU(inplace=True)
    self.maxpool_2 = nn.MaxPool2d(kernel_size=2, stride=2)
    self.avgpool = nn.AdaptiveAvgPool2d(output_size=1)
    self.flatten = nn.Flatten()
    self.fc_1 = nn.Linear(in_features=4096, out_features=120, bias=False)
    self.fc_2 = nn.Linear(in_features=120, out_features=84)
    self.fc_3 = nn.Linear(in_features=84, out_features=num_classes)

  def forward(self, input):
    conv_1_output = self.conv_1(input)
    relu_1_output = self.relu_1(conv_1_output)
    maxpool_1_output = self.maxpool_1(relu_1_output)
    conv_2_output = self.conv_2(maxpool_1_output)
    relu_2_output = self.relu_2(conv_2_output)
    maxpool_2_output = self.maxpool_2(relu_2_output)
    flatten_output = self.flatten((self.avgpool(maxpool_2_output).view(maxpool_2_output.shape[0], -1)))
    fc_1_output = self.fc_1(flatten_output)
    fc_2_output = self.fc_2(fc_1_output)
    fc_3_output = self.fc_3(fc_2_output)

    return fc_3_output


title: How to secure consistency of saved Pytorch Conv1D model for time-series predictions day by day?

body:
I developed a standard Conv1D model in Pytorch to predict time series with classification (4 classes).
I gathered a train set (5000 data) and a test set (1000 data). The model predicts daily data by batches and is quite efficient.
As the results were satisfactory, I then moved to the next step :

I trained my model
I saved the model
I used the trained model on daily new data (bringing data for prediction day-by-day, instead of by batch size).
The results were very deceptive (if not catastrophic).

Therefore, I checked what happen and tried to save my trained model, to clean the GPU cache, to re-load the model and to apply it to the test-set.
Herewith the original code :
    train_dl, test_dl = get_data_loaderRN(X_train, y_train, X_tests, y_tests, batch_size)
    model = Conv_1D(input_shape, nb_classes, num_features, seq_length, batch_size, iter_model, iter_pre, dropout)
    model = model.double()
    model.cuda()
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr= learning)
    y_predtrain, running_loss = train_Conv_1D(model, criterion, optimizer, epochs, learning, verbose, train_dl)
    acc_train = accuracy_score(y_train, y_predtrain)
    y_predsubt, y_pred_proba, running_loss = eval_model_a(model, nb_classes, criterion, test_dl)
    acc_test = accuracy_score(y_tests, y_predsubt)

And the code with save and re-load model :
    train_dl, test_dl = get_data_loaderRN(X_train, y_train, X_tests, y_tests, batch_size)
    model = Conv_1D(input_shape, nb_classes, num_features, seq_length, batch_size, iter_model, iter_pre, dropout)
    model = model.double()
    model.cuda()
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr= learning)
    y_predtrain, running_loss = train_Conv_1D(model, criterion, optimizer, epochs, learning, verbose, train_dl)
    acc_train = accuracy_score(y_train, y_predtrain)
    ## Save model and clean GPU cache + re-seed
    torch.save(model, model_path)
    torch.cuda.empty_cache()
    torch.manual_seed(my_seed)
    torch.cuda.manual_seed(my_seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    ## Reload model and use it to predict
    model = torch.load(model_path)
    model.to(device)
    y_predsubt, y_pred_proba, running_loss = eval_model_a(model, nb_classes, criterion, test_dl)
    acc_test = accuracy_score(y_tests, y_predsubt)

The Save / Clean / Load steps are there to simulate what happened with the model used for daily prediction... and the results are far from those with the original model.
How could I address this problem ?
Thanks in advance for your support.
Best,
NB1. The output is double "y_predsubt" recors the predicted class and "y_pred_proba" records the likelihood associated by the model to each of the 4 classes.
NB2. I did the same with a LSTM model and get the same issue :-(


title: why single pytorch process is displayed on each GPU

body:
When I create a tensor in GPU with pytorch, the same process is displayed on each GPU, even though the memory of only one GPU is allocated.
For example, if I run these:
import torch
a = torch.rand(100,100)
b = a.cuda()

then the result of nvidia-smi will be:
(just ignore process 72497, which is run by other user)
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.23.04    Driver Version: 455.23.04    CUDA Version: 11.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce RTX 3090    On   | 00000000:3D:00.0 Off |                  N/A |
| 30%   19C    P8    24W / 350W |   1001MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  GeForce RTX 3090    On   | 00000000:3E:00.0 Off |                  N/A |
| 30%   20C    P8    27W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   2  GeForce RTX 3090    On   | 00000000:40:00.0 Off |                  N/A |
| 30%   32C    P2   108W / 350W |   7361MiB / 24268MiB |      2%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   3  GeForce RTX 3090    On   | 00000000:41:00.0 Off |                  N/A |
| 30%   21C    P8    17W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   4  GeForce RTX 3090    On   | 00000000:B1:00.0 Off |                  N/A |
| 30%   18C    P8    21W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   5  GeForce RTX 3090    On   | 00000000:B2:00.0 Off |                  N/A |
| 30%   20C    P8    17W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   6  GeForce RTX 3090    On   | 00000000:B4:00.0 Off |                  N/A |
| 30%   21C    P8    21W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   7  GeForce RTX 3090    On   | 00000000:B5:00.0 Off |                  N/A |
| 30%   21C    P8    26W / 350W |      2MiB / 24268MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A     22060      C   python                            999MiB |
|    1   N/A  N/A     22060      C   python                              0MiB |
|    2   N/A  N/A     22060      C   python                              0MiB |
|    2   N/A  N/A     72497      C   python                           7359MiB |
|    3   N/A  N/A     22060      C   python                              0MiB |
|    4   N/A  N/A     22060      C   python                              0MiB |
|    5   N/A  N/A     22060      C   python                              0MiB |
|    6   N/A  N/A     22060      C   python                              0MiB |
|    7   N/A  N/A     22060      C   python                              0MiB |
+-----------------------------------------------------------------------------+

How can I fix it?


title: Mapping tensor in pytorch

body:
I have the following two tensors:

img is a RGB image of shape (224,224,3)
uvs is a tensor with same spacial size e.g. (224, 224, 2) that maps to coordinates (x,y). In other words it provides (x,y) coordinates for every pixel of the input image.

I want to create now a new output image tensor that contains on index (x,y) the value of the input image. So the output should be an image as well with the pixels rearranged according to the mapping tensor.
Small toy example:
img = [[c1,c2], [c3, c4]] where c is a RGB color [r, g, b]
uvs = [[[0,0], [1,1]],[[0,1], [1,0]]]
out = [[c1, c3], [c4, c2]]

How would one achieve such a thing in pytorch in a fast vectorized manner?


title: Wall damage detection

body:
I'm lanning to create a real-time wall damages detector [scratches, Cracks] using YOLOv5 and my custom dataset of images (125).

Do you think I can do transfer learning or it won’t be possible since the coco dataset classes are not similar?
Do you think I need to increase the dataset size?

For now I'm just trying to do a proof of concept. Wanted to have my steps planned in advance.


title: How to download pyaudio

body:
I am unable to download pyaudio.
I tried pip install pipwin.
then pipwin install pyaudio.
but after hitting enter my terminal is not showing anything,
It was almost 3hrs no errors nothing was showed in terminal regarding successful download or any error nothing and also was not able to close the terminal. What's the solution to this.


title: Placebo Neural Network that can back-propagate but will give no improvement

body:
What would be an easy way to create a useless neural network for testing, that can back-propagate (so that there is no need to comment out loss.backward()) and is trainable, but it would not give any improvement to prediction accuracy?
E.g. Below is a neural network for the MNIST dataset. How should I create a counterpart that would also give output of the same dimension (batch_size * 10), that would enable the loss tensor to back-propagate gradients, but would give no improvement in accuracy?
class test_net_1(nn.Module):
    def __init__(self):
        super(test_net_1,self).__init__()
        self.conv1 = nn.Conv2d(1,1,kernel_size=3)
        self.fc1 = nn.Linear(26*26,10)
    def forward(self,x):
        x = self.conv1(x)
        x = x.view(-1,26*26)
        x = self.fc1(x)
        x = F.log_softmax(x)
        return x


title: Trouble implementing "concurrent" softmax function from paper (PyTorch)

body:
I am trying to implement the so called 'concurrent' softmax function given in the paper "Large-Scale Object Detection in the Wild from Imbalanced Multi-Labels". Below is the definition of the concurrent softmax:

NOTE: I have left the (1-rij) term out for the time being because I don't think it applies to my problem given that my training dataset has a different type of labeling compared to the paper.

To keep it simple for myself I am starting off by implementing it in a very inefficient, but easy to follow, way using for loops. However, the output I get seems wrong to me. Below is the code I am using:
# here is a one-hot encoded vector for the multi-label classification
# the image thus has 2 correct labels out of a possible 3 classes
y = [0, 1, 1]

# these are some made up logits that might come from the network.
vec = torch.tensor([0.2, 0.9, 0.7])

def concurrent_softmax(vec, y):
    for i in range(len(vec)):
        zi = torch.exp(vec[i])
        sum_over_j = 0
        for j in range(len(y)):
            sum_over_j += (1-y[j])*torch.exp(vec[j])

        out = zi / (sum_over_j + zi)
        yield out

for result in concurrent_softmax(vec, y):
    print(result)

From this implementation I have realized that, no matter what value I give to the first logit in 'vec' I will always get an output of 0.5 (because it essentially always calculates zi / (zi+zi)). This seems like a major problem, because I would expect the value of the logits to have some influence on the resulting concurrent-softmax value. Is there a problem in my implementation then, or is this behaviour of the function correct and there is something theoretically that I am not understanding?


title: Reinforcement Learning - only size-1 arrays can be converted to Python scalars - is it data problem?

body:
I'm new to pytorch and even though I was searching for this error I can't seem to understand where axactly I'm doing something wrong.
I'm trying to run a codewith a model that trades 3 different stocks. My data is a csv file with three columns with closing prices of stocks.
I'm trying to run this part of code
env.reset()
# In case you're running this a second time with the same model, delete the gradients
del model.rewards[:]
del model.saved_actions[:]

gamma = 0.9
log_interval = 60

def finish_episode():
    R = 0
    saved_actions = model.saved_actions
    policy_losses = []
    value_losses = []
    rewards = []
    for r in model.rewards[::-1]:
        R = r + (gamma * R)
        rewards.insert(0, R)
    rewards = torch.tensor(rewards)

    epsilon = (torch.rand(1) / 1e4) - 5e-5
    # With different architectures, I found the following standardization step sometimes
    # helpful, sometimes unhelpful.
    # rewards = (rewards - rewards.mean()) / (rewards.std(unbiased=False) + epsilon)
    # Alternatively, comment it out and use the following line instead:
    rewards += epsilon

    for (log_prob, value), r in zip(saved_actions, rewards):
        reward = torch.tensor(r - value.item()).cuda()
        policy_losses.append(-log_prob * reward)
        value_losses.append(F.smooth_l1_loss(value, torch.tensor([r]).cuda()))

    optimizer.zero_grad()
    loss = torch.stack(policy_losses).sum() + torch.stack(value_losses).sum()
    loss = torch.clamp(loss, -1e-5, 1e5)
    loss.backward()
    optimizer.step()
    del model.rewards[:]
    del model.saved_actions[:]

running_reward = 0
for episode in range(0, 4000):
    state = env.reset()
    reward = 0
    done = False
    msg = None
    while not done:
        action = model.act(state)
        state, reward, done, msg = env.step(action)
        model.rewards.append(reward)
        if done:
            break
    running_reward = running_reward * (1 - 1/log_interval) + reward * (1/log_interval)
    finish_episode()
    # Resetting the hidden state seems unnecessary - it's effectively random from the previous
    # episode anyway, more random than a bunch of zeros.
    # model.reset_hidden()
    if msg["msg"] == "done" and env.portfolio_value() > env.starting_portfolio_value * 1.1 and running_reward > 500:
        print("Early Stopping: " + str(int(reward)))
        break
    if episode % log_interval == 0:
        print("""Episode {}: started at {:.1f}, finished at {:.1f} because {} @ t={}, \
last reward {:.1f}, running reward {:.1f}""".format(episode, env.starting_portfolio_value, \
              env.portfolio_value(), msg["msg"], env.cur_timestep, reward, running_reward))

But I'm getting such an error:
TypeError                                 Traceback (most recent call last)
<ipython-input-91-ce955397be85> in <module>()
     45     msg = None
     46     while not done:
---> 47         action = model.act(state)
     48         state, reward, done, msg = env.step(action)
     49         model.rewards.append(reward)

1 frames
<ipython-input-89-f463539c7fe3> in forward(self, x)
     16
     17     def forward(self, x):
---> 18         x = torch.tensor(x).cuda()
     19         x = torch.sigmoid(self.input_layer(x))
     20         x = torch.tanh(self.hidden_1(x))

TypeError: only size-1 arrays can be converted to Python scalars

This is the part of code with forward function defined
class Policy(nn.Module):
    def __init__(self):
        super(Policy, self).__init__()
        self.input_layer = nn.Linear(11, 128)
        self.hidden_1 = nn.Linear(128, 128)
        self.hidden_2 = nn.Linear(32,31)
        self.hidden_state = torch.tensor(torch.zeros(2,1,32)).cuda()
        self.rnn = nn.GRU(128, 32, 2)
        self.action_head = nn.Linear(31, 5)
        self.value_head = nn.Linear(31, 1)
        self.saved_actions = []
        self.rewards = []

    def reset_hidden(self):
        self.hidden_state = torch.tensor(torch.zeros(2,1,32)).cuda()

    def forward(self, x):
        x = torch.tensor(x).cuda()
        x = torch.sigmoid(self.input_layer(x))
        x = torch.tanh(self.hidden_1(x))
        x, self.hidden_state = self.rnn(x.view(1,-1,128), self.hidden_state.data)
        x = F.relu(self.hidden_2(x.squeeze()))
        action_scores = self.action_head(x)
        state_values = self.value_head(x)
        return F.softmax(action_scores, dim=-1), state_values

    def act(self, state):
        probs, state_value = self.forward(state)
        m = Categorical(probs)
        action = m.sample()
        if action == 1 and env.state[0] < 1: action = torch.LongTensor([2]).squeeze().cuda()
        if action == 4 and env.state[1] < 1: action = torch.LongTensor([2]).squeeze().cuda()
        if action == 6 and env.state[2] < 1: action = torch.LongTensor([2]).squeeze().cuda()
        self.saved_actions.append((m.log_prob(action), state_value))
        return action.item()

Can you please direct me where I should make changes? Is it the data I'm feeding my model with, or something different?
Thank you so much for help


title: Partial derivative in PyTorch

body:
Suppose f(x) is a neural network with weights w. If I do
loss = -f(f(x))
loss.backward()
self.optim.step()

then this is the same as to go in the direction of f'(f(x))df(x)/dw + df/dw(f(x)). But if I want to ignore the second part and only move in the direction of f'(f(x))df(x)/dw, what would be a simple way to do it in PyTorch?


title: Pytorch ignite metrics - passing objects

body:
I've been playing with metrics in pytorch ignite and have come across an issue with passing ignite objects to other ignite objects, for instance this code
cm = metrics.ConfusionMatrix(10)
cm.reset()
cm.update((y_pred, y_true_sparse))
print(cm.compute())
dice = metrics.DiceCoefficient(cm)
print(dice.compute())

throws ignite.exceptions.NotComputableError: Confusion matrix must have at least one example before it can be computed. even tho the confusion matrix does not fail. The same problem occurred when I tried computing the F beta score (passing precision and recall objects). I've tried some experiments, such as not computing the confusion matrix before passing it down or updating the data again but without success.
What am I doing wrong? This behaviour seems weird to me, even diving into the documentation I can't seem to figure it out and no relatable google search results come up.


title: Incremental Neural Network Training

body:
I am working with a very large dataset with hundreds of long videos to be used as training and I'm using Google Colab to perform some tests. The whole code I wrote is quite simple and uses PyTorch.
When I try to perform the training, if I use more than 200 videos at a time, the RAM fullfills during the training and the Colab crashes. I noticed that this does not happens if I train with lower number of training videos.
For that reason I thought that my model may be trained incrementally creaing a structure as follows:
model = torch.nn.Sequential( # create a model
    ...
    nn.Softmax(dim=1)
)

MAX_VIDEOS_PER_BATCH = 100
for current_batch in range (0, TOTAL_BATCHES): # Perform TOTAL_BATCHES trainings
    videos = []
    labels = []
    for index, video_file_name in enumerate(os.listdir(VIDEOS_DIR)): # Read 100 videos as training set
        if index < MAX_VIDEOS_PER_BATCH * current_batch:
           continue

        ... # read the video and add it to videos
        ... # add the considered labels to videos list

    video_training = torch.tensor(np.asarray(videos)).float() # (batch x frames x channels x height x width)
    learning_rate = 1e-4
    for t in range(ITERATIONS): # Train the model, if I already trained it the model is not resetted
        y_pred = model(torch.FloatTensor(np.asarray(video_training )))

        loss = loss_fn(y_pred, torch.tensor(labels))
        print("#" + str(t), " loss:" + str(loss.item()))

        model.zero_grad()
        loss.backward()
        with torch.no_grad():
        for param in model.parameters():
            param -= learning_rate * param.grad

My question is, is this method correct? I am training the network in a correct manner or this batches approach will create some damages or biases to the model?
When I go from batch 1 to batch 2, the model won't lose the previous trained knowledge, is it correct?


title: Highly imbalanced conllu dataset for NER classification with BERT

body:
I am using Bert for NER with a conllu english dataset. It is very imbalanced as you can see:
Counter({'O': 16348, 'B-PER': 980, 'B-ORG': 421, 'B-GPE_LOC': 112, 'B-DRV': 75, 'B-GPE_ORG': 72, 'B-PROD': 43, 'B-LOC': 39, 'B-EVT': 8})

And when i split it into training and validation
from conllu import parse
data = parse(open("english.conllu", "r").read())

train_df, val_df = train_test_split(data, test_size = 0.2)

then I get this distribution
[training] Counter({'O': 13083, 'B-PER': 795, 'B-ORG': 328, 'B-GPE_LOC': 86, 'B-DRV': 61, 'B-GPE_ORG': 59, 'B-LOC': 31, 'B-PROD': 29, 'B-EVT': 6})
[validation] Counter({'O': 3265, 'B-PER': 185, 'B-ORG': 93, 'B-GPE_LOC': 26, 'B-DRV': 14, 'B-PROD': 14, 'B-GPE_ORG': 13, 'B-LOC': 8, 'B-EVT': 2})

The f1 score is 0.96 but obviously that is not representative of the performance of the model as you can see here
              precision    recall  f1-score   support

       B-DRV       0.00      0.00      0.00       136
       B-EVT       0.00      0.00      0.00        36
   B-GPE_LOC       0.00      0.00      0.00       472
   B-GPE_ORG       0.00      0.00      0.00        83
       B-LOC       0.00      0.00      0.00       140
       B-ORG       0.00      0.00      0.00       623
       B-PER       0.00      0.00      0.00       949
      B-PROD       0.00      0.00      0.00       164
       I-DRV       0.00      0.00      0.00        25
       I-EVT       0.00      0.00      0.00        17
   I-GPE_LOC       0.00      0.00      0.00        49
   I-GPE_ORG       0.00      0.00      0.00         6
       I-LOC       0.00      0.00      0.00        61
       I-ORG       0.00      0.00      0.00       217
       I-PER       0.00      0.00      0.00       520
      I-PROD       0.00      0.00      0.00       172
           O       0.93      1.00      0.97     52359

    accuracy                           0.93     56029
   macro avg       0.05      0.06      0.06     56029
weighted avg       0.87      0.93      0.90     56029

My loss function and my optimizer are:
 criterion = nn.CrossEntropyLoss()
 optimizer = AdamW(model.parameters(), lr=2e-5, eps=1e-8)

How could I go about treating this imbalance? I tried getting the weights and using them in CrossEntropy like this
idx2label = train_dataset.inverse_indexer
count_label = dict(Counter(train_dataset.flat))
inverse_counts = {key:1./value for key, value in count_label.items()}
weights = torch.Tensor(list(inverse_counts.values()))
print(weights)
sum_inverse = np.sum([count for _, count in inverse_counts.items()])
inverse_normalized =  {key:value/sum_inverse for key, value in inverse_counts.items()}
weights = np.array([0.3+inverse_normalized[idx2label[i]] for i in range(len(idx2label))])
weights = [math.log(i) for i in weights]
weights = torch.Tensor(weights)

criterion = nn.CrossEntropyLoss(ignore_index=-1, weight=weights)
But the results were the same.


title: Training a model based on time rather than epochs

body:
Is there a way to train a model based on time?
Say I have the following code to train a model:
import torch
import torch.nn as nn
import torch.optim as optim

class net_x(nn.Module):
        def __init__(self):
            super(net_x, self).__init__()
            self.fc1=nn.Linear(2, 20)
            self.fc2=nn.Linear(20, 20)
            self.out=nn.Linear(20, 4)

        def forward(self, x):
            x=self.fc1(x)
            x=self.fc2(x)
            x=self.out(x)
            return x

nx = net_x()

r = torch.tensor([1.0,2.0])
optimizer = optim.Adam(nx.parameters(), lr = 0.1)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-2, max_lr=0.1, step_size_up=1, mode="triangular2", cycle_momentum=False)

for epoch in range(100):
    optimizer.zero_grad()
    net_predictions = nx(r)
    loss = torch.sum(torch.randint(0,10,(4,)) - net_predictions)
    loss.backward()
    optimizer.step()
    scheduler.step()
    print('loss:' , loss)

Instead of having 100 epochs, can I somehow change it to be for 1 hour of training?


title: How can I avoid this for loop in pytorch? Is there a function for efficient computation?

body:
I have the following code in my Pytorch neural net:
cos = nn.CosineSimilarity(dim=1)

d = torch.zeros(batch_sz, n, n).to(device="cuda")

for i in range(n):
   for j in range(n):
       d[:, i, j] = cos(q[:, i, :], k[:, j, :])

q and k are both of size (batch_sz, n, m).
This piece of Code obviously slows down my program and I'm wondering if Pytorch offers any functions that might make this more efficient.
Thanks so much!


title: Image Net Preprocessing using torch transforms

body:
I am trying to recreate the data preprocessing on the ImageNet data set done in the original publication "Deep Residual Learning for Image Recognition". As said in their paper in section 3.4:
"Our implementation for ImageNet follows the practice in [21, 41].  The image is resized with its shorter side randomly sampled in[256,480]for scale augmentation [41].A 224×224 crop is randomly sampled from an image or its horizontal flip, with the per-pixel mean subtracted [21]. The standard color augmentation in [21] is used."
I have figured out the parts of randomly cropping the original image or the horizontal flip with a crop size of 224x224. The other two parts I have not. The other two parts are The image is resized with its shorter side randomly sampled in[256,480] for scale augmentation and the The standard color augmentation in [21] is used.
For the first one, I can't find a "random resize" function in torch transforms. The second, where its referencing [21], is (according to [21]) a "perform  PCA  on  the  set  of  RGB  pixel  values  throughout  theImageNet training set". Please refer to ImageNet Classification with Deep Convolutional Neural Networks in section "Data Augmentation" for the full explanation.
How would I recreate this type of preprocessing?


title: Add 2-D tensor as column of dataframe

body:
My dataframe looks like
   INCIDENT_NUMBER
0  INC000030884498
1  INC000029956111
2  INC000029555353
3  INC000029555338

I also have a 2-D tensor for the above four incidents like
  sample_concatenated_embedding=
  tensor(
  [[ 0.6993, -0.1427, -0.1532,  ...,  0.8386,  0.5151,  0.8906],
  [ 0.7382, -0.8497,  0.1363,  ...,  0.8054,  0.5432,  0.9082],
  [ 0.0835, -0.2431, -0.0815,  ...,  0.8025,  0.5217,  0.9041],
  [-0.0346, -0.2396, -0.5831,  ...,  0.7591,  0.6138,  0.9649]],
  grad_fn=<ViewBackward>)

The embeddings are of size [4, 161280]
I want to insert the tensor in the four consecutive lines of my Dataframe
The end Dataframe should look like
   INCIDENT_NUMBER      embedding
0  INC000030884498      [ 0.6993, -0.1427, -0.1532,  ...,  0.8386,  0.5151,  0.8906]
1  INC000029956111      [ 0.7382, -0.8497,  0.1363,  ...,  0.8054,  0.5432,  0.9082]
2  INC000029555353      [ 0.0835, -0.2431, -0.0815,  ...,  0.8025,  0.5217,  0.9041]
3  INC000029555338      [-0.0346, -0.2396, -0.5831,  ...,  0.7591,  0.6138,  0.9649]

If the tensors would have been series, i could have simply used the below command
 my_dataframe['embedding'] = sample_concatenated_embedding

I could use a for loop and easily insert into dataframe like
 empty_dataframe = pd.DataFrame(columns=['incident','embedding'])

 for item in range(0,4):
     INCIDENT_NUMBER = my_dataframe['INCIDENT_NUMBER'].iloc[item]
     temp_df = pd.DataFrame([[INCIDENT_NUMBER, sample_concatenated_embedding[item]], columns=['incident','embedding'])
     frames = [empty_dataframe, temp_df]
     empty_dataframe = pd.concat(frames)

But for loop would be inefficent. Is there any shorter way to achieve the end goal


title: Setting up Elastic inference accelerator with aws deep learning AMI

body:
I am trying to launch an instance with AWS deep learning AMI with an elastic inference accelerator. I have set the role for the policy for the IAM to connect as described in the documentation "elastic-inference:Connect". But When i try launch the instance i get the error. Initiating launches failure invalid attachment reservation 0 for accelerator type : eia1.medium
I tried adding further permission. I added the following permissions.

AmazonEC2FullAccess
AmazonEC2ContainerRegistryFullAccess
GlobalAcceleratorFullAccess
trial_policy.json (contains the json which is mentioned in the documentation for adding "elastic-inference:Connect")
AmazonDevOpsGuruFullAccess

But is still get the following error.

Launch Failed Invalid attachment reservation 0 for accelerator type :
eia1.medium.
Verifying entitlement to product  Successful
Initiating launches   Failure


title: Mismatched tensor size error when generating text with beam_search (huggingface library)

body:
I'm using the huggingface library to generate text using the pre-trained distilgpt2 model. In particular, I am making use of the beam_search function, as I would like to include a LogitsProcessorList (which you can't use with the generate function).
The relevant portion of my code looks like this:
beam_scorer = BeamSearchScorer(
            batch_size=btchsze,
            max_length=15,  # not sure why lengths under 20 fail
            num_beams=num_seq,
            device=model.device,
        )
j = input_ids.tile((num_seq*btchsze,1))
next_output = model.beam_search(
            j,
            beam_scorer,
            eos_token_id=tokenizer.encode('.')[0],
            logits_processor=logits_processor
        )

However, the beam_search function throws this error when I try to generate using a max_length of less than 20:
~/anaconda3/envs/techtweets37/lib/python3.7/site-packages/transformers-4.4.2-py3.8.egg/transformers/generation_beam_search.py in finalize(self, input_ids, final_beam_scores, final_beam_tokens, final_beam_indices, pad_token_id, eos_token_id)
    326         # fill with hypotheses and eos_token_id if the latter fits in
    327         for i, hypo in enumerate(best):
--> 328             decoded[i, : sent_lengths[i]] = hypo
    329             if sent_lengths[i] < self.max_length:
    330                 decoded[i, sent_lengths[i]] = eos_token_id

RuntimeError: The expanded size of the tensor (15) must match the existing size (20) at non-singleton dimension 0.  Target sizes: [15].  Tensor sizes: [20]

I can't seem to figure out where 20 is coming from: it's the same even if the input length is longer or shorter, even if I use a different batch size or number of beams. There's nothing I've defined as length 20, nor can I find any default. The max length of the sequence does effect the results of the beam search, so I'd like to figure this out and be able to set a shorter max length.