-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (96 loc) · 4.33 KB
/
Copy pathmain.py
File metadata and controls
153 lines (96 loc) · 4.33 KB
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
# TODO: import necessary packages
import time
import datetime
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from embeddings import WordEmbeddings
# TODO: Define any constants you may need
EPOCHS = 10
BATCH_SIZE = 32
# use this in any functions you deem necessary once fully implemented!
word_embedding_model = WordEmbeddings()
def prepare_data():
# TODO: Prepare the data to be trained on
# TODO: Visualize some data points
# TODO: Clean your data to only keep the features you want
# TODO: Map Airline sentiment to negative = 0, neutral = 1, positive = 2
# TODO: Transform `text` into a representation our MLP can handle
# TODO: Make training and validation splits
# TODO: Implement the train, val datasets, and train, val data loaders
train_dataset = ...
val_dataset = ...
train_loader = ...
val_loader = ...
return train_dataset, val_dataset, train_loader, val_loader
def get_model() -> torch.nn.Module:
# TODO: Return your custom MLP (you may have to define a custom class above!)
pass
def train_one_epoch(model, loss_fn, train_loader, optimizer):
# TODO: Implement following the instructions below
# enable training mode on the model (hint use model.train())
# add variables to keep track of the total loss, number of correct predictions, and total number of elements
# for inputs, targets in train_loader:
# move inputs and targets to device (since device may not be CPU)
# zero out the gradients
# compute model outputs
# compute the loss
# when keeping track of the total loss, you need to use .item() to get the value from the tensor
# compute number correct in the batch and keep track of the total correct, and total processed
# backpropagate the loss
# perform and optimizer step
# return accuracy for the epoch (total correct / total processed) and the average loss
pass
def validate(model, loss_fn, val_loader):
# TODO: Implement following the instructions below
# enable evaluation mode on the model (hint use model.eval())
# add variables to keep track of the total loss, number of correct predictions, and total number of elements
# for inputs, targets in val_loader:
# move inputs and targets to device (since device may not be CPU)
# compute model outputs
# measure accuracy and record loss
# return accuracy and ave loss
pass
def train(model, train_loader, val_loader, epochs):
# TODO: Implement following the instructions below
# Define the loss function
# What kind of task are we performing? What kind of loss function should we use?
# Define the optimizer (hint: start with Adam)
# Define variables for logging (will need to plot train and validation loss and accuracy later on)
train_loss = ...
val_loss = ...
train_accuracy = ...
val_accuracy = ...
# Start training
start_time = time.time()
# Hint: use tqdm to create a progress bar. TQDM can wrap any iterable, like the range(epochs) for example
# pbar = tqdm(range(epochs))
# for epoch in pbar:
# train for one epoch (hint we may have implemented it already)
# get validation accuracy and loss (hint we may have implemented it already)
# save some logging info
# optional: update the progress bar with the train and validation loss and accuracy
# pbar.set_postfix(train_loss=train_loss, val_loss=val_loss, train_acc=train_acc1, val_acc=val_acc1)
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('=' * 50)
print(f'Training time {total_time_str}')
print(f'Max accuracy {0.0:%.4f}')
print('=' * 50)
# return logging information
return train_loss, val_loss, train_accuracy, val_accuracy
def evaluate(trained_model, sample_text):
# TODO: implement this
pass
def main():
model = get_model()
train_dataset, val_dataset, train_loader, val_loader = prepare_data()
train_loss, val_loss, train_accuracy, val_accuracy = train(model, train_loader, val_loader, EPOCHS)
# TODO: 1. plot train loss and validation loss
# TODO: 2. plot train accuracy and validation accuracy
# TODO: save the plots
# TODO: evaluate
evaluate(..., ...)
if __name__ == '__main__':
main()