title: How to save weights in .npy file in a similar format which is being used in CNN?
body: <p>I am using a github repository containing a trained CNN with weight parameters given in <code>.npy</code> file. Model is loading the weights and using the model parameters like this:- </p>
  
  <pre><code>model = CNN_Model(batch_size)
  filename = "weight_file.npy"
  dtype = torch.FloatTensor    
  model.load_state_dict(load_weights(model, weight_file, dtype))
  </code></pre>
  
  <p>And <code>load_weights</code> is defined as:-</p>
  
  <pre><code>def load_weights(model, filename, dtype):
      model_params = model.state_dict()
      data_dict = np.load(filename, encoding='latin1').item()
      model_params["conv1.weight"] = torch.from_numpy(data_dict["conv1"] ["weights"]).type(dtype).permute(3,2,0,1)
      model_params["conv1.bias"] = torch.from_numpy(data_dict["conv1"]["biases"]).type(dtype)
      model_params["bn1.weight"] = torch.from_numpy(data_dict["bn_conv1"]["scale"]).type(dtype)
      model_params["bn1.bias"] = torch.from_numpy(data_dict["bn_conv1"]["offset"]).type(dtype)
      return model_params
  </code></pre>
  
  <p>I have added a training module to it and trying to fine tune the weights  on my own dataset. After training i want to save new weights in <code>.npy</code> file with same indexes of <code>data_dict</code> as there were in previously loaded weight file so i can use them again for CNN model. </p>
  
  <p>How should i do indexing with similar names before saving the data_dict array using: </p>
  
  <pre><code>np.save("trained_weight_file.npy", data_dict)
  </code></pre>
  
  <p>EDIT 1:-
  So on recommendation of @<a href="https://stackoverflow.com/users/11192099/a-d">a-d</a> i did</p>
  
  <pre><code>data_dict = model.state_dict()
  </code></pre>
  
  <p>What it did is it saved all the weights with index of <code>model_params</code>. Output of <code>print data_dict</code> was:- </p>
  
  <pre><code>OrderedDict([('conv1.weight', tensor([[[[....]]]])), ('conv1.bias', tensor([....])), , ('bn1.weight', tensor([....])), ('bn1.bias', tensor([....]))])
  </code></pre>
  
  <p>But what i need is to store in <code>data_dict</code> index so i can read it with same algo from <code>.npy</code> file. Also i tried returning <code>data_dict</code> along with <code>model_params</code> from <code>load_weights</code> definition and then tried to use <code>data_dict = model.state_dict()</code> but it gave me error on `model.load_state_dict(load_weights(model, weight_file, dtype))' line that is:-</p>
  
  <p>Traceback (most recent call last):
    model.load_state_dict(load_weights(model, weight_file, dtype))
    state_dict = state_dict.copy()
  AttributeError: 'tuple' object has no attribute 'copy'</p>