{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 303 Build NN Quickly\n", "\n", "View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/\n", "My Youtube Channel: https://www.youtube.com/user/MorvanZhou\n", "\n", "Dependencies:\n", "* torch: 0.1.11" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import torch\n", "import torch.nn.functional as F" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# replace following class code with an easy sequential network\n", "class Net(torch.nn.Module):\n", " def __init__(self, n_feature, n_hidden, n_output):\n", " super(Net, self).__init__()\n", " self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer\n", " self.predict = torch.nn.Linear(n_hidden, n_output) # output layer\n", "\n", " def forward(self, x):\n", " x = F.relu(self.hidden(x)) # activation function for hidden layer\n", " x = self.predict(x) # linear output\n", " return x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "net1 = Net(1, 10, 1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# easy and fast way to build your network\n", "net2 = torch.nn.Sequential(\n", " torch.nn.Linear(1, 10),\n", " torch.nn.ReLU(),\n", " torch.nn.Linear(10, 1)\n", ")\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Net (\n", " (hidden): Linear (1 -> 10)\n", " (predict): Linear (10 -> 1)\n", ")\n", "Sequential (\n", " (0): Linear (1 -> 10)\n", " (1): ReLU ()\n", " (2): Linear (10 -> 1)\n", ")\n" ] } ], "source": [ "print(net1) # net1 architecture\n", "print(net2) # net2 architecture" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }