Skip to content

Commit

Permalink
Update Large-Scale-Constrained-Linear-Least-Squares.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
mamintoosi committed Oct 5, 2024
1 parent a72e6b9 commit 439e1b7
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions code/Large-Scale-Constrained-Linear-Least-Squares.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -22,12 +22,13 @@
"from skimage.transform import resize\n",
"from skimage.color import rgb2gray\n",
"import requests\n",
"from io import BytesIO"
"from io import BytesIO\n",
"np.set_printoptions(precision=2, suppress=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -36,7 +37,7 @@
"(100, 208)"
]
},
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
Expand Down Expand Up @@ -66,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -75,7 +76,7 @@
"(20800, 20800)"
]
},
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -85,7 +86,7 @@
"mn = m * n\n",
"\n",
"# Add Motion\n",
"# Simulate the effect of vertical motion blurring by averaging each pixel with the 5 pixels above and below. Construct a sparse matrix D to blur with a single matrix multiply.\n",
"# Simulate the effect of horizontal motion blurring by averaging each pixel with the 5 pixels left and right. Construct a sparse matrix D to blur with a single matrix multiply.\n",
"# mn = 5\n",
"blur = 5\n",
"mindex = np.arange(mn) \n",
Expand All @@ -100,21 +101,18 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909,\n",
" 0.09090909, 0. , 0. , 0. , 0. ],\n",
" [0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909,\n",
" 0.09090909, 0.09090909, 0. , 0. , 0. ],\n",
" [0.09090909, 0.09090909, 0.09090909, 0.09090909, 0.09090909,\n",
" 0.09090909, 0.09090909, 0.09090909, 0. , 0. ]])"
"array([[0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0. , 0. , 0. , 0. ],\n",
" [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0. , 0. , 0. ],\n",
" [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0. , 0. ]])"
]
},
"execution_count": 13,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -125,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -146,18 +144,18 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([0.09701371, 0.09705054, 0.09697521, 0.09925708, 0.09626223]),\n",
" array([0.09701371, 0.09705054, 0.09697521, 0.09925708, 0.09626223]),\n",
" array([0.09701371, 0.10293342, 0.10461351, 0.14205522, 0.3358845 ]))"
"(array([0.1, 0.1, 0.1, 0.1, 0.1]),\n",
" array([0.1, 0.1, 0.1, 0.1, 0.1]),\n",
" array([0.1 , 0.1 , 0.1 , 0.14, 0.34]))"
]
},
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -175,16 +173,16 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x1bbb25cd480>"
"<matplotlib.image.AxesImage at 0x1fb39fec4f0>"
]
},
"execution_count": 16,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
Expand Down Expand Up @@ -213,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -223,7 +221,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -232,7 +230,7 @@
"((20800, 20800), (20800,), (20800,))"
]
},
"execution_count": 18,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -252,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -269,9 +267,8 @@
"# # expr = D@x - G\n",
"# # return expr.T@expr\n",
"\n",
"# # sol = minimize(objective, x, bounds=bounds)\n",
"# # sol = minimize(objective, x, bounds=bounds, method='Nelder-Mead') #L-BFGS-B\n",
"# sol = lsq_linear(D, G, bounds=(0,1))\n",
"# sol = minimize(objective, x, bounds=bounds)\n",
"# sol = minimize(objective, x, bounds=bounds, method='Nelder-Mead') #L-BFGS-B\n",
"\n",
"# sol.x"
]
Expand All @@ -285,15 +282,15 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: total: 17.2 s\n",
"Wall time: 21.8 s\n"
"CPU times: total: 19.7 s\n",
"Wall time: 23.2 s\n"
]
}
],
Expand All @@ -307,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -316,17 +313,16 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([0.73120553, 0.79376967, 0.79121882, 0.78839188, 0.78733728]),\n",
" array([0.73222747, 0.79468509, 0.79050181, 0.7884607 , 0.78715651]))"
"(array([0.73, 0.79, 0.79, 0.79, 0.79]), array([0.73, 0.79, 0.79, 0.79, 0.79]))"
]
},
"execution_count": 22,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -337,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -380,7 +376,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -389,7 +385,7 @@
"0.0009316929684256153"
]
},
"execution_count": 24,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -407,7 +403,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -417,10 +413,10 @@
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mLinAlgError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32md:\\git\\fum-cs\\dl\\code\\Large-Scale-Constrained-Linear-Least-Squares.ipynb Cell 22\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> <a href='vscode-notebook-cell:/d%3A/git/fum-cs/dl-fall-2023/code/Large-Scale-Constrained-Linear-Least-Squares.ipynb#X32sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m np\u001b[39m.\u001b[39;49mlinalg\u001b[39m.\u001b[39;49minv(D)\n",
"Cell \u001b[1;32mIn[16], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlinalg\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minv\u001b[49m\u001b[43m(\u001b[49m\u001b[43mD\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m<__array_function__ internals>:200\u001b[0m, in \u001b[0;36minv\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\linalg\\linalg.py:532\u001b[0m, in \u001b[0;36minv\u001b[1;34m(a)\u001b[0m\n\u001b[0;32m 471\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 472\u001b[0m \u001b[39mCompute the (multiplicative) inverse of a matrix.\u001b[39;00m\n\u001b[0;32m 473\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 529\u001b[0m \n\u001b[0;32m 530\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 531\u001b[0m a, wrap \u001b[39m=\u001b[39m _makearray(a)\n\u001b[1;32m--> 532\u001b[0m _assert_stacked_2d(a)\n\u001b[0;32m 533\u001b[0m _assert_stacked_square(a)\n\u001b[0;32m 534\u001b[0m t, result_t \u001b[39m=\u001b[39m _commonType(a)\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\linalg\\linalg.py:183\u001b[0m, in \u001b[0;36m_assert_stacked_2d\u001b[1;34m(*arrays)\u001b[0m\n\u001b[0;32m 181\u001b[0m \u001b[39mfor\u001b[39;00m a \u001b[39min\u001b[39;00m arrays:\n\u001b[0;32m 182\u001b[0m \u001b[39mif\u001b[39;00m a\u001b[39m.\u001b[39mndim \u001b[39m<\u001b[39m \u001b[39m2\u001b[39m:\n\u001b[1;32m--> 183\u001b[0m \u001b[39mraise\u001b[39;00m LinAlgError(\u001b[39m'\u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m-dimensional array given. Array must be \u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m 184\u001b[0m \u001b[39m'\u001b[39m\u001b[39mat least two-dimensional\u001b[39m\u001b[39m'\u001b[39m \u001b[39m%\u001b[39m a\u001b[39m.\u001b[39mndim)\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\linalg\\linalg.py:532\u001b[0m, in \u001b[0;36minv\u001b[1;34m(a)\u001b[0m\n\u001b[0;32m 471\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 472\u001b[0m \u001b[38;5;124;03mCompute the (multiplicative) inverse of a matrix.\u001b[39;00m\n\u001b[0;32m 473\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 529\u001b[0m \n\u001b[0;32m 530\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 531\u001b[0m a, wrap \u001b[38;5;241m=\u001b[39m _makearray(a)\n\u001b[1;32m--> 532\u001b[0m \u001b[43m_assert_stacked_2d\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 533\u001b[0m _assert_stacked_square(a)\n\u001b[0;32m 534\u001b[0m t, result_t \u001b[38;5;241m=\u001b[39m _commonType(a)\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python310\\site-packages\\numpy\\linalg\\linalg.py:183\u001b[0m, in \u001b[0;36m_assert_stacked_2d\u001b[1;34m(*arrays)\u001b[0m\n\u001b[0;32m 181\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m arrays:\n\u001b[0;32m 182\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m2\u001b[39m:\n\u001b[1;32m--> 183\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m LinAlgError(\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m-dimensional array given. Array must be \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 184\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mat least two-dimensional\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39m a\u001b[38;5;241m.\u001b[39mndim)\n",
"\u001b[1;31mLinAlgError\u001b[0m: 0-dimensional array given. Array must be at least two-dimensional"
]
}
Expand Down

0 comments on commit 439e1b7

Please sign in to comment.