我试图用SGD在pytorch中训练一个简单的多项式线性回归模型 . 我写了一些自包含(我认为是非常简单的代码),然而,由于某种原因,我的模型没有按照我的想法进行训练 .
我从正弦曲线中采样了5个点,并尝试用4度多项式拟合它 . 这是一个凸问题,所以只要我们有足够的迭代和足够小的步长,GD或SGD应该找到零列车误差的解决方案 . 尺寸 . 出于某种原因,我的模型不能很好地训练(即使它似乎正在改变模型的参数 . 任何人都知道为什么?这是代码(我尝试使其自包含和最小):
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
import torch
from torch.autograd import Variable
from maps import NamedDict
from plotting_utils import *
def index_batch(X,batch_indices,dtype):
'''
returns the batch indexed/sliced batch
'''
if len(X.shape) == 1: # i.e. dimension (M,) just a vector
batch_xs = torch.FloatTensor(X[batch_indices]).type(dtype)
else:
batch_xs = torch.FloatTensor(X[batch_indices,:]).type(dtype)
return batch_xs
def get_batch2(X,Y,M,dtype):
'''
get batch for pytorch model
'''
# TODO fix and make it nicer, there is pytorch forum question
X,Y = X.data.numpy(), Y.data.numpy()
N = len(Y)
valid_indices = np.array( range(N) )
batch_indices = np.random.choice(valid_indices,size=M,replace=False)
batch_xs = index_batch(X,batch_indices,dtype)
batch_ys = index_batch(Y,batch_indices,dtype)
return Variable(batch_xs, requires_grad=False), Variable(batch_ys, requires_grad=False)
def get_sequential_lifted_mdl(nb_monomials,D_out, bias=False):
return torch