💻 源码
"""
演示
    创建 线性 和 随机张量
设计到的API:
    torch.arange() 和 torch.linspace() 创建线性张量
    torch.manual_seed() 设置随机种子
    torch.rand/randn() 创建随机浮点类型张量
    torch.randint(low, high, size=()) 创建随机整数类型张量

需要掌握的:
    torch.arange()
    torch.linspace()
    torch.manual_seed()
    torch.randint(low, high, size=())

"""

# 导包
import torch

# 1.定义函数,使用torch.arange() 和 torch.linspace()创建线性张量
def demo01():
    # 1.使用torch.arange()创建一个从0到9的线性张量
    t1 = torch.arange(0,10)
    print(f"t1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}")
    # 2.使用torch.linspace()创建一个从0到9的线性张量
    t2 = torch.linspace(0,9,10)
    print(f"t2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}")
# 2.定义函数,演示 创建随机张量
def demo02():
    # 0.设置随机种子
    # torch.random.manual_seed(28)
    torch.manual_seed(28)
    # 1.使用torch.rand创建一个随机张量,(3,5)
    t1 = torch.rand(3,5)
    print(f"t1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}") # (3,5)
    # 2.使用torch.randn创建一个随机张量,(3,5)
    t2 = torch.randn(3,5)
    print(f"t2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}")
    # 3.使用torch.randint创建一个随机整数张量,(3,5)
    t3 = torch.randint(0,10,(6,8))
    print(f"t3:{t3}, shape: {t3.shape}, dtype: {t3.dtype}")
    # 4.打印当前随机种子
    print(f"当前随机种子:{torch.initial_seed()}")

# 测试
if __name__ == '__main__':
    # demo01()
    demo02()