File size: 12,748 Bytes
674a23c
 
 
 
 
 
 
 
 
 
 
 
c37bb09
674a23c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from typing import Callable, List, Tuple

import numpy as np
import pandas as pd

from gensim.models.doc2vec import Doc2Vec, TaggedDocument

import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.text import Tokenizer


def read_data(filepath="./csvs/"):
    """
    Reading CSV files of the dataset.

    Parameters:
    ----------
    - filepath : str
        Defines the path that contains the CSV files.

    Returns:
    --------
    A tuple contains the following:
        - X_train : pd.DataFrame
        - y_train : pd.Series
        - X_test : pd.DataFrame
        - y_test : pd.Series
    """

    X_train = pd.read_csv(filepath + "X_train.csv")
    X_train = X_train.iloc[:, 1:]

    X_test = pd.read_csv(filepath + "X_test.csv")
    X_test = X_test.iloc[:, 1:]

    y_train = pd.read_csv(filepath + "y_train.csv")
    y_train = y_train.iloc[:, 1:]

    y_test = pd.read_csv(filepath + "y_test.csv")
    y_test = y_test.iloc[:, 1:]

    return X_train, X_test, y_train, y_test


def train_model(
    model_building_func: Callable[[], keras.models.Sequential],
    X_train_vectors: pd.DataFrame | np.ndarray | tf.Tensor,
    y_train: pd.Series,
    k: int = 4,
    num_epochs: int = 30,
    batch_size: int = 64,
) -> Tuple[
    List[keras.models.Sequential],
    List[List[float]],
    List[List[float]],
    List[List[float]],
    List[List[float]],
]:
    """
    Trains a model on `X_train_vectors` and `y_train` using k-fold cross-validation.

    Parameters:
    -----------
    - model_building_func : Callable[[], tf.keras.models.Sequential]
        A function that builds and compiles a Keras Sequential model.
    - X_train_vectors : pd.DataFrame
        The training input data.
    - y_train : pd.Series
        The training target data.
    - k : int, optional
        The number of folds for cross-validation (default is 4).
    - num_epochs : int, optional
        The number of epochs to train for (default is 30).
    - batch_size : int, optional
        The batch size to use during training (default is 64).

    Returns:
    --------
    A tuple containing the following items:
        - all_models : List[keras.models.Sequential]
            A list of `k` trained models.
        - all_losses : List[List[float]]
            A `k` by `num_epochs` list containing the training losses for each fold.
        - all_val_losses : List[List[float]]
            A `k` by `num_epochs` list containing the validation losses for each fold.
        - all_acc : List[List[float]]
            A `k` by `num_epochs` list containing the training accuracies for each fold.
        - all_val_acc : List[List[float]]
            A `k` by `num_epochs` list containing the validation accuracies for each fold.
    """

    num_validation_samples = len(X_train_vectors) // k

    all_models = []
    all_losses = []
    all_val_losses = []
    all_accuracies = []
    all_val_accuracies = []

    for fold in range(k):
        print(f"fold: {fold+1}")
        validation_data = X_train_vectors[
            num_validation_samples * fold : num_validation_samples * (fold + 1)
        ]
        validation_targets = y_train[
            num_validation_samples * fold : num_validation_samples * (fold + 1)
        ]

        training_data = np.concatenate(
            [
                X_train_vectors[: num_validation_samples * fold],
                X_train_vectors[num_validation_samples * (fold + 1) :],
            ]
        )
        training_targets = np.concatenate(
            [
                y_train[: num_validation_samples * fold],
                y_train[num_validation_samples * (fold + 1) :],
            ]
        )

        model = model_building_func()
        history = model.fit(
            training_data,
            training_targets,
            validation_data=(validation_data, validation_targets),
            epochs=num_epochs,
            batch_size=batch_size,
        )

        all_models.append(model)
        all_losses.append(history.history["loss"])
        all_val_losses.append(history.history["val_loss"])
        all_accuracies.append(history.history["accuracy"])
        all_val_accuracies.append(history.history["val_accuracy"])

    return (all_models, all_losses, all_val_losses, all_accuracies, all_val_accuracies)


def print_testing_loss_accuracy(
    all_models: List[keras.models.Sequential],
    X_test_vectors: pd.DataFrame | np.ndarray | tf.Tensor,
    y_test: pd.Series,
) -> None:
    """
    Displaying testing loss and testing accuracy of each model in `all_models`,
    and displaying their average.

    Parameters:
    ------------
    - all_models : List[keras.models.Sequential]
        A list of size `k` contains trained models.
    - X_test_vectors : pd.DataFrame
        Contains testing vectors.
    - y_test : pd.Series
        Contains testing labels.
    """

    sum_testing_losses = 0.0
    sum_testing_accuracies = 0.0

    for i, model in enumerate(all_models):
        print(f"model: {i+1}")
        loss_accuracy = model.evaluate(X_test_vectors, y_test, verbose=1)
        sum_testing_losses += loss_accuracy[0]
        sum_testing_accuracies += loss_accuracy[1]
        print("====" * 20)

    num_models = len(all_models)
    avg_testing_loss = sum_testing_losses / num_models
    avg_testing_acc = sum_testing_accuracies / num_models
    print(f"average testing loss: {avg_testing_loss:.3f}")
    print(f"average testing accuracy: {avg_testing_acc:.3f}")


def calculate_average_measures(
    all_losses: list[list[float]],
    all_val_losses: list[list[float]],
    all_accuracies: list[list[float]],
    all_val_accuracies: list[list[float]],
) -> Tuple[
    List[keras.models.Sequential],
    List[List[float]],
    List[List[float]],
    List[List[float]],
    List[List[float]],
]:
    """
    Calculate the average measures of cross-validated results.

    Parameters:
    ------------
    - all_losses : List[List[float]]
        A `k` by `num_epochs` list contains the values of training losses.
    - all_val_losses : List[List[float]]
        A `k` by `num_epochs` list contains the values of validation losses.
    - all_accuracies : List[List[float]]
        A `k` by `num_epochs` list contains the values of training accuracies.
    - all_val_accuracies : List[List[float]]
        A `k` by `num_epochs` list contains the values of validation accuracies.

    Returns:
    --------
    A tuple containing the following items:
        - avg_loss_hist : List[float]
            A list of length `num_epochs` contains the average of training losses.
        - avg_val_loss_hist : List[float]
            A list of length `num_epochs` contains the average of validaton losses.
        - avg_acc_hist : List[float]
            A list of length `num_epochs` contains the average of training accuracies.
        - avg_val_acc_hist : List[float]
            A list of length `num_epochs` contains the average of validation accuracies.
    """

    num_epochs = len(all_losses[0])
    avg_loss_hist = [np.mean([x[i] for x in all_losses]) for i in range(num_epochs)]
    avg_val_loss_hist = [
        np.mean([x[i] for x in all_val_losses]) for i in range(num_epochs)
    ]
    avg_acc_hist = [np.mean([x[i] for x in all_accuracies]) for i in range(num_epochs)]
    avg_val_acc_hist = [
        np.mean([x[i] for x in all_val_accuracies]) for i in range(num_epochs)
    ]

    return (avg_loss_hist, avg_val_loss_hist, avg_acc_hist, avg_val_acc_hist)


class Doc2VecModel:
    """Responsible of creating, initializing, and training Doc2Vec embeddings model."""

    def __init__(self, vector_size=50, min_count=2, epochs=100, dm=1, window=5) -> None:
        """
        Initalize a Doc2Vec model.

        Parameters:
        ------------
        - vector_size : int, optional
            Dimensionality of the feature vectors (Default is 50).
        - min_count : int, optional
            Ignores all words with total frequency lower than this (Default is 2).
        - epochs : int, optional
            Represents the number of training epochs (Default is 100).
        - dm : int, optional
            Defines the training algorithm. If `dm=1`, 'distributed memory' (PV-DM) is used.
            Otherwise, `distributed bag of words` (PV-DBOW) is employed (Default is 1).
        - window : int, optional
            The maximum distance between the current and predicted word within a
            sentence (Default is 5).
        """

        self.doc2vec_model = Doc2Vec(
            vector_size=vector_size,
            min_count=min_count,
            epochs=epochs,
            dm=dm,
            seed=865,
            window=window,
        )

    def train_doc2vec_embeddings_model(
        self, tagged_docs_train: List[TaggedDocument]
    ) -> Doc2Vec:
        """
        Train Doc2Vec model on `tagged_docs_train`.

        Parameters:
        ------------
        - tagged_docs_train : list[TaggedDocument]
            Contains the required format of training Doc2Vec model.

        Returns:
        --------
        - doc2vec_model : Doc2Vec
            The trained Doc2Vec model.
        """

        self.doc2vec_model.build_vocab(tagged_docs_train)
        self.doc2vec_model.train(
            tagged_docs_train,
            total_examples=self.doc2vec_model.corpus_count,
            epochs=self.doc2vec_model.epochs,
        )

        return self.doc2vec_model


class GloveModel:
    """Responsible for creating and generating the glove embedding layer"""

    def __init__(self) -> None:
        pass

    def _generate_glove_embedding_index(
        self, glove_file_path: str = "GloVe/glove.6B.50d.txt"
    ) -> dict:
        """
        Responsible for generating glove embedding index.

        Parameters:
        ------------
        - glove_file_path : str
            Defines the path of the pretrained GloVe embeddings text file
            (Default is "GloVe/glove.6B.50d.txt").

        Returns:
        --------
        - embedding_index : dict
            Contains each word as a key, and its co-effeicents as a value.
        """

        embeddings_index = {}
        with open(glove_file_path, encoding="utf8") as f:
            for line in f:
                values = line.split()
                word = values[0]
                coefs = np.asarray(values[1:], dtype="float32")
                embeddings_index[word] = coefs

        return embeddings_index

    def _generate_glove_embedding_matrix(
        self, word_index: dict, embedding_index: dict, max_length: int
    ) -> np.ndarray:
        """
        Generating embedding matrix of each word in `word_index`.

        Parameters:
        -----------
        - word_index : dict
            Contains words as keys with there indicies as values.
        - embedding_index : dict
            Contains each word as a key, and its co-effeicents as a value.
        - max_length : int
            Defines the size of the embedding vector of each word in the
            embedding matrix.

        Returns:
        --------
        - embedding_matrix : np.ndarray
            Contains all embedding vectors for each word in`word_index`.
        """

        embedding_matrix = np.zeros((len(word_index) + 1, max_length))

        for word, i in word_index.items():
            embedding_vector = embedding_index.get(word)
            if embedding_vector is not None:
                embedding_matrix[i] = embedding_vector

        return embedding_matrix

    def generate_glove_embedding_layer(
        self, glove_tokenizer: Tokenizer, max_length: int = 50
    ) -> keras.layers.Embedding:
        """
        Create GloVe embedding layer for later usage in the neural network.

        Paramters:
        ----------
        - glove_tokenizer : Tokenizer
            Trained tokenizer on training data to extract word index from it.
        - max_length : int, optional
            Defines the maximum length of the output embedding vector for
            each word. (Default is 50).

        Returns:
        --------
        - embedding_layer : keras.layers.Embedding
            An embedding layer of size `word index + 1` by `max_length` with
            trained weights that can be used a vectorizer of case facts.
        """

        word_index = glove_tokenizer.word_index

        embedding_index = self._generate_glove_embedding_index()
        embedding_matrix = self._generate_glove_embedding_matrix(
            word_index, embedding_index, max_length
        )

        embedding_layer = keras.layers.Embedding(
            len(word_index) + 1,
            max_length,
            weights=[embedding_matrix],
            input_length=max_length,
            trainable=False,
        )

        return embedding_layer