-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0265f5
commit 0d2c382
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"authorship_tag": "ABX9TyNVDGf+dNdyK86DjeRVtAfY" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "pP_BRWga5u1n" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import torch\n", | ||
"import torch.nn as nn\n", | ||
"import torch.optim as optim\n", | ||
"from torch.utils.data import DataLoader, TensorDataset\n", | ||
"\n", | ||
"# Set random seed for reproducibility\n", | ||
"np.random.seed(0)\n", | ||
"torch.manual_seed(0)\n", | ||
"\n", | ||
"# Generate synthetic data\n", | ||
"x = np.random.randn(1000,2)\n", | ||
"y = 3*x.sum(axis=1) + 2*np.random.randn(x.shape[0])\n", | ||
"\n", | ||
"x = np.vstack(x)\n", | ||
"y = np.hstack(y)\n", | ||
"\n", | ||
"# Convert to PyTorch tensors\n", | ||
"x_tensor = torch.tensor(x, dtype=torch.float32)\n", | ||
"y_tensor = torch.tensor(y, dtype=torch.float32)\n", | ||
"\n", | ||
"# Create a TensorDataset and DataLoader\n", | ||
"dataset = TensorDataset(x_tensor, y_tensor)\n", | ||
"dataloader = DataLoader(dataset, batch_size=10, shuffle=True)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"\n", | ||
"# Neural network\n", | ||
"class SimpleRegressor(nn.Module):\n", | ||
" def __init__(self):\n", | ||
" super(SimpleRegressor, self).__init__()\n", | ||
" self.fc = nn.Linear(2, 1) # One input feature, one output feature\n", | ||
"\n", | ||
" def forward(self, x):\n", | ||
" return self.fc(x)\n", | ||
"\n", | ||
"model = SimpleRegressor()\n", | ||
"\n", | ||
"# Loss and optimizer\n", | ||
"criterion = nn.MSELoss() # Mean Squared Error for regression\n", | ||
"optimizer = optim.SGD(model.parameters(), lr=0.01)\n", | ||
"\n", | ||
"# Training loop\n", | ||
"num_epochs = 100\n", | ||
"for epoch in range(num_epochs):\n", | ||
" for inputs, targets in dataloader:\n", | ||
" # Forward pass\n", | ||
" outputs = model(inputs)\n", | ||
" loss = criterion(outputs, targets)\n", | ||
"\n", | ||
" # Backward and optimize\n", | ||
" optimizer.zero_grad()\n", | ||
" loss.backward()\n", | ||
" optimizer.step()\n", | ||
"\n", | ||
" if epoch % 10 == 0:\n", | ||
" print(f'Epoch [{epoch}/{num_epochs}], Loss: {loss.item():.4f}')\n" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "ETGXjGCo538n", | ||
"outputId": "75c6af6d-fe44-4c3e-a449-c125a9555b7b" | ||
}, | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Epoch [0/100], Loss: 35.8108\n", | ||
"Epoch [10/100], Loss: 21.7872\n", | ||
"Epoch [20/100], Loss: 13.4255\n", | ||
"Epoch [30/100], Loss: 23.3474\n", | ||
"Epoch [40/100], Loss: 14.4495\n", | ||
"Epoch [50/100], Loss: 9.5928\n", | ||
"Epoch [60/100], Loss: 10.4794\n", | ||
"Epoch [70/100], Loss: 8.2940\n", | ||
"Epoch [80/100], Loss: 25.8117\n", | ||
"Epoch [90/100], Loss: 13.2736\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"\n", | ||
"# Test the model\n", | ||
"model.eval()\n", | ||
"with torch.no_grad():\n", | ||
" predicted = model(x_tensor).detach().numpy()\n", | ||
"\n" | ||
], | ||
"metadata": { | ||
"id": "7SGMBKNP9R-X" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [], | ||
"metadata": { | ||
"id": "dv-nwyAsGuMj" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |