Skip to content

Commit

Permalink
Add constant move (#45)
Browse files Browse the repository at this point in the history
move AddConstant code to the notebooks
  • Loading branch information
sergiomarchio authored Sep 28, 2024
1 parent 84ee426 commit 6471c76
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 54 deletions.
33 changes: 0 additions & 33 deletions edunn/models/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,6 @@ def backward(self, dE_dy: np.ndarray):
return dE_dy, dE_dp


class AddConstant(Model):
"""
A layer that adds a constant
This layer has NO parameters
"""

def __init__(self, value: float, name=None):
super().__init__(name=name)
self.value = value

def forward(self, x: np.ndarray):
"""
:param x: input vector/matrix
:return: `x + a`, constant value, stored in `self.value`
"""

""" YOUR IMPLEMENTATION START """
# default: y = np.zeros_like(x)
y = x + self.value
""" YOUR IMPLEMENTATION END """

return y

def backward(self, dE_dy: np.ndarray):
""" YOUR IMPLEMENTATION START """
# default: dE_dx = np.zeros_like(dE_dy)
dE_dx = dE_dy
""" YOUR IMPLEMENTATION END """

dE_dp = {} # no parameters, no derivatives
return dE_dx, dE_dp


class ReLU(Model):

def forward(self, x: np.ndarray):
Expand Down
63 changes: 53 additions & 10 deletions guides/en/02. AddConstant Layer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"%autoreload 2\n",
"\n",
"from edunn import utils\n",
"import edunn as nn\n",
"import numpy as np"
]
},
Expand Down Expand Up @@ -41,7 +40,7 @@
"y([x_1, x_2, ..., x_n]) = [x_1 + C, x_2 + C, ..., x_n + C]\n",
"$\n",
"\n",
"We start with the `forward` method of the `AddConstant` class, which can be found in the `activations.py` file in the `edunn/models` folder. You need to complete the code between the comments:\n",
"We start with the `forward` method of the `AddConstant` class. You need to complete the code between the comments:\n",
"\n",
"```\n",
"\"\"\" YOUR IMPLEMENTATION START \"\"\"\n",
Expand All @@ -52,9 +51,51 @@
"\"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"```\n",
"\n",
"Between these lines there's some default code (in this case `y = np.zeros_like(x)`) just to avoid errors from the Python interpreter until you write your implementation. Feel free to remove this line to provide a clean implementation!\n",
"\n",
"Don't forget to execute the cell after adding your code!\n",
"\n",
"Then, verify with the following cell for a layer that adds 3 and another that adds -3. If both checks are correct, you will see two messages with <span style='background-color:green;color:white;'>success</span>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from edunn import Model\n",
"\n",
"class AddConstant(Model):\n",
" \"\"\"\n",
" A layer that adds a constant. This layer has NO parameters\n",
" \"\"\"\n",
"\n",
" def __init__(self, value: float, name=None):\n",
" super().__init__(name=name)\n",
" self.value = value\n",
"\n",
" def forward(self, x: np.ndarray):\n",
" \"\"\"\n",
" :param x: input vector/matrix\n",
" :return: `x + a`, constant value, stored in `self.value`\n",
" \"\"\"\n",
"\n",
" \"\"\" YOUR IMPLEMENTATION START \"\"\"\n",
" y = np.zeros_like(x)\n",
" \"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"\n",
" return y\n",
"\n",
" def backward(self, dE_dy: np.ndarray):\n",
" \"\"\" YOUR IMPLEMENTATION START \"\"\"\n",
" dE_dx = np.zeros_like(dE_dy)\n",
" \"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"\n",
" dE_dp = {} # no parameters, no derivatives\n",
" return dE_dx, dE_dp\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -64,12 +105,12 @@
"x = np.array([[3.5, -7.2, 5.3],\n",
" [-3.5, 7.2, -5.3]])\n",
"\n",
"layer = nn.AddConstant(3)\n",
"layer = AddConstant(3)\n",
"y = np.array([[6.5, -4.2, 8.3],\n",
" [-0.5, 10.2, -2.3]])\n",
"utils.check_same(y, layer.forward(x))\n",
"\n",
"layer = nn.AddConstant(-3)\n",
"layer = AddConstant(-3)\n",
"y = np.array([[0.5, -10.2, 2.3],\n",
" [-6.5, 4.2, -8.3]])\n",
"utils.check_same(y, layer.forward(x))"
Expand Down Expand Up @@ -113,7 +154,7 @@
"\n",
"$\\frac{δE}{δx} = [\\frac{δE}{δy_1}, \\frac{δE}{δy_2}, ..., \\frac{δE}{δy_n}] = \\frac{δE}{δy}$\n",
"\n",
"What dos this equation mean? Simply that the layer just propagates the gradients from the next layer to the previous one.\n",
"What does this equation mean? Simply that the layer just propagates the gradients from the next layer to the previous one.\n",
"\n",
"Note that for simplicity, in the code we call these vectors `δEδy` and `δEδx`. Also, remember that in this case $C$ is a constant and NOT a network parameter, so we do not need to calculate $\\frac{δE}{δC}$.\n",
"\n",
Expand Down Expand Up @@ -141,6 +182,8 @@
"\"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"```\n",
"\n",
"Don't forget to execute the cell after adding your code!\n",
"\n",
"Then, verify with the following cell for a layer that adds 3 and another that adds -3. If both checks are correct, you will see two messages with <span style='background-color:green;color:white;'>success</span>."
]
},
Expand All @@ -158,11 +201,11 @@
"input_shape = (5, 2)\n",
"\n",
"# Test derivatives of an AddConstant layer that adds 3\n",
"layer = nn.AddConstant(3)\n",
"layer = AddConstant(3)\n",
"check_gradient.common_layer(layer, input_shape, samples=samples)\n",
"\n",
"# Test derivatives of an AddConstant layer that adds -4\n",
"layer = nn.AddConstant(-4)\n",
"layer = AddConstant(-4)\n",
"check_gradient.common_layer(layer, input_shape, samples=samples)"
]
},
Expand All @@ -187,13 +230,13 @@
"metadata": {},
"outputs": [],
"source": [
"c1 = nn.AddConstant(3)\n",
"c1 = AddConstant(3)\n",
"print(c1.name)\n",
"\n",
"c2 = nn.AddConstant(3)\n",
"c2 = AddConstant(3)\n",
"print(c2.name)\n",
"\n",
"c3 = nn.AddConstant(3, name=\"My first layer :)\")\n",
"c3 = AddConstant(3, name=\"My first layer :)\")\n",
"print(c3.name)\n"
]
}
Expand Down
65 changes: 54 additions & 11 deletions guides/es/02. Capa AddConstant.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"%autoreload 2\n",
"\n",
"from edunn import utils\n",
"import edunn as nn\n",
"import numpy as np"
]
},
Expand All @@ -35,21 +34,63 @@
"source": [
"# Método forward\n",
"\n",
"El método `forward` calcula la salida `y` en base a la entrada `x`, como explicamos antes. En términos formales, si la constante a sumar es $C$ y la entrada a la capa es $x = [x_1,x_2,...,x_n] $, entonces la salida $y$ es:\n",
"El método `forward` calcula la salida `y` en base a la entrada `x`, como explicamos antes. En términos formales, si la constante a sumar es $C$ y la entrada a la capa es $x = [x_1,x_2,...,x_n]$, entonces la salida $y$ es:\n",
"\n",
"$\n",
"y([x_1,x_2,...,x_n])= [x_1+C,x_2+C,...,x_n+C]\n",
"$\n",
"\n",
"Comenzamos con el método `forward` de la clase `AddConstant`, que podrás encontrar en el archivo `activations.py` de la carpeta `edunn/models`. Debés completar el código entre los comentarios:\n",
"Comenzamos con el método `forward` de la clase `AddConstant`. Debés completar el código entre los comentarios:\n",
"\n",
"```\"\"\" YOUR IMPLEMENTATION START \"\"\"```\n",
"\n",
"y\n",
"\n",
"```\"\"\" YOUR IMPLEMENTATION END \"\"\"```\n",
"\n",
"Y luego verificar con la siguiente celda una capa que suma 3 y otra que suma -3. Si ambos chequeos son correctos, verás dos mensajes de <span style='background-color:green;color:white; '>éxito (success)</span>."
"Entre esas líneas hay código por defecto (en este caso `y = np.zeros_like(x)`) solo para evitar errores del intérprete de Python hasta que escribas tu implementación. Podés borrar esa línea para tener una implementación limpia! \n",
"\n",
"No olvides ejecutar la celda después de agregar tu código!\n",
"\n",
"Luego, verificar con la siguiente celda una capa que suma 3 y otra que suma -3. Si ambos chequeos son correctos, verás dos mensajes de <span style='background-color:green;color:white; '>éxito (success)</span>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from edunn import Model\n",
"\n",
"class AddConstant(Model):\n",
" \"\"\"\n",
" A layer that adds a constant. This layer has NO parameters\n",
" \"\"\"\n",
"\n",
" def __init__(self, value: float, name=None):\n",
" super().__init__(name=name)\n",
" self.value = value\n",
"\n",
" def forward(self, x: np.ndarray):\n",
" \"\"\"\n",
" :param x: input vector/matrix\n",
" :return: `x + a`, constant value, stored in `self.value`\n",
" \"\"\"\n",
"\n",
" \"\"\" YOUR IMPLEMENTATION START \"\"\"\n",
" y = np.zeros_like(x)\n",
" \"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"\n",
" return y\n",
"\n",
" def backward(self, dE_dy: np.ndarray):\n",
" \"\"\" YOUR IMPLEMENTATION START \"\"\"\n",
" dE_dx = np.zeros_like(dE_dy)\n",
" \"\"\" YOUR IMPLEMENTATION END \"\"\"\n",
"\n",
" dE_dp = {} # no parameters, no derivatives\n",
" return dE_dx, dE_dp\n"
]
},
{
Expand All @@ -61,12 +102,12 @@
"x = np.array([[3.5, -7.2, 5.3],\n",
" [-3.5, 7.2, -5.3]])\n",
"\n",
"layer = nn.AddConstant(3)\n",
"layer = AddConstant(3)\n",
"y = np.array([[6.5, -4.2, 8.3],\n",
" [-0.5, 10.2, -2.3]])\n",
"utils.check_same(y, layer.forward(x))\n",
"\n",
"layer = nn.AddConstant(-3)\n",
"layer = AddConstant(-3)\n",
"y = np.array([[0.5, -10.2, 2.3],\n",
" [-6.5, 4.2, -8.3]])\n",
"utils.check_same(y, layer.forward(x))"
Expand Down Expand Up @@ -129,6 +170,8 @@
"\n",
"```\"\"\" YOUR IMPLEMENTATION END \"\"\"```\n",
"\n",
"No olvides ejecutar la celda después de agregar tu código!\n",
"\n",
"Y luego verificar con la siguiente celda una capa que suma 3 y otra que suma -3. Si ambos chequeos son correctos, verás dos mensajes de <span style='background-color:green;color:white; '>éxito (success)</span>."
]
},
Expand All @@ -146,11 +189,11 @@
"input_shape = (5, 2)\n",
"\n",
"# Test derivatives of an AddConstant layer that adds 3\n",
"layer = nn.AddConstant(3)\n",
"layer = AddConstant(3)\n",
"check_gradient.common_layer(layer, input_shape, samples=samples)\n",
"\n",
"# Test derivatives of an AddConstant layer that adds -4\n",
"layer = nn.AddConstant(-4)\n",
"layer = AddConstant(-4)\n",
"check_gradient.common_layer(layer, input_shape, samples=samples)\n"
]
},
Expand All @@ -175,13 +218,13 @@
"metadata": {},
"outputs": [],
"source": [
"c1 = nn.AddConstant(3)\n",
"c1 = AddConstant(3)\n",
"print(c1.name)\n",
"\n",
"c2 = nn.AddConstant(3)\n",
"c2 = AddConstant(3)\n",
"print(c2.name)\n",
"\n",
"c3 = nn.AddConstant(3, name=\"Mi primera capa :)\")\n",
"c3 = AddConstant(3, name=\"Mi primera capa :)\")\n",
"print(c3.name)\n"
]
}
Expand Down

0 comments on commit 6471c76

Please sign in to comment.