pytorch_basics

import torch           # 包 torch 包含了多维张量的数据结构以及基于其上的多种数学操作。另外,它也提供了多种工具,其中一些可以更有效地对张量和任意类型进行序列化
import torchvision     # torchvision包 包含了目前流行的数据集,模型结构和常用的图片转换工具
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms


# ================================================================== #
#                     1. Basic autograd example 1                    #
# ================================================================== #

# Create tensors
x = torch.tensor(1., requires_grad = True)  # 有一点因为只有float型才能求梯度
w = torch.tensor(2., requires_grad = True)
b = torch.tensor(3., requires_grad = True)

# Build a computational graph.
y = w * x + b

# Compute gradients.
y.backward()

# Print out the gradients.
print(x.grad)    # x.grad = 2
print(w.grad)    # w.grad = 1
print(b.grad)


# ================================================================== #
#                    2. Basic autograd example 2                     #
# ================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值