Saving custom models
I have defined a custom model as follows: <SCODE>class model(torch.nn.Module):
	"""Defines custom model
	"""
	def __init__(self, dim_input, dim_output):
		
		super(model, self).__init__()
		self._dim_input = dim_input
		self._dim_output = dim_output
		
		'''Initialize nnet layers'''
		self._l1 = torch.nn.Linear(self._dim_input, SIZE_H1)
		self._l2 = torch.nn.Linear(SIZE_H2, SIZE_H1)
	self._l3 = torch.nn.Linear(SIZE_H3, SIZE_H2)


	def forward(self,x):
		self._l1_out = nn.ReLU(self._l1(x))
		self._l2_out = nn.ReLU(self._l2(self._l1_out))
		self._l3_out = nn.BatchNorm1d(self._l3(self._l2_out))
		self._out = nn.Sigmoid(self._l3_out)

		return self._out
<ECODE> How do I save this as a .t7 file? Thanks
Anmol6
alexis-jacq
Kinda embarrassed, but I had the order of the filename and the module reversed in torch.save()… Thanks anyway!
Anmol6
Spoke too soon - I tried saving it as a .t7 file but I can’t load it with load_lua
Anmol6
I want to load this like I would load a Sequential model. Can I not do that?
Anmol6
apaszke
Oh, do you have any suggestions then on how I can best load/save this custom model in python? Should I just do my best to just write my model as a nn module (eg. using Sequential) and load/save with torch.save() and load_lua respectively?
Anmol6
nvm, got it - thanks!
Anmol6
apaszke
Russel_Russel
apaszke
Thanks
devansh20la
Hey , I am beginner and was trying to save parameters of a pretrained network in hdf5 file and wanted to load it in torch , but unsuccessfull . Could you please let me know how to save parameters of a pretrained network (which is in pytorch ) in hdf5 file . Thanks!
Rao_Shivansh
I can tell you how I saved the model and it worked import pickle This saved the model, then when I started training the model instead of starting from zero I loaded the saved model: and loaded the state of the saved model to a already initialized model: shared_model.load_state_dict(existing_model.state_dict()) The training continued from where I stopped the training.
Erik_Janezic
does it save the model in pytorch and load it in lua ?
Rao_Shivansh
Erik_Janezic
Thanks !
Rao_Shivansh
Thanks a lot !
Rao_Shivansh
Hey, Rao, I know this post is a few months old, but were you ever able to package the model to HDF5? Thanks.
Rose