Challenge: Classifying Flowers
Tehtävä
Swipe to start coding
Your goal is to train and evaluate a simple neural network using the Iris dataset, which consists of flower measurements and species classification.
- Split the dataset into training and testing sets allocating 20% for the test set and setting random state to
42
. - Convert
X_train
andX_test
into PyTorch tensors of typefloat32
. - Convert
y_train
andy_test
into PyTorch tensors of typelong
. - Define a neural network model by creating the
IrisModel
class. - Implement two fully connected layers and apply the ReLU activation function in the hidden layer.
- Initialize the model with the correct input size, hidden layer size equal to
16
, and output size. - Define the loss as cross-entropy loss and the optimizer as Adam with a learning rate of
0.01
. - Train the model for 100 epochs by performing forward propagation, computing loss, performing backpropagation, and updating the model's parameters.
- Set the model to evaluation mode after training.
- Disable gradient computation during testing to improve efficiency.
- Compute predictions on the test set using the trained model.
- Determine the predicted class labels based on raw predictions.
Ratkaisu
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import pandas as pd
from sklearn.model_selection import train_test_split
torch.manual_seed(42)
iris_df = pd.read_csv("https://content-media-cdn.codefinity.com/courses/1dd2b0f6-6ec0-40e6-a570-ed0ac2209666/section_2/iris.csv")
X = iris_df.drop(columns=["species"]).values
y = iris_df["species"].values
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create tensors
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
X_test_tensor = torch.tensor(X_test, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.long)
y_test_tensor = torch.tensor(y_test, dtype=torch.long)
# Define the model
class IrisModel(torch.nn.Module):
def __init__(self, input_size, hidden_size, output_size):
# Call the __init__ method of the parent class
super().__init__()
# Define the layers
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, output_size)
# Pass data through layers with activation functions
def forward(self, x):
# Apply ReLU to the outputs of the hidden layer
x = F.relu(self.fc1(x))
# Compute the output
x = self.fc2(x)
return x
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import pandas as pd
from sklearn.model_selection import train_test_split
torch.manual_seed(42)
iris_df = pd.read_csv("https://content-media-cdn.codefinity.com/courses/1dd2b0f6-6ec0-40e6-a570-ed0ac2209666/section_2/iris.csv")
X = iris_df.drop(columns=["species"]).values
y = iris_df["species"].values
# Split the data
X_train, X_test, y_train, y_test = ___
# Create tensors
X_train_tensor = ___
X_test_tensor = ___
y_train_tensor = ___
y_test_tensor =___
# Define the model
___:
def __init__(self, input_size, hidden_size, output_size):
# Call the __init__ method of the parent class
___
# Define the layers
self.fc1 = ___
self.fc2 = ___
# Pass data through layers with activation functions
def forward(self, x):
# Apply ReLU to the output of the hidden layer
x = ___
# Compute the output
x = ___
return x
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme