Skip to content Skip to sidebar Skip to footer

How Do I Build A Tfmodel From Numpy Array Files?

I have a dir with NumPy array files: bias1.npy, kernel1.npy, bias2.npy, kernel2.npy. How can I build a TF model that uses those arrays as kernels and biases of layers?

Solution 1:

To avoid confusion bias matrix for the consistency of the numpy file is the 2D matrix with one column. This post shows how did I reproduce tf's model based on the numpy weights and biases.

classNumpyInitializer(tf.keras.initializers.Initializer):
    # custom class converting numpy arrays to tf's initializers # used to initialize both kernel and biasdef__init__(self, array):
        # convert numpy array into tensor 
        self.array = tf.convert_to_tensor(array.tolist())
        
    def__call__(self, shape, dtype=None):
        # return tensor return self.array 

defrestore_model_from_numpy(directory):

    """
    Recreate model from the numpy files. 
    Numpy files in the directory are ordered by layers
    and bias numpy matrix comes before numpy weight matrix. 

    In example: 
        directory-
            - L1B.npy //numpy bias matrix for layer 1
            - L1W.npy //numpy weights matrix for layer 1
            - L2B.npy //numpy bias matrix for layer 2
            - L2W.npy //numpy weights matrix for layer 2

    Parameters: 
        directory - path to the directory with numpy files
    Return: 
        tf's model recreated from numpy files
    """deffile_iterating(directory):
        """
        Iterate over directory and create 
        dictionary of layers number and it's structure

        layers[layer_number] = [numpy_bias_matrix, numpy_weight_matrix]
        """

        pathlist = Path(directory).rglob("*.npy") # list of numpy files
        layers = {} # initialize dictionary 
        index = 0for file in pathlist: # iterate over file in the directory if index % 2 == 0:
                layers[int(index/2)] = [] # next layer - new key in dictionary
            layers[int(index/2)].append(np.load(file)) # add to dictionary bias or weight 
            index +=1print(file) # optional to show list of files we deal with return layers # return dictionary 

    layers = file_iterating(directory) # get dictionary with model structure

    inputs = Input(shape = (np.shape(layers[0][1])[0])) # create first model input layer
    x = inputs 

    for key, value in layers.items(): # iterate over all levers in the layers dictionary
        bias_initializer = NumpyInitializer(layers[key][0][0]) # create bias initializer for key's layer 
        kernal_initializer = NumpyInitializer(layers[key][1]) # create weights initializer for key's layer 
        layer_size = np.shape(layers[key][0])[-1] # get the size of the layer

        new_layer = tf.keras.layers.Dense( # initialize new Dense layer
            units = layer_size, 
            kernel_initializer=kernal_initializer, 
            bias_initializer = bias_initializer,
            activation="tanh")
        x = new_layer(x) # stack layer at the top of the previous layer
        
    model = tf.keras.Model(inputs, x) # create tf's model based on the stacked layers 
    model.compile() # compile model return model # return compiled model 

In my directory, I had 4 numpy files (layer 1 - L1 and layer 2 - L2):

100_5_25_1Knapsack_Layer1\100_5_25_1Knapsack\L1B.npy,shape:(1,80)100_5_25_1Knapsack_Layer1\100_5_25_1Knapsack\L1W.npy,shape:(100,80)100_5_25_1Knapsack_Layer1\100_5_25_1Knapsack\L2B.npy,shape:(1,100)100_5_25_1Knapsack_Layer1\100_5_25_1Knapsack\L2W.npy,shape:(80,100)

Calling the function result in:

m = restore_model_from_numpy(my_numpy_files_directory)
m.summary()

Model: "model_592"
_________________________________________________________________Layer (type)                 Output Shape              Param #   
=================================================================
input_312 (InputLayer)       [(None, 100)]             0         
_________________________________________________________________
dense_137 (Dense)            (None, 80)                8080      
_________________________________________________________________dense_138 (Dense)            (None, 100)               8100      
=================================================================
Total params: 16,180
Trainable params: 16,180
Non-trainable params: 0
_________________________________________________________________

I hope that this post will be helpful to anyone as it's my first one.

Happy coding :D

Post a Comment for "How Do I Build A Tfmodel From Numpy Array Files?"