-
Notifications
You must be signed in to change notification settings - Fork 0
/
model1DCNN.py
executable file
·66 lines (56 loc) · 1.83 KB
/
model1DCNN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 28 17:23:32 2021
1D-CNN Model
Abdullah BAS
abdullah.bas@boun.edu.tr
BME Bogazici University
Istanbul / Uskudar
@author: abas
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self,activation):
"""Initializing the 1D-CNN
Args:
activation (nn.Functional): Activation function comes
from optuna optimization
"""
super(Net, self).__init__()
self.conv1 = nn.Conv1d(1, 14, 40)
self.pool = nn.MaxPool1d(2)
self.conv2 = nn.Conv1d(14, 7, 40)
self.bn1=nn.BatchNorm1d(14)
self.bn0=nn.BatchNorm1d(1)
self.dropout=nn.Dropout(p=0.2)
self.act=(activation)
self.conv3 = nn.Conv1d(7, 7, 40,padding=0)
self.fc1 = nn.Linear(7*68, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
self.fc4 = nn.Linear(11, 2)
def forward(self, x,age):
"""Forward blocks of the model. Input data moves through from this function.
Args:
x (float): input MRS spectrum
age (float): Age of patient
Returns:
[float]: output of the model
"""
x = self.pool(self.act(self.conv1(x))) # -> n, 6, 14, 14
x=self.dropout(x)
x=self.bn1(x)
x = self.pool(self.act(self.conv2(x))) # -> n, 16, 5, 5
x = self.pool(self.act(self.conv3(x))) # -> n, 16, 5, 5
x=self.dropout(x)
x = x.view(-1, x.shape[1] *x.shape[2])
x = self.act(self.fc1(x))
x = self.act(self.fc2(x))
x = self.fc3(x)
age=(age).unsqueeze(1)/100
x=torch.cat((x,age),dim=1)
x = self.fc4(x) # -> n, 10
return x