{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# パーセプトロンの実装" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 簡単な実装\n", "\n", "AND関数を実装しよう" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "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": [ "## 重みとバイアスの導入\n", "パーセプトロンの条件式を見てみよう\n", "$$\n", " y = \n", " \\begin{cases}\n", " 0 \\quad (w_1 x_1 + w_2 x_2 \\leqq \\theta) \\\\\n", " 1 \\quad (w_1 x_1 + w_2 x_2 > \\theta) \\\\\n", " \\end{cases}\n", "$$\n", "\n", "ここで、$\\theta$ = $-b$ とすると\n", "\n", "$$\n", " y = \n", " \\begin{cases}\n", " 0 \\quad (w_1 x_1 + w_2 x_2 \\leqq - b) \\\\\n", " 1 \\quad (w_1 x_1 + w_2 x_2 > - b) \\\\\n", " \\end{cases}\n", "$$\n", "\n", "式変形して\n", "\n", "$$\n", " y = \n", " \\begin{cases}\n", " 0 \\quad (b + w_1 x_1 + w_2 x_2 \\leqq 0) \\\\\n", " 1 \\quad (b + w_1 x_1 + w_2 x_2 > 0) \\\\\n", " \\end{cases}\n", "$$\n", "\n", "ここで、$b$ を **バイアス** と呼び、$w_1$ $w_2$ を **重み** と呼びます" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.5])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "x = np.array([0, 1]) # 入力\n", "w = np.array([0.5, 0.5]) # 重み\n", "b = -0.7 # バイアス\n", "w * x" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(w * x)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.19999999999999996" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(w * x) + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 重みとバイアスによる実装" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def AND(x1, x2):\n", " \"\"\"\n", " AND関数\n", " \n", " Parameters\n", " ----------\n", " x1 : float\n", " 入力1\n", " x2 : float\n", " 入力2\n", " \"\"\"\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": "markdown", "metadata": {}, "source": [ "* $w_1$ や $w_2$ の重みは、入力信号の重要度をコントロールするパラメータ\n", "* $b$ のバイアスは、発火のしやすさのパラメータ" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def NAND(x1, x2):\n", " \"\"\"\n", " NAND関数\n", " \n", " Parameters\n", " ----------\n", " x1 : float\n", " 入力1\n", " x2 : float\n", " 入力2\n", " \"\"\"\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": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NAND(0, 0)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NAND(1, 0)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NAND(0, 1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NAND(1, 1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def OR(x1, x2):\n", " \"\"\"\n", " OR関数\n", " \n", " Parameters\n", " ----------\n", " x1 : float\n", " 入力1\n", " x2 : float\n", " 入力2\n", " \"\"\"\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": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OR(0, 0)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OR(1, 0)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OR(0, 1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OR(1, 1)" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }