-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2.py
62 lines (47 loc) · 1.91 KB
/
assignment2.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
import argparse
import numpy as np
from utils import load_dataset
from model.model_utils import bag_of_words_matrix, labels_matrix
from model.ffnn import NeuralNetwork
from helper import batch_train, minibatch_train
DATA_PATH = './data/dataset.csv'
def main():
parser = argparse.ArgumentParser(
description='Train feedforward neural network'
)
parser.add_argument(
'--minibatch', dest='minibatch',
help='Train feedforward neural network with mini-batch gradient descent/SGD',
action='store_true'
)
parser.add_argument(
'--train', dest='train',
help='Turn on this flag when you are ready to train the model with backpropagation.',
action='store_true'
)
args = parser.parse_args()
sentences, intent, unique_intent = load_dataset(DATA_PATH)
############################ STUDENT SOLUTION ####################
# YOUR CODE HERE
# TODO:
# 1) Convert the sentences and intent to matrices using
# `bag_of_words_matrix()` and `labels_matrix()`.
# 2) Initiallize the model Class with the appropriate parameters
# Convert sentences to bag-of-words matrix
X = bag_of_words_matrix(sentences)
# Convert intent labels to matrix
Y = labels_matrix((intent, unique_intent))
# Initialize the model
input_size = X.shape[0] # vocabulary size
hidden_size = 150
num_classes = Y.shape[0] # number of unique intents
model = NeuralNetwork(input_size, hidden_size, num_classes)
##################################################################
if not args.minibatch:
print("Training FFNN using batch gradient descent...")
batch_train(X, Y, model, train_flag=args.train)
else:
print("Training FFNN using mini-batch gradient descent...")
minibatch_train(X, Y, model, train_flag=args.train)
if __name__ == "__main__":
main()