I am trying to save the network and parameters from a NN written in Tensorflow 1.4 I am struggling to save the layers (a simple feed-forward 3-layer neural network). This is part of the main code:
class Model:
def __init__(self):
self.__build_flag = -1
self.__train_flag = -1
def __dictNN(self, x):
# Parameters
dim = self.__dim
hdim = self.__hdim
ddim = self.__ddim
kmatdim = ddim + 1 + dim
num_layers = self.__num_layers
std = 1.0 / np.sqrt(hdim)
std_proj = 1.0 / np.sqrt(dim)
with tf.variable_scope("Input_projection",
initializer=tf.orthogonal_initializer()):
P = tf.get_variable(name='weights',
shape=(dim,hdim),
dtype=tf.float64)
res_in = tf.matmul(x, P)
with tf.variable_scope("Residual"):
for j in range(self.__num_layers):
layer_name = "Layer_"+str(j)
with tf.variable_scope(layer_name):
W = tf.get_variable(name="weights", shape=(hdim,hdim),
dtype=tf.float64)
b = tf.get_variable(name="biases", shape=(hdim),
dtype=tf.float64)
if j==0: # first layer
res_out = res_in + self.__tf_nlr(
tf.matmul(res_in, W) + b)
else: # subsequent layers
res_out = res_out + self.__tf_nlr(
tf.matmul(res_out, W) + b)
with tf.variable_scope("Output_projection",
initializer=tf.orthogonal_initializer()):
W = tf.get_variable(name="weights", shape=(hdim, ddim),
dtype=tf.float64)
b = tf.get_variable(name="biases", shape=(1,ddim),
dtype=tf.float64)
out = tf.matmul(res_out, W) + b
return out
What I have thought doing is to create an instance attribute after I defined b and weights
self.weights = W
self.b = b
Then I can save them by using
@property
def save_weights(self):
assert self.__build_flag == 0, "Run build() first."
return self.weights
@property
def save_b(self):
assert self.__build_flag == 0, "Run build() first."
return self.b
After I have trained the model, I have a NN which defines a dictionary ($$ \Gamma_{x}= W_{out} h_3 + b_{out}$$) to convert some data into a "set of observables"
So, I have trained my NN, I have obtained my Weights and biases from it. Now, I am struggling to load them back to the system, so when I can pass my new data through the dictionary defined by the NN to get more observables.
Not sure how to do it. I have some code in tensorflow 2, but not sure if this helps mu goal
def save(self,params_name,model_name):
io.savemat(params_name,self.params)
self.neuralDict.save(model_name)
# Load in a saved, pretrained network
def load_net(self,model_name):
self.neuralDict = keras.models.load_model(model_name, custom_objects=custom_objects)