102 lines
1.9 KiB
Plaintext
102 lines
1.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "7af2ca72",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "0684c0ff",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"X = torch.tensor([[1.0],[2.0],[3.0]])\n",
|
|
"y = torch.tensor([[4.0],[5.0],[6.0]])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"id": "1078a0d7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"w = torch.randn(1, requires_grad=True)\n",
|
|
"b = torch.randn(1, requires_grad=True)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"id": "6d7c95c5",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0.4485014081001282 3.9534966945648193\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for _ in range(100):\n",
|
|
" y_pred = X @ w + b\n",
|
|
" loss = ((y_pred - y) ** 2).mean()\n",
|
|
" loss.backward()\n",
|
|
" with torch.no_grad():\n",
|
|
" w -= 0.05 * w.grad\n",
|
|
" b -= 0.05 * w.grad\n",
|
|
" w.grad.zero_()\n",
|
|
" b.grad.zero_()\n",
|
|
"\n",
|
|
"print(w.item(),b.item())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b23bda3c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1b6fcc44",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv (3.11.15)",
|
|
"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.11.15"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|