NativeVex commited on
Commit
93a2718
·
1 Parent(s): 92601fa

attempt at fix

Browse files
assignment-2/assignment_2/Gaussian Maximum Likelihood.ipynb CHANGED
@@ -1548,28 +1548,50 @@
1548
  },
1549
  {
1550
  "cell_type": "code",
1551
- "execution_count": 7,
1552
  "id": "7c8b167d-c397-4155-93f3-d826c279fbb2",
1553
  "metadata": {
1554
  "tags": []
1555
  },
1556
  "outputs": [
1557
  {
1558
- "ename": "SyntaxError",
1559
- "evalue": "invalid syntax (3400372070.py, line 66)",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1560
  "output_type": "error",
1561
  "traceback": [
1562
- "\u001b[0;36m Cell \u001b[0;32mIn[7], line 66\u001b[0;36m\u001b[0m\n\u001b[0;31m p_dw = lambda w, xi: np.array([primitive(xi, wi) for xi, wi in ])\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
 
 
 
 
 
 
 
1563
  ]
1564
  }
1565
  ],
1566
  "source": [
1567
  "## pset 4\n",
1568
  "\n",
1569
- "# dw = lambda w, x: len(x) * np.exp(np.dot(x, w)) * x - np.sum()\n",
1570
  "\n",
1571
- "primitive = lambda xi, wi: (x.shape[0] * np.exp(wi * xi) * xi) - (xi**2)\n",
1572
- "p_dw = lambda w, xi: np.array([primitive(xi, wi) for xi, wi in ])\n",
1573
  "\n",
1574
  "\n",
1575
  "def SGD_problem4(\n",
@@ -1581,7 +1603,7 @@
1581
  " global log4\n",
1582
  " log4 = []\n",
1583
  " for epoch in range(n_epochs):\n",
1584
- " w -= learning_rate * p_dw(w, x)\n",
1585
  " # custom\n",
1586
  " # loss = x.shape[0] * np.exp(np.dot(x, w))\n",
1587
  " loss_fn = lambda k, l: len(k) * l - np.sum(\n",
@@ -1613,42 +1635,6 @@
1613
  ")"
1614
  ]
1615
  },
1616
- {
1617
- "cell_type": "markdown",
1618
- "id": "69e9148b-70fb-46e3-bc29-a08f471cccab",
1619
- "metadata": {},
1620
- "source": [
1621
- "Seek to maximize likelihood, which is equivalent to minimizing log likelihood, which is equivalent to maximizing MSE.\n",
1622
- "\n",
1623
- "-> Find MSE"
1624
- ]
1625
- },
1626
- {
1627
- "cell_type": "code",
1628
- "execution_count": 2,
1629
- "id": "fb9d2e20-8a02-4f78-a3d3-6fc171f17af6",
1630
- "metadata": {},
1631
- "outputs": [
1632
- {
1633
- "ename": "SyntaxError",
1634
- "evalue": "incomplete input (3680490224.py, line 3)",
1635
- "output_type": "error",
1636
- "traceback": [
1637
- "\u001b[0;36m Cell \u001b[0;32mIn[2], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m def MSE(x: List):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m incomplete input\n"
1638
- ]
1639
- }
1640
- ],
1641
- "source": [
1642
- "from typing import List\n",
1643
- "from numpy import \n",
1644
- "\n",
1645
- "w = np.array(2)\n",
1646
- "\n",
1647
- "def partial_of_Loss(w, x, y):\n",
1648
- " \n",
1649
- " "
1650
- ]
1651
- },
1652
  {
1653
  "cell_type": "code",
1654
  "execution_count": null,
 
1548
  },
1549
  {
1550
  "cell_type": "code",
1551
+ "execution_count": 18,
1552
  "id": "7c8b167d-c397-4155-93f3-d826c279fbb2",
1553
  "metadata": {
1554
  "tags": []
1555
  },
1556
  "outputs": [
1557
  {
1558
+ "name": "stdout",
1559
+ "output_type": "stream",
1560
+ "text": [
1561
+ "SGD Problem 4\n"
1562
+ ]
1563
+ },
1564
+ {
1565
+ "name": "stderr",
1566
+ "output_type": "stream",
1567
+ "text": [
1568
+ "/tmp/ipykernel_615396/2481416868.py:22: RuntimeWarning: divide by zero encountered in log\n",
1569
+ " [ki * np.log(l) for ki in k]\n"
1570
+ ]
1571
+ },
1572
+ {
1573
+ "ename": "ValueError",
1574
+ "evalue": "operands could not be broadcast together with shapes (3,) (214,) ",
1575
  "output_type": "error",
1576
  "traceback": [
1577
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1578
+ "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
1579
+ "Cell \u001b[0;32mIn[18], line 44\u001b[0m\n\u001b[1;32m 40\u001b[0m dflog \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(log4)\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dflog\n\u001b[0;32m---> 44\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mdebug_SGD_3\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 45\u001b[0m \u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdf\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHIGH_T\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mLOW_T\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mPRECIP\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_numpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 46\u001b[0m \u001b[43m \u001b[49m\u001b[43mw\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 47\u001b[0m \u001b[43m)\u001b[49m\n",
1580
+ "Cell \u001b[0;32mIn[18], line 39\u001b[0m, in \u001b[0;36mdebug_SGD_3\u001b[0;34m(data, w)\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdebug_SGD_3\u001b[39m(data, w\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m])):\n\u001b[1;32m 38\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSGD Problem 4\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 39\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mSGD_problem4\u001b[49m\u001b[43m(\u001b[49m\u001b[43mw\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;250;43m \u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 40\u001b[0m dflog \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(log4)\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dflog\n",
1581
+ "Cell \u001b[0;32mIn[18], line 24\u001b[0m, in \u001b[0;36mSGD_problem4\u001b[0;34m(w, x, learning_rate, n_epochs)\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# custom\u001b[39;00m\n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# loss = x.shape[0] * np.exp(np.dot(x, w))\u001b[39;00m\n\u001b[1;32m 21\u001b[0m loss_fn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m k, l: \u001b[38;5;28mlen\u001b[39m(k) \u001b[38;5;241m*\u001b[39m l \u001b[38;5;241m-\u001b[39m np\u001b[38;5;241m.\u001b[39msum(\n\u001b[1;32m 22\u001b[0m [ki \u001b[38;5;241m*\u001b[39m np\u001b[38;5;241m.\u001b[39mlog(l) \u001b[38;5;28;01mfor\u001b[39;00m ki \u001b[38;5;129;01min\u001b[39;00m k]\n\u001b[1;32m 23\u001b[0m ) \u001b[38;5;66;03m# + np.sum([np.log(np.math.factorial(ki)) for ki in k])\u001b[39;00m\n\u001b[0;32m---> 24\u001b[0m loss \u001b[38;5;241m=\u001b[39m \u001b[43mloss_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexp\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mw\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 25\u001b[0m log4\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 26\u001b[0m {\n\u001b[1;32m 27\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEpoch\u001b[39m\u001b[38;5;124m\"\u001b[39m: epoch,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 31\u001b[0m }\n\u001b[1;32m 32\u001b[0m )\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEpoch \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mepoch\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLoss: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mloss\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
1582
+ "Cell \u001b[0;32mIn[18], line 22\u001b[0m, in \u001b[0;36mSGD_problem4.<locals>.<lambda>\u001b[0;34m(k, l)\u001b[0m\n\u001b[1;32m 18\u001b[0m w \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m learning_rate \u001b[38;5;241m*\u001b[39m dw(w, x)\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# custom\u001b[39;00m\n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# loss = x.shape[0] * np.exp(np.dot(x, w))\u001b[39;00m\n\u001b[1;32m 21\u001b[0m loss_fn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m k, l: \u001b[38;5;28mlen\u001b[39m(k) \u001b[38;5;241m*\u001b[39m l \u001b[38;5;241m-\u001b[39m np\u001b[38;5;241m.\u001b[39msum(\n\u001b[0;32m---> 22\u001b[0m [ki \u001b[38;5;241m*\u001b[39m np\u001b[38;5;241m.\u001b[39mlog(l) \u001b[38;5;28;01mfor\u001b[39;00m ki \u001b[38;5;129;01min\u001b[39;00m k]\n\u001b[1;32m 23\u001b[0m ) \u001b[38;5;66;03m# + np.sum([np.log(np.math.factorial(ki)) for ki in k])\u001b[39;00m\n\u001b[1;32m 24\u001b[0m loss \u001b[38;5;241m=\u001b[39m loss_fn(x, np\u001b[38;5;241m.\u001b[39mexp(np\u001b[38;5;241m.\u001b[39mdot(x, w)))\n\u001b[1;32m 25\u001b[0m log4\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 26\u001b[0m {\n\u001b[1;32m 27\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEpoch\u001b[39m\u001b[38;5;124m\"\u001b[39m: epoch,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 31\u001b[0m }\n\u001b[1;32m 32\u001b[0m )\n",
1583
+ "Cell \u001b[0;32mIn[18], line 22\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 18\u001b[0m w \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m learning_rate \u001b[38;5;241m*\u001b[39m dw(w, x)\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# custom\u001b[39;00m\n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# loss = x.shape[0] * np.exp(np.dot(x, w))\u001b[39;00m\n\u001b[1;32m 21\u001b[0m loss_fn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m k, l: \u001b[38;5;28mlen\u001b[39m(k) \u001b[38;5;241m*\u001b[39m l \u001b[38;5;241m-\u001b[39m np\u001b[38;5;241m.\u001b[39msum(\n\u001b[0;32m---> 22\u001b[0m [\u001b[43mki\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog\u001b[49m\u001b[43m(\u001b[49m\u001b[43ml\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m ki \u001b[38;5;129;01min\u001b[39;00m k]\n\u001b[1;32m 23\u001b[0m ) \u001b[38;5;66;03m# + np.sum([np.log(np.math.factorial(ki)) for ki in k])\u001b[39;00m\n\u001b[1;32m 24\u001b[0m loss \u001b[38;5;241m=\u001b[39m loss_fn(x, np\u001b[38;5;241m.\u001b[39mexp(np\u001b[38;5;241m.\u001b[39mdot(x, w)))\n\u001b[1;32m 25\u001b[0m log4\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 26\u001b[0m {\n\u001b[1;32m 27\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEpoch\u001b[39m\u001b[38;5;124m\"\u001b[39m: epoch,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 31\u001b[0m }\n\u001b[1;32m 32\u001b[0m )\n",
1584
+ "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (214,) "
1585
  ]
1586
  }
1587
  ],
1588
  "source": [
1589
  "## pset 4\n",
1590
  "\n",
1591
+ "dw = lambda w, x: np.sum([len(x) * np.exp(np.dot(xi, w)) * x - np.sum(np.dot(x.T,x)) for xi in x])\n",
1592
  "\n",
1593
+ "#primitive = lambda xi, wi: (x.shape[0] * np.exp(wi * xi) * xi) - (xi**2)\n",
1594
+ "#p_dw = lambda w, xi: np.array([primitive(xi, wi) for xi, wi in ])\n",
1595
  "\n",
1596
  "\n",
1597
  "def SGD_problem4(\n",
 
1603
  " global log4\n",
1604
  " log4 = []\n",
1605
  " for epoch in range(n_epochs):\n",
1606
+ " w -= learning_rate * dw(w, x)\n",
1607
  " # custom\n",
1608
  " # loss = x.shape[0] * np.exp(np.dot(x, w))\n",
1609
  " loss_fn = lambda k, l: len(k) * l - np.sum(\n",
 
1635
  ")"
1636
  ]
1637
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1638
  {
1639
  "cell_type": "code",
1640
  "execution_count": null,