Skip to course content
Free PyTorch course

Advanced Deep Learning with PyTorch

Unit 02.01: Creating tensors from NumPy and tabular data

Real data arrives as a spreadsheet or a DataFrame, not as a tensor. The conversion is two lines — and one of them has a trap in it.

From DataFrame to tensor, deliberately

Always state the dtype. Pandas will hand you int64 for a column of whole numbers, and most PyTorch layers expect float32. Being explicit costs nothing and avoids a confusing error later.

Note also the unsqueeze(1) on the target: a loss function comparing predictions of shape (n, 1) against targets of shape (n,) will broadcast into an (n, n) matrix and silently compute nonsense.

import numpy as np
import pandas as pd
import torch

df = pd.DataFrame({
    "hours": [1.0, 2.5, 4.0, 3.0],
    "attempts": [3, 1, 1, 2],
    "passed": [0, 1, 1, 1],
})

features = torch.tensor(df[["hours", "attempts"]].to_numpy(), dtype=torch.float32)
target = torch.tensor(df["passed"].to_numpy(), dtype=torch.float32).unsqueeze(1)

print("features:", tuple(features.shape), features.dtype)
print("target  :", tuple(target.shape), target.dtype)

# from_numpy SHARES memory with the array. Changing one changes the other.
arr = np.array([1.0, 2.0, 3.0])
shared = torch.from_numpy(arr)
arr[0] = 99.0
print("shared memory :", shared[0].item())      # 99.0

copied = torch.tensor(arr)                      # a copy
arr[1] = 77.0
print("copy unaffected:", copied[1].item())     # 2.0

The second half shows the trap. torch.from_numpy shares memory with the array — mutate the array and the tensor changes underneath you. torch.tensor copies. Neither is wrong; using the wrong one produces a bug that appears far from its cause.

The mistake this prevents

Using from_numpy on an array you go on to modify, usually during preprocessing. The tensor changes without any line of code appearing to touch it.

Takeaway

Set the dtype explicitly, shape the target to match the prediction, and know whether you copied or shared.