Skip to content

Commit bc7c3eb

Browse files
Programmer-RD-AIProgrammer-RD-AI
andcommitted
rdm stf
Co-Authored-By: Ranuga <[email protected]>
1 parent c76c2e8 commit bc7c3eb

File tree

4 files changed

+811
-5
lines changed

4 files changed

+811
-5
lines changed

.ipynb_checkpoints/03-checkpoint.ipynb

Lines changed: 634 additions & 1 deletion
Large diffs are not rendered by default.

.ipynb_checkpoints/Theory_02-checkpoint.ipynb

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,82 @@
55
"id": "bb5eee40-7a7b-4d5c-ab73-800776e96510",
66
"metadata": {},
77
"source": [
8-
"# Crross Entropy Loss\n"
8+
"# Crross Entropy Loss\n",
9+
"\n",
10+
"https://towardsdatascience.com/cross-entropy-loss-function-f38c4ec8643e"
911
]
1012
},
1113
{
1214
"cell_type": "markdown",
1315
"id": "49dd39b1-221c-40fa-a34c-a548b7acca8d",
1416
"metadata": {},
1517
"source": [
16-
"# Dropout"
18+
"# Dropout\n",
19+
"\n",
20+
"Dropout is used to make the model not over fit and it reduces the number of nodes used or essentially deactivates them\n",
21+
"\n",
22+
"By dropping a unit out, we mean temporarily removing it from the network, along with all its incoming and outgoing connections\n",
23+
"\n",
24+
"Dilution and dropout are regularization techniques for reducing overfitting in artificial neural networks by preventing complex co-adaptations on training data. They are an efficient way of performing model averaging with neural networks. Wikipedia\n",
25+
"\n",
26+
"https://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf"
1727
]
1828
},
1929
{
2030
"cell_type": "markdown",
2131
"id": "311e5114-dd67-4d9d-a837-f6d8a7a53e2c",
2232
"metadata": {},
2333
"source": [
24-
"# Batch normalization"
34+
"# Batch normalization\n",
35+
"\n",
36+
"Another way to prevent model overfitting\n",
37+
"\n",
38+
"Normalization is a data preprocessing too used\n",
39+
"\n",
40+
"When we normalise the model we can generalize its weights and biases\n",
41+
"\n",
42+
"The batch normalization is kind of a filter where it blends up all of the information\n",
43+
"\n",
44+
"https://arxiv.org/pdf/1502.03167.pdf"
2545
]
2646
},
2747
{
2848
"cell_type": "code",
29-
"execution_count": null,
49+
"execution_count": 3,
3050
"id": "05e944ee-5b08-4ff8-9cfb-25fd73eed387",
3151
"metadata": {},
3252
"outputs": [],
3353
"source": []
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 7,
58+
"id": "2ab2057f-a717-4a50-9245-d1c530ff4bf0",
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"data": {
63+
"text/plain": [
64+
"(tensor([-0.7186, -0.2342, 1.0638, 1.7499, 0.0266, 0.2970, -0.0000, -0.4345,\n",
65+
" -0.0000, 0.0000, -2.3886, 0.0000, -0.2310, 2.1409, -0.9096, -0.7719]),\n",
66+
" tensor([-0.5749, -0.1873, 0.8511, 1.3999, 0.0213, 0.2376, -0.6571, -0.3476,\n",
67+
" -0.0300, 0.8420, -1.9109, 1.0163, -0.1848, 1.7127, -0.7276, -0.6175]))"
68+
]
69+
},
70+
"execution_count": 7,
71+
"metadata": {},
72+
"output_type": "execute_result"
73+
}
74+
],
75+
"source": []
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"id": "0cb61562-a5c7-445c-8218-4752a52d24c1",
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
3484
}
3585
],
3686
"metadata": {

.virtual_documents/03.ipynb.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,118 @@
1+
import torch
2+
import torchvision
3+
from torch import nn, optim
4+
from torchvision import datasets,models,transforms
5+
from torch.utils.data import Dataset,DataLoader
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
import pandas as pd
9+
10+
11+
torch.__version__,torchvision.__version__
12+
13+
14+
# Setup training data
15+
train_data = datasets.FashionMNIST(root="data/03/",train=True,download=True,transform=transforms.ToTensor())
16+
test_data = datasets.FashionMNIST(root="data/03/",train=False,download=True,transform=transforms.ToTensor())
17+
18+
19+
20+
len(train_data),len(test_data)
21+
22+
23+
# See the first training example
24+
image,label = train_data[0]
25+
26+
27+
image.shape,label
28+
29+
30+
class_names = train_data.classes
31+
32+
33+
class_names
34+
35+
36+
class_to_idx = train_data.class_to_idx
37+
38+
39+
class_to_idx
40+
41+
42+
image.shape,label # C,H,W
43+
44+
45+
plt.imshow(image.view(28,28,1),cmap="gray")
46+
plt.title(class_names[label])
47+
plt.axis(False);
48+
49+
50+
# Plot more images
51+
# torch.manual_seed(42)
52+
fig = plt.figure(figsize=(9,9))
53+
row,cols = 4,4
54+
for i in range(1,row*cols+1):
55+
random_idx = torch.randint(0,len(train_data),size=[1]).item()
56+
img,label = train_data[random_idx]
57+
fig.add_subplot(row,cols,i)
58+
plt.imshow(img.view(28,28,1),cmap="gray")
59+
plt.title(class_names[label])
60+
plt.axis(False)
61+
62+
63+
BATCH_SIZE = 32
64+
65+
train_dataloader = DataLoader(train_data,batch_size=BATCH_SIZE,shuffle=True)
66+
test_dataloader = DataLoader(test_data,batch_size=BATCH_SIZE,shuffle=True)
67+
68+
69+
train_features_batch,train_labels_batch = next(iter(train_dataloader))
70+
train_features_batch.shape,train_labels_batch.shape
71+
72+
73+
rdm_idx = torch.randint(0,len(train_features_batch),size = [1]).item()
74+
img,label = train_features_batch[rdm_idx],train_labels_batch[rdm_idx]
75+
plt.imshow(img.squeeze(),cmap="gray")
76+
plt.title(class_names[label])
77+
plt.axis(False)
78+
79+
80+
# Create a flatten layer
81+
flatten_model = nn.Flatten()
82+
x = train_features_batch[0]
83+
x.shape,flatten_model(x).shape
84+
85+
86+
from torch import nn
87+
class FashionMNISTModelV0(nn.Module):
88+
def __init__(self,input_shape:int,hidden_units: int, output_shape:int) -> None:
89+
super().__init__()
90+
self.layer_stack = nn.Sequential(
91+
nn.Flatten(), # No learning parameters
92+
nn.Linear(in_features=input_shape,out_features=hidden_units),
93+
nn.Linear(in_features=hidden_units,out_features=output_shape)
94+
)
95+
96+
def forward(self,X) -> torch.Tensor():
97+
return self.layer_stack(X)
98+
99+
100+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
101+
102+
103+
torch.manual_seed(42)
104+
105+
# Setup model with input parameters
106+
107+
model_0 = FashionMNISTModelV0(input_shape=28*28,hidden_units=2048,output_shape=len(class_names))
108+
109+
110+
optimizer = optim.Adam(model_0.parameters(),lr=0.01)
111+
criterion = nn.CrossEntropyLoss()
112+
113+
114+
dummy_X = torch.rand([1,1,28,28])
115+
model_0(dummy_X)
116+
117+
1118

.virtual_documents/Theory_02.ipynb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11

2+
3+
4+
5+
6+
7+

0 commit comments

Comments
 (0)