Last active
February 25, 2021 14:29
-
-
Save dmohl/8536ea7c02720f9a603590beba443cbb to your computer and use it in GitHub Desktop.
F# Keras.NET Text Sentiment Analysis
This file contains 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "fsharp_keras_colab_v5.ipynb", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": ".net-fsharp", | |
"display_name": "F#" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"id": "ljaRA9itH5ew", | |
"outputId": "fc67caff-352b-47b8-e070-152dcb966169" | |
}, | |
"source": [ | |
"#r \"nuget: Keras.NET.Mono, 3.7.5\"\r\n", | |
"#r \"nuget: Keras.NET, 3.7.5\"\r\n", | |
" \r\n", | |
"open System\r\n", | |
"open Keras\r\n", | |
"open Keras.Layers\r\n", | |
"open Keras.Models\r\n", | |
"open Keras.Datasets\r\n", | |
"open Keras.PreProcessing.sequence\r\n", | |
"open Numpy\r\n", | |
"open System.Linq\r\n", | |
"\r\n", | |
"// Load the dataset but only keep the top n words, zero the rest\r\n", | |
"let top_words = Nullable 20000\r\n", | |
"let max_words = Nullable 200\r\n", | |
"\r\n", | |
"let struct ( struct (x_train, y_train), struct (x_test, y_test) ) = IMDB.LoadData(num_words=top_words)\r\n", | |
"\r\n", | |
"let x_train_pad = SequenceUtil.PadSequences (x_train, maxlen=max_words)\r\n", | |
"let x_test_pad = SequenceUtil.PadSequences(x_test, maxlen=max_words)\r\n", | |
"\r\n", | |
"let model = new Sequential()\r\n", | |
"new Embedding(top_words.Value, 32, input_length=max_words) |> model.Add\r\n", | |
"new Conv1D(filters=32, kernel_size=3, padding=\"same\", activation=\"relu\") |> model.Add\r\n", | |
"new MaxPooling1D(pool_size=2) |> model.Add\r\n", | |
"new Flatten() |> model.Add\r\n", | |
"new Dense(250, activation=\"relu\") |> model.Add\r\n", | |
"new Dense(1, activation=\"sigmoid\") |> model.Add\r\n", | |
"model.Compile(loss=\"binary_crossentropy\", \r\n", | |
" optimizer=StringOrInstance.op_Implicit(\"adam\"), \r\n", | |
" metrics=[| \"accuracy\" |])\r\n", | |
"model.Summary()" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"Installed package Keras.NET version 3.7.5" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"Installed package Keras.NET.Mono version 3.7.5" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "16LBQtk2Xb9O" | |
}, | |
"source": [ | |
"// Fit the model\r\n", | |
"model.Fit(x_train_pad, y_train, validation_data=[| x_test_pad; y_test |], \r\n", | |
" epochs=10, batch_size=Nullable 128, verbose=2) " | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "cH8EU2lzA6Vt", | |
"outputId": "0017e9aa-c810-4b35-86f4-efc74206b151" | |
}, | |
"source": [ | |
"// Final evaluation of the model\r\n", | |
"let scores = model.Evaluate(x_test_pad, y_test, verbose=0)\r\n", | |
"scores.[1] * 100. |> printfn \"Accuracy: %A\"" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Accuracy: 87.23999858\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ErWaelIFU8__" | |
}, | |
"source": [ | |
"let json = model.ToJson()\r\n", | |
"System.IO.File.WriteAllText(\"/model.json\", json)\r\n", | |
"model.SaveWeight(\"/model.h5\")" | |
], | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "m31bULRz7t3N" | |
}, | |
"source": [ | |
"let loaded_model = BaseModel.ModelFromJson(System.IO.File.ReadAllText(\"/model.json\"))\r\n", | |
"loaded_model.LoadWeight(\"/model.h5\")\r\n", | |
"\r\n", | |
"let testText = \"The movie was very bad\"\r\n", | |
"let indexes = IMDB.GetWordIndex()\r\n", | |
"\r\n", | |
"let words = testText.Split()\r\n", | |
"let mutable review = [||]\r\n", | |
"for word in words do\r\n", | |
" match indexes.TryGetValue word with\r\n", | |
" | true, value -> value+3 |> review.Append\r\n", | |
" | _ -> review.Append 2\r\n", | |
"\r\n", | |
"let x = np.array(review) \r\n", | |
"let x_shaped = x.reshape(1, x.shape.[0])\r\n", | |
"\r\n", | |
"let x_pad = SequenceUtil.PadSequences(x_shaped, maxlen=max_words)\r\n", | |
"let y = loaded_model.Predict x_pad\r\n", | |
"printfn \"y = %A\" y\r\n", | |
"let binary = Math.Round(y.[0].asscalar<float>())\r\n", | |
"let result = if binary = 0. then \"Negative\" else \"Positive\"\r\n", | |
"printfn \"Sentiment for \\\"%s\\\": %s\" testText result" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment