<jupyter_start><jupyter_text>305 Batch TrainView more, visit my tutorial page: https://morvanzhou.github.io/tutorials/My Youtube Channel: https://www.youtube.com/user/MorvanZhouDependencies:* torch: 1.4.0<jupyter_code>import torch
import torch.utils.data as Data

torch.manual_seed(1)
x = torch.linspace(1, 10, 10)
y = torch.linspace(10, 1, 10)
BATCH_SIZE = 5

torch_dataset = Data.TensorDataset(x, y, y)    # 可以将任意数量的第0维大小相等的tensor数组传入，供DataLoader生成批处理迭代器。
loader = Data.DataLoader(
    dataset=torch_dataset,      # torch TensorDataset format
    batch_size=BATCH_SIZE,      # mini batch size
    shuffle=True,               # random shuffle for training
    num_workers=2,              # subprocesses for loading data
)
for epoch in range(3):   # train entire dataset 3 times
    for i, (batch_x, batch_y, batch_z) in enumerate(loader):  # for each training step
        # train your data...
        print('Epoch: ', epoch, '| Step: ', i, '| batch x: ',
              batch_x, '| batch y: ', batch_y, '| batch z: ', batch_z)<jupyter_output>Epoch:  0 | Step:  0 | batch x:  tensor([ 5.,  7., 10.,  3.,  4.]) | batch y:  tensor([6., 4., 1., 8., 7.]) | batch z:  tensor([6., 4., 1., 8., 7.])
Epoch:  0 | Step:  1 | batch x:  tensor([2., 1., 8., 9., 6.]) | batch y:  tensor([ 9., 10.,  3.,  2.,  5.]) | batch z:  tensor([ 9., 10.,  3.,  2.,  5.])
Epoch:  1 | Step:  0 | batch x:  tensor([ 4.,  6.,  7., 10.,  8.]) | batch y:  tensor([7., 5., 4., 1., 3.]) | batch z:  tensor([7., 5., 4., 1., 3.])
Epoch:  1 | Step:  1 | batch x:  tensor([5., 3., 2., 1., 9.]) | batch y:  tensor([ 6.,  8.,  9., 10.,  2.]) | batch z:  tensor([ 6.,  8.,  9., 10.,  2.])
Epoch:  2 | Step:  0 | batch x:  tensor([ 4.,  2.,  5.,  6., 10.]) | batch y:  tensor([7., 9., 6., 5., 1.]) | batch z:  tensor([7., 9., 6., 5., 1.])
Epoch:  2 | Step:  1 | batch x:  tensor([3., 9., 1., 8., 7.]) | batch y:  tensor([ 8.,  2., 10.,  3.,  4.]) | batch z:  tensor([ 8.,  2., 10.,  3.,  4.])<jupyter_text>Suppose a different batch size that cannot be fully divided by the number of data entreis:<jupyter_code>BATCH_SIZE = 8
loader = Data.DataLoader(
    dataset=Data.TensorDataset(x, y),      # torch TensorDataset format
    batch_size=BATCH_SIZE,      # mini batch size
    shuffle=True,               # random shuffle for training
    num_workers=2,              # subprocesses for loading data
)
for epoch in range(3):   # train entire dataset 3 times
    for step, (batch_x, batch_y) in enumerate(loader):  # for each training step
        # train your data...
        print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
              batch_x, '| batch y: ', batch_y)<jupyter_output>Epoch:  0 | Step:  0 | batch x:  tensor([ 4., 10.,  9.,  8.,  7.,  6.,  1.,  2.]) | batch y:  tensor([ 7.,  1.,  2.,  3.,  4.,  5., 10.,  9.])
Epoch:  0 | Step:  1 | batch x:  tensor([5., 3.]) | batch y:  tensor([6., 8.])
Epoch:  1 | Step:  0 | batch x:  tensor([9., 8., 4., 6., 5., 3., 7., 2.]) | batch y:  tensor([2., 3., 7., 5., 6., 8., 4., 9.])
Epoch:  1 | Step:  1 | batch x:  tensor([10.,  1.]) | batch y:  tensor([ 1., 10.])
Epoch:  2 | Step:  0 | batch x:  tensor([ 5.,  1.,  3.,  7.,  6., 10.,  9.,  8.]) | batch y:  tensor([ 6., 10.,  8.,  4.,  5.,  1.,  2.,  3.])
Epoch:  2 | Step:  1 | batch x:  tensor([2., 4.]) | batch y:  tensor([9., 7.])
{'fraction_non_alphanumeric': 0.20387174833635813, 'fraction_numerical': 0.06503327283726558, 'mean_word_length': 2.3676171079429733, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '612510', 'license': 'MIT', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1304, 'n_words': 431, 'path': 'tutorial-contents-notebooks/305_batch_train.ipynb', 'repo_name': 'cenariusxz/PyTorch-Tutorial-MorvanZhou', 'size': '3306'}