Defining a Dataset with PyTorch Tensors
Topic Overview
Hello and welcome back! Today we focus on defining datasets using PyTorch Tensors. These datasets are called TensorDatasets and are a very vital feature of the PyTorch library. In this lesson, you will convert an array into a tensor, create a TensorDataset, use DataLoader for dividing the dataset into batches, and iterate through the batches. Let's dive right into it!
What is a TensorDataset?
As you might already know, PyTorch's primary unit of data storage is a tensor. But what if you have more than one tensor of data and you need to keep it collected? That's when TensorDataset comes into play.
A TensorDataset is a dataset that wraps multiple tensors. Each sample is a tuple of tensors where each tensor in the tuple corresponds to a level of the dataset. In simpler terms, it is a way to keep your tensors of input and output data organized together. Using TensorDataset makes it very easy to provide and manage your tensors of varying data types.
While it’s not always necessary to use TensorDataset, it can be very convenient, especially if you want to use a DataLoader for batching and shuffling your data. The major advantage here is that using TensorDataset, PyTorch can efficiently store and access the data, which is crucial while working with large datasets.
Defining an Array in PyTorch
Now, let's get our hands on the first step of our journey - defining an array and converting it into a tensor. For our lesson, we'll start with a simple array of input data and the target outputs for our dataset.
Up till here, we have just defined them as numpy arrays. We now have to convert them into PyTorch tensors.
The conversion code is straightforward, the torch.tensor function helps us transform our numpy array into tensors, and, with the use of the dtype parameter, we can specify them as floating point and integer numbers.
Now we have successfully converted our arrays into PyTorch tensors!
