Add the model and the training notebook
Browse files- alphabet_model.h5 +3 -0
- chatgpt_alphabet.ipynb +232 -0
- chatgpt_alphabet.py +53 -0
alphabet_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8c688ee698acb4e925a258a1617d2be42717084536c47247c928651327ba7d19
|
3 |
+
size 91488
|
chatgpt_alphabet.ipynb
ADDED
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": null,
|
20 |
+
"metadata": {
|
21 |
+
"id": "WaYYbq814jEh"
|
22 |
+
},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"!pip install tensorflow"
|
26 |
+
]
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"cell_type": "code",
|
30 |
+
"source": [
|
31 |
+
"import numpy as np\n",
|
32 |
+
"import tensorflow as tf\n",
|
33 |
+
"from tensorflow.keras.models import Sequential\n",
|
34 |
+
"from tensorflow.keras.layers import LSTM, Dense, Embedding\n"
|
35 |
+
],
|
36 |
+
"metadata": {
|
37 |
+
"id": "m1lkAOKh4nc_"
|
38 |
+
},
|
39 |
+
"execution_count": 2,
|
40 |
+
"outputs": []
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"source": [
|
45 |
+
"alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n",
|
46 |
+
"\n",
|
47 |
+
"# Convert alphabet to integers\n",
|
48 |
+
"char_to_int = dict((c, i) for i, c in enumerate(alphabet))\n",
|
49 |
+
"int_to_char = dict((i, c) for i, c in enumerate(alphabet))\n",
|
50 |
+
"\n",
|
51 |
+
"# Prepare dataset\n",
|
52 |
+
"seq_length = 1\n",
|
53 |
+
"dataX = []\n",
|
54 |
+
"dataY = []\n",
|
55 |
+
"for i in range(0, len(alphabet) - seq_length, 1):\n",
|
56 |
+
" seq_in = alphabet[i:i + seq_length]\n",
|
57 |
+
" seq_out = alphabet[i + seq_length]\n",
|
58 |
+
" dataX.append([char_to_int[char] for char in seq_in])\n",
|
59 |
+
" dataY.append(char_to_int[seq_out])\n",
|
60 |
+
"\n",
|
61 |
+
"X = np.reshape(dataX, (len(dataX), seq_length, 1))\n",
|
62 |
+
"y = tf.keras.utils.to_categorical(dataY)\n"
|
63 |
+
],
|
64 |
+
"metadata": {
|
65 |
+
"id": "kjFJxMNV4oPv"
|
66 |
+
},
|
67 |
+
"execution_count": 3,
|
68 |
+
"outputs": []
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"source": [
|
73 |
+
"model = Sequential()\n",
|
74 |
+
"model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2])))\n",
|
75 |
+
"model.add(Dense(y.shape[1], activation='softmax'))\n",
|
76 |
+
"model.compile(loss='categorical_crossentropy', optimizer='adam')\n"
|
77 |
+
],
|
78 |
+
"metadata": {
|
79 |
+
"id": "P9e2hWnD4pFY"
|
80 |
+
},
|
81 |
+
"execution_count": 4,
|
82 |
+
"outputs": []
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "code",
|
86 |
+
"source": [
|
87 |
+
"model.fit(X, y, epochs=500, batch_size=1, verbose=2)\n"
|
88 |
+
],
|
89 |
+
"metadata": {
|
90 |
+
"id": "PO31MxKH4qGb"
|
91 |
+
},
|
92 |
+
"execution_count": null,
|
93 |
+
"outputs": []
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"cell_type": "code",
|
97 |
+
"source": [
|
98 |
+
"for pattern in dataX:\n",
|
99 |
+
" x = np.reshape(pattern, (1, len(pattern), 1))\n",
|
100 |
+
" prediction = model.predict(x, verbose=0)\n",
|
101 |
+
" index = np.argmax(prediction)\n",
|
102 |
+
" result = int_to_char[index]\n",
|
103 |
+
" seq_in = [int_to_char[value] for value in pattern]\n",
|
104 |
+
" print(seq_in, \"->\", result)\n"
|
105 |
+
],
|
106 |
+
"metadata": {
|
107 |
+
"colab": {
|
108 |
+
"base_uri": "https://localhost:8080/"
|
109 |
+
},
|
110 |
+
"id": "bozl3EuF4q8k",
|
111 |
+
"outputId": "2cc54eed-8af5-4f06-d2c5-79d3ea2380f3"
|
112 |
+
},
|
113 |
+
"execution_count": 8,
|
114 |
+
"outputs": [
|
115 |
+
{
|
116 |
+
"output_type": "stream",
|
117 |
+
"name": "stdout",
|
118 |
+
"text": [
|
119 |
+
"['A'] -> B\n",
|
120 |
+
"['B'] -> C\n",
|
121 |
+
"['C'] -> D\n",
|
122 |
+
"['D'] -> E\n",
|
123 |
+
"['E'] -> F\n",
|
124 |
+
"['F'] -> G\n",
|
125 |
+
"['G'] -> H\n",
|
126 |
+
"['H'] -> I\n",
|
127 |
+
"['I'] -> J\n",
|
128 |
+
"['J'] -> K\n",
|
129 |
+
"['K'] -> L\n",
|
130 |
+
"['L'] -> M\n",
|
131 |
+
"['M'] -> N\n",
|
132 |
+
"['N'] -> O\n",
|
133 |
+
"['O'] -> O\n",
|
134 |
+
"['P'] -> P\n",
|
135 |
+
"['Q'] -> R\n",
|
136 |
+
"['R'] -> T\n",
|
137 |
+
"['S'] -> T\n",
|
138 |
+
"['T'] -> V\n",
|
139 |
+
"['U'] -> V\n",
|
140 |
+
"['V'] -> X\n",
|
141 |
+
"['W'] -> Z\n",
|
142 |
+
"['X'] -> Z\n",
|
143 |
+
"['Y'] -> Z\n"
|
144 |
+
]
|
145 |
+
}
|
146 |
+
]
|
147 |
+
},
|
148 |
+
{
|
149 |
+
"cell_type": "code",
|
150 |
+
"source": [
|
151 |
+
"model.save('alphabet_model.h5')\n",
|
152 |
+
"from google.colab import files\n",
|
153 |
+
"files.download('alphabet_model.h5')\n"
|
154 |
+
],
|
155 |
+
"metadata": {
|
156 |
+
"colab": {
|
157 |
+
"base_uri": "https://localhost:8080/",
|
158 |
+
"height": 17
|
159 |
+
},
|
160 |
+
"id": "FUCsjNyY8ZCs",
|
161 |
+
"outputId": "496fd160-13f1-4af1-9ae3-ea6c89513adb"
|
162 |
+
},
|
163 |
+
"execution_count": 7,
|
164 |
+
"outputs": [
|
165 |
+
{
|
166 |
+
"output_type": "display_data",
|
167 |
+
"data": {
|
168 |
+
"text/plain": [
|
169 |
+
"<IPython.core.display.Javascript object>"
|
170 |
+
],
|
171 |
+
"application/javascript": [
|
172 |
+
"\n",
|
173 |
+
" async function download(id, filename, size) {\n",
|
174 |
+
" if (!google.colab.kernel.accessAllowed) {\n",
|
175 |
+
" return;\n",
|
176 |
+
" }\n",
|
177 |
+
" const div = document.createElement('div');\n",
|
178 |
+
" const label = document.createElement('label');\n",
|
179 |
+
" label.textContent = `Downloading \"${filename}\": `;\n",
|
180 |
+
" div.appendChild(label);\n",
|
181 |
+
" const progress = document.createElement('progress');\n",
|
182 |
+
" progress.max = size;\n",
|
183 |
+
" div.appendChild(progress);\n",
|
184 |
+
" document.body.appendChild(div);\n",
|
185 |
+
"\n",
|
186 |
+
" const buffers = [];\n",
|
187 |
+
" let downloaded = 0;\n",
|
188 |
+
"\n",
|
189 |
+
" const channel = await google.colab.kernel.comms.open(id);\n",
|
190 |
+
" // Send a message to notify the kernel that we're ready.\n",
|
191 |
+
" channel.send({})\n",
|
192 |
+
"\n",
|
193 |
+
" for await (const message of channel.messages) {\n",
|
194 |
+
" // Send a message to notify the kernel that we're ready.\n",
|
195 |
+
" channel.send({})\n",
|
196 |
+
" if (message.buffers) {\n",
|
197 |
+
" for (const buffer of message.buffers) {\n",
|
198 |
+
" buffers.push(buffer);\n",
|
199 |
+
" downloaded += buffer.byteLength;\n",
|
200 |
+
" progress.value = downloaded;\n",
|
201 |
+
" }\n",
|
202 |
+
" }\n",
|
203 |
+
" }\n",
|
204 |
+
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
|
205 |
+
" const a = document.createElement('a');\n",
|
206 |
+
" a.href = window.URL.createObjectURL(blob);\n",
|
207 |
+
" a.download = filename;\n",
|
208 |
+
" div.appendChild(a);\n",
|
209 |
+
" a.click();\n",
|
210 |
+
" div.remove();\n",
|
211 |
+
" }\n",
|
212 |
+
" "
|
213 |
+
]
|
214 |
+
},
|
215 |
+
"metadata": {}
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"output_type": "display_data",
|
219 |
+
"data": {
|
220 |
+
"text/plain": [
|
221 |
+
"<IPython.core.display.Javascript object>"
|
222 |
+
],
|
223 |
+
"application/javascript": [
|
224 |
+
"download(\"download_10038af2-1923-4b7b-afba-d12d0f08f61e\", \"alphabet_model.h5\", 91488)"
|
225 |
+
]
|
226 |
+
},
|
227 |
+
"metadata": {}
|
228 |
+
}
|
229 |
+
]
|
230 |
+
}
|
231 |
+
]
|
232 |
+
}
|
chatgpt_alphabet.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""chatgpt-alphabet.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1BFy2tYkYPr7mev8Gg9AMT8ZD_Nj0hH3U
|
8 |
+
"""
|
9 |
+
|
10 |
+
# !pip install tensorflow
|
11 |
+
|
12 |
+
import numpy as np
|
13 |
+
import tensorflow as tf
|
14 |
+
from tensorflow.keras.models import Sequential
|
15 |
+
from tensorflow.keras.layers import LSTM, Dense, Embedding
|
16 |
+
|
17 |
+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
18 |
+
|
19 |
+
# Convert alphabet to integers
|
20 |
+
char_to_int = dict((c, i) for i, c in enumerate(alphabet))
|
21 |
+
int_to_char = dict((i, c) for i, c in enumerate(alphabet))
|
22 |
+
|
23 |
+
# Prepare dataset
|
24 |
+
seq_length = 1
|
25 |
+
dataX = []
|
26 |
+
dataY = []
|
27 |
+
for i in range(0, len(alphabet) - seq_length, 1):
|
28 |
+
seq_in = alphabet[i:i + seq_length]
|
29 |
+
seq_out = alphabet[i + seq_length]
|
30 |
+
dataX.append([char_to_int[char] for char in seq_in])
|
31 |
+
dataY.append(char_to_int[seq_out])
|
32 |
+
|
33 |
+
X = np.reshape(dataX, (len(dataX), seq_length, 1))
|
34 |
+
y = tf.keras.utils.to_categorical(dataY)
|
35 |
+
|
36 |
+
model = Sequential()
|
37 |
+
model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2])))
|
38 |
+
model.add(Dense(y.shape[1], activation='softmax'))
|
39 |
+
model.compile(loss='categorical_crossentropy', optimizer='adam')
|
40 |
+
|
41 |
+
model.fit(X, y, epochs=500, batch_size=1, verbose=2)
|
42 |
+
|
43 |
+
for pattern in dataX:
|
44 |
+
x = np.reshape(pattern, (1, len(pattern), 1))
|
45 |
+
prediction = model.predict(x, verbose=0)
|
46 |
+
index = np.argmax(prediction)
|
47 |
+
result = int_to_char[index]
|
48 |
+
seq_in = [int_to_char[value] for value in pattern]
|
49 |
+
print(seq_in, "->", result)
|
50 |
+
|
51 |
+
model.save('alphabet_model.h5')
|
52 |
+
from google.colab import files
|
53 |
+
files.download('alphabet_model.h5')
|