Skip to content

Instantly share code, notes, and snippets.

View oscar-defelice's full-sized avatar
:atom:
Cooking & Coding

Oscar oscar-defelice

:atom:
Cooking & Coding
View GitHub Profile
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)
# 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))
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))
#save Keras model
saved_model_path = "modelCNN.h5"
keras_model.save(saved_model_path)
import json
with open( 'tokeniser.json' , 'w' ) as file:
json.dump(tokeniser.to_json() , file )
<!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>
async function loadVocab(vocabPath) {
let word2index = await (await fetch(vocabPath)).json();
return word2index;
}
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) {
async function loadModel() {
const model = await tf.loadLayersModel(modelPath);
return model;
}
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){