pytorch-用线性回归梯度下降模型-预测水果产量

  1. 假设有如下数据集, 如何预测水果产量
    在这里插入图片描述
    假设水果产量与地区无关, 与只与温度, 降雨量, 湿度有关, 若用线性模型预测, 可以通过线性回归的方法, 预测产量。加入线性无关, 通过蓝色方框参数可以求解W1, W2, W3值。

  2. 根据线性模型, 加入喂入数据权重w, 与偏差值b, 建立线性方程。

yield_apple = w11 * temp + w12 * rainfall + w13 * humidity + b1
yield_orange = w21 * temp + w22 * rainfall + w23 * humidity + b2
降雨量, 温度与苹果参量的关系可能如下图:
在这里插入图片描述
通过梯度下降法, 不断喂入数据w, 可以求出理想的w值。

  1. 引入pytorch 和 numpy
import numpy as np
import torch
  1. 加入训练数据
# Input (temp, rainfall, humidity)
inputs = np.array([[73, 67, 43], 
                   [91, 88, 64], 
                   [87, 134, 58], 
                   [102, 43, 37], 
                   [69, 96, 70]], dtype='float32')
# Targets (apples, oranges)
targets = np.array([[56, 70], 
                    [81, 101], 
                    [119, 133], 
                    [22, 37], 
                    [103, 119]], dtype='float32')
  1. 转化 为tensor数据, 因为numpy提取csv文件, tensor训练数据
# Convert inputs and targets to tensors
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
print(inputs)
print(targets)
    tensor([[ 73.,  67.,  43.],
    [ 91.,  88.,  64.],
    [ 87., 134.,  58.],
    [102.,  43.,  37.],
    [ 69.,  96.,  70.]]) 
    tensor([[ 56.,  70.],
    [ 81., 101.],
    [119., 133.],
    [ 22.,  37.],
    [103., 119.]])
  1. 建立线性模型:
    在这里插入图片描述
# Weights and biases
w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(2, requires_grad=
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值