{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "55f5b823", "metadata": {}, "outputs": [], "source": [ "from tensorflow import keras\n", "from tensorflow.keras.preprocessing.text import tokenizer_from_json\n", "import json\n", "import pandas as pd\n", "from tensorflow.keras.preprocessing.sequence import pad_sequences" ] }, { "cell_type": "code", "execution_count": 17, "id": "2c29b948", "metadata": {}, "outputs": [], "source": [ "vocab_size = 20000\n", "max_len = 50" ] }, { "cell_type": "code", "execution_count": null, "id": "18691acc", "metadata": {}, "outputs": [], "source": [ "model = keras.models.load_model(\"model1_simple_neural_network.keras\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f0ac1b5a", "metadata": {}, "outputs": [], "source": [ "with open(\"tokenizer_simple_neural_network.json\", \"r\", encoding=\"utf-8\") as f:\n", " tokenizer_json_str = f.read() # <- string\n", "\n", "tokenizer = tokenizer_from_json(tokenizer_json_str)\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "d7b81cad", "metadata": {}, "outputs": [], "source": [ "target_sentence = \"I feel sad\"" ] }, { "cell_type": "code", "execution_count": 27, "id": "d974a21c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 I feel sad\n", "Name: new_test, dtype: str" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_example = pd.Series([target_sentence], name=\"new_test\")\n", "X_example.head()" ] }, { "cell_type": "code", "execution_count": 28, "id": "834ebe3a", "metadata": {}, "outputs": [], "source": [ "X_example_seq = tokenizer.texts_to_sequences(X_example)" ] }, { "cell_type": "code", "execution_count": null, "id": "bde8906e", "metadata": {}, "outputs": [], "source": [ "X_example_seq = tokenizer.texts_to_sequences(X_example)\n", "X_example_pad = pad_sequences(X_example_seq, maxlen=max_len, padding='post', truncating='post')\n", "y_prediction = (model.predict(X_example_pad) > 0.5).astype(int).ravel()" ] }, { "cell_type": "code", "execution_count": 33, "id": "5bcc5f00", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.True_" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = y_prediction[0]\n", "result == 0" ] }, { "cell_type": "code", "execution_count": 34, "id": "865e67bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sad\n" ] } ], "source": [ "if result == 0:\n", " print(\"sad\")\n", "else:\n", " print(\"happy\")" ] }, { "cell_type": "code", "execution_count": null, "id": "5227180e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.14" } }, "nbformat": 4, "nbformat_minor": 5 }