{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "WaYYbq814jEh" }, "outputs": [], "source": [ "!pip install tensorflow" ] }, { "cell_type": "code", "source": [ "import numpy as np\n", "import tensorflow as tf\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import LSTM, Dense, Embedding\n" ], "metadata": { "id": "m1lkAOKh4nc_" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n", "\n", "# Convert alphabet to integers\n", "char_to_int = dict((c, i) for i, c in enumerate(alphabet))\n", "int_to_char = dict((i, c) for i, c in enumerate(alphabet))\n", "\n", "# Prepare dataset\n", "seq_length = 1\n", "dataX = []\n", "dataY = []\n", "for i in range(0, len(alphabet) - seq_length, 1):\n", " seq_in = alphabet[i:i + seq_length]\n", " seq_out = alphabet[i + seq_length]\n", " dataX.append([char_to_int[char] for char in seq_in])\n", " dataY.append(char_to_int[seq_out])\n", "\n", "X = np.reshape(dataX, (len(dataX), seq_length, 1))\n", "y = tf.keras.utils.to_categorical(dataY)\n" ], "metadata": { "id": "kjFJxMNV4oPv" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "model = Sequential()\n", "model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2])))\n", "model.add(Dense(y.shape[1], activation='softmax'))\n", "model.compile(loss='categorical_crossentropy', optimizer='adam')\n" ], "metadata": { "id": "P9e2hWnD4pFY" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "model.fit(X, y, epochs=500, batch_size=1, verbose=2)\n" ], "metadata": { "id": "PO31MxKH4qGb" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for pattern in dataX:\n", " x = np.reshape(pattern, (1, len(pattern), 1))\n", " prediction = model.predict(x, verbose=0)\n", " index = np.argmax(prediction)\n", " result = int_to_char[index]\n", " seq_in = [int_to_char[value] for value in pattern]\n", " print(seq_in, \"->\", result)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bozl3EuF4q8k", "outputId": "2cc54eed-8af5-4f06-d2c5-79d3ea2380f3" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['A'] -> B\n", "['B'] -> C\n", "['C'] -> D\n", "['D'] -> E\n", "['E'] -> F\n", "['F'] -> G\n", "['G'] -> H\n", "['H'] -> I\n", "['I'] -> J\n", "['J'] -> K\n", "['K'] -> L\n", "['L'] -> M\n", "['M'] -> N\n", "['N'] -> O\n", "['O'] -> O\n", "['P'] -> P\n", "['Q'] -> R\n", "['R'] -> T\n", "['S'] -> T\n", "['T'] -> V\n", "['U'] -> V\n", "['V'] -> X\n", "['W'] -> Z\n", "['X'] -> Z\n", "['Y'] -> Z\n" ] } ] }, { "cell_type": "code", "source": [ "model.save('alphabet_model.h5')\n", "from google.colab import files\n", "files.download('alphabet_model.h5')\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "FUCsjNyY8ZCs", "outputId": "496fd160-13f1-4af1-9ae3-ea6c89513adb" }, "execution_count": 7, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "\n", " async function download(id, filename, size) {\n", " if (!google.colab.kernel.accessAllowed) {\n", " return;\n", " }\n", " const div = document.createElement('div');\n", " const label = document.createElement('label');\n", " label.textContent = `Downloading \"${filename}\": `;\n", " div.appendChild(label);\n", " const progress = document.createElement('progress');\n", " progress.max = size;\n", " div.appendChild(progress);\n", " document.body.appendChild(div);\n", "\n", " const buffers = [];\n", " let downloaded = 0;\n", "\n", " const channel = await google.colab.kernel.comms.open(id);\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", "\n", " for await (const message of channel.messages) {\n", " // Send a message to notify the kernel that we're ready.\n", " channel.send({})\n", " if (message.buffers) {\n", " for (const buffer of message.buffers) {\n", " buffers.push(buffer);\n", " downloaded += buffer.byteLength;\n", " progress.value = downloaded;\n", " }\n", " }\n", " }\n", " const blob = new Blob(buffers, {type: 'application/binary'});\n", " const a = document.createElement('a');\n", " a.href = window.URL.createObjectURL(blob);\n", " a.download = filename;\n", " div.appendChild(a);\n", " a.click();\n", " div.remove();\n", " }\n", " " ] }, "metadata": {} }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "application/javascript": [ "download(\"download_10038af2-1923-4b7b-afba-d12d0f08f61e\", \"alphabet_model.h5\", 91488)" ] }, "metadata": {} } ] } ] }