"We want to build a network of 2 layers of neurons with a given nonlinear function (e.g. tanh), whose weights are optimized to reduce a loss function (error between output and a corresponding target for each sample)."
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "b2cb5fa6-b1ad-47c9-9062-f1bca177e52f",
"metadata": {},
"outputs": [],
"source": [
"# layer of artificial neurons, batch calculations\n",
"class layer:\n",
" \n",
" # forward function\n",
" def f(self, x):\n",
" # sigmoid\n",
" return 1 / (1 + np.exp(-x))\n",
"\n",
" def df_finv(self, y):\n",
" # sigmoid\n",
" return y * (1 - y)\n",
"\n",
" # forward pass for x of shape (input dim)\n",
" def fwd(self, x, W, return_x1=False):\n",
" # augmented vector by one extra element (for bias)\n",
" x1 = np.ones([x.shape[0]+1])\n",
" x1[:-1] = x\n",
" # calculate output after weight multiplication and function f\n",
" y = self.f(np.dot(W, x1))\n",
" # return y or y and x1\n",
" if return_x1:\n",
" return y, x1\n",
" else:\n",
" return y\n",
"\n",
" # backward path\n",
" def bckwd(self, x, W, tgt):\n",
" # calculate output\n",
" y, x1 = self.fwd(x, W, return_x1=True)\n",
" # calculate derivative term corresponding to loss\n",
We want to build a network of 2 layers of neurons with a given nonlinear function (e.g. tanh), whose weights are optimized to reduce a loss function (error between output and a corresponding target for each sample).