From abe4725ba115fba07a96f2ca67e36dbae63f897b Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 6 Feb 2025 10:19:25 +0100 Subject: [PATCH] I got the cool neuron things in basic --- tasks/04 - neuron.ipynb | 170 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 tasks/04 - neuron.ipynb diff --git a/tasks/04 - neuron.ipynb b/tasks/04 - neuron.ipynb new file mode 100644 index 0000000..dadfbaf --- /dev/null +++ b/tasks/04 - neuron.ipynb @@ -0,0 +1,170 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from math import e\n", + "from numpy.typing import NDArray as array\n", + "from numpy import float64 as float" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "def sigmoid(z):\n", + " return 1/ (1+ e** (-z))\n", + "\n", + "def ht(weights, x):\n", + " return g(weights.T @ x)\n", + "\n", + "def g(x):\n", + " return sigmoid(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.9999546021312976\n", + "4.539786870243442e-05\n", + "9.357622968839314e-14\n", + "4.539786870243442e-05\n" + ] + } + ], + "source": [ + "# and gate?\n", + "weights_and = np.array([-30, 20, 20])\n", + "\n", + "print(ht(weights_and, np.array([1,1,1])))\n", + "print(ht(weights_and, np.array([1,0,1])))\n", + "print(ht(weights_and, np.array([1,0,0])))\n", + "print(ht(weights_and, np.array([1,1,0])))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.357622968839314e-14\n", + "4.539786870243442e-05\n", + "0.9999546021312976\n", + "4.539786870243442e-05\n" + ] + } + ], + "source": [ + "# not x and not y gate?\n", + "weights = np.array([10, -20, -20])\n", + "\n", + "print(ht(weights, np.array([1,1,1])))\n", + "print(ht(weights, np.array([1,0,1])))\n", + "print(ht(weights, np.array([1,0,0])))\n", + "print(ht(weights, np.array([1,1,0])))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.9999999999999065\n", + "0.9999546021312976\n", + "4.539786870243442e-05\n", + "0.9999546021312976\n" + ] + } + ], + "source": [ + "# or gate?\n", + "weights = np.array([-10, 20, 20])\n", + "print(ht(weights, np.array([1,1,1])))\n", + "print(ht(weights, np.array([1,0,1])))\n", + "print(ht(weights, np.array([1,0,0])))\n", + "print(ht(weights, np.array([1,1,0])))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "# make it more generic\n", + "def layer(a: array, w: array, debug=False) -> array:\n", + " a_with_one: array = np.concatenate(([1],a))\n", + " z = w @ a_with_one\n", + " a_next = sigmoid(z)\n", + "\n", + " return a_next" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.9999999999999656\n", + "0.999983298578152\n", + "0.0001233945759862318\n", + "0.999983298578152\n" + ] + } + ], + "source": [ + "# or gate but cooler\n", + "weights = np.array([1, -10, 20, 20])\n", + "print(layer(np.array([1,1,1]),weights ))\n", + "print(layer(np.array([1,0,1]),weights ))\n", + "print(layer(np.array([1,0,0]),weights ))\n", + "print(layer(np.array([1,1,0]),weights ))\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ki", + "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.13.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}