Took a look at the GRUCell source 3 and seen that if one were to pass to the cell the hidden state as None then it will initialize itself to zeros.
With this information I further changed my code so instead of manually setting the hidden state to zeros I set it to None thus relying on the implicit conversion. This seemed to solve my issue as now I am not encountering the error message.
Now I just have to come up with a solution where I can reset the hidden state manually.
Code:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.distributions import Categorical


class MinimalGru(nn.Module):
    def __init__(self):
        super(MinimalGru, self).__init__()

        self.rnn = nn.GRUCell(input_size=(7), hidden_size=100)

        # Retrieve data from the GRU output in shape (batch, num_layers * num_directions,  hidden_size)
        self.policy_head = nn.Linear(100, 4)
        self.value_head = nn.Linear(100, 1)

        self.rnn_hidden_state = None

    def forward(self, state):
        self.rnn_hidden_state = self.rnn(state, self.rnn_hidden_state)
        action_scores = self.policy_head(self.rnn_hidden_state)
        value_score = self.value_head(self.rnn_hidden_state)

        return torch.softmax(action_scores, dim=1), torch.sigmoid(value_score)


def reinforce():
    torch.autograd.set_detect_anomaly(True)

    number_of_episodes = 123456
    length_of_episode = 123

    model = MinimalGru().train()
    optimizer = optim.Adam(model.parameters(), lr=0.001)

    for i_episode in range(number_of_episodes):

        log_probs = []
        optimizer.zero_grad()

        for i_step in range(length_of_episode):
            state = torch.rand([1, 7])
            a_t = torch.rand([1, 3])
            r_t = torch.rand([1, 1])

            probs, value = model(state)

            m = Categorical(probs)
            action = m.sample()
            log_probs.append(m.log_prob(action))
            action = action.item()

        print('End of episode {}'.format(i_episode))

        # Calculate loss
        log_probs_tensor = torch.cat(log_probs, dim=0)
        policy_loss = -log_probs_tensor * torch.rand([length_of_episode])
        loss = policy_loss.mean()

        # Update policy
        loss.backward(retain_graph=True)
        optimizer.step()

        # The game ends, reset the hidden state of the GRU AFTER the backward()
        model.rnn_hidden_state = None


if __name__ == "__main__":
    reinforce()

Edit:
I tried to substitute initializing the hidden state to torch.zeros([1, 100], dtype=torch.float) instead of None and it worked as expected. From that information it seems that my initial error originates from this function:



 Dudly01:

def reset_hidden_state_element(self, index):
        self.rnn_hidden_state[index] = torch.zeros([100], dtype=torch.float)