Hlubší pohled na načítání dat

Introduction to Deep Learning with PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Náš dataset zvířat

import pandas as pd
animals = pd.read_csv('animal_dataset.csv')
animal_name hair feathers eggs milk predator legs tail type
sparrow 0 1 1 0 0 2 1 0
eagle 0 1 1 0 1 2 1 0
cat 1 0 0 1 1 4 1 1
dog 1 0 0 1 0 4 1 1
lizard 0 0 1 0 1 4 1 2

Kategorie typů: pták (0), savec (1), plaz (2)

Introduction to Deep Learning with PyTorch

Dataset zvířat: definice příznaků

import numpy as np

# Define input features
features = animals.iloc[:, 1:-1]


X = features.to_numpy() print(X)
[[0 1 1 0 0 2 1]
 [0 1 1 0 1 2 1]
 [1 0 0 1 1 4 1]
 [1 0 0 1 0 4 1]
 [0 0 1 0 1 4 1]]
Introduction to Deep Learning with PyTorch

Dataset zvířat: definice cílových hodnot

# Define target values (ground truth)
target = animals.iloc[:, -1]
y = target.to_numpy()
print(y)
[0 0 1 1 2]
Introduction to Deep Learning with PyTorch

TensorDataset

import torch
from torch.utils.data import TensorDataset


# Instantiate dataset class dataset = TensorDataset(torch.tensor(X), torch.tensor(y))
# Access an individual sample input_sample, label_sample = dataset[0] print('input sample:', input_sample) print('label sample:', label_sample)
input sample: tensor([0, 1, 1, 0, 0, 2, 1])
label sample: tensor(0)
Introduction to Deep Learning with PyTorch

DataLoader

from torch.utils.data import DataLoader


batch_size = 2
shuffle = True
# Create a DataLoader dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle)

$$

  • Epocha: jeden úplný průchod trénovacím dataloaderem
  • Generalizace: model dobře funguje na neviděných datech
Introduction to Deep Learning with PyTorch

DataLoader

# Iterate over the dataloader
for batch_inputs, batch_labels in dataloader:
    print('batch_inputs:', batch_inputs)
    print('batch_labels:', batch_labels)
batch_inputs: tensor([[1, 0, 0, 1, 1, 4, 1],
        [1, 0, 0, 1, 0, 4, 1]])
batch_labels: tensor([1, 1])

batch_inputs: tensor([[0, 1, 1, 0, 1, 2, 1], [0, 0, 1, 0, 1, 4, 1]]) batch_labels: tensor([0, 2])
batch_inputs: tensor([[0, 1, 1, 0, 0, 2, 1]]) batch_labels: tensor([0])
Introduction to Deep Learning with PyTorch

Pojďme si procvičit!

Introduction to Deep Learning with PyTorch

Preparing Video For Download...