Skip to content

Commit

Permalink
Implement forward propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
justanotherbyte committed Oct 6, 2024
1 parent 0110bb5 commit 30b676b
Showing 1 changed file with 84 additions and 22 deletions.
106 changes: 84 additions & 22 deletions lecture1/forward_propagation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,35 +36,73 @@
" activation: typing.Callable[[np.floating], np.floating] = sigmoid\n",
" ):\n",
" self.id = id\n",
" self.activation = activation"
" self.activation = activation\n",
"\n",
" def compute(\n",
" self,\n",
" inputs: np.ndarray,\n",
" weights: np.ndarray\n",
" ):\n",
" bias = weights[0]\n",
" return self.activation(\n",
" np.dot(inputs, weights[1:]) + bias\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"class InputPerceptron(Perceptron):\n",
" def compute(self, inputs: np.ndarray, *_):\n",
" return inputs"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"class DenseLayer:\n",
" def __init__(self, n_perceptrons: int):\n",
" self.perceptrons = [Perceptron(i) for i in range(n_perceptrons)]\n",
" self.size = n_perceptrons"
" self.size = n_perceptrons\n",
"\n",
" def compute(\n",
" self,\n",
" inputs: np.ndarray,\n",
" weights: list[np.ndarray]\n",
" ):\n",
" vec = []\n",
" for idx, w in enumerate(weights):\n",
" perceptron = self.perceptrons[idx]\n",
" val = perceptron.compute(inputs, w)\n",
" vec.append(val)\n",
"\n",
" return np.array(vec)\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"class InputLayer(DenseLayer):\n",
" def __init__(self, n_perceptrons: int):\n",
" super().__init__(n_perceptrons)"
" self.perceptrons = [InputPerceptron(i) for i in range(n_perceptrons)]\n",
" self.size = n_perceptrons\n",
"\n",
" def compute(self, inputs: np.ndarray, *_):\n",
" return inputs"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -84,7 +122,6 @@
" num = next_layer.size\n",
"\n",
" for _ in range(num):\n",
" # vec = np.random.uniform(0, 1, (1, dim))\n",
" vec = np.array([random.random() for _ in range(dim)])\n",
" intermediate_weights.append(vec)\n",
"\n",
Expand All @@ -93,46 +130,71 @@
" if idx + 1 == len(self.layers) - 1:\n",
" break\n",
" \n",
" return weights\n"
" return weights\n",
" \n",
" def forward_propagate(self, X: np.ndarray):\n",
" prop_vec = X\n",
"\n",
" for idx, layer in enumerate(self.layers):\n",
" weights = self.weights[idx - 1]\n",
" prop_vec = layer.compute(prop_vec, weights)\n",
"\n",
" return prop_vec\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"nn = Network([\n",
" InputLayer(2),\n",
" DenseLayer(4),\n",
" DenseLayer(2)\n",
" DenseLayer(1)\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[array([0.86790844, 0.96834797, 0.15993637]),\n",
" array([0.35122612, 0.58513149, 0.14872007]),\n",
" array([0.35887828, 0.67909424, 0.33210523]),\n",
" array([0.95527097, 0.86995393, 0.18074543])],\n",
" [array([0.13968463, 0.00826856, 0.3395287 , 0.39243069, 0.58516214]),\n",
" array([0.53442484, 0.30436439, 0.61744243, 0.13276608, 0.13068895])]]"
"[[array([0.7627113 , 0.5958149 , 0.49293627])]]"
]
},
"execution_count": 53,
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nn.weights"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.91248798])"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = np.array([1, 2])\n",
"nn.forward_propagate(X)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 30b676b

Please sign in to comment.