{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 多層パーセプトロン3層までの学習には、ヘブ則を使うとよい" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ヘッブの法則 https://bsd.neuroinf.jp/wiki/%E3%83%98%E3%83%96%E5%89%87" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AND\n", "[ 26-May-2020 06:45:49 ] HEBB: Last Weights\n", "[0.01 0.01]\n", "Hebbian Weight: [-0.01 0.01 0.01]\n", "Hebbian Predict: [ 1 -1 -1 -1]\n", "NAND\n", "[ 26-May-2020 06:45:49 ] HEBB: Last Weights\n", "[-0.01 -0.01]\n", "Hebbian Weight: [-0.01 -0.01 -0.01]\n", "Hebbian Predict: [-1 -1 -1 1]\n", "OR\n", "[ 26-May-2020 06:45:49 ] HEBB: Last Weights\n", "[0.01 0.01]\n", "Hebbian Weight: [0.01 0.01 0.01]\n", "Hebbian Predict: [ 1 1 1 -1]\n" ] } ], "source": [ "#!/usr/local/bin/python3\n", "\n", "# ---------------------------------------------------------------------------------------------------------------------\n", "#\n", "# Bioinformatics Research Group\n", "# http://biorg.cis.fiu.edu/\n", "# Florida International University\n", "#\n", "# This software is a \"Camilo Valdes Work\" under the terms of the United States Copyright Act. Please cite the\n", "# author(s) in any work or product based on this material. Base implementation based on Sebastian Raschka at\n", "# https://github.com/rasbt/python-machine-learning-book/.\n", "#\n", "# OBJECTIVE:\n", "# The purpose of this program is to implement the Hebbian classifier.\n", "#\n", "# NOTES:\n", "# Please see the dependencies section below for the required libraries (if any).\n", "#\n", "# DEPENDENCIES:\n", "#\n", "# • Numpy\n", "#\n", "# The above libraries & modules are required. You can check the modules currently installed in your\n", "# system by running: python -c \"help('modules')\"\n", "#\n", "# USAGE:\n", "# Run the program with the \"--help\" flag to see usage instructions.\n", "#\n", "# AUTHOR: Camilo Valdes (cvalde03@fiu.edu)\n", "# Bioinformatics Research Group,\n", "# School of Computing and Information Sciences,\n", "# Florida International University (FIU)\n", "#\n", "#\n", "# ---------------------------------------------------------------------------------------------------------------------\n", "\n", "# Python Modules\n", "import time\n", "import numpy as np\n", "\n", "\n", "class Hebbian(object):\n", " \"\"\"\n", " Hebbian Classifier.\n", " This module implements the Hebbian Learning algorithm.\n", " Parameters:\n", " l_r (float): Learning rate (between 0.0 and 1.0)\n", " n_iter (int): Set to 1 for Hebb training rule, but can be adjusted for debugging.\n", " Attributes:\n", " w_ (1-d array): Weights after fitting.\n", " errors_ (list): Number of misclassifications.\n", " \"\"\"\n", "\n", " def __init__(self, l_r=0.01, n_iter=1):\n", " self.l_r = l_r\n", " self.n_iter = n_iter\n", "\n", " def fit(self, X, y):\n", " \"\"\"\n", " Fits the training data, and allows for Hebbian learning.\n", " Args:\n", " X (array-like): Training vectors, where n_samples is the number of samples and n_features is the number of\n", " features. Shape = [n_samples, n_feature].\n", " y (array-like): Target Values. Shape =[n_samples]\n", " Returns:\n", " self (object): Returns itself with updated weights.\n", " \"\"\"\n", "\n", " # Weight initialization. Note shape[1] is number of columns, and shape[0] is number of rows.\n", " self.w_ = np.zeros(1 + X.shape[1])\n", "\n", " # Track the misclassifications the single pass over the data.\n", " self.errors_ = []\n", "\n", " for _ in range(self.n_iter):\n", "\n", " errors = 0\n", "\n", " # The 'zip()' function returns a list of tuples, where the i-th tuple contains the i-th element from\n", " # each of the argument sequences or iterables.\n", " for xi, target in zip(X, y):\n", "\n", " # Hebb Learning Rule (self.l_r is the learning rate).\n", " # Weights updated based on\n", " # weight_change = l_r * input * output\n", " #\n", " if self.predict(xi) * target <= 0:\n", " hebb_update = self.l_r * target\n", " else:\n", " hebb_update = 0\n", "\n", " # Update the weights (including the bias)\n", " self.w_[1:] += hebb_update * xi\n", " self.w_[0] += hebb_update\n", "\n", " # Stopping Condition - Keep track of the errors\n", " errors += int(hebb_update != 0.0)\n", "\n", " self.errors_.append(errors)\n", "\n", " print(\"[ \" + time.strftime('%d-%b-%Y %H:%M:%S',\n", " time.localtime()) + \" ] HEBB: Last Weights\")\n", " print(self.w_[1:])\n", "\n", " return self\n", "\n", " def net_input(self, X):\n", " \"\"\"\n", " Calculates the Net Input for a neuron.\n", " Args:\n", " X (array-like): Training vectors, where n_samples is the number of samples and n_features is the number of\n", " features. Shape = [n_samples, n_feature].\n", " Returns:\n", " float: The net input (dot product) calculated from the input layer.\n", " \"\"\"\n", "\n", " # Return the dot-product of w (transposed) and x\n", " # Note: self.w_[0] is basically the \"threshold\" or so-called \"bias unit.\"\n", " # print(\"Bias: \" + str(self.w_[0]))\n", " return np.dot(X, self.w_[1:]) + self.w_[0]\n", "\n", " def predict(self, X):\n", " \"\"\"\n", " Returns the class label after a unit step.\n", " Args:\n", " X (array-like): Training vectors, where n_samples is the number of samples and n_features is the number of\n", " features. Shape = [n_samples, n_feature].\n", " Returns:\n", " ndarray: A Numpy array value with the expected (predicted) label of the pattern.\n", " \"\"\"\n", " return np.where(self.net_input(X) >= 0.0, 1, -1)\n", "\n", "\n", "def train(X, y):\n", " hebbian = Hebbian(n_iter=100)\n", " hebbian.fit(X, y)\n", " print('Hebbian Weight: ', hebbian.w_)\n", " print('Hebbian Predict: ', hebbian.predict(X))\n", "\n", "\n", "def main():\n", " print('AND')\n", " X = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])\n", " y = np.array([1, -1, -1, -1])\n", " train(X, y)\n", " print('NAND')\n", " X = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])\n", " y = np.array([-1, -1, -1, 1])\n", " train(X, y)\n", " print('OR')\n", " X = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])\n", " y = np.array([1, 1, 1, -1])\n", " train(X, y)\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] } ], "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 }