1. Introduction to Deep Learning and PyTorch
Activity 1.01: Creating a Single-Layer Neural Network
Solution
- Import the required libraries, including pandas, for importing a CSV file:
import pandas as pd import torch import torch.nn as nn import matplotlib.pyplot as plt
- Read the CSV file containing the dataset:
data = pd.read_csv("SomervilleHappinessSurvey2015.csv")
- Separate the input features from the target. Note that the target is located in the first column of the CSV file. Convert the values into tensors, making sure the values are converted into floats:
x = torch.tensor(data.iloc[:,1:].values).float() y = torch.tensor(data.iloc[:,:1].values).float()
- Define the architecture of the model and store it in a variable named
model
. Remember to create a single-layer model:model = nn.Sequential(nn.Linear(6, 1), nn.Sigmoid())
...