File size: 6,809 Bytes
a3d0be2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from transformers import BertConfig,BertModel
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Function
from huggingface_hub import PyTorchModelHubMixin


class CSIBERT(nn.Module):
    def __init__(self,bertconfig,input_dim,carrier_attention=False, time_emb=True):
        super().__init__()
        self.bertconfig=bertconfig
        self.auto_pos=time_emb
        self.bert=BertModel(bertconfig)
        self.hidden_dim=bertconfig.hidden_size
        self.input_dim=input_dim
        self.carrier_attention=carrier_attention
        if carrier_attention:
            self.attention = SelfAttention(bertconfig.max_position_embeddings, 128, input_dim)
            self.emb=nn.Sequential(
            nn.Linear(input_dim, 64),
            nn.ReLU(),
            nn.Linear(64, self.hidden_dim)
            )

        else:
            self.emb=nn.Sequential(
            nn.Linear(input_dim, 64),
            nn.ReLU(),
            nn.Linear(64, self.hidden_dim)
            )


    def forward(self,x,attn_mask=None,timestamp=None):
        if self.carrier_attention:
            x=x.permute(0,2,1)
            attn_mat = self.attention(x)
            x = torch.bmm(attn_mat, x)
            x = x.permute(0, 2, 1)
        x=self.emb(x)
        if timestamp is not None:
            pos_emb=self.positional_embedding(timestamp)
            x=x+pos_emb
        y=self.bert(inputs_embeds=x,attention_mask=attn_mask, output_hidden_states=True)
        y=y.hidden_states[-1]
        return y

    def mask(self,batch_size=1,min=None,max=None,std=None,avg=None):
        if std is not None and avg is not None:
            device=std.device
            result=torch.randn((batch_size, self.bertconfig.max_position_embeddings ,self.input_dim)).to(device)
            result=result*std+avg
        else:
            result=torch.rand((batch_size, self.bertconfig.max_position_embeddings ,self.input_dim))
            if min is not None and max is not None:
                device = max.device
                result=result.to(device)
                result=result*(max-min)+min
        return result

    def positional_embedding(self,timestamp,t=1):
        timestamp**=t
        device=timestamp.device
        min=torch.min(timestamp,dim=-1,keepdim=True)[0]
        max=torch.max(timestamp,dim=-1,keepdim=True)[0]
        ran=timestamp.shape[-1]
        timestamp=(timestamp-min)/(max-min)*ran
        d_model=self.hidden_dim
        dim=torch.tensor(list(range(d_model))).to(device)
        batch_size,length=timestamp.shape
        timestamp=timestamp.unsqueeze(2).repeat(1, 1, d_model)
        dim=dim.reshape([1,1,-1]).repeat(batch_size,length,1)
        sin_emb = torch.sin(timestamp/10000**(dim//2*2/d_model))
        cos_emb = torch.cos(timestamp/10000**(dim//2*2/d_model))
        mask=torch.zeros(d_model).to(device)
        mask[::2]=1
        emb=sin_emb*mask+cos_emb*(1-mask)
        return emb

class Token_Classifier(nn.Module):
    def __init__(self,bert,class_num=52):
        super().__init__()
        self.bert=bert
        self.classifier=nn.Sequential(
            nn.Linear(bert.hidden_dim, 64),
            nn.ReLU(),
            nn.Linear(64, class_num)
        )

    def forward(self,x,attn_mask=None,timestamp=None):
        x=self.bert(x,attn_mask,timestamp)
        x=self.classifier(x)
        return x

# GRL
class GRL(Function):
    @staticmethod
    def forward(ctx, x, alpha=1):
        ctx.alpha = alpha
        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        output = grad_output.neg() * ctx.alpha
        return output, None


class Sequence_Classifier(nn.Module):
    def __init__(self,bert,class_num=6):
        super().__init__()
        self.bert=bert
        self.query = nn.Linear(bert.hidden_dim, 64)
        self.key = nn.Linear(bert.hidden_dim, 64)
        self.value = nn.Linear(bert.hidden_dim, 64)
        self.self_attention = nn.MultiheadAttention(embed_dim=64, num_heads=4, dropout=0, batch_first=True)
        self.norm1=nn.BatchNorm1d(64)
        self.Linear=nn.Linear(64, 64)
        self.norm2 = nn.BatchNorm1d(bert.bertconfig.max_position_embeddings * 64)
        self.classifier=nn.Sequential(
            nn.Linear(bert.bertconfig.max_position_embeddings * 64, 64),
            nn.ReLU(),
            nn.Linear(64, class_num)
        )
        self.GRL = GRL()


    def forward(self,x,attn_mask=None,timestamp=None,adversarial=False,alpha=1):
        x=self.bert(x,attn_mask,timestamp)
        if adversarial:
            x = self.GRL.apply(x,alpha)
        batch_size,length,hidden_dim=x.shape
        x_attn, _ = self.self_attention(self.query(x), self.key(x), self.value(x))
        x = x + x_attn
        x1 = x.reshape(-1, 64)
        x1 = self.norm1(x1)
        x1 = self.Linear(x1)
        x2 = x1.reshape(batch_size, -1)
        x2 = self.norm2(x2)
        x2=self.classifier(x2)
        return x2

class SelfAttention(nn.Module):
    def __init__(self, input_dim, da, r):
        super().__init__()
        self.ws1 = nn.Linear(input_dim, da, bias=False)
        self.ws2 = nn.Linear(da, r, bias=False)

    def forward(self, h):
        attn_mat = F.softmax(self.ws2(torch.tanh(self.ws1(h))), dim=1)
        attn_mat = attn_mat.permute(0, 2, 1)
        return attn_mat

class Classification(nn.Module):
    def __init__(self, csibert, class_num, hs=64, da=128, r=4):
        super().__init__()
        self.bert = csibert
        self.attention = SelfAttention(hs, da, r)
        self.classifier = nn.Sequential(
            nn.Linear(hs * r, 256),
            nn.ReLU(),
            nn.Linear(256, class_num)
        )
        self.GRL = GRL()


    def forward(self, x, attn=None, timestamp=None,adversarial=False):
        x = self.bert(x, attn, timestamp)
        if adversarial:
            x = self.GRL.apply(x)
        attn_mat = self.attention(x)
        m = torch.bmm(attn_mat, x)
        flatten = m.view(m.size()[0], -1)
        res = self.classifier(flatten)
        return res

class CSI_BERT(    nn.Module,
    PyTorchModelHubMixin
):
    def __init__(self, max_len=100, hs=64, layers=4, heads=4, intermediate_size=128, carrier_dim=52, carrier_attn=False, time_embedding=True):
        super().__init__()
        self.config = BertConfig(max_position_embeddings=max_len, hidden_size=hs, num_hidden_layers=layers,num_attention_heads=heads, intermediate_size=intermediate_size)
        self.model = CSIBERT(self.config,carrier_dim,carrier_attn,time_embedding)


    def forward(self, x, attn_mask=None, timestamp=None):
        return self.model(x,attn_mask,timestamp)