harishforaiandml commited on
Commit
e805a59
·
verified ·
1 Parent(s): bcbc146

Upload Math_Add_Sub_Mul_Div.ipynb

Browse files
Files changed (1) hide show
  1. Math_Add_Sub_Mul_Div.ipynb +376 -0
Math_Add_Sub_Mul_Div.ipynb ADDED
@@ -0,0 +1,376 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": 55,
20
+ "metadata": {
21
+ "id": "SHsGymlK6Q5d"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "!pip install torch safetensors huggingface_hub"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "source": [
31
+ "import torch\n",
32
+ "import torch.nn as nn\n",
33
+ "import torch.optim as optim\n",
34
+ "\n",
35
+ "from safetensors.torch import save_file"
36
+ ],
37
+ "metadata": {
38
+ "id": "-hrp9a078H5x"
39
+ },
40
+ "execution_count": 56,
41
+ "outputs": []
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "source": [
46
+ "import random\n",
47
+ "\n",
48
+ "X = []\n",
49
+ "Y = []\n",
50
+ "\n",
51
+ "for _ in range(5000):\n",
52
+ "\n",
53
+ " a = random.uniform(1, 10)\n",
54
+ " b = random.uniform(1, 10)\n",
55
+ "\n",
56
+ " inputs = [a, b]\n",
57
+ "\n",
58
+ " outputs = [\n",
59
+ " a + b,\n",
60
+ " a - b,\n",
61
+ " a * b,\n",
62
+ " a / b\n",
63
+ " ]\n",
64
+ "\n",
65
+ " X.append(inputs)\n",
66
+ " Y.append(outputs)\n",
67
+ "\n",
68
+ "X = torch.tensor(X, dtype=torch.float32)\n",
69
+ "Y = torch.tensor(Y, dtype=torch.float32)\n",
70
+ "\n",
71
+ "print(X.shape)\n",
72
+ "print(Y.shape)"
73
+ ],
74
+ "metadata": {
75
+ "id": "WYAJTDJI8Kqm"
76
+ },
77
+ "execution_count": 57,
78
+ "outputs": []
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "source": [
83
+ "class MathModel(nn.Module):\n",
84
+ "\n",
85
+ " def __init__(self):\n",
86
+ " super().__init__()\n",
87
+ "\n",
88
+ " self.net = nn.Sequential(\n",
89
+ "\n",
90
+ " nn.Linear(2, 32),\n",
91
+ " nn.ReLU(),\n",
92
+ "\n",
93
+ " nn.Linear(32, 64),\n",
94
+ " nn.ReLU(),\n",
95
+ "\n",
96
+ " nn.Linear(64, 32),\n",
97
+ " nn.ReLU(),\n",
98
+ "\n",
99
+ " nn.Linear(32, 4)\n",
100
+ " )\n",
101
+ "\n",
102
+ " def forward(self, x):\n",
103
+ " return self.net(x)"
104
+ ],
105
+ "metadata": {
106
+ "id": "a9T_97D_8NAc"
107
+ },
108
+ "execution_count": 58,
109
+ "outputs": []
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "source": [
114
+ "model = MathModel()\n",
115
+ "\n",
116
+ "print(model)"
117
+ ],
118
+ "metadata": {
119
+ "id": "1jxqfO6v8PGD"
120
+ },
121
+ "execution_count": null,
122
+ "outputs": []
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "source": [
127
+ "criterion = nn.MSELoss()\n",
128
+ "\n",
129
+ "optimizer = optim.Adam(\n",
130
+ " model.parameters(),\n",
131
+ " lr=0.001\n",
132
+ ")"
133
+ ],
134
+ "metadata": {
135
+ "id": "BhcNFeMZ8Qti"
136
+ },
137
+ "execution_count": 60,
138
+ "outputs": []
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "source": [
143
+ "epochs = 500\n",
144
+ "\n",
145
+ "for epoch in range(epochs):\n",
146
+ "\n",
147
+ " predictions = model(X)\n",
148
+ "\n",
149
+ " loss = criterion(predictions, Y)\n",
150
+ "\n",
151
+ " optimizer.zero_grad()\n",
152
+ "\n",
153
+ " loss.backward()\n",
154
+ "\n",
155
+ " optimizer.step()\n",
156
+ "\n",
157
+ " if epoch % 50 == 0:\n",
158
+ " print(f\"Epoch {epoch} Loss: {loss.item():.4f}\")"
159
+ ],
160
+ "metadata": {
161
+ "id": "ZdNlDDp48lVE"
162
+ },
163
+ "execution_count": 61,
164
+ "outputs": []
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "source": [
169
+ "test_input = torch.tensor([[6.0, 2.0]])\n",
170
+ "\n",
171
+ "prediction = model(test_input)\n",
172
+ "\n",
173
+ "print(prediction)"
174
+ ],
175
+ "metadata": {
176
+ "id": "bwfPd2e_8nkb"
177
+ },
178
+ "execution_count": 62,
179
+ "outputs": []
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "source": [
184
+ "a = 6\n",
185
+ "b = 2\n",
186
+ "\n",
187
+ "test_input = torch.tensor([[a, b]], dtype=torch.float32)\n",
188
+ "\n",
189
+ "prediction = model(test_input).detach().numpy()[0]\n",
190
+ "\n",
191
+ "print(\"INPUT\")\n",
192
+ "print(a, b)\n",
193
+ "\n",
194
+ "print(\"\\nPREDICTIONS\")\n",
195
+ "\n",
196
+ "print(\"ADD :\", prediction[0])\n",
197
+ "print(\"SUB :\", prediction[1])\n",
198
+ "print(\"MUL :\", prediction[2])\n",
199
+ "print(\"DIV :\", prediction[3])"
200
+ ],
201
+ "metadata": {
202
+ "id": "2yRAy_ek8peN"
203
+ },
204
+ "execution_count": 63,
205
+ "outputs": []
206
+ },
207
+ {
208
+ "cell_type": "code",
209
+ "source": [
210
+ "save_file(\n",
211
+ " model.state_dict(),\n",
212
+ " \"math_add_sub_mul_div_model.safetensors\"\n",
213
+ ")\n",
214
+ "\n",
215
+ "print(\"Saved!\")"
216
+ ],
217
+ "metadata": {
218
+ "id": "FcPuskx58rkF"
219
+ },
220
+ "execution_count": 64,
221
+ "outputs": []
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "source": [
226
+ "import os\n",
227
+ "\n",
228
+ "print(os.listdir())"
229
+ ],
230
+ "metadata": {
231
+ "id": "8vhzfzAe8tKW"
232
+ },
233
+ "execution_count": null,
234
+ "outputs": []
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "source": [
239
+ "from huggingface_hub import login\n",
240
+ "\n",
241
+ "login()"
242
+ ],
243
+ "metadata": {
244
+ "id": "U5qADMmz8u65"
245
+ },
246
+ "execution_count": 66,
247
+ "outputs": []
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "source": [
252
+ "from huggingface_hub import HfApi\n",
253
+ "\n",
254
+ "api = HfApi()\n",
255
+ "\n",
256
+ "api.upload_file(\n",
257
+ " path_or_fileobj=\"math_add_sub_mul_div_model.safetensors\",\n",
258
+ " path_in_repo=\"math_add_sub_mul_div_model.safetensors\",\n",
259
+ " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n",
260
+ " repo_type=\"model\",\n",
261
+ ")\n",
262
+ "\n",
263
+ "print(\"Uploaded!\")"
264
+ ],
265
+ "metadata": {
266
+ "id": "5o1XmEOX8ycJ"
267
+ },
268
+ "execution_count": 67,
269
+ "outputs": []
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "source": [
274
+ "readme = \"\"\"\n",
275
+ "# Basic Math AI\n",
276
+ "\n",
277
+ "Tiny neural network trained to perform:\n",
278
+ "\n",
279
+ "- Addition\n",
280
+ "- Subtraction\n",
281
+ "- Multiplication\n",
282
+ "- Division\n",
283
+ "\n",
284
+ "Inputs:\n",
285
+ "[a, b]\n",
286
+ "\n",
287
+ "Outputs:\n",
288
+ "[a+b, a-b, a*b, a/b]\n",
289
+ "\n",
290
+ "Saved using SafeTensors.\n",
291
+ "\"\"\"\n",
292
+ "\n",
293
+ "with open(\"README.md\", \"w\") as f:\n",
294
+ " f.write(readme)"
295
+ ],
296
+ "metadata": {
297
+ "id": "SjlZCtl69H8R"
298
+ },
299
+ "execution_count": 68,
300
+ "outputs": []
301
+ },
302
+ {
303
+ "cell_type": "code",
304
+ "source": [
305
+ "api.upload_file(\n",
306
+ " path_or_fileobj=\"README.md\",\n",
307
+ " path_in_repo=\"README.md\",\n",
308
+ " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n",
309
+ " repo_type=\"model\",\n",
310
+ ")\n",
311
+ "\n",
312
+ "print(\"README uploaded!\")"
313
+ ],
314
+ "metadata": {
315
+ "id": "N3ALvS4n9hKe"
316
+ },
317
+ "execution_count": 69,
318
+ "outputs": []
319
+ },
320
+ {
321
+ "cell_type": "code",
322
+ "source": [
323
+ "from huggingface_hub import hf_hub_download\n",
324
+ "from safetensors.torch import load_file"
325
+ ],
326
+ "metadata": {
327
+ "id": "r5gb1XAO-7bh"
328
+ },
329
+ "execution_count": 70,
330
+ "outputs": []
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "source": [
335
+ "def load_math_model():\n",
336
+ "\n",
337
+ " model_path = hf_hub_download(\n",
338
+ " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n",
339
+ " filename=\"math_add_sub_mul_div_model.safetensors\"\n",
340
+ " )\n",
341
+ "\n",
342
+ " state_dict = load_file(model_path)\n",
343
+ "\n",
344
+ " model = MathModel()\n",
345
+ "\n",
346
+ " model.load_state_dict(state_dict)\n",
347
+ "\n",
348
+ " model.eval()\n",
349
+ "\n",
350
+ " return model"
351
+ ],
352
+ "metadata": {
353
+ "id": "JEnr8lUS-PVr"
354
+ },
355
+ "execution_count": 71,
356
+ "outputs": []
357
+ },
358
+ {
359
+ "cell_type": "code",
360
+ "source": [
361
+ "model = load_math_model()\n",
362
+ "\n",
363
+ "x = torch.tensor([[10.0, 5.0]])\n",
364
+ "\n",
365
+ "result = model(x)\n",
366
+ "\n",
367
+ "print(result)"
368
+ ],
369
+ "metadata": {
370
+ "id": "lbVdBthn-RIv"
371
+ },
372
+ "execution_count": null,
373
+ "outputs": []
374
+ }
375
+ ]
376
+ }