forked from Thinklab-SJTU/EDA-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnd.py
62 lines (51 loc) · 1.82 KB
/
rnd.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
import torch.nn.functional as F
import torch.nn as nn
import torch
import torch.optim as optim
import numpy as np
from torch.nn import init
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class RNDModel(nn.Module):
def __init__(self, input_size, output_size):
super(RNDModel, self).__init__()
self.input_size = input_size
self.output_size = output_size
feature_output = 7 * 7 * 64
self.predictor = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=8, stride=4),
nn.LeakyReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.LeakyReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.LeakyReLU(),
Flatten(),
nn.Linear(feature_output, 512),
nn.ReLU(),
nn.Linear(512, 512),
# nn.ReLU(),
# nn.Linear(512, 512)
)
self.target = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=8, stride=4),
nn.LeakyReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.LeakyReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.LeakyReLU(),
Flatten(),
nn.Linear(feature_output, 512)
)
# Initialize weights
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
init.orthogonal_(m.weight, np.sqrt(2))
m.bias.data.zero_()
# Set target parameters as untrainable
for param in self.target.parameters():
param.requires_grad = False
def forward(self, next_obs):
target_feature = self.target(next_obs)
predict_feature = self.predictor(next_obs)
return predict_feature, target_feature