Skip to content

Instantly share code, notes, and snippets.

@tomgrek
Last active September 1, 2018 20:53
Show Gist options
  • Save tomgrek/5fbf25d3360a4a45cf40058588fe0ad8 to your computer and use it in GitHub Desktop.
Save tomgrek/5fbf25d3360a4a45cf40058588fe0ad8 to your computer and use it in GitHub Desktop.
My neural network for actor critic financial trading
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
self.input_layer = nn.Linear(8, 128)
self.hidden_1 = nn.Linear(128, 128)
self.hidden_2 = nn.Linear(32,31)
self.hidden_state = torch.tensor(torch.zeros(2,1,32), requires_grad=False).cuda()
self.rnn = nn.GRU(128, 32, 2)
self.action_head = nn.Linear(31, 5)
self.value_head = nn.Linear(31, 1)
self.saved_actions = []
self.rewards = []
def reset_hidden(self):
self.hidden_state = torch.tensor(torch.zeros(2,1,32), requires_grad=False).cuda()
def forward(self, x):
x = torch.tensor(x).cuda()
x = torch.sigmoid(self.input_layer(x))
x = torch.tanh(self.hidden_1(x))
x, self.hidden_state = self.rnn(x.view(1,-1,128), self.hidden_state.data)
x = F.relu(self.hidden_2(x.squeeze()))
action_scores = self.action_head(x)
state_values = self.value_head(x)
return F.softmax(action_scores, dim=-1), state_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment