forked from RichardS0268/CNN-for-Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
100 lines (81 loc) · 3.94 KB
/
model.py
File metadata and controls
100 lines (81 loc) · 3.94 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# each day is displayed in 3 pixels
# use cnn to predict
from __init__ import *
class CNN5d(nn.Module):
# Input: [N, (1), 32, 15]; Output: [N, 2]
# Two Convolution Blocks
def init_weights(self, m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
torch.nn.init.xavier_uniform(m.weight)
m.bias.data.fill_(0.01)
def __init__(self):
super(CNN5d, self).__init__()
self.conv1 = nn.Sequential(OrderedDict([
('Conv', nn.Conv2d(1, 64, (5, 3), padding=(2, 1), stride=(1, 1), dilation=(1, 1))), # output size: [N, 64, 32, 15]
('BN', nn.BatchNorm2d(64, affine=True)),
('ReLU', nn.ReLU()),
('Max-Pool', nn.MaxPool2d((2,1))) # output size: [N, 64, 16, 15]
]))
self.conv1 = self.conv1.apply(self.init_weights)
self.conv2 = nn.Sequential(OrderedDict([
('Conv', nn.Conv2d(64, 128, (5, 3), padding=(2, 1), stride=(1, 1), dilation=(1, 1))), # output size: [N, 128, 16, 15]
('BN', nn.BatchNorm2d(128, affine=True)),
('ReLU', nn.ReLU()),
('Max-Pool', nn.MaxPool2d((2,1))) # output size: [N, 128, 8, 15]
]))
self.conv2 = self.conv2.apply(self.init_weights)
self.DropOut = nn.Dropout(p=0.5)
self.FC = nn.Linear(15360, 2)
self.init_weights(self.FC)
self.Softmax = nn.Softmax(dim=1)
def forward(self, x): # input: [N, 32, 15]
x = x.unsqueeze(1).to(torch.float32) # output size: [N, 1, 32, 15]
x = self.conv1(x) # output size: [N, 64, 16, 15]
x = self.conv2(x) # output size: [N, 128, 8, 15]
x = self.DropOut(x.view(x.shape[0], -1))
x = self.FC(x) # output size: [N, 2]
x = self.Softmax(x)
return x
class CNN20d(nn.Module):
# Input: [N, (1), 64, 60]; Output: [N, 2]
# Three Convolution Blocks
def init_weights(self, m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
torch.nn.init.xavier_uniform(m.weight)
m.bias.data.fill_(0.01)
def __init__(self):
super(CNN20d, self).__init__()
self.conv1 = nn.Sequential(OrderedDict([
('Conv', nn.Conv2d(1, 64, (5, 3), padding=(3, 1), stride=(3, 1), dilation=(2, 1))), # output size: [N, 64, 21, 60]
('BN', nn.BatchNorm2d(64, affine=True)),
('ReLU', nn.ReLU()),
('Max-Pool', nn.MaxPool2d((2,1))) # output size: [N, 64, 10, 60]
]))
self.conv1 = self.conv1.apply(self.init_weights)
self.conv2 = nn.Sequential(OrderedDict([
('Conv', nn.Conv2d(64, 128, (5, 3), padding=(3, 1), stride=(1, 1), dilation=(1, 1))), # output size: [N, 128, 12, 60]
('BN', nn.BatchNorm2d(128, affine=True)),
('ReLU', nn.ReLU()),
('Max-Pool', nn.MaxPool2d((2,1))) # output size: [N, 128, 6, 60]
]))
self.conv2 = self.conv2.apply(self.init_weights)
self.conv3 = nn.Sequential(OrderedDict([
('Conv', nn.Conv2d(128, 256, (5, 3), padding=(2, 1), stride=(1, 1), dilation=(1, 1))), # output size: [N, 256, 6, 60]
('BN', nn.BatchNorm2d(256, affine=True)),
('ReLU', nn.ReLU()),
('Max-Pool', nn.MaxPool2d((2,1))) # output size: [N, 256, 3, 60]
]))
self.conv3 = self.conv3.apply(self.init_weights)
self.DropOut = nn.Dropout(p=0.5)
self.FC = nn.Linear(46080, 2)
self.init_weights(self.FC)
self.Softmax = nn.Softmax(dim=1)
def forward(self, x): # input: [N, 64, 60]
x = x.unsqueeze(1).to(torch.float32) # output size: [N, 1, 64, 60]
x = self.conv1(x) # output size: [N, 64, 10, 60]
x = self.conv2(x) # output size: [N, 128, 6, 60]
x = self.conv3(x) # output size: [N, 256, 3, 60]
x = self.DropOut(x.view(x.shape[0], -1))
x = self.FC(x) # output size: [N, 2]
x = self.Softmax(x)
return x