{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Training a Perceptron\n", "\n", "In this notebook, we will construct simple perceptron models. We'll start by implementing a perceptron model, and seeing how it behaves. We'll then outline the steps to train a perceptron to classify a point as above or below a line.\n", "\n", "This discussion follows the excellent example and discussion at [The Nature of Code](https://natureofcode.com/book/chapter-10-neural-networks/). Please see that reference for additional details, and a more sophisticated coding strategy (using Classes in Python)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preliminaries\n", "Text preceded by a `#` indicates a 'comment'. I will use comments to explain what we're doing and to ask you questions. Also, comments are useful in your own code to note what you've done (so it makes sense when you return to the code in the future). It's a good habit to *always* comment your code. I'll try to set a good example, but won't always . . . \n", "\n", "Before beginning, let's load in the Python packages we'll need:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pylab import *\n", "%matplotlib inline\n", "rcParams['figure.figsize']=(12,3) # Change the default figure size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1: A simple perceptron model.\n", "\n", "Let's examine a simple perceptron that accepts inputs, processes those inputs, and returns an output. To do so, please consider this function:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def my_perceptron(input1, input2, w1, w2, theta):\n", " # Define the activity of the perceptron, x.\n", " x = input1*w1 + input2*w2 + theta\n", " \n", " # Apply a binary threshold,\n", " if x > 0:\n", " return 1\n", " else:\n", " return 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "