-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
73 lines (63 loc) · 2.61 KB
/
CNN.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary
class Net(nn.Module):
def __init__(self, size, action_size, num_resBlocks, num_hidden):
super().__init__()
# Check if a GPU is available, otherwise use CPU
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Initial block: Convolution + Batch Normalization + ReLU
self.startBlock = nn.Sequential(
nn.Conv2d(3, num_hidden, kernel_size=3, padding='same'),
nn.BatchNorm2d(num_hidden),
nn.ReLU()
)
# Backbone with multiple residual blocks
self.backBone = nn.ModuleList(
[ResBlock(num_hidden, size) for i in range(num_resBlocks)]
)
# Policy head: Convolution + Batch Normalization + ReLU + Flatten + Linear + Softmax
self.policyHead = nn.Sequential(
nn.Conv2d(num_hidden, 32, kernel_size=3, padding='same'),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Flatten(),
nn.Linear(32 * size * size, action_size),
nn.Softmax(dim=1)
)
# Value head: Convolution + Batch Normalization + ReLU + Flatten + Linear + Tanh
self.valueHead = nn.Sequential(
nn.Conv2d(num_hidden, size, kernel_size=3, padding='same'),
nn.BatchNorm2d(size),
nn.ReLU(),
nn.Flatten(),
nn.Linear(size * size * size, 1),
nn.Tanh()
)
# Set the model to run on the selected device
self.to(self.device)
def forward(self, x):
# Forward pass through the network
x = self.startBlock(x)
for resBlock in self.backBone:
x = resBlock(x)
policy = self.policyHead(x)
value = self.valueHead(x)
return policy, value
class ResBlock(nn.Module):
def __init__(self, num_hidden, size):
super().__init__()
# Residual block: Convolution + Batch Normalization + ReLU + Convolution + Batch Normalization
self.conv1 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding='same')
self.bn1 = nn.BatchNorm2d(num_hidden)
self.conv2 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding='same')
self.bn2 = nn.BatchNorm2d(num_hidden)
def forward(self, x):
# Forward pass through the residual block
residual = x
x = F.relu(self.bn1(self.conv1(x)))
x = self.bn2(self.conv2(x))
x += residual
x = F.relu(x)
return x