-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_dem.py
66 lines (49 loc) · 1.75 KB
/
cnn_dem.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import os
import json
import torch
from torch import nn
import torch.nn.functional as F
from torch.autograd import *
import numpy as np
import sys
sys.path.append('tools')
import parse, py_op
def conv3(in_channels, out_channels, stride=1, kernel_size=3):
return nn.Conv1d(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=1, bias=False)
class CNN(nn.Module):
def __init__(self, args):
super(CNN, self).__init__()
self.args = args
self.dd_embedding = nn.Embedding (args.n_ehr, args.embed_size ) #demographics
self.dd_mapping = nn.Sequential(
nn.Linear ( args.embed_size, args.embed_size),
nn.ReLU ( ),
nn.Dropout(0.1),
nn.Linear ( args.embed_size, args.embed_size),
nn.ReLU ( ),
nn.Dropout(0.1),
)
self.pooling = nn.AdaptiveMaxPool1d(1)
# # unstructureL clinical notes
# if args.use_unstructure:
# self.vocab_layer = nn.Sequential(
# nn.Dropout(0.2),
# conv3(args.embed_size, args.embed_size, 2, 2),
# nn.BatchNorm1d(args.embed_size),
# nn.Dropout(0.2),
# nn.ReLU(),
# )
def forward(self, dd):
if dd is not None:
# demo embedding
dsize = list(dd.size()) + [-1]
d = self.dd_embedding(dd.view(-1)).view(dsize)
d = self.dd_mapping(d)
d = torch.transpose(d, 1,2).contiguous() # (50, 200, 100)
d = self.pooling(d)
d = d.view((d.size(0), -1))
return d