{"id":5103,"date":"2023-01-12T14:31:59","date_gmt":"2023-01-12T06:31:59","guid":{"rendered":"https:\/\/fanyuzhao.com\/?p=5103"},"modified":"2023-01-12T14:31:59","modified_gmt":"2023-01-12T06:31:59","slug":"gradient-derivative-in-python","status":"publish","type":"post","link":"https:\/\/fanyuzhao.com\/?p=5103","title":{"rendered":"Gradient \/ Derivative in Python"},"content":{"rendered":"\n<p>By definition:<\/p>\n\n\n\n<p>$$ \\nabla f(x_1, x_2) =\\frac{\\partial f}{\\partial x_1} + \\frac{\\partial f}{\\partial x_1} $$<\/p>\n\n\n\n<p>$$ \\frac{\\partial f}{\\partial x_1} = \\lim_{h\\rightarrow 0}\\frac{f(x+h)-f(x)}{h} $$<\/p>\n\n\n\n<p>$$ \\frac{\\partial f}{\\partial x_1} = \\lim_{h\\rightarrow 0}\\frac{f(x+h)-f(x-h)}{2h} $$<\/p>\n\n\n\n<ul><li>Code:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>func_1 = lambda x: x**2 +5\nfunc_2 = lambda x: x&#91;0]**2 + x&#91;1]**3 +1\nx_lim = np.arange(-5,5,0.01)\ninput_val = np.array(&#91;2.0,3.0])\n\n\nclass Differentiate:\n    def __init__(self):\n        self.h = 1e-5\n        self.dx = None\n        \n    def d1_diff(self, f, x):\n        fh1 = f(x+self.h)\n        fh2 = f(x-self.h)\n        self.dx = (fh1 - fh2)\/(2*self.h)\n        return self.dx\n        \n    def tangent(self, series, f, x_loc):\n        \"\"\"\n        Return a Tangent Line, for ploting.\n        \"\"\"\n        y_loc = f(x_loc)\n        self.d1_diff(func_1, x_loc)\n        b = y_loc - self.dx * x_loc\n        y_series = self.dx * series + b\n        return y_series\n    \n    # for f(x1, x2, x_3, ...)\n    def dn_diff(self, f, x):\n        grad = np.zeros_like(x)\n        for i in range(len(x)):\n            temp_val = x&#91;i]\n            x&#91;i] = temp_val + self.h\n            fxh1 = f(x)\n            x&#91;i] = temp_val - self.h\n            fxh2 = f(x)\n            grad&#91;i] = (fxh1 - fxh2) \/ (2*self.h)\n            x&#91;i] = temp_val\n            self.dx = grad\n        return self.dx\n    \n    def gradient_descent(self, f, init_x, lr = 0.01, step_num = 1000):\n        x = init_x   \n        for i in range(step_num):\n            self.dn_diff(f, x)\n            x -= lr * self.dx\n        return x<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>By definition: $$ \\nabla f(x_1, x_2) =\\frac{\\partial f}{\\partial x_1} + \\frac{\\partial f}{\\partial x_1} $$ $$ \\frac{\\partial f}{\\partial x_1} = \\lim_{h\\rightarrow 0}\\frac{f(x+h)-f(x)}{h} $$ $$ \\frac{\\partial f}{\\partial x_1} = \\lim_{h\\rightarrow 0}\\frac{f(x+h)-f(x-h)}{2h} $$ Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/5103"}],"collection":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5103"}],"version-history":[{"count":7,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/5103\/revisions"}],"predecessor-version":[{"id":5110,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=\/wp\/v2\/posts\/5103\/revisions\/5110"}],"wp:attachment":[{"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fanyuzhao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}