This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| emb_dim = 64 | |
| dropout_rate = 0.3 | |
| n_labels = y.shape[1] | |
| learning_rate = 0.0006 | |
| loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True) | |
| metric = tf.keras.metrics.CategoricalAccuracy('accuracy') | |
| opt = tf.keras.optimizers.Adam(learning_rate = learning_rate) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # build the model | |
| keras_model = Sequential() | |
| keras_model.add(Embedding(vocab_size, output_dim = emb_dim, input_length=max_len)) | |
| keras_model.add(Dropout(dropout_rate)) | |
| keras_model.add(Conv1D(50, 3, activation='relu', padding='same', strides=1)) | |
| keras_model.add(MaxPool1D()) | |
| keras_model.add(Dropout(dropout_rate)) | |
| keras_model.add(Conv1D(100, 3, activation='relu', padding='same', strides=1)) | |
| keras_model.add(MaxPool1D()) | |
| keras_model.add(Dropout(dropout_rate)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| y_pred = to_categorical(np.argmax(keras_model.predict(tokenised_text_test), axis=1)) | |
| print(classification_report(y_test, y_pred, target_names=labels.values(), digits=4)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #save Keras model | |
| saved_model_path = "modelCNN.h5" | |
| keras_model.save(saved_model_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| with open( 'tokeniser.json' , 'w' ) as file: | |
| json.dump(tokeniser.to_json() , file ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script> | |
| <meta charset="UTF-8"> | |
| <title>Text Classifier</title> | |
| </head> | |
| <body> | |
| <h1>Text Classifier</h1> | |
| <div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function loadVocab(vocabPath) { | |
| let word2index = await (await fetch(vocabPath)).json(); | |
| return word2index; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function tokenise(text) { | |
| text = text.toLowerCase(); | |
| var splitted_text = text.split(' '); | |
| var tokens = []; | |
| splitted_text.forEach(element => { | |
| if (word2index[element] != undefined) { | |
| tokens.push(word2index[element]); | |
| } | |
| }); | |
| while (tokens.length < maxLen) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function loadModel() { | |
| const model = await tf.loadLayersModel(modelPath); | |
| return model; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function predict() { | |
| const predictedClass = tf.tidy(() => { | |
| const text = document.getElementById("myText").value; | |
| const tokenisation = tokenise(text, word2index); | |
| const predictions = model.predict(tf.tensor2d(tokenisation, [1, maxLen])); | |
| return predictions.as1D().argMax(); | |
| }); | |
| const classId = (await predictedClass.data())[0]; | |
| var predictionText = ""; | |
| switch(classId){ |