{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"id": "f5ab3881",
"metadata": {},
"outputs": [],
"source": [
"import pickle,gzip,math,os,time,shutil,torch,matplotlib as mpl,numpy as np,matplotlib.pyplot as plt\n",
"import fastcore.all as fc\n",
"from collections.abc import Mapping\n",
"from pathlib import Path\n",
"\n",
"from torch import tensor,nn,optim\n",
"from torch.utils.data import DataLoader,default_collate\n",
"import torch.nn.functional as F\n",
"from datasets import load_dataset,load_dataset_builder"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "84a947f2",
"metadata": {},
"outputs": [],
"source": [
"import torchvision.transforms.functional as TF\n",
"from fastcore.test import test_close\n",
"\n",
"torch.set_printoptions(precision=2, linewidth=140, sci_mode=False)\n",
"torch.manual_seed(1)\n",
"mpl.rcParams['image.cmap'] = 'gray'\n",
"\n",
"import logging\n",
"logging.disable(logging.WARNING)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "195306d7",
"metadata": {},
"outputs": [],
"source": [
"# from torch.utils.data.sampler import BatchSampler, RandomSampler, SequentialSampler\n",
"# from transformers import default_data_collator\n",
"# from collections.abc import Mapping"
]
},
{
"cell_type": "markdown",
"id": "518c5f72",
"metadata": {},
"source": [
"## Dict model"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "6e610b16",
"metadata": {},
"outputs": [],
"source": [
"x,y = 'image','labels'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4ded6c4e",
"metadata": {},
"outputs": [],
"source": [
"def data_loader(ds, batch_size, as_tuple=True):\n",
" kw = {'collate_fn':collate_dict(ds)} if as_tuple else {}\n",
" return DataLoader(ds, batch_size=batch_size, **kw)"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "a5b2c21b",
"metadata": {},
"outputs": [],
"source": [
"dls = DataLoaders.from_dd(tds, bs, as_tuple=False)"
]
},
{
"cell_type": "code",
"execution_count": 214,
"id": "f9edb11a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'image': tensor([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]]),\n",
" 'label': tensor([9, 0, 0, 3, 0, 2, 7, 2, 5, 5, 0, 9, 5, 5, 7, 9, 1, 0, 6, 4, 3, 1, 4, 8, 4, 3, 0, 2, 4, 4, 5, 3, 6, 6, 0, 8, 5, 2, 1, 6, 6, 7, 9, 5,\n",
" 9, 2, 7, 3, 0, 3, 3, 3, 7, 2, 2, 6, 6, 8, 3, 3, 5, 0, 5, 5, 0, 2, 0, 0, 4, 1, 3, 1, 6, 3, 1, 4, 4, 6, 1, 9, 1, 3, 5, 7, 9, 7, 1, 7,\n",
" 9, 9, 9, 3, 2, 9, 3, 6, 4, 1, 1, 8, 8, 0, 1, 1, 6, 8, 1, 9, 7, 8, 8, 9, 6, 6, 3, 1, 5, 4, 6, 7, 5, 5, 9, 2, 2, 2, 7, 6])}"
]
},
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = next(iter(dls.train))\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 219,
"id": "41de7ff5",
"metadata": {},
"outputs": [],
"source": [
"class FashionMLP(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.l1 = nn.Linear(m,nh)\n",
" self.relu = nn.ReLU()\n",
" self.l2 = nn.Linear(nh,10)\n",
"\n",
" def forward(self, b):\n",
" xb = b[x]\n",
" yb = b[y]\n",
" pred = self.l2(self.relu(self.l1(xb)))\n",
" return {'preds':pred, 'loss':F.cross_entropy(pred, yb)}"
]
},
{
"cell_type": "code",
"execution_count": 220,
"id": "e9682e1d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 True 1.5404933774903384 0.6351112739872068\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 False 2.156612927400613 0.7089596518987342\n"
]
}
],
"source": [
"model = FashionMLP()\n",
"learn = Learner(model, dls, identity, lr=0.001, cbs=cbs)\n",
"learn.fit(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c9c8f8f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}