{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2章 パーセプトロン" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.2 単純な論理回路" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2.1 AND ゲート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\rm AND}(x_1, x_2) = \\begin{cases}\n", "1 & (x_1 = 1\\, かつ\\, x_2 = 1) \\\\\n", "0 & (それ以外)\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|x1|x2|y|\n", "|--:|--:|-:|\n", "|0|0|0|\n", "|1|0|0|\n", "|0|1|0|\n", "|1|1|1|\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2.2 NAND ゲートと OR ゲート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NAND ゲート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\rm NAND}(x_1, x_2) = \\begin{cases}\n", "0 & (x_1 = 1\\, かつ\\, x_2 = 1) \\\\\n", "1 & (それ以外)\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|x1|x2|y|\n", "|--:|--:|-:|\n", "|0|0|1|\n", "|1|0|1|\n", "|0|1|1|\n", "|1|1|0|\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OR ゲート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\rm OR}(x_1, x_2) = \\begin{cases}\n", "1 & (x_1 = 1\\, または\\, x_2 = 1) \\\\\n", "0 & (それ以外)\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|x1|x2|y|\n", "|--:|--:|-:|\n", "|0|0|0|\n", "|1|0|1|\n", "|0|1|1|\n", "|1|1|1|\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.3 パーセプトロンの実装" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3.1 簡単な実装" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def AND(x1, x2):\n", " w1, w2, theta = 0.5, 0.5, 0.7\n", " tmp = x1*w1 + x2*w2\n", " if tmp <= theta:\n", " return 0\n", " elif tmp > theta:\n", " return 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AND(0, 0)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AND(1, 0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AND(0, 1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "AND(1, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3.2 重みとバイアスの導入" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.array([0, 1]) # 入力" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "w = np.array([0.5, 0.5]) # 重み" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = -0.7 # バイアス" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.5])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "w * x" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(w*x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.19999999999999996" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(w*x) + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3.3 重みとバイアスによる実装" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def AND(x1, x2):\n", " x = np.array([x1, x2])\n", " w = np.array([0.5, 0.5])\n", " b = -0.7\n", " tmp = np.sum(w*x) + b\n", " if tmp <= 0:\n", " return 0\n", " else:\n", " return 1" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def NAND(x1, x2):\n", " x = np.array([x1, x2])\n", " w = np.array([-0.5, -0.5])\n", " b = 0.7\n", " tmp = np.sum(w*x) + b\n", " if tmp <= 0:\n", " return 0\n", " else:\n", " return 1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def OR(x1, x2):\n", " x = np.array([x1, x2])\n", " w = np.array([0.5, 0.5])\n", " b = -0.2\n", " tmp = np.sum(w*x) + b\n", " if tmp <= 0:\n", " return 0\n", " else:\n", " return 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "※ for 文をネストすることでまとめて実行・確認" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 0\n", "1 0 0\n", "0 1 0\n", "1 1 1\n" ] } ], "source": [ "for x2 in (0, 1):\n", " for x1 in (0, 1):\n", " print(x1, x2, AND(x1, x2))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 1\n", "1 0 1\n", "0 1 1\n", "1 1 0\n" ] } ], "source": [ "for x2 in (0, 1):\n", " for x1 in (0, 1):\n", " print(x1, x2, NAND(x1, x2))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 0\n", "1 0 1\n", "0 1 1\n", "1 1 1\n" ] } ], "source": [ "for x2 in (0, 1):\n", " for x1 in (0, 1):\n", " print(x1, x2, OR(x1, x2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.4 パーセプトロンの限界" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4.1 XOR ゲート" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\rm XOR}(x_1, x_2) = \\begin{cases}\n", "1 & (x_1, x_2 いずれか一方のみ = 1) \\\\\n", "0 & (それ以外)\n", "\\end{cases}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|x1|x2|y|\n", "|--:|--:|-:|\n", "|0|0|0|\n", "|1|0|1|\n", "|0|1|1|\n", "|1|1|0|\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "w = np.array([1.0, 1.0])\n", "b = -0.5" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4VPXZxvHvkwVCAkQhQCKLgGyyiWyyJlpZBUVUXIti\na3GjILS12r6v0sVWu4Dgjhtq1QoKyhpZ2iasmoABEiCAAQnIjoYl7PzePzL2RS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n70WdpVWM7bvazPJ8+yvDzJ7wos7SMLM3zGx3YWN9QmDfFbV9wbzv6pvZv81sre/35qjzLFPy/eec\nC4gHcDkFAyr+A3QsZJlw4EugMVAJWAW09Lr2Ym7fX4DHfM8fA54pZLktQJzX9RZzm4rcH8B1wFzA\ngC7AZ17X7eftuxqY5XWtpdy+RKA9kFnI/KDdd8XcvmDedwlAe9/zasAGf/zbC5gjBBf6bTAGAW/5\nnr8F3OhhLf5SnP0xCHjbFVgOXGRmCeVdaCkF8/9vRXLOpQL7f2CRYN53xdm+oOWc2+GcW+l7fpCC\nOzjrnrVYifdfwARCMZ2vDcbZ/xECVR3n3HcNVHYCdQpZzgELzGyFr41HICvO/gjmfVbc2rv5Dsnn\nmlmr8imtXATzviuuoN93ZtYQuBL47KxZJd5/5dHL6L/Kuw1Gefuh7TvzhXPOmVlh9/v2cM5tN7Pa\nwHwzW+/7S0cC00qggXPukJldB3wMNPW4JimeoN93ZlYV+Ah4xDl3oKyfV66B4EK8DcYPbZ+Z7TKz\nBOfcDt9h2+5CPmO77+duM5tOwWmLQA2E4uyPgN5nRSiy9jP/ETrn5pjZi2YW55wLhcZpwbzvihTs\n+87MIikIg3edc9POs0iJ91+wnTIK5jYYM4B7fM/vAc45IjKzGDOr9t1zoA8F3zkRqIqzP2YAd/vu\neOgC5J1x6izQFbl9ZhZvZuZ73pmCf1P7yr3SCyOY912Rgnnf+ep+HVjnnBtXyGIl339eXy0/44r4\nYArOcR0DdgGf+qZfAsw568r5Bgru/vit13WXYPtqAguBjcACoMbZ20fB3SyrfI+sYNi+8+0P4AHg\nAd9zo+ALkr4E1lDIHWSB+ijG9o3w7atVwHKgm9c1l2Db3gd2ACd8//Z+GmL7rqjtC+Z914OC642r\ngQzf47qy7j+1rhARESD4ThmJiMgFokAQERFAgSAiIj4KBBERARQIIiLio0AQERFAgSAiIj7/B380\nMHF+WSXOAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f = lambda x: x*-w[0]/w[1]-b\n", "x = np.arange(-1, 2, 0.5)\n", "plt.hlines([0], -1, 2, colors=\"grey\", linestyles=\"dashed\")\n", "plt.vlines([0], -1, 2, colors=\"grey\", linestyles=\"dashed\")\n", "plt.plot(x, f(x))\n", "plt.xlim(-1, 2)\n", "plt.ylim(-1, 2)\n", "plt.plot([0, 1], [0, 1], \"x\")\n", "plt.plot([0, 1], [1, 0], \"o\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.5 多層パーセプトロン" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.5.2 XOR ゲートの実装" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def XOR(x1, x2):\n", " s1 = NAND(x1, x2)\n", " s2 = OR(x1, x2)\n", " y = AND(s1, s2)\n", " return y" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 0\n", "1 0 1\n", "0 1 1\n", "1 1 0\n" ] } ], "source": [ "for x2 in (0, 1):\n", " for x1 in (0, 1):\n", " print(x1, x2, XOR(x1, x2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "※おまけ：NAND と OR を行列演算で書いてみた。" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def XOR2(x1, x2):\n", " W = np.array([[-0.5, -0.5], [0.5, 0.5]])\n", " B = np.array([0.7, -0.2])\n", " X = np.array([x1, x2])\n", " a = W.dot(X) + B\n", " Y = (a > 0).astype(\"float\")\n", " return AND(*Y)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 0\n", "1 0 1\n", "0 1 1\n", "1 1 0\n" ] } ], "source": [ "for x2 in (0, 1):\n", " for x1 in (0, 1):\n", " print(x1, x2, XOR2(x1, x2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "※おまけ2：Python 以外の言語での例\n", "\n", "+ [Julia](https://github.com/antimon2/MLNSC_2017A/blob/chapter2/Chapter2.jl.ipynb)\n", "+ [Ruby](https://github.com/antimon2/MLNSC_2017A/blob/chapter2/Chapter1_and_2.rb.ipynb)（第1章・第2章両方の内容）" ] }, { "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }