return x num_example=50 net=simple_net(4,nethid,1) num_inputs=1
feature=w[:,1:5] feature=np.reshape(feature,(-1,4)) feature=torch.tensor(feature, dtype=torch.float) lable=w[:,6] lable=torch.tensor(lable,dtype=torch.float) batch_size=3 loss = torch.nn.MSELoss() optimizer = optim.SGD(net.parameters(), lr=0.000003) num=0 sum=0 for epoch in range(10000): for X,y in data_iter(batch_size,feature,lable): output = net(X) l = loss(output, y.view(-1, 1)) optimizer.zero_grad() # 梯度清零,等价于net.zero_grad() l.backward() optimizer.step() num=num+1 if epoch%16==0: sum=0 print('epoch %d, loss: %f' % (epoch, l.item())) for epoch in range(155128): for X,y in data_iter(1000,feature,lable): output = net(X) l = loss(output, y.view(-1, 1)) optimizer.zero_grad() # 梯度清零,等价于net.zero_grad() l.backward() optimizer.step() num=num+1 if epoch%16==0: sum=0 print('epoch %d, loss: %f' % (epoch, l.item()))