%matplotlib inline import random import torch from d2l import torch as d2l def synthetic_data(w,b,num_exaples): """生成 y = Xw + b + 噪声""" X = torch.normal(0,1,(num_exaples,len(w))) y = torch.matmul(X,w) + b y += torch.normal(0,0.01,y.shape) return X, y.reshape((-1,1)) true_w = torch.tensor([2,-3.4]) true_b = 4.2 features, labels = synthetic_data(true_w, true_b, 1000) print('features:',features[0],'\nlabel:',labels[0]) d2l.set_figsize() d2l.plt.scatter(features[:,(1)].detach().numpy(),labels.detach().numpy(),1) # data_iter函数接收批量大小、特征矩阵和标签向量作为输入,生成大小为batch_size的小批量 def data_iter(batch_size, features, labels): # 填空1: 获取样本的总数 num_examples = # 填空2: 创建样本的索引列表 indices = None # 这些样本是随即读取的,没有特定的顺序 random.shuffle(indices) # 把索引随机打乱 for i in range(0, num_examples, batch_size): batch_indices = torch.tensor(indices[i:min(i + batch_size, num_examples)]) # 当i+batch_size超出时,取num_examples yield features[batch_indices], labels[batch_indices] # 获得随机顺序的特征及对应的标签 batch_size = 10 for X, y in data_iter(batch_size, features, labels): # 填空3: 打印一个小批量的特征 X 和对应的标签 y print(None) break
理解问题%matplotlib inline import random import torch from d2l import torch as d2l def synthetic_data(w,b,num_exaples): """生成 y = Xw + b + 噪声""" X = torch.normal(0,1,(num_exaples,len(w))) y = torch.matmul(X,w) + b y += torch.normal(0,0.01,y.shape) return X, y.reshape((-1,1)) true_w = torch.tensor([2,-3.4]) true_b = 4.2 features, labels = synthetic_data(true_w, true_b, 1000) print('features:',features[0],'\nlabel:',labels[0]) d2l.set_figsize() d2l.plt.scatter(features[:,(1)].detach().numpy(),labels.detach().numpy(),1) # data_iter函数接收批量大小、特征矩阵和标签向量作为输入,生成大小为batch_size的小批量 def data_iter(batch_size, features, labels): # 填空1: 获取样本的总数 num_examples = # 填空2: 创建样本的索引列表 indices = None # 这些样本是随即读取的,没有特定的顺序 random.shuffle(indices) # 把索引随机打乱 for i in range(0, num_examples, batch_size): batch_indices = torch.tensor(indices[i:min(i + batch_size, num_examples)]) # 当i+batch_size超出时,取num_examples yield features[batch_indices], labels[batch_indices] # 获得随机顺序的特征及对应的标签 batch_size = 10 for X, y in data_iter(batch_size, features, labels): # 填空3: 打印一个小批量的特征 X 和对应的标签 y print(None) break
已完成理解「%matplotlib inline import random import torch from d2l import torch as d2l def synthetic_data(w,b,num_exaples): """生成 y = Xw + b + 噪声""" X = torch.normal(0,1,(num_exaples,len(w))) y = torch.matmul(X,w) + b y += torch.normal(0,0.01,y.shape) return X, y.reshape((-1,1)) true_w = torch.tensor([2,-3.4]) true_b = 4.2 features, labels = synthetic_data(true_w, true_b, 1000) print('features:',features[0],'\nlabel:',labels[0]) d2l.set_figsize() d2l.plt.scatter(features[:,(1)].detach().numpy(),labels.detach().numpy(),1) # data_iter函数接收批量大小、特征矩阵和标签向量作为输入,生成大小为batch_size的小批量 def data_iter(batch_size, features, labels): # 填空1: 获取样本的总数 num_examples = # 填空2: 创建样本的索引列表 indices = None # 这些样本是随即读取的,没有特定的顺序 random.shuffle(indices) # 把索引随机打乱 for i in range(0, num_examples, batch_size): batch_indices = torch.tensor(indices[i:min(i + batch_size, num_examples)]) # 当i+batch_size超出时,取num_examples yield features[batch_indices], labels[batch_indices] # 获得随机顺序的特征及对应的标签 batch_size = 10 for X, y in data_iter(batch_size, features, labels): # 填空3: 打印一个小批量的特征 X 和对应的标签 y print(None) break」
展开阅读网页