PyTorch tutorial: tensors to house price prediction

Build a PyTorch house price model with scikit-learn preprocessing, then examine training loss and prediction errors on an 87-feature dataset.

Player not loading? Watch on YouTube

This English tutorial explains tensors visually, then builds a local Python script for house price prediction. The presenter compares tensor initialization methods, shows how image pixels become numerical inputs, and explains linear layers, activation functions, loss, backpropagation and gradient descent.

The coding section creates a virtual environment and installs PyTorch, scikit-learn and pandas. Matplotlib is optional for plotting loss. The CSV contains 87 property features, including one-hot encoded postcodes, with price as the target. The example reserves 20% of the data for testing with a random seed of 15. It uses scikit-learn for splitting and scaling, converts data to floating-point tensors, and retains scaling statistics to return predictions to price units.

The PyTorch model has hidden layers of 64 and 32 neurons, ReLU activations and one price output. The training loop uses Adam and mean squared error loss for 100 epochs at a learning rate of 0.01. It clears gradients, makes predictions, calculates loss, runs backpropagation and updates the weights. The presenter also covers saving the model and printing loss every 10 epochs.

Running the script on a MacBook Air M4 produces a reported training loss decrease from about 1 to 0.07. Evaluation reports a mean absolute error of £300,000, MAPE of 19%, and 39% of predictions within 10% of the actual price. The presenter considers the model insufficiently precise for practical use and suggests improving the dataset or trying XGBoost. XGBoost is not demonstrated.

The preprocessing explanation needs care: scalers should learn their parameters from training data and apply those same parameters to test inputs. The transcript alone does not establish whether the example handles that correctly.

The source description links the accompanying article and downloadable example at https://0byte.io/articles/pytorch_introduction.html.