282 lines
6.0 KiB
Plaintext
282 lines
6.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Neuron with 3 inputs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Output: 2.3\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"inputs = [1, 2, 3]\n",
|
|
"weights = [0.2, 0.8, -0.5]\n",
|
|
"bias = 2\n",
|
|
"\n",
|
|
"output = (inputs[0]*weights[0] + inputs[1]*weights[1] + inputs[2]*weights[2] + bias)\n",
|
|
"\n",
|
|
"print(f\"Output: {output}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Neuron with 4 inputs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Output: 4.8\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"inputs = [1.0, 2.0, 3.0, 2.5]\n",
|
|
"weights = [0.2, 0.8, -0.5, 1.0]\n",
|
|
"bias = 2\n",
|
|
"\n",
|
|
"output = (inputs[0]*weights[0] + inputs[1]*weights[1] + inputs[2]*weights[2] + inputs[3]*weights[3] + bias)\n",
|
|
"\n",
|
|
"print(output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Layer of 3 neurons with 4 inputs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[4.8, 1.21, 2.385]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"num_neurons = 3\n",
|
|
"num_inputs = 4\n",
|
|
"inputs = [1.0, 2.0, 3.0, 2.5]\n",
|
|
"weights = [[0.2, 0.8, -0.5, 1.0],\n",
|
|
" [0.5, -0.91, 0.26, -0.5],\n",
|
|
" [-0.26, -0.27, 0.17, 0.87]]\n",
|
|
"biases = [2, 3, 0.5]\n",
|
|
"\n",
|
|
"outputs = []\n",
|
|
"for i in range(num_neurons):\n",
|
|
" output = 0\n",
|
|
" for j in range(num_inputs):\n",
|
|
" output += inputs[j]*weights[i][j]\n",
|
|
" output += biases[i]\n",
|
|
" outputs.append(output)\n",
|
|
"\n",
|
|
"print(outputs)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Layer of 3 neurons with 4 inputs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[4.8, 1.21, 2.385]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"num_neurons = 3\n",
|
|
"num_inputs = 4\n",
|
|
"inputs = [1.0, 2.0, 3.0, 2.5]\n",
|
|
"weights = [[0.2, 0.8, -0.5, 1.0],\n",
|
|
" [0.5, -0.91, 0.26, -0.5],\n",
|
|
" [-0.26, -0.27, 0.17, 0.87]]\n",
|
|
"biases = [2, 3, 0.5]\n",
|
|
"\n",
|
|
"outputs = []\n",
|
|
"for neuron_weights, neuron_bias in zip(weights, biases):\n",
|
|
" neuron_output = 0\n",
|
|
" for input, weight in zip(inputs, neuron_weights):\n",
|
|
" neuron_output += input*weight\n",
|
|
" neuron_output += neuron_bias\n",
|
|
" outputs.append(neuron_output)\n",
|
|
"\n",
|
|
"print(outputs)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Single Neuron using Numpy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"4.8\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"inputs = [1.0, 2.0, 3.0, 2.5]\n",
|
|
"weights = [0.2, 0.8, -0.5, 1.0]\n",
|
|
"bias = 2\n",
|
|
"\n",
|
|
"output = np.dot(inputs, weights) + bias\n",
|
|
"print(output)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Layer of Neurons Using Numpy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[4.8 1.21 2.385]\n",
|
|
"[4.8 1.21 2.385]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"inputs = [1.0, 2.0, 3.0, 2.5]\n",
|
|
"weights = np.array([\n",
|
|
" [0.2, 0.8, -0.5, 1],\n",
|
|
" [0.5, -0.91, 0.26, -0.5],\n",
|
|
" [-0.26, -0.27, 0.17, 0.87]\n",
|
|
"])\n",
|
|
"biases = [2.0, 3.0, 0.5]\n",
|
|
"\n",
|
|
"layer_outputs = np.dot(weights, inputs) + biases\n",
|
|
"# must be dot(weights, inputs), not dot(inputs, weights)\n",
|
|
"# this takes the dot each row of the weights by the column of inputs (remember the second term is transposed)\n",
|
|
"print(layer_outputs)\n",
|
|
"\n",
|
|
"layer_outputs_2 = np.dot(inputs, weights.T) + biases\n",
|
|
"# this takes each input and multiplies by the weight. also correct.\n",
|
|
"print(layer_outputs_2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Layer of Neurons and Batch of Data Using Numpy\n",
|
|
"Batch of data is simply a set of inputs."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[[ 4.8 1.21 2.385]\n",
|
|
" [ 8.9 -1.81 0.2 ]\n",
|
|
" [ 1.41 1.051 0.026]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"inputs = [ # Batch of inputs\n",
|
|
" [1.0, 2.0, 3.0, 2.5], \n",
|
|
" [2.0, 5.0, -1.0, 2.0], \n",
|
|
" [-1.5, 2.7, 3.3, -0.8]\n",
|
|
"]\n",
|
|
"weights = np.array([\n",
|
|
" [0.2, 0.8, -0.5, 1],\n",
|
|
" [0.5, -0.91, 0.26, -0.5],\n",
|
|
" [-0.26, -0.27, 0.17, 0.87]\n",
|
|
"])\n",
|
|
"biases = [2.0, 3.0, 0.5]\n",
|
|
"\n",
|
|
"outputs = np.dot(inputs, weights.T) + biases\n",
|
|
"# For every row of inputs, compute the dot of input set and weights\n",
|
|
"print(outputs)"
|
|
]
|
|
}
|
|
],
|
|
"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.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|