-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.py
113 lines (95 loc) · 4.6 KB
/
Model.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
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
from transformers import BertModel
import Data_Preprocessing
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
kernel = 4
input_ff = int(393216/(kernel * kernel))
zwischenlayer1 = int(100)
zwischenlayer2 = int(10)
self.bert = BertModel.from_pretrained("bert-base-uncased")
self.pooling = nn.MaxPool2d(kernel)
self.linear11 = nn.Linear(input_ff, zwischenlayer1)
self.linear12 = nn.Linear(zwischenlayer1, zwischenlayer2)
self.linear13 = nn.Linear(zwischenlayer2, 2)
self.linear21 = nn.Linear(input_ff, zwischenlayer1)
self.linear22 = nn.Linear(zwischenlayer1, zwischenlayer2)
self.linear23 = nn.Linear(zwischenlayer2, 2)
def forward_x1(self, input_data):
bert_output = self.bert(**input_data)
bert_output = self.pooling(bert_output[0])
bert_output = bert_output[0].view(-1, 24576)
x1 = torch.sigmoid(self.linear11(bert_output))
x1 = torch.sigmoid(self.linear12(x1))
x1 = torch.sigmoid(self.linear13(x1))
return x1
def forward_x2(self, input_data):
bert_output = self.bert(**input_data)
bert_output = self.pooling(bert_output[0])
bert_output = bert_output[0].view(-1, 24576)
x1 = torch.sigmoid(self.linear21(bert_output))
x1 = torch.sigmoid(self.linear22(x1))
x1 = torch.sigmoid(self.linear23(x1))
return x1
def train_x1(self, features, labels, loss_function, optimizer, epochs):
log_interval = 5
evaluation_list = list()
for epoch in range(epochs):
for i, data in enumerate(features):
data = Data_Preprocessing.preprocessing(data)
optimizer.zero_grad()
output1 = self.forward_x1(data)
target = torch.tensor(labels[i])
target = target.to(torch.float32)
# print(torch.tensor(output1[0][0]),torch.tensor(float(labels[i]['multi-author'])))
print(output1[0],target)
# loss_x1 = loss_function(torch.tensor(output1[0][0]), torch.tensor(float(labels[i]['multi-author'])))
loss_x1 = loss_function(output1[0],target)
evaluation_list.append(([output1,torch.tensor(labels[i]), loss_x1]))
print(i, ' ist=', output1, ' soll=', torch.tensor(labels[i]), ' Loss=', loss_x1)
loss_x1.backward()
optimizer.step()
return evaluation_list
def train_x2(self, features, labels, loss_function, optimizer, epochs):
log_interval = 5
evaluation_list = list()
for epoch in range(epochs):
for i, data in enumerate(features):
data = Data_Preprocessing.preprocessing(data)
optimizer.zero_grad()
output1 = self.forward_x2(data)
target = torch.tensor(labels[i])
target = target.to(torch.float32)
# print(torch.tensor(output1[0][0]),torch.tensor(float(labels[i]['multi-author'])))
print(output1, target)
# loss_x1 = loss_function(torch.tensor(output1[0][0]), torch.tensor(float(labels[i]['multi-author'])))
loss_x1 = loss_function(output1[0], target)
evaluation_list.append(([output1, torch.tensor(labels[i]), loss_x1]))
print(i, ' ist=', output1, ' soll=', torch.tensor(labels[i]), ' Loss=', loss_x1)
loss_x1.backward()
optimizer.step()
return evaluation_list
def test_model_x1(self, features, labels, loss_function):
log_interval = 5
evaluation_list = list()
for index, data in enumerate(features):
data = Data_Preprocessing.preprocessing(data)
output = self.forward_x1(data)
target = torch.tensor(labels[index])
target = target.to(torch.float32)
loss = loss_function(output[0], target)
evaluation_list.append(([output,torch.tensor(labels[index]['multi-author']), loss]))
return evaluation_list
def test_model_x2(self, features, labels, loss_function):
log_interval = 5
evaluation_list = list()
for index, data in enumerate(features):
data = Data_Preprocessing.preprocessing(data)
output = self.forward_x2(data)
target = torch.tensor(labels[index])
target = target.to(torch.float32)
loss = loss_function(output[0], target)
evaluation_list.append(([output, torch.tensor(labels[index]), loss]))
return evaluation_list