Spaces:
Runtime error
Runtime error
File size: 4,652 Bytes
0645704 |
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 |
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
import numpy as np
import urllib.request
import zipfile
import os
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
from transformers import BertTokenizer, BertModel
from model import SentimentClassifier
# Download dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00331/sentiment%20labelled%20sentences.zip"
filename = "sentiment.zip"
if not os.path.exists(filename):
urllib.request.urlretrieve(url, filename)
# Extract dataset
with zipfile.ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall()
# Load dataset
filepath_dict = {'yelp': 'sentiment labelled sentences/yelp_labelled.txt',
'amazon': 'sentiment labelled sentences/amazon_cells_labelled.txt',
'imdb': 'sentiment labelled sentences/imdb_labelled.txt'}
df_list = []
for source, filepath in filepath_dict.items():
df = pd.read_csv(filepath, names=['sentence', 'label'], sep='\t')
df['source'] = source
df_list.append(df)
df = pd.concat(df_list)
# Split dataset into train and test sets
sentences = df['sentence'].values
labels = df['label'].values
train_sentences, test_sentences, train_labels, test_labels = train_test_split(
sentences, labels, test_size=0.25)
# Define tokenizer
tokenizer = BertTokenizer.from_pretrained(
'bert-base-uncased', do_lower_case=True)
# Define dataset
class SentimentDataset(Dataset):
def __init__(self, sentences, labels, tokenizer, max_len):
self.sentences = sentences
self.labels = labels
self.tokenizer = tokenizer
self.max_len = max_len
def __len__(self):
return len(self.sentences)
def __getitem__(self, item):
sentence = str(self.sentences[item])
label = self.labels[item]
encoding = self.tokenizer.encode_plus(
sentence,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt'
)
return {'sentence': sentence,
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'label': torch.tensor(label, dtype=torch.long)}
# Define model
# Set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Define hyperparameters
MAX_LEN = 100
BATCH_SIZE = 16
EPOCHS = 5
# Define dataloaders
train_dataset = SentimentDataset(
train_sentences, train_labels, tokenizer, MAX_LEN)
test_dataset = SentimentDataset(
test_sentences, test_labels, tokenizer, MAX_LEN)
train_dataloader = DataLoader(
train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
test_dataloader = DataLoader(
test_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
# Define model and optimizer
model = SentimentClassifier(2)
model = model.to(device)
optimizer = optim.Adam(model.parameters(), lr=2e-5)
# Define loss function
criterion = nn.CrossEntropyLoss()
# Train model
for epoch in range(EPOCHS):
print('Epoch:', epoch+1)
train_loss = 0
train_acc = 0
model.train()
for batch in train_dataloader:
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
labels = batch['label'].to(device)
optimizer.zero_grad()
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
train_acc += (outputs.argmax(1) == labels).sum().item()
train_loss /= len(train_dataloader)
train_acc /= len(train_dataset)
print('Train loss:', train_loss, 'Train accuracy:', train_acc)
model.eval()
test_loss = 0
test_acc = 0
with torch.no_grad():
for batch in test_dataloader:
input_ids = batch['input_ids'].to(device)
attention_mask = batch['attention_mask'].to(device)
labels = batch['label'].to(device)
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
loss = criterion(outputs, labels)
test_loss += loss.item()
test_acc += (outputs.argmax(1) == labels).sum().item()
test_loss /= len(test_dataloader)
test_acc /= len(test_dataset)
print('Test loss:', test_loss, 'Test accuracy:', test_acc)
torch.save(model.cpu().state_dict(), 'sentiment_model.pth')
|