{ "cells": [ { "cell_type": "markdown", "id": "20fce00f", "metadata": {}, "source": [ "### Deep Learning\n", "#### Artificial neuron \n", "An **artificial neuron** gets an input vector $\\boldsymbol{x}$ and produces a scalar value $y$, which is modeled by the following equation:\n", "
$\\large y=\\phi(\\boldsymbol{x}^T\\boldsymbol{w}+b)=\\phi(\\boldsymbol{w}^T\\boldsymbol{x}+b)$\n", "
Where $b$ is the bias. The weight vector is $\\boldsymbol{w}=[w_1,w_2,...,w_m]^T$. The input vector is $\\boldsymbol{x}=[x_1,x_2,...,x_m]^T$. The $\\phi$ is an activation function. \n", "- Common activation functions are: *Logistic*, *ReLU*, *Tanh*, and *Linear*.\n", "\n", "
\n", "In the following, we implement an arificial neuron based on the formula mentioned above.\n", "
\n", "https://github.com/ostad-ai/Machine-Learning\n", "
Explanation: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/Data-Visualization" ] }, { "cell_type": "code", "execution_count": 1, "id": "22ebfdcc", "metadata": {}, "outputs": [], "source": [ "# Import required module\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "57cfa2b6", "metadata": {}, "outputs": [], "source": [ "# Define the neuron with y=φ(x^Tw+b)\n", "class ArtificialNeuron:\n", " def __init__(self, n_inputs, activation='logistic'):\n", " self.weights = np.random.randn(n_inputs)\n", " self.bias = np.random.randn()\n", " self.activation = activation\n", "\n", " def __call__(self, x):\n", " z = np.dot(x, self.weights) + self.bias\n", " if self.activation == 'logistic':\n", " return 1 / (1 + np.exp(-z))\n", " elif self.activation == 'relu':\n", " return np.maximum(0, z)\n", " else: # Linear\n", " return z" ] }, { "cell_type": "code", "execution_count": 3, "id": "b6d79cba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input is: [ 0.3 -1.2 0.7]\n", "Output is:0.9308216406202954\n", "Weights are: [ 1.41314181 -1.01381938 -0.12499518];\n", "and Bias is:1.0463504203241891\n" ] } ], "source": [ "# Example\n", "neuron = ArtificialNeuron(n_inputs=3)\n", "x = np.array([0.3, -1.2, 0.7])\n", "y = neuron(x)\n", "print(f'Input is: {x}')\n", "print(f'Output is:{y}') # Output after activation\n", "print(f'Weights are: {neuron.weights};\\nand Bias is:{neuron.bias}')" ] }, { "cell_type": "code", "execution_count": null, "id": "7cb5407a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }