{ "cells": [ { "cell_type": "code", "execution_count": 2, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0.0\n" ] } ], "source": [ "import torch; print(torch.__version__)\n", "import torch.nn as nn" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 3, "outputs": [], "source": [ "# Define the neural network architecture\n", "class SimpleNN(nn.Module):\n", " def __init__(self, input_size, output_size):\n", " super(SimpleNN, self).__init__()\n", " self.fc1 = nn.Linear(input_size, 3)\n", " self.relu1 = nn.ReLU()\n", " self.fc2 = nn.Linear(3, 2)\n", " self.relu2 = nn.ReLU()\n", " self.fc3 = nn.Linear(2, output_size)\n", "\n", " def forward(self, x):\n", " x = self.fc1(x)\n", " x = self.relu1(x)\n", " x = self.fc2(x)\n", " x = self.relu2(x)\n", " x = self.fc3(x)\n", " return x" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 4, "outputs": [], "source": [ "# Set the parameters\n", "input_size = 4 # Number of input features\n", "output_size = 3 # Number of output neurons (binary classification)\n", "\n", "# Create the neural network\n", "model = SimpleNN(input_size, output_size)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [], "source": [ "input = torch.randn(size=(4,))\n", "output = model(input)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 12, "outputs": [ { "data": { "text/plain": "tensor([-0.5835, 0.2246, -0.0567], grad_fn=)" }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 7, "outputs": [], "source": [ "target = torch.tensor([0.0, 0.0, 1.0])" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 8, "outputs": [ { "data": { "text/plain": "tensor([0., 0., 1.])" }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "target" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "- https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 9, "outputs": [], "source": [ "cross_entropy_loss = nn.CrossEntropyLoss()" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 10, "outputs": [], "source": [ "loss = cross_entropy_loss(output, target)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 11, "outputs": [ { "data": { "text/plain": "tensor(1.0700, grad_fn=)" }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loss" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 16, "outputs": [ { "data": { "text/plain": "tensor(1.0700, grad_fn=)" }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1.0 * torch.log(torch.softmax(output, dim=-1))[2]" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }