-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
126 lines (95 loc) · 4.21 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
114
115
116
117
118
119
120
121
122
123
124
125
126
import torch.nn as nn
from torch.nn import BCEWithLogitsLoss
from transformers import ElectraModel, ElectraPreTrainedModel
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics.pairwise import manhattan_distances
import numpy as np
import pandas as pd
import os
def cosine_sim_output(analysis_result):
data_path = 'data/'
result = pd.read_csv(os.path.join(data_path, 'last_data.csv'), encoding="utf-8")
for i in analysis_result:
a_list = i["scores"]
b_list = i["labels"]
new = pd.DataFrame([a_list], columns=b_list)
new['anger'] = new['anger']+new['annoyance']+new['disgust']
new['sadness'] = new['sadness']+new['grief']
new['joy'] = new['joy']+new['amusement']
new['confusion'] = new['confusion']+new['nervousness']
new['disappointment'] = new['disappointment']+new['remorse']
new['admiration'] = new['admiration']+new['surprise']
new = new.drop(['annoyance','disgust','grief','amusement','nervousness','remorse','surprise'], axis=1)
temp = result.iloc[:,[i for i in range(3,24)]]
cosine_sim = cosine_similarity(temp[::1],[new.iloc[0]])
indx = np.argsort(cosine_sim, axis=0)
indx = indx[-3:] # top-K
topk = result.iloc[indx[0]]
for i in indx[1::]:
topk = pd.concat([topk, result.iloc[i]], axis=0)
return topk
def manhattan_dis_output(analysis_result):
data_path = '../../data/'
music_data = pd.read_csv(os.path.join(data_path, 'last_data.csv'), encoding="utf-8")
labels = ['admiration', 'anger', 'approval', 'caring', 'confusion',
'curiosity', 'desire', 'disappointment', 'disapproval', 'embarrassment',
'excitement', 'fear', 'gratitude', 'joy', 'love', 'optimism',
'pride', 'realization', 'relief', 'sadness', 'neutral']
result = music_data.groupby(['title', 'artist'], as_index=False)[labels].agg('sum')
for i in analysis_result:
a_list = i["scores"]
b_list = i["labels"]
new = pd.DataFrame([a_list], columns=b_list)
new['anger'] = new['anger']+new['annoyance']+new['disgust']
new['sadness'] = new['sadness']+new['grief']
new['joy'] = new['joy']+new['amusement']
new['confusion'] = new['confusion']+new['nervousness']
new['disappointment'] = new['disappointment']+new['remorse']
new['admiration'] = new['admiration']+new['surprise']
new = new.drop(['annoyance','disgust','grief','amusement','nervousness','remorse','surprise'], axis=1)
temp = result.iloc[:,[i for i in range(2,23)]]
manhattan_dis = manhattan_distances(temp[::1],[new.iloc[0]])
x = 360
t = 0 # index
for i, j in enumerate(manhattan_dis):
if j <= x:
t = i
x = j
emo = []
for i, j in enumerate(result.iloc[t][2::]):
if j > 0.5:
x = result.columns[i+2]
emo.append(x)
name = result.iloc[t]['title']
artist = result.iloc[t]['artist']
return name, artist, emo
class ElectraForMultiLabelClassification(ElectraPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.electra = ElectraModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, self.config.num_labels)
self.loss_fct = BCEWithLogitsLoss()
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
):
discriminator_hidden_states = self.electra(
input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds
)
pooled_output = discriminator_hidden_states[0][:, 0]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
outputs = (logits,) + discriminator_hidden_states[1:] # add hidden states and attention if they are here
if labels is not None:
loss = self.loss_fct(logits, labels)
outputs = (loss,) + outputs
return outputs # (loss), logits, (hidden_states), (attentions)