{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# unit 1.2 - Binary net in PyTorch with manual weights\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/culurciello/deep-learning-course-source/blob/main/source/lectures/12-binary-net-pytorch-manual.ipynb)\n", "\n", "Now we will learn what is a \"proper\" way to define this super simple AND neural network in PyTorch" ] }, { "cell_type": "code", "execution_count": 1, "id": "d7fbcff1", "metadata": {}, "outputs": [], "source": [ "import torch\n", "from torch import nn\n", "import torch.nn.functional as F" ] }, { "cell_type": "code", "execution_count": 2, "id": "44eee2e4", "metadata": {}, "outputs": [], "source": [ "# professional way to define model in pytorch:\n", "\n", "class Logic_net(nn.Module):\n", " def __init__(self):\n", " super(Logic_net, self).__init__()\n", " self.l1 = nn.Linear(2, 2)\n", " self.l2 = nn.Linear(2, 1)\n", "\n", " def forward(self, x):\n", " x = F.relu(self.l1(x))\n", " x = self.l2(x) \n", " return x\n", " \n", "model = Logic_net()\n", "\n", "# AND model weights:\n", "model.l1.weight = nn.Parameter(torch.Tensor([[0,0],[0.45,0.45]]))\n", "model.l1.bias = nn.Parameter(torch.Tensor([0,0]))\n", "model.l2.weight = nn.Parameter(torch.Tensor([[0,1]]))\n", "model.l2.bias = nn.Parameter(torch.Tensor([0]))" ] }, { "cell_type": "code", "execution_count": 3, "id": "04f83446", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "out: 0.0 0.44999998807907104 0.44999998807907104 0.8999999761581421\n" ] } ], "source": [ "# AND model test:\n", "i1 = torch.Tensor([0,0])\n", "o1 = model(i1)\n", "i2 = torch.Tensor([0,1])\n", "o2 = model(i2)\n", "i3 = torch.Tensor([1,0])\n", "o3 = model(i3)\n", "i4 = torch.Tensor([1,1])\n", "o4 = model(i4)\n", "\n", "print(\"out:\", o1.item(),o2.item(),o3.item(),o4.item())" ] }, { "cell_type": "code", "execution_count": 4, "id": "623db121", "metadata": {}, "outputs": [], "source": [ "# another easier way to define model:\n", "\n", "model = torch.nn.Sequential(\n", " torch.nn.Linear(2, 2),\n", " torch.nn.ReLU(),\n", " torch.nn.Linear(2, 1),\n", ")\n", "\n", "# print(model)\n", "# print(model[0].weight)\n", "\n", "# a AND model weights:\n", "model[0].weight = nn.Parameter(torch.Tensor([[0.3,-0.2],[1,1]]))\n", "model[0].bias = nn.Parameter(torch.Tensor([-0.5,-0.5]))\n", "model[2].weight = nn.Parameter(torch.Tensor([[-0.2,0.9]]))\n", "model[2].bias = nn.Parameter(torch.Tensor([-0.2]))" ] }, { "cell_type": "code", "execution_count": 5, "id": "d8f2fee9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "out: -0.20000000298023224 0.2499999850988388 0.2499999850988388 1.1499998569488525\n" ] } ], "source": [ "# AND model test:\n", "i1 = torch.Tensor([0,0])\n", "o1 = model(i1)\n", "i2 = torch.Tensor([0,1])\n", "o2 = model(i2)\n", "i3 = torch.Tensor([1,0])\n", "o3 = model(i3)\n", "i4 = torch.Tensor([1,1])\n", "o4 = model(i4)\n", "\n", "print(\"out:\", o1.item(),o2.item(),o3.item(),o4.item())" ] }, { "cell_type": "markdown", "id": "c4601502", "metadata": {}, "source": [ "## HOMEWORK\n", "\n", "Try to make the XOR or XNOR neural network on your own.\n", "\n", "Tip: think about decomposing the XOR function: XOR(x1,x2) = $x1*x2 + NOT(x1)*NOT(+x2)$\n", "\n", "Note that $x1*x2$ = AND(x1, x2)\n", "\n", "Can it be solved with 1 neuron?" ] } ], "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }