Soumarya20's picture
Update REINFORCE CartPole-v1 agent with agent.py
b78fc28 verified
Raw
History Blame Contribute Delete
676 Bytes
import torch
import torch.nn as nn
import torch.nn.functional as F
class Policy(nn.Module):
def __init__(self, s_size, a_size, h_size):
super(Policy, self).__init__()
self.fc1 = nn.Linear(s_size, h_size)
self.fc2 = nn.Linear(h_size, a_size)
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.softmax(x, dim=1)
def act(self, state):
state = torch.from_numpy(state).float().unsqueeze(0).to(next(self.parameters()).device)
probs = self(state).cpu()
m = torch.distributions.Categorical(probs)
action = m.sample()
return action.item(), m.log_prob(action)