-
Notifications
You must be signed in to change notification settings - Fork 2
/
neural_network_lstm_model.py
81 lines (64 loc) · 2.66 KB
/
neural_network_lstm_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import torch
import torch.nn as nn
import math
class extract_tensor(nn.Module):
def forward(self,x):
tensor, _ = x
return tensor
class Representation_function(nn.Module):
def __init__(self,
observation_space_dimensions,
state_dimension,
action_dimension,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
self.state_norm = nn.Linear(observation_space_dimensions, state_dimension)
def forward(self, state):
return self.state_norm(state)
class Dynamics_function(nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
self.action_space = action_dimension
lstm_reward = [
nn.Linear(state_dimension + action_dimension, hidden_layer_dimensions),
nn.LSTM(hidden_layer_dimensions, state_dimension,number_of_hidden_layer),
extract_tensor()
]
lstm_state = [
nn.Linear(state_dimension + action_dimension, hidden_layer_dimensions),
nn.LSTM(hidden_layer_dimensions, state_dimension,number_of_hidden_layer),
extract_tensor(),
]
self.reward = nn.Sequential(*tuple(lstm_reward))
self.next_state_normalized = nn.Sequential(*tuple(lstm_state))
def forward(self, state_normalized, action):
x = torch.cat([state_normalized.T, action.T]).T
return self.reward(x), self.next_state_normalized(x)
class Prediction_function(nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer):
super().__init__()
lstm_policy = [
nn.Linear(state_dimension, hidden_layer_dimensions),
nn.LSTM(hidden_layer_dimensions, action_dimension,number_of_hidden_layer),
extract_tensor()
]
lstm_value = [
nn.Linear(state_dimension, hidden_layer_dimensions),
nn.LSTM(hidden_layer_dimensions , state_dimension,number_of_hidden_layer),
extract_tensor(),
]
self.policy = nn.Sequential(*tuple(lstm_policy))
self.value = nn.Sequential(*tuple(lstm_value))
def forward(self, state_normalized):
return self.policy(state_normalized), self.value(state_normalized)