{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "H7gQFbUxOQtb" }, "source": [ "# Deep Learning with PyTorch" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FuhJIaeXO2W9", "outputId": "bf494471-115e-45a8-c7cb-15a26f12154a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.4.0\n" ] } ], "source": [ "import torch\n", "import torch.nn as nn\n", "import os\n", "\n", "## print out the pytorch version used\n", "print(torch.__version__)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Set up the number of CPU cores to use" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Tell PyTorch it can use multiple threads.\n", "# If you are doing parallel data loading be careful\n", "# as the total num of threads+data loaders can't be\n", "# greater than n_cores \n", "# Get the assigned number of cores \n", "n_cores = int(os.environ.get('NSLOTS',1))\n", "torch.set_num_threads(n_cores)" ] }, { "cell_type": "markdown", "metadata": { "id": "0a2C_nneO_wp" }, "source": [ "## Preparation of Neural Network\n", "\n", "The first step is to process and prepare the data. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "JWFtgUX85iwO" }, "outputs": [], "source": [ "x = torch.tensor([[-1.0], [0.0], [1.0], [2.0], [3.0], [4.0]], dtype=torch.float)\n", "y = torch.tensor([[-3.0], [-1.0], [1.0], [3.0], [5.0], [7.0]], dtype=torch.float)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NcQUjR_95z5J", "outputId": "6db5df38-6f9d-454e-87d6-cee0c29dccb3" }, "outputs": [], "source": [ "## print size of the input tensor\n", "x.size()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "N1Ii5JRz3Jud" }, "outputs": [], "source": [ "## Neural network with 1 hidden layer\n", "layer1 = nn.Linear(1,1, bias=False)\n", "model = nn.Sequential(layer1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "3hglFpejArxx" }, "outputs": [], "source": [ "## loss function\n", "criterion = nn.MSELoss()\n", "\n", "## optimizer algorithm\n", "optimizer = torch.optim.SGD(model.parameters(), lr=0.01)" ] }, { "cell_type": "markdown", "metadata": { "id": "FKj6jvZTUtGh" }, "source": [ "## Training the Neural Network Model\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JeOr9i-aBzRv", "outputId": "299a0b60-a64c-46c4-d031-8aaf1cacbff9" }, "outputs": [], "source": [ "## training\n", "for ITER in range(150):\n", " model = model.train()\n", "\n", " ## forward\n", " output = model(x)\n", " loss = criterion(output, y)\n", " optimizer.zero_grad()\n", "\n", " ## backward + update model params\n", " loss.backward()\n", " optimizer.step()\n", "\n", " model.eval()\n", " print('Epoch: %d | Loss: %.4f' %(ITER, loss.detach().item()))" ] }, { "cell_type": "markdown", "metadata": { "id": "Bp50Q7J0Xkiw" }, "source": [ "## Testing the Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "V1odfZpGFoBi", "outputId": "a447b232-729e-4ccf-adc2-5aeaf79cc2ea" }, "outputs": [], "source": [ "## test the model\n", "sample = torch.tensor([10.0], dtype=torch.float)\n", "predicted = model(sample)\n", "print(predicted.detach().item())" ] } ], "metadata": { "colab": { "provenance": [] }, "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.11.9" } }, "nbformat": 4, "nbformat_minor": 4 }