Spaces:
Runtime error
Runtime error
Upload model.py
#8
by liangc40 - opened
model.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
from transformers import BertModel, BertTokenizer, AdamW, get_linear_schedule_with_warmup
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from matplotlib import rc
|
| 8 |
+
from sklearn.model_selection import train_test_split
|
| 9 |
+
from sklearn.metrics import confusion_matrix, classification_report
|
| 10 |
+
from collections import defaultdict
|
| 11 |
+
from textwrap import wrap
|
| 12 |
+
|
| 13 |
+
from torch import nn, optim
|
| 14 |
+
from torch.utils.data import Dataset, DataLoader
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
|
| 17 |
+
class DepressionClassifier(nn.Module):
|
| 18 |
+
|
| 19 |
+
def __init__(self, n_classes, pre_trained_model_name):
|
| 20 |
+
super(DepressionClassifier, self).__init__()
|
| 21 |
+
self.bert = BertModel.from_pretrained(pre_trained_model_name)
|
| 22 |
+
self.drop = nn.Dropout(p=0.3)
|
| 23 |
+
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
|
| 24 |
+
|
| 25 |
+
def forward(self, input_ids, attention_mask):
|
| 26 |
+
_, pooled_output = self.bert(
|
| 27 |
+
input_ids=input_ids,
|
| 28 |
+
attention_mask=attention_mask,
|
| 29 |
+
return_dict = False #here
|
| 30 |
+
)
|
| 31 |
+
output = self.drop(pooled_output)
|
| 32 |
+
return self.out(output)
|