forked from YangRui2015/2048_env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN_module.py
72 lines (61 loc) · 2.34 KB
/
NN_module.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
# -*- coding: utf-8 -*-
# NN.py
# author: yangrui
# description:
# created: 2019-10-30T16:32:31.081Z+08:00
# last-modified: 2019-10-30T16:32:31.081Z+08:00
# email: [email protected]
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# CNN网络
class CNN_Net(nn.Module):
def __init__(self, input_len, output_num, conv_size=(32, 64), fc_size=(1024, 128), out_softmax=False):
super(CNN_Net, self).__init__()
self.input_len = input_len
self.output_num = output_num
self.out_softmax = out_softmax
self.conv1 = nn.Sequential(
nn.Conv2d(1, conv_size[0], kernel_size=3, stride=1, padding=1),
# nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
)
self.conv2 = nn.Sequential(
nn.Conv2d(conv_size[0], conv_size[1], kernel_size=3, stride=1, padding=1),
# nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
# nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc1 = nn.Linear(conv_size[1] * self.input_len * self.input_len, fc_size[0])
self.fc2 = nn.Linear(fc_size[0], fc_size[1])
self.head = nn.Linear(fc_size[1], self.output_num)
def forward(self, x):
x = x.reshape(-1,1,self.input_len, self.input_len)
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
output = self.head(x)
if self.out_softmax:
output = F.softmax(output, dim=1) #值函数估计不应该有softmax
return output
# 全连接网络
class FC_Net(nn.Module):
def __init__(self, input_num, output_num, fc_size=(1024, 128), out_softmax=False):
super(FC_Net, self).__init__()
self.input_num = input_num
self.output_num = output_num
self.out_softmax = out_softmax
self.fc1 = nn.Linear(self.input_num, fc_size[0])
self.fc2 = nn.Linear(fc_size[0], fc_size[1])
self.head = nn.Linear(fc_size[1], self.output_num)
def forward(self, x):
x = x.reshape(-1, self.input_num)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
output = self.head(x)
if self.out_softmax:
output = F.softmax(output, dim=1) #值函数估计不应该有softmax
return output