Pytorch之线性回归
Pytorch之线性回归 线性回归原理 加载数据 feature = 2 samples = 1000 # 设置参数 true_w = [ 2 , - 3.4 ] true_b = 4.2 # 生成数据 data = t . randn ( samples , feature , dtype = t . float32 ) labels = true_w [ 0 ] * data [ : , 0 ] + true_w [ 1 ] * data [ : , 1 ] + true_b # 数据添加噪声 labels += t . tensor ( np . random . normal ( 0 , 0.01 , size = labels . size ( ) ) , dtype = t . float32 ) 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 = t .