Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Classifying Flowers | Neural Networks in PyTorch
PyTorch Essentials
course content

Course Content

PyTorch Essentials

PyTorch Essentials

1. PyTorch Introduction
2. More Advanced Concepts
3. Neural Networks in PyTorch

book
Challenge: Classifying Flowers

Task

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.

  1. Split the dataset into training and testing sets allocating 20% for the test set and setting random state to 42.
  2. Convert X_train and X_test into PyTorch tensors of type float32.
  3. Convert y_train and y_test into PyTorch tensors of type long.
  4. Define a neural network model by creating the IrisModel class.
  5. Implement two fully connected layers and apply the ReLU activation function in the hidden layer.
  6. Initialize the model with the correct input size, hidden layer size equal to 16, and output size.
  7. Define the loss as cross-entropy loss and the optimizer as Adam with a learning rate of 0.01.
  8. Train the model for 100 epochs by performing forward propagation, computing loss, performing backpropagation, and updating the model's parameters.
  9. Set the model to evaluation mode after training.
  10. Disable gradient computation during testing to improve efficiency.
  11. Compute predictions on the test set using the trained model.
  12. Determine the predicted class labels based on raw predictions.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

book
Challenge: Classifying Flowers

Task

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.

  1. Split the dataset into training and testing sets allocating 20% for the test set and setting random state to 42.
  2. Convert X_train and X_test into PyTorch tensors of type float32.
  3. Convert y_train and y_test into PyTorch tensors of type long.
  4. Define a neural network model by creating the IrisModel class.
  5. Implement two fully connected layers and apply the ReLU activation function in the hidden layer.
  6. Initialize the model with the correct input size, hidden layer size equal to 16, and output size.
  7. Define the loss as cross-entropy loss and the optimizer as Adam with a learning rate of 0.01.
  8. Train the model for 100 epochs by performing forward propagation, computing loss, performing backpropagation, and updating the model's parameters.
  9. Set the model to evaluation mode after training.
  10. Disable gradient computation during testing to improve efficiency.
  11. Compute predictions on the test set using the trained model.
  12. Determine the predicted class labels based on raw predictions.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt