import torch
import numpy as np
import sys
sys.path.append("路径")
import d2lzh_pytorch as d2l
'''
-----------------------------生成人工数据集
样本数n=200
特征数=3
三阶多项式y=1.2x-3.4x^2+5.6x^3+5+ε
'''
n_train, n_test, true_w, true_b = 100, 100, [1.2, -3.4, 5.6], 5
sample_features = torch.randn(n_train + n_test, 1) # 200x1 单算的一个特征
poly_sample_features = torch.cat((sample_features, torch.pow(sample_features, 2), torch.pow(sample_features, 3)),
dim=1) # 组合成3个特征
# 因为poly_features取列的元素时,没有对列加[]限制,所以取出来的值不保有原来的维度,而是成为了一维张量,所以labels相应的也是一维张量
labels = true_w[0] * poly_sample_features[:, 0] + true_w[1] * poly_sample_features[:
01-19
1311
06-19
1690