pytorch 实现线性回归

import torch
from matplotlib import pyplot as plt
import numpy as np
import random

"""
生成数据集
"""
num_inputs = 2
num_examples = 1000
true_w = [2, -2.3]
true_b = 4.8
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.scatter(features[:, 0].numpy(), labels.numpy(), 1)
plt.show()

"""
读取数据
"""
def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    random.s

你可能感兴趣的:(#,pytorch,pytorch,线性回归,深度学习)