Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter2/ Batch Size Notebook: Modify the DataSet class/Remove redundant cells #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 90 additions & 123 deletions Chapter02/Specifying_batch_size_while_training_a_model.ipynb
Original file line number Diff line number Diff line change
@@ -1,170 +1,117 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Specifying_batch_size_while_training_a_model.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/PacktPublishing/Hands-On-Computer-Vision-with-PyTorch/blob/master/Chapter02/Specifying_batch_size_while_training_a_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:09.313991Z",
"start_time": "2020-09-25T19:38:09.019280Z"
},
"id": "qMRMqX5bxknN"
},
"outputs": [],
"source": [
"from torch.utils.data import Dataset, DataLoader\n",
"import torch\n",
"import torch.nn as nn"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:09.317494Z",
"start_time": "2020-09-25T19:38:09.315108Z"
},
"id": "pwlhqEFtxldo"
},
"outputs": [],
"source": [
"x = [[1,2],[3,4],[5,6],[7,8]]\n",
"y = [[3],[7],[11],[15]]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:09.322276Z",
"start_time": "2020-09-25T19:38:09.319108Z"
},
"id": "Xvn8m82PxqGL"
},
"source": [
"X = torch.tensor(x).float()\n",
"Y = torch.tensor(y).float()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:10.908620Z",
"start_time": "2020-09-25T19:38:09.324375Z"
},
"id": "suZ9rXQqxrUi"
},
"outputs": [],
"source": [
"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
"X = X.to(device)\n",
"Y = Y.to(device)"
],
"execution_count": null,
"outputs": []
"device = 'cuda' if torch.cuda.is_available() else 'cpu'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:10.913405Z",
"start_time": "2020-09-25T19:38:10.909638Z"
},
"id": "GZwy83Zaxsrw",
"outputId": "cb09c682-1509-420d-d663-91dff0f00e91",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 105
}
},
"id": "GZwy83Zaxsrw",
"outputId": "cb09c682-1509-420d-d663-91dff0f00e91"
},
"outputs": [],
"source": [
"class MyDataset(Dataset):\n",
" def __init__(self,x,y):\n",
" self.x = torch.tensor(x).float()\n",
" self.y = torch.tensor(y).float()\n",
" self.x = torch.tensor(x).float().to(device)\n",
" self.y = torch.tensor(y).float().to(device)\n",
" def __len__(self):\n",
" return len(self.x)\n",
" def __getitem__(self, ix):\n",
" return self.x[ix], self.y[ix]\n",
"ds = MyDataset(X, Y)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"/home/yyr/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" This is separate from the ipykernel package so we can avoid doing imports until\n",
"/home/yyr/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" after removing the cwd from sys.path.\n"
],
"name": "stderr"
}
"ds = MyDataset(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:10.917418Z",
"start_time": "2020-09-25T19:38:10.915031Z"
},
"id": "K28I9mj-x1yB"
},
"outputs": [],
"source": [
"dl = DataLoader(ds, batch_size=2, shuffle=True)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:10.922176Z",
"start_time": "2020-09-25T19:38:10.918671Z"
},
"id": "i1uNDQLDx3bb"
},
"outputs": [],
"source": [
"class MyNeuralNet(nn.Module):\n",
" def __init__(self):\n",
Expand All @@ -177,42 +124,50 @@
" x = self.hidden_layer_activation(x)\n",
" x = self.hidden_to_output_layer(x)\n",
" return x"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:10.928421Z",
"start_time": "2020-09-25T19:38:10.924741Z"
},
"id": "M5GAnaPmx5w5"
},
"outputs": [],
"source": [
"mynet = MyNeuralNet().to(device)\n",
"loss_func = nn.MSELoss()\n",
"from torch.optim import SGD\n",
"opt = SGD(mynet.parameters(), lr = 0.001)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:11.011394Z",
"start_time": "2020-09-25T19:38:10.929771Z"
},
"id": "_da8xi-9x7oJ",
"outputId": "7a59135d-f162-4178-f05e-2e5492893216",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"id": "_da8xi-9x7oJ",
"outputId": "7a59135d-f162-4178-f05e-2e5492893216"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.9255454540252686\n"
]
}
],
"source": [
"import time\n",
"loss_history = []\n",
Expand All @@ -227,91 +182,103 @@
" loss_history.append(loss_value)\n",
"end = time.time()\n",
"print(end - start)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"0.07826399803161621\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:11.014114Z",
"start_time": "2020-09-25T19:38:11.012332Z"
},
"id": "ou1Hdxb4x9TP"
},
"outputs": [],
"source": [
"val_x = [[10,11]]"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:11.017321Z",
"start_time": "2020-09-25T19:38:11.015262Z"
},
"id": "OtA5Oswrx_Sl"
},
"outputs": [],
"source": [
"val_x = torch.tensor(val_x).float().to(device)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-25T19:38:11.025594Z",
"start_time": "2020-09-25T19:38:11.018406Z"
},
"id": "UysdiiFSyAf1",
"outputId": "f11228a4-0aaa-4851-eee2-a855a93acd91",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"id": "UysdiiFSyAf1",
"outputId": "f11228a4-0aaa-4851-eee2-a855a93acd91"
},
"source": [
"mynet(val_x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[20.7331]], device='cuda:0', grad_fn=<AddmmBackward>)"
"tensor([[20.8937]], device='cuda:0', grad_fn=<AddmmBackward0>)"
]
},
"metadata": {
"tags": []
},
"execution_count": 12
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mynet(val_x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rHYynD1wyByW"
},
"source": [
""
],
"execution_count": null,
"outputs": []
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"include_colab_link": true,
"name": "Specifying_batch_size_while_training_a_model.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.11.0"
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}