title: Model parameter initialization
body: <p>I am new to PyTorch. I am writing a simple program for linear regression and i want to compare the results by using different methods (SGD,momentum,ADAM,etc). The problem I have is that I want every time a loop ends for the model parameters to be reinitialized to the same value that the previous model started with  so the comparison is valid. </p>
  
  <p>This is what i have so far, this is my training data:</p>
  
  <pre><code>      x1=np.arange(0,10,1).reshape(10,1)
        y1=2*x1+1+np.random.normal(0,1,(10,1))
        x=torch.from_numpy(x1)
        y=torch.from_numpy(y1)
  </code></pre>
  
  <p>Here i train the data</p>
  
  <pre><code>      from torch.utils.data import TensorDataset,DataLoader
        train=TensorDataset(xdata,ydata) 
        size_batch=10
        dl=DataLoader(train,size_batch,shuffle=True)
  </code></pre>
  
  <p>Define the model and opt</p>
  
  <pre><code>     model=nn.Linear(1,1)
       opt=torch.optim.SGD(model.parameters(), lr=1e-4)
       import torch.nn.functional as F
       loss1=F.mse_loss
       loss=loss1(model(x),y)
  </code></pre>
  
  <p>Function </p>
  
  <pre><code>     def fitmodel(nepochs, model, loss1, opt):
           for epoch in range(nepochs):
               for xm,ym in dl:
                   predict = model(xm)
                   loss = loss1(predict, ym)
                   loss.backward()
                   opt.step()
                   opt.zero_grad()  
  </code></pre>
  
  <p>Call the function </p>
  
  <pre><code>     fitmodel(1000,model,loss1,opt)
  </code></pre>
  
  <p>Now i want to rerun the above but for different optimization algorithms. If I just rerun fitmodel it will use some of the parameters it already has calculated. I want to start from the same 'initial conditions' as i started the previous run. Anyone has any ideas how to do that?       </p>
  
  <p><strong>Edit</strong>
  Before i run the fitmodel I copy the initial bias and weight</p>
  
  <pre><code>   w1=model.weight
     b1=model.bias
     fitmodel(1000,model,loss1,opt)
     model.weight=w1
     model.bias=b1
     loss=[]
  </code></pre>
  
  <p>But i get this error:
  TypeError: cannot assign 'list' as parameter 'bias' (torch.nn.Parameter or None expected)</p>