minor changes

This commit is contained in:
Morten Hjorth-Jensen
2024-10-31 08:24:12 +01:00
parent 9f280e559e
commit 5718fba7a2
2 changed files with 382 additions and 916 deletions
File diff suppressed because it is too large Load Diff
+49 -19
View File
@@ -1983,7 +1983,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"id": "24784ec8",
"metadata": {},
"outputs": [
@@ -1992,13 +1992,13 @@
"output_type": "stream",
"text": [
"Own inversion\n",
"[[2.]\n",
" [3.]\n",
" [4.]]\n",
"[[2. ]\n",
" [3. ]\n",
" [1.5]]\n",
"theta from own AdaGrad\n",
"[[2.00018601]\n",
" [2.99886063]\n",
" [4.00112005]]\n"
"[[2.00063579]\n",
" [2.996584 ]\n",
" [1.50373614]]\n"
]
}
],
@@ -2017,7 +2017,7 @@
"\n",
"n = 1000\n",
"x = np.random.rand(n,1)\n",
"y = 2.0+3*x +4*x*x\n",
"y = 2.0+3*x +1.5*x*x\n",
"\n",
"X = np.c_[np.ones((n,1)), x, x*x]\n",
"XT_X = X.T @ X\n",
@@ -2029,8 +2029,8 @@
"# Note that we request the derivative wrt third argument (theta, 2 here)\n",
"training_gradient = grad(CostOLS,2)\n",
"# Define parameters for Stochastic Gradient Descent\n",
"n_epochs = 50\n",
"M = 5 #size of each minibatch\n",
"n_epochs = 100\n",
"M = 10 #size of each minibatch\n",
"m = int(n/M) #number of minibatches\n",
"# Guess for unknown parameters theta\n",
"theta = np.random.randn(3,1)\n",
@@ -2071,10 +2071,25 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 5,
"id": "770a0f44",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Own inversion\n",
"[[2. ]\n",
" [3. ]\n",
" [1.5]]\n",
"theta from own RMSprop\n",
"[[1.99889288]\n",
" [3.00367262]\n",
" [1.4940189 ]]\n"
]
}
],
"source": [
"# Using Autograd to calculate gradients using RMSprop and Stochastic Gradient descent\n",
"# OLS example\n",
@@ -2090,7 +2105,7 @@
"\n",
"n = 1000\n",
"x = np.random.rand(n,1)\n",
"y = 2.0+3*x +4*x*x# +np.random.randn(n,1)\n",
"y = 2.0+3*x +1.5*x*x# +np.random.randn(n,1)\n",
"\n",
"X = np.c_[np.ones((n,1)), x, x*x]\n",
"XT_X = X.T @ X\n",
@@ -2102,7 +2117,7 @@
"# Note that we request the derivative wrt third argument (theta, 2 here)\n",
"training_gradient = grad(CostOLS,2)\n",
"# Define parameters for Stochastic Gradient Descent\n",
"n_epochs = 50\n",
"n_epochs = 1000\n",
"M = 5 #size of each minibatch\n",
"m = int(n/M) #number of minibatches\n",
"# Guess for unknown parameters theta\n",
@@ -2142,10 +2157,25 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 10,
"id": "ebe031fe",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Own inversion\n",
"[[2. ]\n",
" [3. ]\n",
" [1.5]]\n",
"theta from own ADAM\n",
"[[1.99999721]\n",
" [3.0000173 ]\n",
" [1.49998153]]\n"
]
}
],
"source": [
"# Using Autograd to calculate gradients using RMSprop and Stochastic Gradient descent\n",
"# OLS example\n",
@@ -2161,7 +2191,7 @@
"\n",
"n = 1000\n",
"x = np.random.rand(n,1)\n",
"y = 2.0+3*x +4*x*x# +np.random.randn(n,1)\n",
"y = 2.0+3*x +1.5*x*x# +np.random.randn(n,1)\n",
"\n",
"X = np.c_[np.ones((n,1)), x, x*x]\n",
"XT_X = X.T @ X\n",
@@ -2173,14 +2203,14 @@
"# Note that we request the derivative wrt third argument (theta, 2 here)\n",
"training_gradient = grad(CostOLS,2)\n",
"# Define parameters for Stochastic Gradient Descent\n",
"n_epochs = 50\n",
"n_epochs = 1000\n",
"M = 5 #size of each minibatch\n",
"m = int(n/M) #number of minibatches\n",
"# Guess for unknown parameters theta\n",
"theta = np.random.randn(3,1)\n",
"\n",
"# Value for learning rate\n",
"eta = 0.01\n",
"eta = 0.001\n",
"# Value for parameters beta1 and beta2, see https://arxiv.org/abs/1412.6980\n",
"beta1 = 0.9\n",
"beta2 = 0.999\n",