title: Deep Q Learning model seems to halt training

body:
I created a Deep Q Learning bot to play connect 4, however when training it seems to stop working and doesn't make any progress. The DL framework used is pytorch and I coded the game logic myself. As for computing power, I've run it on my PC and Google Colab so the fault shouldn't be due to my machine.
class Board():
    def __init__(self):
        self.rows = 6
        self.columns = 7

        self.game_state = np.zeros((self.rows, self.columns)) # Stores state of board

    def reset(self):
        self.game_state = np.zeros((self.rows, self.columns))

    def action(self, col, player=1):
        active_column = self.game_state[:, col] # Selects column to play in

        placed = False
        for i in range(0, 6):
            i = 5 - i

            if active_column[i] == 0:
                active_column[i] = player
                placed = True# Places piece at lowest possible location, does nothing if no space
                break

        return placed

    def check_win(self):
        # Horionztal and Vertical Checking
        # For horionztal just loop through entire grid, with a counter and current player tracker
        # For vertical hold memory for each column with counter and player tracker

        col_tracker = np.zeros((self.columns, 1))
        col_memory = np.zeros((self.columns, 1))
        for i in range(0, self.rows):
            mem_counter = 0
            current_player = 0

            for j in range(0, self.columns):
                if self.game_state[i, j] == 0:
                    mem_counter = 0
                    col_tracker[j] = 0
                    col_memory[j] = 0
                elif self.game_state[i, j] == current_player:
                    mem_counter += 1
                else:
                    current_player = self.game_state[i, j]
                    mem_counter = 1
                if mem_counter == 4:
                    return current_player, "horizontal"

                if self.game_state[i, j] == col_tracker[j] and self.game_state[i, j] != 0:
                    col_tracker[j] = self.game_state[i, j]
                    col_memory[j] += 1
                elif self.game_state[i, j] == 0:
                    pass
                else:
                    col_tracker[j] = self.game_state[i, j]
                    col_memory[j] = 1

            if i > 2: # Don't bother checking win state if not possible yet
                for j in range(0, self.columns):
                    if col_memory[j] == 4:
                        return col_tracker[j][0], "vert"

        # Digagonal Checking
        # Loop through first two rows, checking every possible diagonal.
        # If one is found return winning player

        for i in range(0, 2): # Loop through the two top rows
            for j in range(0, self.columns):
                mem_counter = 0
                current_player = self.game_state[i, j]
                for k in range(1, 4):
                    try:
                        if self.game_state[i+k, j+k] != current_player:
                            mem_counter = 0
                        else:
                            mem_counter += 1

                            if mem_counter == 3 and current_player != 0:
                                return current_player, "diagonal"
                    except:
                        continue
                mem_counter = 0
                current_player = self.game_state[i, j]
                for k in range(1, 4):
                    try:
                        if self.game_state[i-k, j-k] != current_player:
                            mem_counter = 0

                        else:
                            mem_counter += 1

                            if mem_counter == 3 and current_player != 0:
                                return current_player, "diagonal"
                    except:
                        continue

        return 0, None

    def get_board(self, invert=False):
        if invert:
            board = np.zeros((self.rows, self.columns))
            for i in range(0, self.rows):
                for j in range(0, self.columns):
                    if self.game_state[i, j] == 0:
                        board[i, j] = 0
                    elif self.game_state[i, j] == 1:
                        board[i, j] = 2
                    else:
                        board[i, j] = 1
            board = torch.tensor(board).reshape(42)

        else:
            board = torch.tensor(self.game_state).reshape(42)

        return board

class DQN(nn.Module):
    def __init__(self, input_size, action_size):
        super(DQN, self).__init__()

        self.input_size = input_size
        self.action_size = action_size

        self.fc1 = nn.Linear(42, 256)
        self.fc2 = nn.Linear(256, 256)
        self.fc3 = nn.Linear(256, action_size)

    def forward(self, x):
        x = x.float().to(device)

        x = self.fc1(x)
        x = F.relu(x)

        x = self.fc2(x)
        x = F.relu(x)

        x = self.fc3(x)

        return x

class Agent():
    def __init__(self, input_size, action_size, mem_size, lr, gamma, epsilon, eps_dec, eps_min, batch_size):
        self.input_size = input_size
        self.action_size = action_size
        self.action_space = [i for i in range(self.action_size)]
        self.mem_size = mem_size

        self.epsilon = epsilon
        self.eps_dec = eps_dec
        self.eps_min = eps_min

        self.mem_ptr = 0

        self.lr = lr
        self.gamma = gamma
        self.epsilon = epsilon
        self.eps_dec = eps_dec
        self.eps_min = eps_min

        self.batch_size = batch_size

        self.net = DQN(input_size[0]*input_size[1], action_size)
        self.net.to(device)

        self.state_memory = np.zeros((self.mem_size, 42), dtype=np.float32)
        self.new_state_memory = np.zeros((self.mem_size, 42), dtype=np.float32)
        self.action_memory = np.zeros(self.mem_size, dtype=np.int32)
        self.reward_memory = np.zeros(self.mem_size, dtype=np.float32)
        self.terminal_memory = np.zeros(self.mem_size, dtype=np.bool)

        self.optimizer = optim.Adam(self.net.parameters(), self.lr)
        self.loss = nn.MSELoss()

    def choose_action(self, observation, greedy=False):
        if np.random.random() > self.epsilon or greedy:
            actions = self.net(observation)
            action = torch.argmax(actions).item()
        else:
            action = np.random.choice(self.action_space)

        return action

    def store_memory(self, obs, new_obs, action, reward, terminal):
        index = self.mem_ptr % self.mem_size

        self.state_memory[index] = obs
        self.new_state_memory[index] = new_obs
        self.action_memory[index] = action
        self.reward_memory[index] = reward
        self.terminal_memory[index] = terminal

        self.mem_ptr += 1

    def train(self):
        if self.mem_ptr < self.batch_size:
            return

        self.optimizer.zero_grad()

        max_mem = min(self.mem_ptr, self.mem_size)

        batch = np.random.choice(max_mem, self.batch_size, replace=False)

        batch_index = np.arange(self.batch_size, dtype=np.int32)

        state_batch = torch.tensor(self.state_memory[batch]).to(device)
        new_state_batch = torch.tensor(self.new_state_memory[batch]).to(device)
        action_batch = self.action_memory[batch]
        reward_batch = torch.tensor(self.reward_memory[batch]).to(device)
        terminal_batch = torch.tensor(self.terminal_memory[batch]).to(device)

        q_eval = self.net.forward(state_batch)[batch_index, action_batch]

        q_next = self.net.forward(new_state_batch)

        q_next[terminal_batch] = 0.0

        q_target = reward_batch + int(self.gamma)*torch.max(q_next,dim=1)[0]

        loss = self.loss(q_target, q_eval).to(device)
        loss.backward()
        self.optimizer.step()

        self.epsilon = self.epsilon - self.eps_dec if self.epsilon > self.eps_min \
                       else self.eps_min

class Env():
    def __init__(self):
        self.game = Board()
        self.agent = Agent(input_size=[self.game.rows, self.game.columns], action_size=5,
                          mem_size=10000, lr=0.001, gamma=0.99, epsilon=1.0, eps_dec=5e-4, eps_min=0.01,
                          batch_size=32)
        self.num_wins = 0
        self.num_games = 0

        self.scores = []

    def play(self):
        terminated = False

        while not terminated:
            print(self.game.game_state)
            player_move = int(input("Select a column (0 - 6)"))

            _ = self.game.action(player_move, player=2)

            action = self.agent.choose_action(self.game.get_board())
            _, _, terminated = self.make_action(action)

            print(terminated)

    def train_loop(self, n_episodes):
        for i in range(0, n_episodes):
            self.num_games += 1
            terminated = False

            if np.random.random() > 0.8: # Randomly select training opponent
                opp = "self"
            else:
                opp = "rand"
            score = 0
            while not terminated:
                obs = self.game.get_board()

                action = self.agent.choose_action(obs)

                obs_, reward, terminated = self.make_action(action)

                score += reward
                self.agent.store_memory(obs, obs_, action, reward, terminated)

                if opp == "rand":
                    _ = self.game.action(np.random.choice(self.agent.action_space), player=2)
                else:
                    inv_obs = self.game.get_board(invert=True)
                    action = self.agent.choose_action(inv_obs)

                    _ = self.game.action(action, player=2)

                self.agent.train()

            self.scores.append(score)
            avg_score = np.mean(self.scores[-100:])
            self.game.reset()
            print("Episode number: ", i, "\nScore: ", score, "Average Score: ", avg_score, "Win %: ", self.num_wins / self.num_games * 100)

    def make_action(self, action, player=1):
        valid_move = self.game.action(action, player=player)
        obs_ = self.game.get_board()

        winner, win_type = self.game.check_win()
        reward = 0
        terminated = False
        if winner == 0:
            if valid_move:
                reward += 10 # Move actually worked
            else:
                reward -= 10
        elif winner == 2:
            reward -= 100 # Lost the game
            terminated = True
        else:
            self.num_wins += 1
            if win_type == "vert": # Diagonal wins are worth the most, vertical the least
                reward += 50
            elif win_type == "diagonal":
                reward += 150
            else:
                reward += 100
            terminated = True

        return obs_, reward, terminated

enviroment = Env()
enviroment.train_loop(40)


title: I conduct Super Resolution task, should I normalize the target and input?

body:
I run the LapSRN model this days. The input and target of this model are both images, and I split the RGB channels to train different channels.
And the mean and std of my datasets of different channels are as following:
LR:
tensor([0.1416, 0.0411, 0.0000]) tensor([0.1310, 0.0906, 0.0000])
==> Computing mean and std..
HR_2:
tensor([0.1123, 0.0403, 0.0000]) tensor([0.0967, 0.0871, 0.0000])
==> Computing mean and std..
HR_4:
tensor([0.0999, 0.0403, 0.0000]) tensor([0.0881, 0.0881, 0.0000])

Because there are no informations in blue channel, so I just train on red and green channels.
Now I faced the situation, the green channel can train well and the loss on this channl can descend quicker and lower. I think this is because the mean and std are more similar on green channl. While the mean and std gap very far away on red channel.
So should I Normalize the red channels? If need, how to choose the mean and std for target and input?


title: Repeat 3d tensor's rows in pytorch

body:
I have a BxCxd tensor of coordinates and want to repeat each row in the following way:
 [[[1,0,0],[0,1,0],[0,0,1]]] -> [[[1,0,0],[1,0,0],[0,1,0],[0,1,0],[0,0,1],[0,0,1]]]

In the above example each row is repeated 2 times. What's especially important is the ordering. Each row in the first tensor should appear k times in the second one before the next row appears.
I tried the following code:
print(x.size())
params = x.repeat_interleave(self.k, dim=-1).permute(0,2,1)

In the above snippet, x is of size 32x128x4 before repeat_interleave. With self.k = 64 I would expect the result to be a 32x8192x4 tensor, however the result I am getting is 32x256x128 which does not make sense to me. What am I missing here?


title: Bengio Language Model - FFNN using pytorch

body:
I am given this code https://github.com/pytorch/examples/blob/master/word_language_model/main.py.
And told to build a class Feed forward neural network similar to the recurrent neural network given in the code in the above link and implement the Bengio Language Model. Can anybody please help in this? The code is very complicated and I am unable to understand the same.


title: What is the difference between PyTorch nn.Sequential and nn.ModuleList?

body:
If one wants to create a module consisting of multiple layers from a list, one may proceed in a following way:
# here we fill the python list containing the layers

module = nn.Sequential(*layer_list)
# or
module = nn.ModuleList(layer_list)

Are both ways completely equivalent or there is some difference? Looking in the shell at following example:
model1 = nn.ModuleList([nn.Linear(10, 10) for i in range(3)])
model2 = nn.Sequential(*[nn.Linear(10, 10) for i in range(3)])
model1
# ModuleList(
#  (0): Linear(in_features=10, out_features=10, bias=True)
#  (1): Linear(in_features=10, out_features=10, bias=True)
#  (2): Linear(in_features=10, out_features=10, bias=True)
# )
model2
# Sequential(
#   (0): Linear(in_features=10, out_features=10, bias=True)
#   (1): Linear(in_features=10, out_features=10, bias=True)
#   (2): Linear(in_features=10, out_features=10, bias=True)
# )

I would suppose that both options do essentialy the same.


title: when I run this it executes all perfect but I couldn't able to see this seaborn object

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

My question is can I know where this object is saved or where I can see this plotted object? please help me with this. below it mentions the object at  0x000001FBD3F305B0>. but, I didn't understand where it is?
C:\Users\vinot\.conda\envs\python21\python.exe D:/python/python_work/pythonProject/diabetes.py
<seaborn.axisgrid.PairGrid object at 0x000001FBD3F305B0>

Process finished with exit code 0


title: How to keep a feature in a pytorch model that isn't used for training

body:
I have several timeseries and I am trying to predict a particular feature on them. To achieve this I have built a seq2seq model with teacher forcing that takes 120 points and predicts 30 points (it's an encoder decoder architecture). I wanted to add predictible features that will be passed in the decoder in inference mode such as day number, month number etc.. And I also normalized my target feature.
Let's say my dataframe is like this :
index       target      d1  d2  d3  d4  d5  d6  d7
2015-03-04  0.075269    0   0   0   1   0   0   0
2015-03-05  0.126744    0   0   0   0   1   0   0
2015-03-06  0.038530    0   0   0   0   0   1   0
2015-03-07  0.000000    0   0   0   0   0   0   1

And let's say the predictible features are the dummies of the days of the week.
My training is going fine and well, and the model is apparently converging but I when I want to predict in order to see the results I have an obvious issue. Since the predictible features are passed in the decoder, I need to extrapolate them which is not complicated with the date but because of the conversion into tensors, I loose the index so i can't infer the features. Do you know if it is possible to index tensors in order to recover the date of the last row to extrapolate the features ?
PS: I can't use a trick such as finding the last day in our example because I have more than 100+ rows of dummies features, so I really need to find a way to keep the date but to exclude it from training
Thanks


title: How to gather prediction result on TPU (Pytorch)?

body:
I'm trying to fine-tune my bert-based QA model(PyTorch) with Tpu v3-8 provided by Kaggle. In the validation process I used a ParallelLoader to make predictions on 8 cores at the same time. But after that I don't know what should I do to gather all the results back from each core(and in the correct order corresponding to dataset), in order to calculate the overall EM & F1 score. Can anybody help?
Code:
def _run():
    MAX_LEN = 192 # maximum text length in the batch (cannot have too high due to memory constraints)
    BATCH_SIZE = 16 # batch size (cannot have too high due to memory constraints)
    EPOCHS = 2 # number of epochs
    train_sampler = torch.utils.data.distributed.DistributedSampler(
          tokenized_datasets['train'],
          num_replicas=xm.xrt_world_size(), # tell PyTorch how many devices (TPU cores) we are using for training
          rank=xm.get_ordinal(), # tell PyTorch which device (core) we are on currently
          shuffle=True
    )

    train_data_loader = torch.utils.data.DataLoader(
        tokenized_datasets['train'],
        batch_size=BATCH_SIZE,
        sampler=train_sampler,
        drop_last=True,
        num_workers=0,
    )

    valid_sampler = torch.utils.data.distributed.DistributedSampler(
          tokenized_datasets['validation'],
          num_replicas=xm.xrt_world_size(),
          rank=xm.get_ordinal(),
          shuffle=False
    )

    valid_data_loader = torch.utils.data.DataLoader(
        tokenized_datasets['validation'],
        batch_size=BATCH_SIZE,
        sampler=valid_sampler,
        drop_last=False,
        num_workers=0
    )

    device = xm.xla_device() # device (single TPU core)
    model = model.to(device) # put model onto the TPU core
    xm.master_print('done loading model')
    xm.master_print(xm.xrt_world_size(),'as size')

    lr = 0.5e-5 * xm.xrt_world_size()
    optimizer = AdamW(model.parameters(), lr=lr) # define our optimizer

    for epoch in range(EPOCHS):
        gc.collect()
        # use ParallelLoader (provided by PyTorch XLA) for TPU-core-specific dataloading:
        para_loader = pl.ParallelLoader(train_data_loader, [device])
        xm.master_print('parallel loader created... training now')
        gc.collect()

        call training loop:
        train_loop_fn(para_loader.per_device_loader(device), model, optimizer, device, scheduler=None)
        del para_loader
        model.eval()
        para_loader = pl.ParallelLoader(valid_data_loader, [device])
        gc.collect()

        model.eval()
        # call evaluation loop
        print("call evaluation loop")
        start_logits, end_logits = eval_loop_fn(para_loader.per_device_loader(device), model, device)


title: random tensor with range pytorch

body:
I want to create a random tensor that would have random values based on the range I specify
context:- I am implementing label smoothing for patchGAN training say fake_op (discriminator output on fake image) and other real_op (discriminator output on real image)
I want to create one tensor with the same shape as fake_op with random values between 0-0.3
and one tensor with the same shape as real_op with random values between 0.7-1


title: How to define number of classes in pytorch geometric

body:
I'm new to graph neural network, I'm trying to use pytorch-geometric to read an sdf file to make predictions,
I have the following dataset as my inputs
Data(edge_attr=[52, 6], edge_index=[2, 52], x=[24, 82])
Data(edge_attr=[60, 6], edge_index=[2, 60], x=[26, 82])
Data(edge_attr=[50, 6], edge_index=[2, 50], x=[23, 82])
Data(edge_attr=[48, 6], edge_index=[2, 48], x=[22, 82])
...

I want my target prediction looks like
14
17
22
20
...

as you can observe, the target is smaller or equal to the number of nodes in input x, where x = [num_nodes, num_node_features].
I'm wondering how can I build a graph neural network for such a dataset?
should I resize my inputs x or something else?
Thank you!


title: Missing dll while importing pytorch

body:
I am getting the following error while importing pytorch
"OSError: [WinError 126] The specified module could not be found. Error loading "C:\Users\Nabanita Paul\AppData\Roaming\Python\Python38\site-packages\torch\lib\caffe2_detectron_ops.dll" or one of its dependencies."  When I check it in the specific  folder, the dll. is available there. Can somebody help me in solving this error.


title: PyTorch LSTM with varying time steps

body:
Is it possible to create an LSTM in PyTorch where the time steps are varying? For example, heights where measurements are taken at various times. The data might look like this:

Person id
Inches tall
Date

1
12
2020-01-01

1
15
2020-03-09

1
32
2020-04-01

1
40
2021-01-01

2
38
2020-05-20

...

Given a history of 4 heights and dates, I'd like to get a prediction of a height at a particular date:
lstm(torch.tensor[(21, '2020-01-02'),
                  (23, '2020-02-02'),
                  (29, '2020-04-12'),
                  (41, '2020-12-02')],
     '2022-01-01')

I passed in '2022-01-01' after the 4 tuples above because I want to get the prediction for my height on this date.
Is this possible? How can I do this?


title: PyTorch supported with py_version 3.6 or earlier only?

body:
SageMaker Python SDK supports Python 3.8 (according to this and this)
However, when creating a PyTorch Estimator (using this), py_version argument can only be 'py2' or 'py3'

py_version (str) – Python version you want to use for executing your model training code. One of ‘py2’ or ‘py3’

Is it possible that we can use the SageMaker Python SDK with Python 3.8, but have to downgrade our PyTorch code back to 3.6 to run a training job for it?


title: How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python?

body:
I downloaded MNIST dataset and plot the first image of dataset with imshow function in grayscale.
Here is my code.
import numpy as np
import matplotlib.pyplot as plt
import torch
import torchvision
from torchvision import transforms

# MNIST dataset
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = torchvision.datasets.MNIST(root='./data',
                                           train=True,
                                           download=True,
                                           transform=transform)

# Change data type : torch.Tensor -> numpy array
X = train_dataset.train_data.numpy()
y = train_dataset.train_labels.numpy()
print('Data size: ' + str(X.shape))

num_samples, height, width = X.shape
dim = height * width

# Visualize MNIST data (1st digit)
plt.figure(1)
plt.imshow(X[0],cmap='gray_r')
plt.colorbar()
plt.title('1st image label: 5')

And I got this figure

After reshape the 2-dimensional image vector to the one-dimensional vector, I would like to plot
the figure like this.

The x-axis represents pixel number, and the y-axis represents its pixel value as a grayscale color, and
the plot is like that, sort of spectrum.
I know how to reshape the vector, but I don't know how to make the plot.
Can I make the plot like the second one with function in matplotlib module? or in somewhere?


title: How to batch sequence to sequence mapping in an autoencoder?

body:
I am having trouble to understand what kind of input / output mapping would make most sense if I have a vanilla autoencoder structure (unlike LSTM AE).
I have 6 input sequences (sine signals) that I want to map to two output quasi sine signals. The full dataset has the shape (8, 2000000).
Inputs:

Outputs:

How should one batch look like? (One period is 400 time steps long.)

Do I take 400 steps of each signal and concatenate them? (this would make one very long vector)
Do I take 400 steps of each signal and input each signal separately (mapping it to the same output segment)
Do I take 1 step of each signal and concatenate them?
Do I take X step of each signal, concatenate them, and slide a moving window on the data?


title: Initialize weights in pytorch with customized weight?

body:
I want to initialize the weights of my neural network with parameters samples from a specific distribution, that is not already present in nn.init module. Let's say for example a beta distribution.
I saw that you can use the method described there: Custom weight initialization in PyTorch
But there you need to use the nn.init. What is an elegant way to initialize weights from a distribution that I specify


title: PyTorch: Reinforcement Learning + OpenAI is not learning

body:
I am trying to build a model which is going to predict a BUY or SELL signal from stocks using reinforcement learning with Actor-Critic policy.
I'm new to machine learning and PyTorch in general and in my research of the problem I've realized that I am not learning anything in a way... What I mean by that is, if I am just watching how the wandb graphs are evolving then it looks like am learning something.

also, I am saving and loading the model by these 2 functions:

    def save_model(self, path: str, name: str):
        torch.save(self.actor.state_dict(), os.path.join(path, f"{name}_actor"))
        torch.save(self.critic.state_dict(), os.path.join(path, f"{name}_critic"))

    def load_model(self, path: str, name: str):
        self.actor.load_state_dict(torch.load(os.path.join(path, f"{name}_actor")))
        self.critic.load_state_dict(torch.load(os.path.join(path, f"{name}_critic")))

But what I found really strange is that in my select action function I will ALWAYS select action 1 (sell) out of the 2 (buy or sell). The only time when the action is going to differ from 1 is when random_for_egreedy is greater then epsilon
    def select_action(self, state, epsilon):
        random_for_egreedy = torch.rand(1)[0]
        if random_for_egreedy > epsilon:
            with torch.no_grad():
                state = torch.Tensor(state.values).to(device)
                actor_action = self.actor(state)
                action = torch.argmax(actor_action)
                action = action.item()
        else:
            action = self.gym.action_space.sample()
        return action

This is my optimize function:

    def optimize(self):
        if len(self.memory) < self.config.batch_size:
            return
        self.optimizer_actor.zero_grad()
        self.optimizer_critic.zero_grad()

        state, action, new_state, reward, done = self.memory.sample(batch_size=self.config.batch_size)

        state = torch.Tensor(np.array(state)).to(device)
        new_state = torch.Tensor(np.array(new_state)).to(device)
        reward = torch.Tensor(reward).to(device)
        action = torch.LongTensor(action).to(device)
        done = torch.Tensor(done).to(device)
        dist = torch.distributions.Categorical(self.actor(state))
        advantage = reward + (1 - done) * self.config.gamma * self.critic(new_state) - self.critic(state)

        critic_loss = advantage.pow(2).mean()
        self.optimizer_critic.zero_grad()
        critic_loss.backward()
        self.optimizer_critic.step()

        actor_loss = -dist.log_prob(action) * advantage.detach()
        self.optimizer_actor.zero_grad()
        actor_loss.mean().backward()
        self.optimizer_actor.step()

        wandb.log({"Actor Loss": actor_loss.mean(), "Critic Loss": critic_loss})

And here is my training loop:

for ep in range(conf.num_episode):
    state = env.reset()
    step = 0
    # qnet_agent.reset_running_loss()

    wandb.log({"Episode": ep})
    if ep % save_after_episode == 0:
        qnet_agent.save_model("checkpoints", model_save_name)

    while True:
        wandb.log({"step": step})
        step += 1
        frames_total += 1

        epsilon = calculate_epsilon(frames_total)

        action = qnet_agent.select_action(state, epsilon)

        wandb.log({"last action": action})

        new_state, reward, done, info = env.step(action)
        wandb.log({"Current profit": info['current_profit']})

        wandb.log({"Total profit": info['total_profit']})
        wandb.log({"reward": reward})

        memory.push(state, action, new_state, reward, done)
        qnet_agent.optimize()
        state = new_state

        if done:
            steps_total.append(step)
            break

Could any of you tell me if I missed something? Or I am doing something wrong?


title: winError 127 the specified procedure can not be found when trying to import torch-geometric on anaconda

body:
I try to install torch-geometirc on my machine, but first I stalled cuda toolkit (11.0) and torch (1.7.1) that I need to detect RTX3090 graphic card, other versions didn't work for me.
so now:
python -c "import torch; print(torch.__version__)"
1.7.1

and
python -c "import torch; print(torch.version.cuda)"
11.0

but when I install torch-geometric dependencies and then torch-geometric itself Iget an error
[WinError 127] La procédure spécifiée est introuvabl** (traduction: winError 126 the specified procedure can not be found)".

thanks for your help!


title: PyTorch Iterative Pruning

body:
I have been trying to implement pruning using " `torch.nn.utils.prune" according to the tutorial here. For experimenting purposes, I am using LeNet-300-100 dense neural network with MNIST dataset which can be accessed here.
# Pruning multiple parameters-
# Prune multiple parameters/layers in a given model-
for name, module in best_model.named_modules():
    # prune 20% of weights/connections in for all hidden layaers-
    if isinstance(module, torch.nn.Linear) and name != 'output':
        prune.l1_unstructured(module = module, name = 'weight', amount = 0.2)

    # prune 10% of weights/connections for output layer-
    elif isinstance(module, torch.nn.Linear) and name == 'output':
        prune.l1_unstructured(module = module, name = 'weight', amount = 0.1)

# Sanity check: verify that all of the defined pruning exists as masks-
print(dict(best_model.named_buffers()).keys())
# dict_keys(['fc1.weight_mask', 'fc2.weight_mask', 'output.weight_mask'])

I then train the model with the following code snippet:
# Training loop-
for epoch in range(num_epochs):
    running_loss = 0.0
    running_corrects = 0.0

    if loc_patience >= patience:
        print("\n'EarlyStopping' called!\n")
        break

    running_loss, running_corrects = train_model(best_model, train_loader)

    epoch_loss = running_loss / len(train_dataset)
    epoch_acc = running_corrects.double() / len(train_dataset)
    # epoch_acc = 100 * running_corrects / len(trainset)
    # print(f"\nepoch: {epoch + 1} training loss = {epoch_loss:.4f}, training accuracy = {epoch_acc * 100:.2f}%\n")

    running_loss_val, correct, total = test_model(best_model, test_loader)

    epoch_val_loss = running_loss_val / len(test_dataset)
    val_acc = 100 * (correct / total)
    # print(f"\nepoch: {epoch + 1} training loss = {epoch_loss:.4f}, training accuracy = {epoch_acc * 100:.2f}%, val_loss = {epoch_val_loss:.4f} & val_accuracy = {val_acc:.2f}%\n")

    print(f"\nepoch: {epoch + 1} training loss = {epoch_loss:.4f}, training accuracy = {epoch_acc * 100:.2f}%, val_loss = {epoch_val_loss:.4f} & val_accuracy = {val_acc:.2f}%")

    curr_params = count_params(best_model)
    print(f"Number of parameters = {curr_params}\n")

    percentage_pruned = ((orig_params - curr_params.numpy()) / orig_params * 100).numpy()

    # Code for manual Early Stopping:
    # if np.abs(epoch_val_loss < best_val_loss) >= minimum_delta:
    if (epoch_val_loss < best_val_loss) and np.abs(epoch_val_loss - best_val_loss) >= minimum_delta:
        # print(f"epoch_val_loss = {epoch_val_loss:.4f}, best_val_loss = {best_val_loss:.4f}")

        # update 'best_val_loss' variable to lowest loss encountered so far-
        best_val_loss = epoch_val_loss

        # reset 'loc_patience' variable-
        loc_patience = 0

        print(f"\nSaving model with lowest val_loss = {epoch_val_loss:.4f}")

        # Save trained model with validation accuracy-
        # torch.save(model.state_dict, f"LeNet-300-100_Trained_{val_acc}.pth")
        torch.save(best_model.state_dict(), f"LeNet-300-100_{percentage_pruned:.2f}.pth")

    else:  # there is no improvement in monitored metric 'val_loss'
        loc_patience += 1  # number of epochs without any improvement

The output is:

epoch: 1 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
Saving model with lowest val_loss = 0.0980
epoch: 2 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
epoch: 3 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
epoch: 4 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
epoch: 5 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
epoch: 6 training loss = 0.0266, training accuracy = 99.11%, val_loss = 0.0980 & val_accuracy = 97.94%
Number of parameters = 266610
'EarlyStopping' called!

This shows that:

The model is "frozen". It doesn't learn anything since and val_accuracy and val_loss values stay the same.

The number of parameters stays the same and therefore, the define pruning doesn't seem to be having any effect.

How can I fix them?


title: CUDA out of memory error when calling PyTorch train script in a loop from another python script

body:
I'm trying to run the same pytorch training script with different arguments (argparse) from another python script. I'm using os.system() for the same.
Here's what I'm trying to do -
train.py = > the script which contains the train-loop.
runner.py => the file which runs the train script in a loop.
# runner.py
for hp in hyperparams:
    os.system(f"CUDA_VISIBLE_DEVICES=1 python train.py --arg1 hp")

A few models get trained but I eventually end up getting a CUDA out of memory error. For instance, if there were 10 models, it will successfully train 8 and then give a CUDA error for 9 and 10.
My guess is that the GPU memory is not being cleared after every loop. What can I do to mitigate this?


title: Cuda running out of memory despite it being available

body:
I am trying to run a GAN over some images that are of size 256x256. However, whenever I begin training I receive this error:
RuntimeError: CUDA out of memory. Tried to allocate 2.19 GiB (GPU 0; 10.00 GiB total capacity; 437.15 MiB already allocated; 6.38 GiB free; 1.51 GiB reserved in total by PyTorch)

Im not sure how to solve it. It seems as though there is plenty of memory, so I am confused as to why this would happen.


title: extracting subtensor from a tensor according to an index tensor

body:
I have this tensor:
tensor([[[1, 2],
         [3, 4]],
        [[5, 6],
         [7, 8]]])

and I have this index tensor:
tensor([0, 1])

and what I want to get is the subtensors according to dim 1 and the corresponding indices in the index tensor, that is:
tensor([[1, 2],
        [7, 8]])

tried to use torch.gather() function and advanced indexing with no success, can anyone help?


title: How to use ImageFolder with list of images for train and test sets?

body:
I am working on Stanford Dog Dataset that provides 120 classes of images and a list of 12000 image names for training and about 8000+ names for testing.
How can I use ImageFolder to load train and test sets based on the image names? Below is a how the list looks like.
Train Set:
       [array(['n02085620-Chihuahua/n02085620_4441.jpg'], dtype='<U38')],
       [array(['n02085620-Chihuahua/n02085620_1502.jpg'], dtype='<U38')],
       ...,
       [array(['n02116738-African_hunting_dog/n02116738_6754.jpg'], dtype='<U48')],
       [array(['n02116738-African_hunting_dog/n02116738_9333.jpg'], dtype='<U48')],
       [array(['n02116738-African_hunting_dog/n02116738_2503.jpg'], dtype='<U48')]],
      dtype=object)

Test Set:
array([[array(['n02085620-Chihuahua/n02085620_2650.jpg'], dtype='<U38')],
       [array(['n02085620-Chihuahua/n02085620_4919.jpg'], dtype='<U38')],
       [array(['n02085620-Chihuahua/n02085620_1765.jpg'], dtype='<U38')],
       ...,
       [array(['n02116738-African_hunting_dog/n02116738_3635.jpg'], dtype='<U48')],
       [array(['n02116738-African_hunting_dog/n02116738_2988.jpg'], dtype='<U48')],
       [array(['n02116738-African_hunting_dog/n02116738_6330.jpg'], dtype='<U48')]],
      dtype=object)


title: Does dissecting a Pytorch model lower memory usage?

body:
Suppose I have a Pytorch autoencoder model defined as:
class ae(torch.nn.Module):
   def __init__(self, z_dim, n_channel=3, size_=8):
       super(ae, self).__init__()
       self.encoder = Encoder()
       self.decoder = Decoder()

   def forward(self, x):
       z = self.encoder(x)
       x_reconstructed = self.decoder(z)
       return z, x_reconstructe

Now instead of defining an specific ae model and loading it, I can use the Encoder and Decoder code directly in my code. I know the number of total parameters wouldn't change but here's my question: since these two models are now separated, is it possible that the code can run on lower ram/gpu-memory? Does separating them means they do not need to be loaded into memory at once?
(Note that autoencoder is just an example, My question is really about any models that consists of several sub-modules).


title: Problem with freezing pytorch model - requires_grad is always true

body:
I have tried to freeze part of my model but it does not work. Gradient computation is still enabled for each layer. Is that some sort of bug or am I doing something wrong? :)
model = models.resnet18(pretrained=True)

# To freeze the residual layers
for param in model.parameters():
    param.require_grad = False
for param in model.fc.parameters():
    param.require_grad = True

# Replace last layer
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, 2)
model.fc = nn.Dropout(0.5)

# Find total parameters and trainable parameters
total_params = sum(p.numel() for p in model.parameters())
print(f'{total_params:,} total parameters.')
# >>> 21,284,672 total parameters.

total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f'{total_trainable_params:,} training parameters.')
# >>> 21,284,672 training parameters.


title: Fake a class to be python python-pickleable using dill

body:
I am using pytorch which in turn relies on the python native pickle module for multiprocessing.
I have a not pickleable class since it holds a few lambdas (I don't want to change that).
Since I can't change pytorch's implementation of multiprocessing (to support dill), I would like my class to be python native pickleable.
I wondered whether I could change my class so that it is pickleable from the outside but internally just refers to dill?
class NonPickleableClass:
    #...
    def __getstate__(self):
        # HACK: make it pickleable with dill :D
        return dill.dumps(self.__dict__)

    def __setstate__(self, state):
        self.__dict__.update(dill.loads(state))

while it works nicely in my unit test, it breaks down with Recursion limit exceeded in pytorch (don't know exactly why?).
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 955, in _batch_appends
    save(x)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 692, in save_reduce
    save(args)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 691, in save_reduce
    save(func)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 536, in save
    self.framer.commit_frame()
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/site-packages/dill/_dill.py", line 1444, in save_function
    pickler.save_reduce(_create_function, (obj.__code__,
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 901, in save_tuple
    save(element)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 220, in commit_frame
    if f.tell() >= self._FRAME_SIZE_TARGET or force:
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 691, in save_reduce
    save(func)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 536, in save
    self.framer.commit_frame()
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/site-packages/dill/_dill.py", line 941, in save_module_dict
    StockPickler.save_dict(pickler, obj)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 220, in commit_frame
    if f.tell() >= self._FRAME_SIZE_TARGET or force:
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 971, in save_dict
    self._batch_setitems(obj.items())
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 1002, in _batch_setitems
    save(v)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 931, in save_list
    self._batch_appends(obj)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 955, in _batch_appends
    save(x)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/site-packages/dill/_dill.py", line 1444, in save_function
    pickler.save_reduce(_create_function, (obj.__code__,
RecursionError: maximum recursion depth exceeded while calling a Python object
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 691, in save_reduce
    save(func)
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 536, in save
    self.framer.commit_frame()
  File "/home/AD/muellelu/xconda3/envs/hand_seg/lib/python3.9/pickle.py", line 220, in commit_frame
    if f.tell() >= self._FRAME_SIZE_TARGET or force:
RecursionError: maximum recursion depth exceeded while calling a Python object
RecursionError: maximum recursion depth exceeded while calling a Python object


title: LSTM - How to structure Input Data (PyTorch)

body:
for a university project I am trying to come up with a LSTM network, that predicts stock market prices. Although I believe I understand the basic functioning of a LSTM, I am struggling with the correct structure of my input data, for the PyTorch LSTM module.
The features of my input data are the lagged prices (e.g. last 3 days). Let's assume I have the stock prices of the last 9 days of a company.
Then my data would look the following way:
Price Data:
[8,
10,
2,
3,
4,
1,
4,
1,
3]
Features:
[[8, 10, 2],
[3, 4, 1],
[4, 1, 3]]
My question now is, which dependency does a LSTM try to model? Is it between the features, i.e. the columns in each row, or is it the dependency between the rows? To my understanding it is the former, but then how would the whole thing work if I would want to add a feature that is not sequentially related to the other features. For example, I might want to add some weather data as a 4th feature, i.e. as the fourth column. Then to me, it doesn't make sense to call a row a sequence anymore.
It's so confusing, because in many guides for LSTMs the features are actually sequentially (from column to column such as lagged prices), while for others the features of one row are not sequential, e.g. "Opening, Close, Low, High, Volume" of a stock.
In this PyTorch Tutorial (https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html?highlight=lstm), the sequence is a sentence, and each row represents a word through 3 features. The features themselves, are not related sequentially. So in that example, it doesn't make sense that the model "looks" for dependencies between the features of a word, but rather between the rows (words). But why do then so many LSTMs use lagged stock prices as features? Wouldn't then only the 1 price suffice for each row, as the dependancy is between rows not columns?
Maybe to sum the whole question up, do the features within a row have to be a sequence, or do the rows make up a sequence?
I hope this makes at least a little bit of sense. Thanks!


title: how to pick the right distribution of the prior of the latent variables in VAE?

body:
I'd like to ask about deciding the right prior of the latent variables in VAE. Most importantly, I am curious about the key points to be considered while picking the right distribution (could be the data structure, model structure, etc...), especially on the colored (3 channeled) datasets. I'd like to move on with a small scenario that may ease answering the questions.
Scenario: Firstly, if I would have, say, instances that are 8-bit images that have [3, 27, 27] dimensions in the dataset. And, the instance values lie between [0., 1.). Moreover, if I would put it in a VAE setup that is constructed by convolutional encoder and decoder. The encoder and the decoder may have a structural design like this;
# Simple Conv Encoder
self.encoder = nn.Sequential(
    nn.Conv2d(self.input_channels, 36, 4, 1, 0),
    nn.LeakyReLU(0.2),
    nn.MaxPool2d(2, 2),
    nn.Conv2d(36, 48, 3, 1, 0),
    nn.LeakyReLU(0.2),
    nn.MaxPool2d(2, 2),
    nn.Conv2d(48, 32, 3, 1, 0)
)
# Simple Conv Decoder
self.decoder = nn.Sequential(
    nn.ConvTranspose2d(16, 36, 3, 1, 0),
    nn.ReLU(),
    nn.Upsample(scale_factor=2),
    nn.ConvTranspose2d(36, 48, 3, 1, 0),
    nn.ReLU(),
    nn.Upsample(scale_factor=2),
    nn.ConvTranspose2d(48, 100, 4, 1, 0),
    nn.Sigmoid()
)

Since ReLU would imply max{0, input} to the neurons,

I wonder what kind of prior picking would be the correct choice?
Additionally, would discretize mixture of a logistic type of loss be coherent with this type of architecture?
More importantly, does sigmoid satisfy the need for this kind of loss? what additional losses can be applied besides what I've mentioned?

I wanted to keep this in a scenario since all of those theoretical considerations blow my mind and cause me to stuck at that point :(
Thanks in advance!


title: How to replace torch.sparse with other pytorch function?

body:
I would like to replace the torch.sparse function using the other Pytorch function.
i = torch.LongTensor([[0, 1, 1], [2, 0, 2]])
v = torch.FloatTensor([3, 4, 5])
out1 = torch.sparse.FloatTensor(i, v, torch.Size([2,3])).to_dense()
out2 = ???
out1 == out2
> tensor(True)

Background:
I'm converting a Pytorch model to CoreML, but the sparse_coo_tensor operator defined in the torch.norm function is not implemented with CoreMLTools.
A few people have trouble with this problem, but CoreMLTools is still unsupported. So I'd like to replace it without the operator used in torch.sparse.FloatTensor.
I have tried torch.sparse_coo_tensor but it was not supported.
I have created a simple colaboratory notebook that reproduces this. Please test it using the following colab.
https://colab.research.google.com/drive/1TzpeJqEcmCy4IuXhhl6LChFocfZVaR1Q?usp=sharing
I've asked about different operators on stackoverflow before, so please refer to that.
How to replace torch.norm with other pytorch function?


title: AttributeError: 'BoundingBoxesOnImage' object has no attribute 'items'

body:
I am solving an object detection problem. My code works in Jupyter Notebook without any problems.
But when I run it in Colab  there is an AttributeError in the next piece of code (it is a part of a custom Dataset class):
from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage

bb = BoundingBoxesOnImage([BoundingBox(*box_coords)], shape=image.size)
image, box = self.seq(image=np.array(image), bounding_boxes=bb)
# convert box coordinates back to list
new_box_coords = box.items[0].coords.reshape(4).tolist()

where seq is an augmentation sequence:
seq = imgaug.augmenters.Sequential([
                imgaug.augmenters.flip.Fliplr(0.5),
                imgaug.augmenters.Affine(rotate=(15, -15))
                ])

The error is:
  'BoundingBoxesOnImage' object has no attribute 'items'.

But I found an attribute items in imgaug documentation.
I checked versions of imgaug library: it is  0.4.0 both locally and in Colab.
Why Colab does not know this attribute? How can I fix this problem?


title: Converting Keras layers to PyTorch

body:
I have this CNN Layer in Keras:
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shapnumpye=(27, 27, 3)))
model.add(BatchNormalization())
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.4))

How can I write it in PyTorch?
Thank you!


title: In Pytorch how to slice tensor across multiple dims with BoolTensor masks?

body:
I want to use BoolTensor indices to slice a multidimensional tensor in Pytorch. I expect for the indexed tensor, the parts where the indices are true are kept, while the parts where the indices are false are sliced out.
My code is like
import torch
a = torch.zeros((5, 50, 5, 50))

tr_indices = torch.zeros((50), dtype=torch.bool)
tr_indices[1:50:2] = 1
val_indices = ~tr_indices

print(a[:, tr_indices].shape)
print(a[:, tr_indices, :, val_indices].shape)

I expect a[:, tr_indices, :, val_indices] to be of shape [5, 25, 5, 25], however it returns [25, 5, 5]. The result is
torch.Size([5, 25, 5, 50])
torch.Size([25, 5, 5])

I'm very confused. Can anyone explain why?


title: Pytorch tensor pickling inconsistent between runs

body:
I'm trying to unpickle a pytorch tensor, but pickling it back yields different results across runs:
>>> import pickle

>>> tensor1 = pickle.load(f) # I cannot reproduce the issue with some minimal manually-created tensor, only with this specific file
>>> tensor2 = pickle.load(f)
>>> pickled_tensor1 = pickle.dumps(tensor1)
>>> pickled_tensor2 = pickle.dumps(tensor2)
>>> pickled_tensor1 == pickled_tensor2
False

Below are the values of pickled_tensor1 and pickled_tensor2 respectively:
b'\x80\x04\x95\x98\x01\x00\x00\x00\x00\x00\x00\x8c\x0ctorch._utils\x94\x8c\x12_rebuild_tensor_v2\x94\x93\x94(\x8c\rtorch.storage\x94\x8c\x10_load_from_bytes\x94\x93\x94B\r\x01\x00\x00\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03.\x80\x02}q\x00(X\x10\x00\x00\x00protocol_versionq\x01M\xe9\x03X\r\x00\x00\x00little_endianq\x02\x88X\n\x00\x00\x00type_sizesq\x03}q\x04(X\x05\x00\x00\x00shortq\x05K\x02X\x03\x00\x00\x00intq\x06K\x04X\x04\x00\x00\x00longq\x07K\x04uu.\x80\x02(X\x07\x00\x00\x00storageq\x00ctorch\nFloatStorage\nq\x01X\x0f\x00\x00\x00140382183041680q\x02X\x03\x00\x00\x00cpuq\x03K\x04Ntq\x04Q.\x80\x02]q\x00X\x0f\x00\x00\x00140382183041680q\x01a.\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x94\x85\x94R\x94K\x00K\x02K\x02\x86\x94K\x02K\x01\x86\x94\x89\x8c\x0bcollections\x94\x8c\x0bOrderedDict\x94\x93\x94)R\x94t\x94R\x94.'
b'\x80\x04\x95\x98\x01\x00\x00\x00\x00\x00\x00\x8c\x0ctorch._utils\x94\x8c\x12_rebuild_tensor_v2\x94\x93\x94(\x8c\rtorch.storage\x94\x8c\x10_load_from_bytes\x94\x93\x94B\r\x01\x00\x00\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03.\x80\x02}q\x00(X\x10\x00\x00\x00protocol_versionq\x01M\xe9\x03X\r\x00\x00\x00little_endianq\x02\x88X\n\x00\x00\x00type_sizesq\x03}q\x04(X\x05\x00\x00\x00shortq\x05K\x02X\x03\x00\x00\x00intq\x06K\x04X\x04\x00\x00\x00longq\x07K\x04uu.\x80\x02(X\x07\x00\x00\x00storageq\x00ctorch\nFloatStorage\nq\x01X\x0f\x00\x00\x00140382172016592q\x02X\x03\x00\x00\x00cpuq\x03K\x04Ntq\x04Q.\x80\x02]q\x00X\x0f\x00\x00\x00140382172016592q\x01a.\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x94\x85\x94R\x94K\x00K\x02K\x02\x86\x94K\x02K\x01\x86\x94\x89\x8c\x0bcollections\x94\x8c\x0bOrderedDict\x94\x93\x94)R\x94t\x94R\x94.'

My question is why is it happening and how can I prevent this?
I am using Python 3.8; pytorch 1.7.0
Cheers, Hlib.


title: How to build a CNN model to get face embeddings

body:
I have to build a model from scratch for ID-Selfie matching purposes. I need to build this model from scratch. My basic approach is to feed 2 images to the CNN model and get face embeddings. Then pass those embeddings to a function which calculates cosine similarity and decides if the images are of the same person or not.
How do I go around building such a model? And how do I train it and evaluate it's accuracy?


title: How to Tensorize loss of multiple 3D Keypoints

body:
I have a tensor of ground truth values of 3D points of G=[18000x3], and an output from my network of the same size O=[18000x3].

I need to compute a loss so that I basically have the square root of the distance between each 3D point, summed over all keypoints and normalized over 18000. How do I write this efficiently?


title: A strange problem with torch.optim: the variable changes, but the difference between the old and the updated variable is shown to be zero

body:
I stumbled upon a rather weird issue while debugging a larger code in PyTorch. A simple instance to reproduce the issue is here: https://colab.research.google.com/drive/11kBaxMOxN9i0X1vtaX47Yz7rkkHz7Kpu

As you can see, the variable x gets updated in every epoch (see that their norms are different), however, the norm of their difference (rel_diff) turns out to be zero. Any help is deeply appreciated.


title: Calculate loss (MSE) on neural network output with different measurement units

body:
I am training a neural network with pytorch. The input/features is 6-dimensional and of the same unit of measurement. The outputs/labels is 7-dimensional with 2 different unit of measurements (in my case mm and deg). How can I compute a MSE loss on this output? The problem is that 1 deg doesn't correspond to 1 mm. So even if I normalize my data, the network cannot differentiate between the units and treats degrees like mms. I know that some people use empirical weight factors to scale the different labels to get better results. Is there a better way?

I know I could train simply two different networks with one output being in mm and the other in deg.

I am thankful for any informed advise!


title: Neural network in pytorch

body: I wanna create a Neural Network in PyTorch, that will have 2 inputs and 3 outputs with 1 hidden layer. The two inputs will be float numbers that represents features of an image and 3 outputs will be real numbers between 0 and 1. For example output (1, 0, 0) would mean that it is square and (0,1,0) would mean it is rectangle. Any idea how to do it in pytorch?


title: Back prop graph broken in pytorch

body:
I am trying to build a  model which finds the best gain value for kaiming initialisation on a given tensor shape.But it seems my backprop is broken.Any help would be appreciated.Thank You

class Gain(nn.Module):
  def forward(self, a):
    return math.sqrt((1 + a**2)/2)

class Kaiming_init(nn.Module):
  def __init__(self, b = math.sqrt(3.), fan_out = False):
    super().__init__()
    self.gain = Gain()
    self.b = b
    self.fan_out = fan_out
  def forward(self, x, a):
    nf, ni, *_ = x.shape
    rec_fs = x[0, 0].numel()
    inp = nf*rec_fs if self.fan_out else ni*rec_fs
    inp = self.gain(a)/math.sqrt(inp)
    limit = self.b*inp
    x.data.uniform_(-float(limit), float(limit))
    return x

class Stats(nn.Module):
  def forward(self, x):
    return torch.tensor([x.mean(), x.std()])

class Init_Inference_Layer(nn.Module):
  def __init__(self, initialiser, stat_inf):
    super().__init__()
    self.initialiser = initialiser
    self.stat_inf = stat_inf
  def forward(self, x, a):
    data = [self.initialiser(nn.Conv2d(int(nf), int(ni), int(k)).weight, a) for (nf, ni, k), a in zip(x, a)]
    stat = [self.stat_inf(data) for data in data]
    return torch.tensor(stat, requires_grad = True)

class Init_Model(nn.Module):
  def __init__(self):
    super().__init__()
    self.lin = nn.Linear(3, 1)
    self.relu = nn.ReLU()
    self.initialiser = Kaiming_init()
    self.stat_inf = Stats()
    self.inf_layer = Init_Inference_Layer(self.initialiser, self.stat_inf)

  def __call__(self, x):
    self.inp = x
    a = self.relu(self.lin(x))
    outs = self.inf_layer(self.inp, a)
    return outs

But when use mae loss and back propagate; i get 'None' for the models linear layer which i wanted to train.Pytorch suggested that my back prop graph is disconnected somewhere.Please help me.I couldnt configure how and where the graph is broken


title: How to design the last layer of my neural network?

body:
I have a neural network with this design:

class CONTROLLER(nn.Module):

def __init__(self, numb_features ,k):
    super(CONTROLLER, self).__init__()
    self.fc1 = nn.Linear(numb_features, 100)
    self.fc2 = nn.Linear(100, 100)
    self.fc3 = nn.Linear(100,k)
    self.relu = nn.ReLU()
    self.softmax = nn.Softmax(k)

    for m in self.modules():
        if isinstance(m, nn.Linear):
            torch.nn.init.xavier_uniform_(m.weight)

def forward(self, x):
    #x = x.view(-1, 1 * 28 * 28)
    x = self.fc1(x)
    x = self.relu(x)
    x = self.fc2(x)
    x = self.relu(x)
    x = self.fc3(x)
    k_probabilities = self.softmax(x)

    return k_probabilities

I want to feed this network with a mini_batch_size=100. The output k=20 is 20 probabilities and I need the value of index+1 of the highest probability. I get this value for each input image and for 100 images, I must have 100 such values. Then I get the average.
What I would like to know is that can I embed this step into the network as a layer? I get a 20100 matrix, can I get a 201 matrix that gives me the highest probability for all the 100 images in the mini-batch?


title: Vectorize/Support Batching a PyTorch Model

body:
I have the following PyTorch model for CBOW embedding:
class CBOW(nn.Module):
    def __init__(self, vocab_size, context_radius, embedding_dim):
        super(CBOW, self).__init__()
        self.embedding = nn.Embedding(vocab_size, embedding_dim)
        self.linear1 = nn.Linear(2 * context_radius * embedding_dim, 128)
        self.linear2 = nn.Linear(128, vocab_size)

    def forward(self, input):
        output1 = self.embedding(input).view(1, -1)
        output2 = F.relu(self.linear1(output1))
        output3 = self.linear2(output2)

        return F.log_softmax(output3, dim=1)

It works well when I send it a tensor of the dimension (2*context_radius), but not when I send (N, 2*context_radius) where N > 1. For example, if my context radius is 2 (hence context is 4), I can send the input [36, 30, 12, 7] and I get an output of the shape (1, vocab_size). But I would like to be able to send a batch of contexts and retrieve a batch of outputs.


title: Model is not learning: problem with custom activation function and/or with custom loss function

body:
class QuaternionLoss(torch.nn.Module):
    def __init__(self):
        super(QuaternionLoss, self).__init__()

    def forward(self, output, target):
        loss = 100 * (1 - torch.dot(output.squeeze(0), target.squeeze(0)))
        return loss

class LinearNormalized(torch.nn.Module):
    def __init__(self):
        super(LinearNormalized, self).__init__() # init the class

    def forward(self, x):
        return linear_normalized(x)

class VGGOrientation(torch.nn.Module):
    def __init__(self):
        super(VGGOrientation, self).__init__()
        self.model_vgg_orientation = torchvision.models.vgg16(pretrained=True)
        self.model_vgg_orientation.classifier = torch.nn.Sequential(
            torch.nn.Linear(25088, 256),
            torch.nn.ReLU(inplace=True),
            torch.nn.Linear(256, 64),
            torch.nn.ReLU(inplace=True),
            torch.nn.Linear(64, 2),
            LinearNormalized()
        )

    def forward(self, x):
        output_orientation = self.model_vgg_orientation(x)
        return output_orientation

When I train my model this is what I have ( I printed out the loss function, my target and the output of my model):
enter image description here
Do you have any idea on why my model is not learning? Something has to be wrong with the activation function or the loss because my data is good. In fact I have created the same model on TensorFlow and it works.


title: Ensemble loss not going down proportionally

body:
I have implemented an ensemble consisting of 3-layer MLPs with the following architecture:
super(MLP, self).__init__()
        self.linear1 = torch.nn.Linear(D_in, H)
        self.relu1 = torch.nn.ReLU()
        self.batch1 = torch.nn.BatchNorm1d(H)
        self.hidden1 = torch.nn.Linear(H, H)
        self.relu2 = torch.nn.ReLU()
        self.batch2 = torch.nn.BatchNorm1d(H)
        self.hidden2 = torch.nn.Linear(H, H)
        self.hidden3 = torch.nn.Linear(H, D_out)
        self.logSoftMax = torch.nn.LogSoftmax(dim=1)
        self.SoftMax = torch.nn.Softmax(dim=1)

When testing the loss for the ensemble I don't see the loss decreasing when the number of models is increased. For a single model, it starts out with a reasonable value and then as the ensemble increases it goes up and then down. I think it might be something wrong with how we add the predictions. This is how we are doing it:
def avg_evaluate(models):
    loss_fn = torch.nn.NLLLoss()
    loss = 0
    total = 0
    for batch_idx, (data, target) in enumerate(test_loader):
        y_preds = []
        for idx, model in enumerate(models):
            model = model.eval()
            data = data.view(data.shape[0], -1)

            y_pred, _ = model(data)
            y_preds.append(y_pred)

        loss = loss + loss_fn(torch.div(torch.stack(y_preds, dim=0).sum(dim=0), len(models)), target).item()

    print("Final loss")
    print(loss / len(test_loader))
    loss = loss / len(test_loader)

Does anyone have any idea of what could be wrong? Would appreciate any help! Thanks.


title: Shared Pytorch model instances in multiple flask api

body:
I have a sample Flask app like this:
from flask import Flask
app = Flask(__name__)
model = pytorch_model_instance

@app.route('/task1')
def do_task1():
    pred = model.predict()
    return pred

@app.route('/task2')
def do_task2():
    pred = model.predict()
    return pred

The above 2 endpoints could use the model simultaneously for prediction, my question is if i define a single model for these 2 endpoints, are there any problems if both of them access the model instance at the same time for prediction?


title: PyTorch plot latent variations by class

body:
I'm sure this has been asked loads of times, but I'm trying to figure out what's wrong with my code. I'm just trying to sample images of columns of latent variations over rows of classes.
def sample(self, n):
        l_dim = int(math.sqrt(n))
        sample = []
        labels = torch.randint(0, self.num_classes, (l_dim,))
        for i in range(l_dim):
            label = labels[i].repeat(l_dim)
            one_hot = F.one_hot(label, num_classes=self.num_classes)
            sample_z = torch.randn(l_dim, self.z_dim)
            sample_i = torch.cat((sample_z, one_hot), dim=1)
            sample.append(sample_i)
        s = torch.cat(sample, 0).to(self.device)
        return self.decode(s)

With this, I seem to be getting identical images in all columns — I do get rows of different classes. What am I missing here? (I fully confess to not being super comfortable with PyTorch!)


title: how can i load a trainned model and use graphviz or anythink to visualize it?

body:
im using Implementation of "Tracking without bells and whistles” and the multi-object tracking "Tracktor" code, and they use a trainned model saved on a ".model" file.
how can i load that file to a script and evaluate it, visualize the process?
using pytorch*


title: image not found when installing the Auto Pytorch

body:
When installing the Auto pytorch from git and trying there is a error shown below
OSError: dlopen(/Users/sivapathasundaramjanu/Applications/anaconda3/lib/python3.7/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
Referenced from: /Users/sivapathasundaramjanu/Applications/anaconda3/lib/python3.7/site-packages/lightgbm/lib_lightgbm.so
Reason: image not found


title: Loading model causes different result (probably dropout active)

body:
In Pytorch when i run a model after training (without loading) i get good results. But when i load the model i get completely different results that are false. I have a dropout in my forward. I can not figure out how to do this because there hasnt been a single post about it in a year and things have changed in pytorch. Here is the code (it has a few features implemented):
    class Netz(nn.Module):
    def __init__(self):
        super(Netz, self).__init__()
        self.conv1 = nn.Conv2d(1, 2, kernel_size=5) #(wir haben 3 input chanel, output chanel setzen wir auf 10) da wir mit Bilder arbeiten kommt eine convolutional Schicht rein, gleich als erstes
        self.conv2 = nn.Conv2d(2, 4, kernel_size=5) #normalerweise macht man gleich mehrere convolutional Schichten
        self.conv3 = nn.Conv2d(4, 8, kernel_size=5) #mit den Zahlen kann man am Ende noch ein bisschen herumspielen
        self.fc1 = nn.Linear(7616, 1000)
        #fully connected layer soll als input genau die Anzahl an Neuronen haben, die torch.Size(, ., ., .) => . mal . mal .  ausspuckt
        self.fc2 = nn.Linear(1000, 2)

    def forward(self, x): #gehen davon aus, dass irgendetwas kaputt gehen wird
        x = self.conv1(x)
        x = F.relu(x)
        x = F.max_pool2d(x,2)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x,2)
        x = self.conv3(x)
        x = F.relu(x)
        x = F.max_pool2d(x,2)
        x = x.view(-1, 7616) #-1, da der erste Wert, also die batch size, uns nicht interessiert
        x = F.relu(self.fc1(x)) # als Aktivierungslayer für unser erstes fully connected layer
        x = F.dropout(x)
        x = self.fc2(x)
        return F.softmax(x, dim=1)
        #jetzt müssen wir in unseren fully connected layer umsteigen, aber wir wissen ja nicht, wie unsere Daten in dem Fall jetzt aussehen
        exit()

model = Netz()
optimizer = optim.RMSprop(model.parameters(), lr = 0.001) #Learning rate

def train(epoch): #model existiert zwar noch nicht, aber wir erschaffen uns unser eigenes model
    model.train() #model auf train setzen, damit es auch wirklich trainiert wird und nicht nur ausgeführt
    for idx, (data, target) in enumerate(train_data): # Daten aus vorgefertigten Daten beziehen, wir brauchen hier die train data
        target = torch.Tensor(target)
        data = Variable(data)
        target = Variable(target) # müssen data und target ja immer noch als Variablen verarbeiten
        optimizer.zero_grad() #setzen unseren Gradienten auf 0, damit wir überhaupt etwas lernen können
        out = model(data) #output soll das model data sein
        criterion = F.binary_cross_entropy #wollen ja Bilder klassifizieren in zwei Klassen
        loss = criterion(out, target) #wollen unser Loss als criterion von output und target berechnen -- zu beachten: target hat zwei Elemente und am Ende soll unser output auch zwei Elemente haben
        loss.backward() # wollen unseren Loss back - propagaten
        optimizer.step() # wollen unseren Optimizer sagen, dass er etwas tun soll
# das war es mit unserem Trainingsalgorithmus

epoch_num = range(0,int(args.epochs))

if path.exists(args.model) == False: #wir schauen ob unsere Model schon trainiert ist
    for idx, epoch in enumerate(epoch_num, start=1): #wollen 30 epcohen lang trainieren
        print("Epoch " + str(idx) + " out of " + str(max(epoch_num)+1) + " started!")
        train(epoch)
        print("Epoch " + str(idx) + " out of " + str(max(epoch_num)+1) + " finished!")
    torch.save(model, args.model) #wir speichern unsere trainierte Model
else:
    model = torch.load(args.model)


title: Gathering values of a list of matrix indices/values into a matrix with torch.gather()

body:
I have a numpy array of shape (80,000, 3). Each row of this array has entries [j,i,s] which correspond the the value s at index [i,j] of a matrix R. I happen to know the shape that this matrix should take is (1682, 943), and each s must be a value in [1,2,3,4,5]. Note that R does not have data in some entries,
I can't seem to wrap my head around the PyTorch torch.gather function, which I presume is perfect for the problem of trying to assemble my matrix R from this numpy array. I've read the documentation and looked at examples. Could anyone help me understand how to use torch.gather to assemble my matrix?


title: Pytorch 2D Feature Tensor 1D output Same weights across dimension

body:
I am handeling a timeseries dataset with n timesteps, m features and k objects.
As a result my feature vector has a shape of (n,k,m) While my targets shape is (n,m)
I want to predict the targets for every timestep and object, but with the same weights for every opject. Also my loss function looks like this.
average_loss = loss_func(prediction, labels)
sum_loss = loss_func(sum(prediction), sum(labels))
loss = loss_weight * average_loss + (1-loss_weight) * sum_loss

My plan is to not only make sure, that I predict every item as good as possible, but also that the sum of all items get perdicted. loss_weights is a constant.
Currently I am doing this kind of ugly solution:
features = local_batch.squeeze(dim = 0)
labels = torch.unsqueeze(local_labels.squeeze(dim = 0), 1)
prediction = net(features)

I set my batchsize = 1. And squeeze it to make the k objects my batch.
My network looks like this:
def __init__(self, n_feature, n_hidden, n_output):
    super(Net, self).__init__()
    self.hidden = torch.nn.Linear(n_feature, n_hidden)   # hidden layer
    self.predict = torch.nn.Linear(n_hidden, n_output)   # output layer

def forward(self, x):
    x = F.relu(self.hidden(x))      # activation function for hidden layer
    x = self.predict(x)             # linear output
    return x

How do I make sure I do a reasonable convolution over the opject dimension in order to keep the same weights for all objects, without commiting to batchsize=1? Also, how do I achieve the same loss function, where I compute the loss of the prediction sum vs target sum for any timestamp?


title: Error in load group of png images into colab using imagefolder

body:
I am writing a code to load a group of images (50 images) from my google drive into colab system , then display them. i upload all image on drive and mount them correctly but i still have a problem
here is my code:


title: use the vocab of one data field into another data field in torchtext

body:
In pytorch's torchtext library,
I have two data fields
from torchtext import data,TabularDataset
A = data.Field(sequential=True)
B = data.Field(sequential=True)

where A points to one column of TabularDataset and B points to another column of TabularDataset. But the vocabulary is mostly same in both the columns.
I want to do something like this,
Get the A's vocab
A.build_vocab(TabularDataset)
A_vocab = A.vocab.stoi

and give it to B
B.vocab(A_vocab)


title: Error: "python has stopped working" on Windows and Segmentation fault on ubuntu

body:
I am running the PyTorch python code and getting the error from windows 10 that shows "python has stopped working".
when I run the same code on Ubuntu it returns the error of Segmentation fault.
I searched regarding windows error and found that this is because of race condition.
I figured out the block of code causing trouble.
Can anyone find the race condition and fix it out?
Code causing error:
filt = True
if filt:

    if tails[(s.item(), r.item())]._nnz() > 1:
        tmp = dst_scores[t].item()
        dst_scores += tails[(s.item(), r.item())] * 1e30
        dst_scores[t] = tmp
    if heads[(t.item(), r.item())]._nnz() > 1:
        tmp = src_scores[s].item()
        src_scores += heads[(t.item(), r.item())] * 1e30
        src_scores[s] = tmp


title: Validation loss keeps increasing at a very high rate and validation accuracy is constant

body:
I am trying to make a model that predicts whether the tweet is about a disaster or not.
Link for the question's description.
I have tried a lot of things but the validation accuracy remains constant. It seems like my model is overfitting after just 1 epoch. My validation loss is increasing at a very high speed. I am out of moves at this point. If you can check the provided notebook and give me some points to focus on, that would be great, thank you.
My Model
  def forward(self, x, hidden):
    batch=len(x)
    embedd=self.embedding(x)
    lstm_out1, hidden=self.lstm1(embedd, hidden)
    lstm_out=lstm_out1.contiguous().view(-1, self.hidden_dim)
    out=self.dropout(F.relu(lstm_out))
    out=self.fc1(out)
    out=self.dropout(F.relu(out))
    out=self.fc2(out)
    out=self.dropout(F.relu(out))
    out=self.fc3(out)
    sig_out=self.sigmoid(out)
    sig_out=sig_out.view(batch, -1)
    sig_out=sig_out[:, -1]
    return sig_out, hidden

My Parameters
learning_rate=0.001
epochs=15
vocab_size = len(vocab_to_int)+1 # +1 for the 0 padding = 29364
output_size = 2 # The output size we want
embedding_dim = 50 # output of embedding layer
hidden_dim = 32 # the output size of 2 outputs of lstm cell
n_layers = 2
batch_size = 23
net=RealOrFakeLSTM(vocab_size, output_size, embedding_dim, hidden_dim, n_layers, False, 0.3)
net.to(device)
criterion=nn.BCELoss()
optimizer=torch.optim.Adam(net.parameters(),lr=learning_rate)
net.train()

At last, My training code.
for i in range(epochs):
  hidden=net.init_hidden(batch_size)
  for input,label in train_loader:
    input=input.to(device)
    label=label.to(device)
    optimizer.zero_grad()
    input=input.clone().detach().long()
    out, hidden=net(input, hidden)
    loss_train=criterion(out.squeeze(),label.float())
    loss_train_arr=np.append(loss_train_arr,loss_train.cpu().detach().numpy())
    loss_train.backward()
    optimizer.step()
    total_train_loss+=loss_train
  total_train_loss=total_train_loss/len(train_loader)
  lossPerEpoch_train=np.append(lossPerEpoch_train,total_train_loss.cpu().detach().numpy())


title: Is there a way to make a model that creates a mask to drop certain inputs before feeding the masked data to another network?

body:
This might be a question that is kind of dumb but I'm trying to construct a model that is able to filter out inputs before feeding the filtered output to another network.
For example, I have an image that I would to match with a database of about 100 pictures, then I would apply the first network to do some operations that would output the top 10 pictures that is most likely to correctly match. Afterwards, I would apply a second network to rematch those top 10 pictures using a secondary network.
INPUT --> | NETWORK 1 | --> FILTERED OUTPUT --> | NETWORK 2 | --> FINAL OUTPUT

Wondering if there is a way to accomplish this sort of filtration behaviour where that filtered output is fed to the second model like that.


title: Why are my dimensions our of range when trying to extract QValues for next_states?

body:
I have defined a class QValues with a static method .get_next() in order to extract the q_values of the next states. However, when I call .get_next(),  I receive an error saying that my dimensions are out of range and I am extremely confused. Why might this be happening?
Here is some code:
Running the Cartpole-V0 environment
episode_durations = []
for episode in range(num_episodes):
  em.reset()

  state = em.get_state()

  for timestep in count():
    action = agent.select_action(state, policy_net)
    reward = em.take_action(action)
    next_state = em.get_state()
    memory.push(Experience(state, action, reward, next_state))

    if memory.can_provide_sample(batch_size):
      experiences = memory.sample(batch_size)
      states, actions, rewards, next_states = extract_tensors(experiences)

      current_q_values = QValues.get_current(policy_net, states, actions)
      next_q_values = QValues.get_next(target_net, next_states)

Here is the error that I am getting:
<ipython-input-91-9001413ab21d> in <module>()
     17
     18       current_q_values = QValues.get_current(policy_net, states, actions) # get current return q-values for any given state action pairs predicted by the policy network
---> 19       next_q_values = QValues.get_next(target_net, next_states) # maximum q-values for the for the next states for the best corresponding actions
     20       target_q_values = (next_q_values * gamma) + rewards # bellman equation basically
     21

<ipython-input-78-18dcfcc8c5d6> in get_next(target_net, next_states)
      8   @staticmethod
      9   def get_next(target_net, next_states):
---> 10     final_state_locations = next_states.flatten(start_dim = 1).unsqueeze(1).max(dim=1).eq(0).type(torch.bool) # final state == True; non-final state == False
     11     non_final_state_locations = (final_state_locations == False) # true for non-final state; false for final state
     12     non_final_states = next_states[non_final_state_locations]

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)


title: Type of Pytorch Sequential class & Convolution Layers class are not matching

body:
I'm trying to  write a module that compares the parameters of 2 pytorch Sequential objects.
In order to do so, I wrote some toolbox functions that extract them from a Sequence of Convolution Layers.

import torch.nn as nn

def get_params_from_layer(layer):
    '''
    get params from a layer (Conv2D) of torch sequential model
    '''
    # params keys are the non-private ones (i.e not starting with '_')
    return [i[1] for i in layer.__dict__.items() if not i[0].startswith('_')]

def get_params_from_model(encoder_decoder):
    '''
    get params from a torch sequential model
    '''
    return [get_params_from_layer(e) for e in encoder_decoder]

# example
test_seq = torch.nn.Sequential(torch.nn.Conv2d(1, 2, 2))
get_params_from_model(test_seq)

# output:
# [[True, 1, 2, (2, 2), (1, 1), (0, 0), (1, 1), False, (0, 0), 1, 'zeros']]

All works fine BUT I am also trying to put the typing module for type inference (using Pyright). So, the code becomes:
def get_params_from_layer(layer: nn.modules.conv.Conv2d) -> List[Any]:
    '''
    get params from a layer of torch sequential model
    '''
    # params keys are the non-private ones (i.e not starting with '_')
    return [i[1] for i in layer.__dict__.items() if not i[0].startswith('_')]

def get_params_from_model(encoder_decoder: nn.ModuleList) -> List[List[Any]]:
    '''
    get params from a torch sequential model
    '''
    return [get_params_from_layer(e) for e in encoder_decoder]

Unfortunately, this doesn't work. I have the following error:
Argument of type "Module" cannot be assigned to parameter "layer" of type "Conv2d" in function "get_params_from_layer"
Indeed Sequential can be an iterator of Module but Conv2D is not inheriting from the Module class. Any idea on how to have my Pyright to allow this without using Any?


title: CPU usage 99%+ : Stuck when running two different pytorch programs with two different gpus

body:
I have a very simple code which is supposed to run on one gpu. However, whenever I run a program on one gpu and trigger another run on another gpu, both of the programs either get stuck or slow down significantly.

from __future__ import print_function
from __future__ import division
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
from torch.utils.tensorboard import SummaryWriter
import matplotlib.pyplot as plt
import time
import os
import copy
import argparse
#print("PyTorch Version: ",torch.__version__)
#print("Torchvision Version: ",torchvision.__version__)

# Number of classes in the dataset
num_classes = 2 # for now

# Batch size for training (change depending on how much memory you have)
batch_size = 32

# Number of epochs to train for
num_epochs = 50

# Flag for feature extracting. When False, we finetune the whole model,
# when True we only update the reshaped layer params
feature_extract = True

unfreeze_layer = 0

# Implement early stop
early_stop_tol = 5

parser = argparse.ArgumentParser()
parser.add_argument("gpu", type=str,
                    help="gpu number (e.g. \"0\" )")
parser.add_argument("unfreeze_layer", type=int)
parser.add_argument("path", type=str, help="path to input dir")
parser.add_argument("base_path", type=str, help="path to this python dir")
args = parser.parse_args()

data_dir = args.path

def unfreeze(model, min_layer):
    print("params to learn:")
    ct = 0
    for child in model_ft.children():
        ct += 1
        if ct >= min_layer:
            for name,param in model_ft.named_parameters():
                param.requires_grad = True
                print("\t",name)

def train_model(model, dataloaders, criterion, optimizer, num_epochs=25, init_acc = 0.0):
    since = time.time()

    val_acc_history = []

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = init_acc
    tol = 0

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0

            # Iterate over data.
            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(inputs)
                    loss = criterion(outputs, labels)

                    _, preds = torch.max(outputs, 1)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # statistics
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / len(dataloaders[phase].dataset)
            epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)

            if phase == 'train':
                writer.add_scalar("Loss/train", epoch_loss, epoch)
                writer.add_scalar("Acc/train", epoch_acc, epoch)
            if phase == 'val':
                writer.add_scalar("Loss/val", epoch_loss, epoch)
                writer.add_scalar("Acc/val", epoch_acc, epoch)

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

            # save model
            # torch.save(model, os.path.join(model_output_path, "model_" + str(epoch)))

            # deep copy the model
            if phase == 'val' and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = copy.deepcopy(model.state_dict())
            if phase == 'val':
                val_acc_history.append(epoch_acc)
                tol += 1

        if tol >= early_stop_tol:
            print("reached max tol")
            break

        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights
    model.load_state_dict(best_model_wts)
    return model, val_acc_history

def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False

def initialize_model(num_classes, feature_extract, use_pretrained=True):
    """
    Initialize input model.
    """
    model_ft = models.resnet18(pretrained=use_pretrained)
    set_parameter_requires_grad(model_ft, feature_extract)
    num_ftrs = model_ft.fc.in_features
    model_ft.fc = nn.Linear(num_ftrs, num_classes)
    input_size = 224
    return model_ft, input_size

for learning_rate in np.geomspace(0.000001, 0.1, 100):
    for finetune_learning_rate in np.geomspace(0.0000001, 0.01, 80):
        # Initialize the model for this run
        model_ft, input_size = initialize_model(num_classes, feature_extract, use_pretrained=True)

        # Data augmentation and normalization for training
        # Just normalization for validation
        data_transforms = {
            'train': transforms.Compose([
                transforms.Resize(input_size),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ]),
            'val': transforms.Compose([
                transforms.Resize(input_size),
                transforms.ToTensor(),
                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ]),
        }

        # Create training and validation datasets
        image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']}
        # Create training and validation dataloaders
        dataloaders_dict = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=0) for x in ['train', 'val']}
        images, labels = next(iter(dataloaders_dict['train']))

        # Detect if we have a GPU available
        device = torch.device(args.gpu if torch.cuda.is_available() else "cpu")

        # Send the model to GPU
        model_ft = model_ft.to(device)

        # Gather the parameters to be optimized/updated in this run. If we are
        #  finetuning we will be updating all parameters. However, if we are
        #  doing feature extract method, we will only update the parameters
        #  that we have just initialized, i.e. the parameters with requires_grad
        #  is True.
        params_to_update = model_ft.parameters()
        print("Params to learn:")
        if feature_extract:
            params_to_update = []
            for name,param in model_ft.named_parameters():
                if param.requires_grad == True:
                    params_to_update.append(param)
                    print("\t",name)
        else:
            for name,param in model_ft.named_parameters():
                if param.requires_grad == True:
                    print("\t",name)

        writer = SummaryWriter(os.path.join(os.path.join(args.base_path, 'runs'), "batch_size" + str(learning_rate) + "_lr_" + str(finetune_learning_rate)))

        # Observe that all parameters are being optimized
        optimizer_ft = optim.SGD(params_to_update, lr=learning_rate, momentum=0.9)

        # Setup the loss fxn
        criterion = nn.CrossEntropyLoss()

        # Train and evaluate
        model_ft, hist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs)

        print("start finetuning")

        unfreeze(model_ft, args.unfreeze_layer)

        # Observe that all parameters are being optimized
        finetune_optimizer_ft = optim.SGD(params_to_update, lr=finetune_learning_rate, momentum=0.9)

        # Setup the loss fxn
        finetune_criterion = nn.CrossEntropyLoss()

        model_ft, hist = train_model(model_ft, dataloaders_dict, finetune_criterion, finetune_optimizer_ft, num_epochs=num_epochs, init_acc = hist[-1])

        writer.flush()
        writer.close()

        # Save model
        torch.save(model_ft, os.path.join(os.path.join(args.base_path, 'model') + "batch_size" + str(learning_rate) + "_lr_" + str(finetune_learning_rate)))
        del model_ft, criterion, optimizer_ft
        torch.cuda.empty_cache()

I pass in "0" for gpu for the first program, and "1" for the gpu for the second program.
nvidia-smi status after running two separate programs
EDIT : It seems like our CPU usage reaches 99.9% if we run the above code. Would there be any suggestion how to optimize? Thanks!


title: Whereis CUDA/Cuda/cuda - Ubuntu 20.4 - cuda install issues

body:
I recently switched from windows to ubuntu. I installed miniconda here, created a virtual environment with python 3.8.5, activated that environment and ran
conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch

I ran torch.cuda.is_available() and it says True, but when I run which CUDA/Cuda/cuda or whereis CUDA/Cuda/cuda, I get nothing. nvcc is also not working. I think I might have to update bash file. Would someone please be able to guide me.
Also, Do I need to install cuDNN to work on my ML models using CUDA or is that not needed.


title: How to use a custom Rank to GPU mapping in Pytorch Distributed

body:
I'm running PyTorch distributed on a single 4-gpu node, where I am not the only one running projects. As a result, sometimes certain GPUs are too busy, while others are idle.
Let's say GPU 0 and GPU 2 are busy, but GPU 1 and GPU 3 are not, so I want to use just these GPUs. It seems that by default, Rank 0 is always GPU 0 in pytorch distributed.
How can I specify to PyTorch that Rank 0 should correspond to GPU 1, and Rank 1 should correspond to GPU 3?


title: Logging training metrics to a csv file

body:
I want to log all training metrics to a csv file while it is training on YOLOV5 which is written with pytorch but the problem is that I don't want to use tensorboard.
To achieve this goal I tried some techniques like below:
-First log it into tensorboard and then try to convert it to a csv file (failed)
-Extract log files from Weights & Biases (failed)
-Write to a csv file directly during training (failed)
Here I tried opening a csv file and transforming tensor data into string but couldn't succeed.
So I really wonder how could I achieve this goal because YOLOV5 is not using pytorch utilites like model.fit which we can use callbacks with.
Thanks in advance.


title: where should I change the code to tell PyTorch not to use the GPU?

body:
As mentioned in How to tell PyTorch to not use the GPU?, in order to tell PyTorch not to use the GPU you should change a few lines inside PyTorch code.
Where should I make the change?
Where is the line of code that needs to be modified?
I tried to find it but couldn't...


title: create representation of questions using LSTM via a pre-trained word embedding such as GloVe

body:
I am new in LSTM and python. My goal is to represent the sentence using LSTM.
Could u tell me I am doing the right? how to fix the error when running the following code ?
"TypeError: embedding(): argument 'indices' (position 2) must be Tensor, not str"
import torch
import torch.nn as nn
import numpy as np
from torch import optim
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
import torchvision.datasets as datasets  # Standard datasets
import torchvision.transforms as transforms
import json

class RNN_LSTM(nn.Module)

    def __init__(self, input_size, hidden_size, num_layers, num_classes, vocab_size,
                 lstm_dropout, device, word_emb_file):
        super(RNN_LSTM, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm_dropout = lstm_dropout
        self.lstm_drop = nn.Dropout(p=self.lstm_dropout)
        self.word_emb_file = word_emb_file
        self.device = device

        # initialize text embeddings
        self.word_embeddings = nn.Embedding(vocab_size, input_size)
        self.word_embeddings.weight = nn.Parameter(
                torch.from_numpy(
                    np.pad(np.load(self.word_emb_file), ((0, 1), (0, 0)), 'constant')).type(
                    'torch.FloatTensor'))

        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)

    def forward(self, sentence, question_len):
        embeds = self.word_embeddings(sentence)
        packed_output = pack_padded_sequence(embeds, question_len, batch_first=True)
        outputs, (hidden, cell_state) = self.lstm(packed_output)
        outputs, outputs_length = pad_packed_sequence(outputs, batch_first=True)
        outputs = torch.cat([hidden[0,:,:], hidden[1,:,:]], dim=-1)
        return outputs

lstm_dropout = 0.3
input_size = 300
hidden_size = 256
num_classes = 10
num_layers = 1
device = 'cpu'
vocab_size = 2000
word_emb_file = "/home/project/word_emb_300d.npy"

model = RNN_LSTM(input_size, hidden_size, num_layers, num_classes, vocab_size, lstm_dropout, device, word_emb_file)

model.word_embeddings('Blue Skye')


title: Size Mismatch for Functional Linear Layer

body:
I apologize that this is probably a simple question that has been answered before, but I could not find the answer. I’m attempting to use a CNN to extract features and then input that into a FC network that outputs 2 variables. I’m attempting to use the functional linear layer as a way to dynamically handle the flattened features. The self.cnn is a Sequential container which last layer is the nn.Flatten(). When I print the size of x after the CNN I see it is 15x152064, so I’m unclear why the F.linear layer is failing to run with the error below. Any help would be appreciated.
RuntimeError: size mismatch, get 15, 15x152064,2
x = self.cnn(x)
batch_size, channels = x.size()
x = F.linear(x, torch.Tensor([256,channels]))
y_hat = self.FC(x)


title: Optimising custom 2d-convolution which can quantize MAC results during convolution in pytorch

body:
I'm a student studying low power memory circuits / in-memory computing, and I'm trying to bring multiplication-accumulation(MAC) operation into SRAM array so most/all of convolution can be done inside memory array.
Since computation inside SRAM cannot handle full precision operation like GPU, i'm trying to cut the MAC results' bit precision to some point without suffering severe accuracy loss.
So I'm trying to verify it with pytorch platform.
However, conv2d does not seem to support such function so i'm working on custom-building the convolution  from the ground.
I'm using customised-mini-vgg : 128C3-128C3-MP2-256C3-256C3-MP2-512C3-512C3-MP2-1024FC-1024FC-10FC
I'm still working on code, but I'm stuck with error saying "CUDA out of memory".
I get why I'm getting the error but can't come up with solution.
Is there any way I can optimise the code without changing the batch size or network?
Also, if anyone knows of a custom 2d convolution that I can refer to I would apperciate it very much.
Thank you for reading my question!
(Please forgive that my writing and code may be hard to read. :'( I'm completely new to python/pytorch and knows only basics of neural network.)
import torch
import matplotlib.pyplot as plt
import numpy as np
from torch import nn
from torch.autograd import Function
import pdb

class Conv2DFunctionCustom(Function):
    @staticmethod
    def forward(ctx, input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1):

        ctx.save_for_backward(input, weight, bias)
        ctx.stride, ctx.padding, ctx.dilation, ctx.groups = stride, padding, dilation, groups

        zeropad = nn.ZeroPad2d(ctx.padding[0])
        batch_size = len(input)
        input_channel = len(input[0])
        input_size = len(input[0][0])
        ochannel = len(weight)
        ichannel = len(weight[0])
        kernel_size = len(weight[0][0])

        if padding:
            inp_pad = zeropad(input)
        else:
            inp_pad = input

        input_un_tensor = inp_pad.unfold(1,input_channel,input_channel)

        input_reshape = input_un_tensor.transpose(1,4).reshape(len(inp_pad),len(inp_pad[0]),len(inp_pad[0][0]),len(inp_pad[0][0]))

        weight_un_tensor = weight.unfold(1,ichannel,ichannel)
        weight_reshape = weight_un_tensor.transpose(1,4).reshape(len(weight),len(weight[0]),len(weight[0][0]),len(weight[0][0]))

        input_unfold = torch.nn.functional.unfold(input_reshape, (kernel_size, kernel_size))
        print('unfoldinput:',input_unfold.size())

        mult = input_unfold.transpose(1, 2).unfold(2,16,16)[None,:] * weight_reshape.view(weight_reshape.size(0), -1).unfold(1,16,16)[:,None,None]
        psum = torch.sum(mult, dim=4)
        psum = torch.sum(psum, dim=3)

        out = torch.nn.functional.fold(psum, (input_size, input_size), (1, 1)).transpose(0,1)

        return out