{ "cells": [ { "cell_type": "markdown", "id": "prepared-nitrogen", "metadata": {}, "source": [ "Note on pytorch. NN optimization acts imperitively/by side effect as follows.\n", " - Define model\n", " - loop\n", " - Calculate loss\n", " - Zero gradients\n", " - backprop to model\n", " - check conditions for exit\n", " - report diagnostics\n", " - disect results\n", " \n", " \n", "## Split result from NN\n", "Goal is to train the NN and then get a couple of outputs at the end that can be used to split between value function partials and launch functions." ] }, { "cell_type": "code", "execution_count": 1, "id": "grateful-conviction", "metadata": {}, "outputs": [], "source": [ "import torch" ] }, { "cell_type": "code", "execution_count": 2, "id": "incorrect-animal", "metadata": {}, "outputs": [], "source": [ "class DoubleNetwork(torch.nn.Module):\n", " def __init__(self, input_size,output_size,layers_size):\n", " super().__init__()\n", " \n", " #So, this next section constructs different layers within the NN\n", " #sinlge linear section\n", " self.linear_step_1a = torch.nn.Linear(input_size,layers_size)\n", " \n", " #single linear section\n", " self.linear_step_2a = torch.nn.Linear(layers_size,output_size)\n", " self.linear_step_2b = torch.nn.Linear(layers_size,output_size)\n", " \n", " def forward(self, input_values):\n", " \n", " intermediate_values_a = self.linear_step_1a(input_values)\n", " \n", " out_values_a = self.linear_step_2a(intermediate_values_a)\n", " out_values_b = self.linear_step_2b(intermediate_values_a)\n", " \n", " return out_values_a,out_values_b" ] }, { "cell_type": "code", "execution_count": 3, "id": "ruled-letter", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " tensor(3.5646, grad_fn=)\n", "\n", " tensor(11.7849, grad_fn=)\n", "\n", " tensor(24.8772, grad_fn=)\n", "\n", " tensor(5.4752, grad_fn=)\n", "\n", " tensor(0.4457, grad_fn=)\n", "\n", " tensor(0.0925, grad_fn=)\n", "\n", " tensor(0.0490, grad_fn=)\n", "\n", " tensor(0.0290, grad_fn=)\n", "\n", " tensor(0.0178, grad_fn=)\n", "\n", " tensor(0.0111, grad_fn=)\n", "\n", " tensor(0.0070, grad_fn=)\n", "\n", " tensor(0.0045, grad_fn=)\n", "\n", " tensor(0.0029, grad_fn=)\n", "\n", " tensor(0.0019, grad_fn=)\n", "\n", " tensor(0.0012, grad_fn=)\n", "\n", " tensor(0.0008, grad_fn=)\n", "\n", " tensor(0.0005, grad_fn=)\n", "\n", " tensor(0.0003, grad_fn=)\n", "\n", " tensor(0.0002, grad_fn=)\n", "\n", " tensor(0.0001, grad_fn=)\n" ] } ], "source": [ "model = DoubleNetwork(input_size = 5, output_size=5, layers_size=15)\n", "\n", "data_in = torch.tensor([1.5,2,3,4,5])\n", "\n", "data_in\n", "\n", "target = torch.zeros(5)\n", "\n", "def loss_fn2(output,target):\n", " return sum((output[1] +output[0] - target)**2)\n", " #could add a simplicity assumption i.e. l1 on parameters.\n", "\n", "#Prep Optimizer\n", "optimizer = torch.optim.SGD(model.parameters(),lr=0.01)\n", "\n", "for i in range(20):\n", " #training loop\n", " optimizer.zero_grad()\n", "\n", " output = model.forward(data_in)\n", " output\n", "\n", " l = loss_fn2(output, target)\n", "\n", " l.backward()\n", "\n", " optimizer.step()\n", "\n", " print(\"\\n\",l)" ] }, { "cell_type": "code", "execution_count": 4, "id": "quantitative-keeping", "metadata": {}, "outputs": [], "source": [ "class SplitNetwork(torch.nn.Module):\n", " def __init__(self, input_size,output_size_a,output_size_b,layers_size):\n", " super().__init__()\n", " \n", " #So, this next section constructs different layers within the NN\n", " #sinlge linear section\n", " self.linear_step_1 = torch.nn.Linear(input_size,layers_size)\n", " self.linear_step_2 = torch.nn.Linear(layers_size,layers_size)\n", " self.linear_step_3 = torch.nn.Linear(layers_size,layers_size)\n", " self.linear_step_4 = torch.nn.Linear(layers_size,layers_size)\n", " \n", " #single linear section\n", " self.linear_step_split_a = torch.nn.Linear(layers_size,output_size_a)\n", " self.linear_step_split_b = torch.nn.Linear(layers_size,output_size_b)\n", " \n", " def forward(self, input_values):\n", " \n", " intermediate_values = self.linear_step_1(input_values)\n", " intermediate_values = self.linear_step_2(intermediate_values)\n", " intermediate_values = self.linear_step_3(intermediate_values)\n", " intermediate_values = self.linear_step_4(intermediate_values)\n", " \n", " out_values_a = self.linear_step_split_a(intermediate_values)\n", " out_values_b = self.linear_step_split_b(intermediate_values)\n", " \n", " return out_values_a,out_values_b" ] }, { "cell_type": "code", "execution_count": 5, "id": "vietnamese-prophet", "metadata": {}, "outputs": [], "source": [ "model = SplitNetwork(input_size = 6, output_size_a=5, output_size_b=7, layers_size=15)\n", "\n", "data_in = torch.tensor([1.5,2,3,4,5,6])\n", "\n", "\n", "target_a = torch.zeros(5)\n", "target_b = torch.ones(7)\n", "\n", "def loss_fn3(output,target_a, target_b):\n", " return sum((output[0] - target_a)**2) + sum((output[1] - target_b)**2)\n", " #could add a simplicity assumption i.e. l1 on parameters." ] }, { "cell_type": "code", "execution_count": 6, "id": "limiting-slide", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " tensor(9.6420, grad_fn=)\n", "\n", " tensor(4.1914, grad_fn=)\n", "\n", " tensor(5.1337, grad_fn=)\n", "\n", " tensor(1.4943, grad_fn=)\n", "\n", " tensor(0.5210, grad_fn=)\n", "\n", " tensor(0.1217, grad_fn=)\n", "\n", " tensor(0.0605, grad_fn=)\n", "\n", " tensor(0.0256, grad_fn=)\n", "\n", " tensor(0.0126, grad_fn=)\n", "\n", " tensor(0.0057, grad_fn=)\n", "\n", " tensor(0.0028, grad_fn=)\n", "\n", " tensor(0.0013, grad_fn=)\n", "\n", " tensor(0.0006, grad_fn=)\n", "\n", " tensor(0.0003, grad_fn=)\n", "\n", " tensor(0.0001, grad_fn=)\n", "\n", " tensor(7.2050e-05, grad_fn=)\n", "\n", " tensor(3.5139e-05, grad_fn=)\n", "\n", " tensor(1.7068e-05, grad_fn=)\n", "\n", " tensor(8.3342e-06, grad_fn=)\n", "\n", " tensor(4.0624e-06, grad_fn=)\n", "\n", " tensor(1.9857e-06, grad_fn=)\n", "\n", " tensor(9.7029e-07, grad_fn=)\n", "\n", " tensor(4.7492e-07, grad_fn=)\n", "\n", " tensor(2.3232e-07, grad_fn=)\n", "\n", " tensor(1.1381e-07, grad_fn=)\n" ] } ], "source": [ "#Prep Optimizer\n", "optimizer = torch.optim.SGD(model.parameters(),lr=0.01)\n", "\n", "for i in range(25):\n", " #training loop\n", " optimizer.zero_grad()\n", "\n", " output = model.forward(data_in)\n", " output\n", "\n", " l = loss_fn3(output, target_a, target_b)\n", "\n", " l.backward()\n", "\n", " optimizer.step()\n", "\n", " print(\"\\n\",l)" ] }, { "cell_type": "code", "execution_count": 7, "id": "elder-karen", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(tensor([ 3.4232e-05, 3.7350e-05, 5.3748e-05, -2.7344e-05, -1.0052e-04],\n", " grad_fn=),\n", " tensor([1.0001, 1.0001, 1.0000, 1.0000, 1.0001, 1.0000, 1.0001],\n", " grad_fn=))" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "agreed-community", "metadata": {}, "outputs": [], "source": [] } ], "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }