liveBook · Manning

Tensor

As explained in Build AI Drug Discovery Pipelines

A tensor is a multidimensional numerical array that stores data and supports operations for machine-learning computation.

A tensor is a multidimensional array and the fundamental data structure in PyTorch. Like a NumPy array, it holds numerical data; it also supports mathematical operations used in deep learning. Tensors encode a model’s inputs, outputs, and parameters, and can reside on a CPU or GPU. section 8.1.2

Create and inspect a tensor

Create a two-dimensional tensor with `tensor_2d = torch.tensor([[1, 2], [3, 4]])`. PyTorch infers its data type from the list. Inspect `tensor_2d.shape`, `tensor_2d.dtype`, and `tensor_2d.device` when debugging: shape, data type, and device mismatches are common problems. section 8.1.2

>>> Two-dimensional tensor: tensor([[1, 2], [3, 4]])
print(f"Shape of tensor: {tensor_2d.shape}")
>>> Shape of tensor: torch.Size([2, 2])
print(f"Data type of tensor: {tensor_2d.dtype}")
>>> Data type of tensor: torch.int64
print(f"Device tensor is stored on: {tensor_2d.device}")
>>> Device tensor is stored on: cpu
 

Inspect the example tensor’s shape, data type, and device. section 8.1.2

Specify a data type when creating a tensor, or convert an existing tensor with `.to(torch.float32)`. For example, converting an integer tensor to a 32-bit floating-point tensor makes its intended precision explicit. section 8.1.2

Common tensor operations include:

  • Adding a scalar to each element, as in `tensor_2d + 2`. section 8.1.2
  • Changing shape with `reshape` or `view`, and transposing with `.T`. section 8.1.2
  • Selecting or modifying rows and columns by indexing, and joining tensors with `torch.cat`. section 8.1.2
  • Multiplying matrices with `matmul` or `@`. section 8.1.2

Molecules as model inputs

For a kinase activity model, a dataset converts each molecule’s numerical fingerprint into a `torch.float32` tensor and its activity into another float tensor. The model can then use these numerical representations as inputs and targets. section 8.1.2

Listing 8.3 MoleculeDataset Data Loader
def smiles_to_fp(smiles, fp_size=2048):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None
    mfpgen = rdFingerprintGenerator.GetMorganGenerator(
        radius=3, fpSize=fp_size
    )
    fp = mfpgen.GetFingerprint(mol)
    arr = np.zeros((fp_size,), dtype=np.int8)
    DataStructs.ConvertToNumpyArray(fp, arr)
    return arr
 
class MoleculeDataset(Dataset):
    def __init__(self, smiles, activities):
        self.smiles = smiles
        self.activities = activities
        self.fingerprints = [smiles_to_fp(s) for s in smiles]
        
    def __len__(self):
        return len(self.smiles)
    
    def __getitem__(self, idx):
        fp = torch.tensor(self.fingerprints[idx], dtype=torch.float32)
        activity = torch.tensor([self.activities[idx]], dtype=torch.float32)
        return fp, activity
 

See how molecular fingerprints and activities are converted to tensors in a dataset. section 8.1.2

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Defines tensors and demonstrates creation, inspection, conversion, indexing, reshaping, concatenation, and mathematical operations.
Examples
Demonstrates two-dimensional arrays and converts molecular fingerprints and kinase activities into tensors.
What you can do after
Apply it via PyTorch to create, inspect, convert, and manipulate tensors for a neural-network dataset.
How it connects
Connects tensors to NumPy arrays, neural networks, molecular fingerprints, and GPU computation.

Also explained in

Compare how 10 books explain tensor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.