Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
TonmoyTalukder committed Aug 7, 2023
1 parent a5a4c17 commit ceee387
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions PyTorch_Explore/1_Regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"colab": {
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyPcflmMmcoKWMeJCvnHkpzn",
"authorship_tag": "ABX9TyMuXeJk+Zh3Dm2wwP6dBkFI",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -72,15 +72,16 @@
"cell_type": "code",
"source": [
"# 0) Prepare Data\n",
"X_numpy, y_numpy = datasets.make_regression(n_samples=100,\n",
" n_features=1,\n",
" noise=20,\n",
"X_numpy, y_numpy = datasets.make_regression(n_samples=100, # Number of datapoints\n",
" n_features=1, # Number of features\n",
" noise=20, # random noise added to the target values\n",
" random_state=1)\n",
"X = torch.from_numpy(X_numpy.astype(np.float32))\n",
"y = torch.from_numpy(y_numpy.astype(np.float32))\n",
"y = y.view(y.shape[0], 1)\n",
"X = torch.from_numpy(X_numpy.astype(np.float32)) # Converts NumPy array to a PyTorch tensor\n",
"y = torch.from_numpy(y_numpy.astype(np.float32)) # Converts NumPy array to a PyTorch tensor\n",
"y = y.view(y.shape[0], 1) # Reshape the target tensor to have a shape of (100, 1)\n",
"# the target tensor should have the same number of columns as the input tensor\n",
"\n",
"n_samples, n_features = X.shape"
"n_samples, n_features = X.shape # store the number of samples and features in the data"
],
"metadata": {
"id": "OWVhelS0UreC"
Expand All @@ -92,9 +93,12 @@
"cell_type": "code",
"source": [
"# 1) model\n",
"\n",
"# store the number of input and output features for the model\n",
"input_size = n_features\n",
"output_size = 1\n",
"model = nn.Linear(input_size, output_size)"
"\n",
"model = nn.Linear(input_size, output_size) # Linead Regression Model"
],
"metadata": {
"id": "ODgAKQuPUc1b"
Expand All @@ -106,8 +110,8 @@
"cell_type": "code",
"source": [
"# 2) loss and optimizer\n",
"learning_rate = 0.01\n",
"criterion = nn.MSELoss()\n",
"learning_rate = 0.01 # specify the learning rate of the optimizer\n",
"criterion = nn.MSELoss() # loss function\n",
"optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)"
],
"metadata": {
Expand All @@ -125,15 +129,15 @@
"for epoch in range(num_epochs):\n",
" # forward pass and loss\n",
" y_predicted = model(X)\n",
" loss = criterion(y_predicted, y)\n",
" loss = criterion(y_predicted, y) # calculate the loss between the predictions and the ground truth labels\n",
"\n",
" # backward pass\n",
" loss.backward()\n",
" loss.backward() # backpropagates the loss through the model\n",
"\n",
" # update weights\n",
" optimizer.step()\n",
"\n",
" optimizer.zero_grad()\n",
" optimizer.zero_grad() # sets the gradients of the model to zero\n",
"\n",
" if (epoch+1) % 10 == 0:\n",
" print(f'epoch: {epoch+1}, loss = {loss.item():.4f}')"
Expand Down Expand Up @@ -169,7 +173,7 @@
"cell_type": "code",
"source": [
"# Plot\n",
"predicted = model(X).detach().numpy()\n",
"predicted = model(X).detach().numpy() # converts prediction to numpy arrays\n",
"plt.plot(X_numpy, y_numpy, 'ro')\n",
"plt.plot(X_numpy, predicted, 'b')\n",
"plt.show()"
Expand Down

0 comments on commit ceee387

Please sign in to comment.