动手学深度学习PyTorch版---day01

 

目录

Day01

1.线性回归

方法1---手动实现

方法2---pytorch实现

2.Softmax

1.相关概念

2.交叉熵损失函数

3.从零实现softmax回归

4.pytorch实现softmax回归

3.多层感知机


Day01

线性回归,softmax回归与分类模型,多层感知机

参考:https://tangshusen.me/Dive-into-DL-PyTorch/#/

《动手学深度学习》组队学习 学员学习手册  https://shimo.im/docs/pdr3wkyHKrxJYdyT/read

1.线性回归

方法1---手动实现

import torch
#import IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random

num_inputs = 2
num_examples = 1000

true_w = [2, -3,4]
true_b = 4.2

features = torch.randn(num_examples,num_inputs,
                       dtype=torch.float32) #标准正态分布
labels = true_w[0]*features[:,0]+true_w[1]*features[:,1]+true_b
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()),
                      dtype=torch.float32)
# plt.figure()
# #画散点图
# plt.scatter(features[:,1].numpy(),labels.numpy(),1)
# plt.show()

#读取数据
def data_iter(batch_size,features,labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        j = torch.LongTensor(indices[i:min(i+batch_size, num_examples)])
        yield features.index_select(0, j), labels.index_select(0, j)

batch_size = 10
for X,y in data_iter(batch_size, features, labels):
    print(X,'\n', y)
    break

# 初始化模型参数
w = torch.tensor(np.random.normal(0,0.01,(num_inputs,1)),dtype=torch.float32)
b = torch.zeros(1,dtype=torch.float32)

w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值