Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MOD_4_6-TD2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Massala Corentin
MOD_4_6-TD2
Commits
459977c8
Commit
459977c8
authored
1 year ago
by
Massala Corentin
Browse files
Options
Downloads
Patches
Plain Diff
Delete Test.ipynb
parent
b8f3911d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Test.ipynb
+0
-237
0 additions, 237 deletions
Test.ipynb
with
0 additions
and
237 deletions
Test.ipynb
deleted
100644 → 0
+
0
−
237
View file @
b8f3911d
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Pytorch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.6650, 2.0888, -0.3361, -0.6212, 0.6276, -0.3091, 1.6601, -0.1404,\n",
" 0.0224, -1.1360],\n",
" [-0.8311, -1.6385, 0.3011, 1.2989, 0.2328, -0.0061, 0.4039, 0.3956,\n",
" -0.2067, -0.8877],\n",
" [-0.2899, -1.7310, -0.4318, 1.3837, 3.1771, 1.7354, 0.3680, -0.0743,\n",
" -0.1610, 0.8281],\n",
" [-0.2591, -2.0913, 0.8630, -0.3533, 0.9323, -1.4520, 0.4476, -0.2588,\n",
" -0.0963, 1.8449],\n",
" [ 0.4599, 0.3258, 0.7780, 0.6943, -0.4343, -0.0536, -0.1049, 0.2867,\n",
" 0.5493, -1.2934],\n",
" [-0.6990, 0.0783, -0.8745, 1.2521, 1.5363, 0.8770, -0.5319, -0.2629,\n",
" 0.7732, -0.5001],\n",
" [-0.2902, -0.8901, 0.1904, 0.7456, 0.5802, 0.0443, -1.2447, 2.1954,\n",
" 0.5382, 0.2219],\n",
" [ 1.0372, -0.7516, 0.7940, 0.8207, 0.6601, 0.0317, 0.1410, -1.7062,\n",
" -0.6549, 0.6287],\n",
" [ 0.6680, 1.0136, -0.0813, -1.4382, 0.4640, 1.2923, -1.0299, 0.5684,\n",
" 1.6626, -1.1921],\n",
" [-1.1864, -0.6625, -1.0846, 0.7550, -0.8748, 0.2835, -1.4264, 1.0279,\n",
" -0.6046, 0.6298],\n",
" [-1.9253, -0.4960, -0.8379, 1.8726, -2.0282, 0.0869, 1.2508, 0.0390,\n",
" -1.7213, 0.3269],\n",
" [ 1.7196, -1.6188, -0.4604, -1.0196, -0.8883, 1.2987, 0.4795, 0.5581,\n",
" -1.0138, -0.2184],\n",
" [-0.6600, 0.5816, -0.6574, 0.4684, 1.2546, -0.4140, -0.2636, 0.4267,\n",
" 0.1736, -1.5019],\n",
" [-0.8852, 0.6677, -1.3074, -1.2241, -1.4054, 0.0919, 1.5832, 1.4357,\n",
" -1.9016, 0.7274]])\n",
"AlexNet(\n",
" (features): Sequential(\n",
" (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))\n",
" (1): ReLU(inplace=True)\n",
" (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
" (4): ReLU(inplace=True)\n",
" (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (7): ReLU(inplace=True)\n",
" (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (9): ReLU(inplace=True)\n",
" (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
" (11): ReLU(inplace=True)\n",
" (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
" )\n",
" (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))\n",
" (classifier): Sequential(\n",
" (0): Dropout(p=0.5, inplace=False)\n",
" (1): Linear(in_features=9216, out_features=4096, bias=True)\n",
" (2): ReLU(inplace=True)\n",
" (3): Dropout(p=0.5, inplace=False)\n",
" (4): Linear(in_features=4096, out_features=4096, bias=True)\n",
" (5): ReLU(inplace=True)\n",
" (6): Linear(in_features=4096, out_features=1000, bias=True)\n",
" )\n",
")\n"
]
}
],
"source": [
"import torch\n",
"\n",
"N, D = 14, 10\n",
"x = torch.randn(N, D).type(torch.FloatTensor)\n",
"print(x)\n",
"\n",
"from torchvision import models\n",
"\n",
"alexnet = models.alexnet()\n",
"print(alexnet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1: CNN on CIFAR10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See if cuda is available ==> it is not"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CUDA is not available. Training on CPU ...\n"
]
}
],
"source": [
"import torch\n",
"\n",
"# check if CUDA is available\n",
"train_on_gpu = torch.cuda.is_available()\n",
"\n",
"if not train_on_gpu:\n",
" print(\"CUDA is not available. Training on CPU ...\")\n",
"else:\n",
" print(\"CUDA is available! Training on GPU ...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the CIFAR10 dataset"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Files already downloaded and verified\n",
"Files already downloaded and verified\n"
]
}
],
"source": [
"import numpy as np\n",
"from torchvision import datasets, transforms\n",
"from torch.utils.data.sampler import SubsetRandomSampler\n",
"\n",
"# number of subprocesses to use for data loading\n",
"num_workers = 0\n",
"# how many samples per batch to load\n",
"batch_size = 20\n",
"# percentage of training set to use as validation\n",
"valid_size = 0.2\n",
"\n",
"# convert data to a normalized torch.FloatTensor\n",
"transform = transforms.Compose(\n",
" [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]\n",
")\n",
"\n",
"# choose the training and test datasets\n",
"train_data = datasets.CIFAR10(\"data\", train=True, download=True, transform=transform)\n",
"test_data = datasets.CIFAR10(\"data\", train=False, download=True, transform=transform)\n",
"\n",
"# obtain training indices that will be used for validation\n",
"num_train = len(train_data)\n",
"indices = list(range(num_train))\n",
"np.random.shuffle(indices)\n",
"split = int(np.floor(valid_size * num_train))\n",
"train_idx, valid_idx = indices[split:], indices[:split]\n",
"\n",
"# define samplers for obtaining training and validation batches\n",
"train_sampler = SubsetRandomSampler(train_idx)\n",
"valid_sampler = SubsetRandomSampler(valid_idx)\n",
"\n",
"# prepare data loaders (combine dataset and sampler)\n",
"train_loader = torch.utils.data.DataLoader(\n",
" train_data, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers\n",
")\n",
"valid_loader = torch.utils.data.DataLoader(\n",
" train_data, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers\n",
")\n",
"test_loader = torch.utils.data.DataLoader(\n",
" test_data, batch_size=batch_size, num_workers=num_workers\n",
")\n",
"\n",
"# specify the image classes\n",
"classes = [\n",
" \"airplane\",\n",
" \"automobile\",\n",
" \"bird\",\n",
" \"cat\",\n",
" \"deer\",\n",
" \"dog\",\n",
" \"frog\",\n",
" \"horse\",\n",
" \"ship\",\n",
" \"truck\",\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
## Test Pytorch
%% Cell type:code id: tags:
```
python
import
torch
N
,
D
=
14
,
10
x
=
torch
.
randn
(
N
,
D
).
type
(
torch
.
FloatTensor
)
print
(
x
)
from
torchvision
import
models
alexnet
=
models
.
alexnet
()
print
(
alexnet
)
```
%% Output
tensor([[ 0.6650, 2.0888, -0.3361, -0.6212, 0.6276, -0.3091, 1.6601, -0.1404,
0.0224, -1.1360],
[-0.8311, -1.6385, 0.3011, 1.2989, 0.2328, -0.0061, 0.4039, 0.3956,
-0.2067, -0.8877],
[-0.2899, -1.7310, -0.4318, 1.3837, 3.1771, 1.7354, 0.3680, -0.0743,
-0.1610, 0.8281],
[-0.2591, -2.0913, 0.8630, -0.3533, 0.9323, -1.4520, 0.4476, -0.2588,
-0.0963, 1.8449],
[ 0.4599, 0.3258, 0.7780, 0.6943, -0.4343, -0.0536, -0.1049, 0.2867,
0.5493, -1.2934],
[-0.6990, 0.0783, -0.8745, 1.2521, 1.5363, 0.8770, -0.5319, -0.2629,
0.7732, -0.5001],
[-0.2902, -0.8901, 0.1904, 0.7456, 0.5802, 0.0443, -1.2447, 2.1954,
0.5382, 0.2219],
[ 1.0372, -0.7516, 0.7940, 0.8207, 0.6601, 0.0317, 0.1410, -1.7062,
-0.6549, 0.6287],
[ 0.6680, 1.0136, -0.0813, -1.4382, 0.4640, 1.2923, -1.0299, 0.5684,
1.6626, -1.1921],
[-1.1864, -0.6625, -1.0846, 0.7550, -0.8748, 0.2835, -1.4264, 1.0279,
-0.6046, 0.6298],
[-1.9253, -0.4960, -0.8379, 1.8726, -2.0282, 0.0869, 1.2508, 0.0390,
-1.7213, 0.3269],
[ 1.7196, -1.6188, -0.4604, -1.0196, -0.8883, 1.2987, 0.4795, 0.5581,
-1.0138, -0.2184],
[-0.6600, 0.5816, -0.6574, 0.4684, 1.2546, -0.4140, -0.2636, 0.4267,
0.1736, -1.5019],
[-0.8852, 0.6677, -1.3074, -1.2241, -1.4054, 0.0919, 1.5832, 1.4357,
-1.9016, 0.7274]])
AlexNet(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
(classifier): Sequential(
(0): Dropout(p=0.5, inplace=False)
(1): Linear(in_features=9216, out_features=4096, bias=True)
(2): ReLU(inplace=True)
(3): Dropout(p=0.5, inplace=False)
(4): Linear(in_features=4096, out_features=4096, bias=True)
(5): ReLU(inplace=True)
(6): Linear(in_features=4096, out_features=1000, bias=True)
)
)
%% Cell type:markdown id: tags:
## Exercise 1: CNN on CIFAR10
%% Cell type:markdown id: tags:
See if cuda is available ==> it is not
%% Cell type:code id: tags:
```
python
import
torch
# check if CUDA is available
train_on_gpu
=
torch
.
cuda
.
is_available
()
if
not
train_on_gpu
:
print
(
"
CUDA is not available. Training on CPU ...
"
)
else
:
print
(
"
CUDA is available! Training on GPU ...
"
)
```
%% Output
CUDA is not available. Training on CPU ...
%% Cell type:markdown id: tags:
Load the CIFAR10 dataset
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
torchvision
import
datasets
,
transforms
from
torch.utils.data.sampler
import
SubsetRandomSampler
# number of subprocesses to use for data loading
num_workers
=
0
# how many samples per batch to load
batch_size
=
20
# percentage of training set to use as validation
valid_size
=
0.2
# convert data to a normalized torch.FloatTensor
transform
=
transforms
.
Compose
(
[
transforms
.
ToTensor
(),
transforms
.
Normalize
((
0.5
,
0.5
,
0.5
),
(
0.5
,
0.5
,
0.5
))]
)
# choose the training and test datasets
train_data
=
datasets
.
CIFAR10
(
"
data
"
,
train
=
True
,
download
=
True
,
transform
=
transform
)
test_data
=
datasets
.
CIFAR10
(
"
data
"
,
train
=
False
,
download
=
True
,
transform
=
transform
)
# obtain training indices that will be used for validation
num_train
=
len
(
train_data
)
indices
=
list
(
range
(
num_train
))
np
.
random
.
shuffle
(
indices
)
split
=
int
(
np
.
floor
(
valid_size
*
num_train
))
train_idx
,
valid_idx
=
indices
[
split
:],
indices
[:
split
]
# define samplers for obtaining training and validation batches
train_sampler
=
SubsetRandomSampler
(
train_idx
)
valid_sampler
=
SubsetRandomSampler
(
valid_idx
)
# prepare data loaders (combine dataset and sampler)
train_loader
=
torch
.
utils
.
data
.
DataLoader
(
train_data
,
batch_size
=
batch_size
,
sampler
=
train_sampler
,
num_workers
=
num_workers
)
valid_loader
=
torch
.
utils
.
data
.
DataLoader
(
train_data
,
batch_size
=
batch_size
,
sampler
=
valid_sampler
,
num_workers
=
num_workers
)
test_loader
=
torch
.
utils
.
data
.
DataLoader
(
test_data
,
batch_size
=
batch_size
,
num_workers
=
num_workers
)
# specify the image classes
classes
=
[
"
airplane
"
,
"
automobile
"
,
"
bird
"
,
"
cat
"
,
"
deer
"
,
"
dog
"
,
"
frog
"
,
"
horse
"
,
"
ship
"
,
"
truck
"
,
]
```
%% Output
Files already downloaded and verified
Files already downloaded and verified
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment