-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
59 lines (52 loc) · 1.77 KB
/
Copy pathmodel.py
File metadata and controls
59 lines (52 loc) · 1.77 KB
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
import torch
import torch.nn as nn
from parameters import *
#Actor网络
class Actor(nn.Module):
def __init__(self,N_S,N_A):
super(Actor,self).__init__()
self.fc1 = nn.Linear(N_S,64)
self.fc2 = nn.Linear(64,64)
self.sigma = nn.Linear(64,N_A)
self.mu = nn.Linear(64,N_A)
#再缩小10倍,且立即替换原值
self.mu.weight.data.mul_(0.1)
self.mu.bias.data.mul_(0.0)
# self.set_init([self.fc1,self.fc2, self.mu, self.sigma])
self.distribution = torch.distributions.Normal
#初始化网络参数
def set_init(self,layers):
for layer in layers:
nn.init.normal_(layer.weight,mean=0.,std=0.1)
nn.init.constant_(layer.bias,0.)
def forward(self,s):
x = torch.tanh(self.fc1(s))
x = torch.tanh(self.fc2(x))
mu = self.mu(x)
log_sigma = self.sigma(x)
#log_sigma = torch.zeros_like(mu)
sigma = torch.exp(log_sigma)
return mu,sigma
def choose_action(self,s):
mu,sigma = self.forward(s)
Pi = self.distribution(mu,sigma)
return Pi.sample().numpy()
#Critic网洛
class Critic(nn.Module):
def __init__(self,N_S):
super(Critic,self).__init__()
self.fc1 = nn.Linear(N_S,64)
self.fc2 = nn.Linear(64,64)
self.fc3 = nn.Linear(64,1)
self.fc3.weight.data.mul_(0.1)
self.fc3.bias.data.mul_(0.0)
# self.set_init([self.fc1, self.fc2, self.fc2])
def set_init(self,layers):
for layer in layers:
nn.init.normal_(layer.weight,mean=0.,std=0.1)
nn.init.constant_(layer.bias,0.)
def forward(self,s):
x = torch.tanh(self.fc1(s))
x = torch.tanh(self.fc2(x))
values = self.fc3(x)
return values