<jupyter_start><jupyter_text>Simple Example<jupyter_code># random normal
x = torch.randn(5, 3)
print (x)
x = torch.randm(5, 3)
prin
# build a layer
linear = nn.Linear(3, 2)
# Sess weight and bias
print (linear.weight)
print (linear.bias)
# forward propagate
y = linear(Variable(x))
print (y)<jupyter_output>Variable containing:
-1.0986 -0.1575
-2.0311  0.4378
-0.6131  1.3938
 0.6790  0.7929
-0.3134  0.8351
[torch.FloatTensor of size 5x2]<jupyter_text>Convert numpy array to torch tensor<jupyter_code># convert numpy array to tensor
a = np.array([[1,2], [3,4]])
b = torch.from_numpy(a)
print (b)<jupyter_output><empty_output><jupyter_text>Input pipeline  (1) Preprocessing<jupyter_code># Image Preprocessing 
transform = transforms.Compose([
    transforms.Scale(40),
    transforms.RandomHorizontalFlip(),
    transforms.RandomCrop(32),
    transforms.ToTensor()])<jupyter_output><empty_output><jupyter_text>(2) Define Dataset<jupyter_code># download and loading dataset f
train_dataset = dsets.CIFAR10(root='./data/',
                               train=True, 
                               transform=transform,
                               download=True)

image, label = train_dataset[0]
print (image.size())
print (label)<jupyter_output>Files already downloaded and verified
torch.Size([3, 32, 32])
6<jupyter_text>(3) Data Loader<jupyter_code># data loader provides queue and thread in a very simple way
train_loader = data.DataLoader(dataset=train_dataset,
                               batch_size=100, 
                               shuffle=True,
                               num_workers=2)
# iteration start then queue and thread start
data_iter = iter(train_loader)

# mini-batch images and labels
images, labels = data_iter.next()

for images, labels in train_loader:
    # your training code will be written here
    pass<jupyter_output><empty_output><jupyter_text>(4) What about custom dataset not cifar10?<jupyter_code>class CustomDataset(data.Dataset):
    def __init__(self):
        pass
    def __getitem__(self, index):
        # You should build this function to return one data for given index
        pass
    def __len__(self):
        pass
custom_dataset = CustomDataset()
data.DataLoader(dataset=custom_dataset,
                batch_size=100, 
                shuffle=True,
                num_workers=2)<jupyter_output><empty_output><jupyter_text>Using Pretrained Model<jupyter_code># Download and load pretrained model
resnet = torchvision.models.resnet18(pretrained=True)
# delete top layer for finetuning
sub_model = nn.Sequentialtial(*list(resnet.children()[:-1]))

# for test
images = Variable(torch.randn(10, 3, 256, 256))
print (resnet(images).size())
print (sub_model(images).size())<jupyter_output><empty_output><jupyter_text>Save and Load Model<jupyter_code># Save and load the trained model
torch.save(sub_model, 'model.pkl')

model = torch.load('model.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10773951585407433, 'fraction_numerical': 0.03443573133310603, 'mean_word_length': 3.739903069466882, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '174895', 'license': 'Apache-2.0', 'n_tokens_mistral': 1015, 'n_tokens_neox': 962, 'n_words': 225, 'path': 'deep_learning/pytorch/tutorials/00 - PyTorch Basics/.ipynb_checkpoints/basics-checkpoint.ipynb', 'repo_name': 'trhongbinwang/data_science_journey', 'size': '2933'}