|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "79cfe34c", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "words = open('names.txt', 'r').read().splitlines()" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 5, |
| 16 | + "id": "aba02a00", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import torch\n", |
| 21 | + "\n", |
| 22 | + "chars = sorted(list(set(''.join(words))))\n", |
| 23 | + "stoi = {s:i+1 for i,s in enumerate(chars)}\n", |
| 24 | + "stoi['.'] = 0\n", |
| 25 | + "itos = {i:s for s,i in stoi.items()}\n", |
| 26 | + "\n", |
| 27 | + "\n", |
| 28 | + "xs, ys = [], []\n", |
| 29 | + "for w in words[:1]:\n", |
| 30 | + " chs = ['.'] + list(w) + ['.']\n", |
| 31 | + " for ch1, ch2 in zip(chs, chs[1:]):\n", |
| 32 | + " ix1 = stoi[ch1]\n", |
| 33 | + " ix2 = stoi[ch2]\n", |
| 34 | + " xs.append(ix1)\n", |
| 35 | + " ys.append(ix2)\n", |
| 36 | + "\n", |
| 37 | + "xs = torch.tensor(xs)\n", |
| 38 | + "ys = torch.tensor(ys)\n" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": null, |
| 44 | + "id": "b6b72d4b", |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "import matplotlib.pyplot as plt\n", |
| 49 | + "import torch.nn.functional as F\n", |
| 50 | + "xenc = F.one_hot(xs, num_classes=len(stoi)).float()\n" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "id": "b2a3f08c", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "W = torch.randn((len(stoi), len(stoi)), requires_grad=True)\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "metadata": { |
| 65 | + "kernelspec": { |
| 66 | + "display_name": "makemore", |
| 67 | + "language": "python", |
| 68 | + "name": "python3" |
| 69 | + }, |
| 70 | + "language_info": { |
| 71 | + "codemirror_mode": { |
| 72 | + "name": "ipython", |
| 73 | + "version": 3 |
| 74 | + }, |
| 75 | + "file_extension": ".py", |
| 76 | + "mimetype": "text/x-python", |
| 77 | + "name": "python", |
| 78 | + "nbconvert_exporter": "python", |
| 79 | + "pygments_lexer": "ipython3", |
| 80 | + "version": "3.10.18" |
| 81 | + } |
| 82 | + }, |
| 83 | + "nbformat": 4, |
| 84 | + "nbformat_minor": 5 |
| 85 | +} |
0 commit comments