I have a neural network that is supposed to train an agent to do a task, but when I run the model, the weight on the second fully connected layer (fc2) on the Critic becomes very small.
> tensor([[ 4.1137e-41, 2.2757e-42, -2.6926e-41, ..., -2.0870e-41,
> -6.6328e-41, 2.3732e-06],
> [-2.2255e-41, 5.6645e-41, 1.4829e-41, ..., -1.2250e-41,
> 1.5626e-41, 4.0754e-04],
> [ 4.9889e-41, 1.1880e-41, -3.1507e-41, ..., 5.8521e-41,
> 5.0874e-41, 4.2433e-06],
> ...,
> [-5.5420e-41, 3.9210e-41, 3.0439e-41, ..., 2.9665e-42,
> -3.1451e-41, -1.0182e-04],
> [-5.7013e-41, -1.1474e-41, -4.4497e-41, ..., 9.9604e-42,
> -3.4307e-41, -4.3556e-07],
> [ 2.5703e-41, 5.2402e-41, -6.8396e-41, ..., 2.6696e-41,
> 4.3401e-41, -4.7599e-09]], requires_grad=True)
This causes my agent to grab the upper limits on my action space and stay there. I thought nn.LayerNorm will help solve this problem, but I having no luck.
Here is my code.
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
class CriticNetwork(nn.Module):
def __init__(self, beta, input_dims, fc1_dims, fc2_dims, n_actions, name,
chkpt_dir='tmp/ddpg'):
super(CriticNetwork, self).__init__()
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.name = name
self.checkpoint_dir = chkpt_dir
self.checkpoint_file = os.path.join(self.checkpoint_dir, name + '_ddpg')
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
self.fc2 = nn.Linear(self.fc1_dims, self.fc2_dims)
self.bn1 = nn.LayerNorm(self.fc1_dims)
self.bn2 = nn.LayerNorm(self.fc2_dims)
self.action_value = nn.Linear(self.n_actions, self.fc2_dims)
self.q = nn.Linear(self.fc2_dims, 1)
f1 = 1./np.sqrt(self.fc1.weight.data.size()[0])
self.fc1.weight.data.uniform_(-f1, f1)
self.fc1.bias.data.uniform_(-f1, f1)
f2 = 1./np.sqrt(self.fc2.weight.data.size()[0])
self.fc2.weight.data.uniform_(-f2, f2)
self.fc2.bias.data.uniform_(-f2, f2)
f3 = 0.003
self.q.weight.data.uniform_(-f3, f3)
self.q.bias.data.uniform_(-f3, f3)
#print(self.q.weight.data)
f4 = 1./np.sqrt(self.action_value.weight.data.size()[0])
self.action_value.weight.data.uniform_(-f4, f4)
self.action_value.bias.data.uniform_(-f4, f4)
self.optimizer = optim.Adam(self.parameters(), lr=beta, weight_decay=0.01)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.to(self.device)
def forward(self, state, action):
state_value = self.fc1(state)
state_value = self.bn1(state_value)
state_value = F.relu(state_value)
state_value = self.fc2(state_value)
state_value = self.bn2(state_value)
action_value = self.action_value(action)
state_action_value = F.relu(torch.add(state_value, action_value))
state_action_value = self.q(state_action_value)
return state_action_value
def save_checkpoint(self):
print('... saving checkpoint ...')
torch.save(self.state_dict(), self.checkpoint_file)
def load_checkpoint(self):
print(' ... loading checkpoint ... ')
self.load_state_dict(torch.load(self.checkpoint_file))
#def troubleshoot(self):
#print('')
class ActorNetwork(nn.Module):
def __init__(self, alpha, input_dims, fc1_dims, fc2_dims, n_actions, name,
chkpt_dir='tmp/ddpg'):
super(ActorNetwork, self).__init__()
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.name = name
self.checkpoint_dir = chkpt_dir
self.checkpoint_file = os.path.join(self.checkpoint_dir, name + '_ddpg')
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
self.fc2 = nn.Linear(self.fc1_dims, self.fc2_dims)
self.bn1 = nn.LayerNorm(self.fc1_dims)
self.bn2 = nn.LayerNorm(self.fc2_dims)
self.mu = nn.Linear(self.fc2_dims, self.n_actions)
f1 = 1./np.sqrt(self.fc1.weight.data.size()[0])
self.fc1.weight.data.uniform_(-f1, f1)
self.fc1.bias.data.uniform_(-f1, f1)
f2 = 1./np.sqrt(self.fc2.weight.data.size()[0])
self.fc2.weight.data.uniform_(-f2, f2)
self.fc2.bias.data.uniform_(-f2, f2)
f3 = 0.003
self.mu.weight.data.uniform_(-f3, f3)
self.mu.bias.data.uniform_(-f3, f3)
self.optimizer = optim.Adam(self.parameters(), lr=alpha)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.to(self.device)
def forward(self, state):
x = self.fc1(state)
x = self.bn1(x)
x = F.relu(x)
x = self.fc2(x)
x = self.bn2(x)
x = F.relu(x)
x = torch.tanh(self.mu(x))
return x
def save_checkpoint(self):
print('... saving checkpoint ...')
torch.save(self.state_dict(), self.checkpoint_file)
def load_checkpoint(self):
print(' ... loading checkpoint ... ')
self.load_state_dict(torch.load(self.checkpoint_file))
I need help preventing the critic network weights from becoming small (vanishing gradient). Thanks!