Computer_Vision/Chapter01/Gradient_descent.ipynb

120 lines
3.6 KiB
Plaintext
Raw Permalink Normal View History

2024-02-13 03:34:51 +01:00
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Gradient_descent.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"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.7.6"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter01/Gradient_descent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-24T13:36:49.845714Z",
"start_time": "2020-09-24T13:36:49.666592Z"
},
"id": "TTWK_bQBC7mf"
},
"source": [
"import numpy as np \n",
"from copy import deepcopy\n",
"import matplotlib.pyplot as plt\n",
"x = np.array([[1,1]])\n",
"y = np.array([[0]])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-24T13:36:49.850377Z",
"start_time": "2020-09-24T13:36:49.846832Z"
},
"id": "V8f13jAKC9Rv"
},
"source": [
"from copy import deepcopy\n",
"import numpy as np\n",
"def feed_forward(inputs, outputs, weights): \n",
" pre_hidden = np.dot(inputs,weights[0])+ weights[1]\n",
" hidden = 1/(1+np.exp(-pre_hidden))\n",
" out = np.dot(hidden, weights[2]) + weights[3]\n",
" mean_squared_error = np.mean(np.square(out - outputs))\n",
" return mean_squared_error"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-24T13:36:49.855575Z",
"start_time": "2020-09-24T13:36:49.851797Z"
},
"id": "8mF3nQbMC_Ja"
},
"source": [
"def update_weights(inputs, outputs, weights, lr):\n",
" original_weights = deepcopy(weights)\n",
" temp_weights = deepcopy(weights)\n",
" updated_weights = deepcopy(weights)\n",
" original_loss = feed_forward(inputs, outputs, original_weights)\n",
" for i, layer in enumerate(original_weights):\n",
" for index, weight in np.ndenumerate(layer):\n",
" temp_weights = deepcopy(weights)\n",
" temp_weights[i][index] += 0.0001\n",
" _loss_plus = feed_forward(inputs, outputs, temp_weights)\n",
" grad = (_loss_plus - original_loss)/(0.0001)\n",
" updated_weights[i][index] -= grad*lr\n",
" return updated_weights, original_loss"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QqapuMCwDQiJ"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}