书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理。
# -*- coding: utf-8 -*-
# All codes and comments from <>
# Code url : https://github.com/zhouzhoujack/pytorch-book
# lesson_1 : Basic code syntax of PT(Pytorch)
import torch as t
import numpy as np
# 1.The version of torch
print(t.__version__)# version is 1.0.1
# 2.The defination of tensor
x = t.Tensor(5, 3) # 构建 5x3 矩阵,只是分配了空间,未初始化
x = t.Tensor([[1,2],[3,4]])
# 3.Initialize tensor by [0,1] uniform distribution
x = t.rand(5, 3)
print(x, x.size()[0]) # torch.Size 是tuple对象的子类,因此它支持tuple的所有操作,如x.size()[0]
# 4.Addition
x = t.rand(5, 3)
y = t.rand(5, 3)
print("first method: ", x + y)
print("second method: ", t.add(x,y))