How do I build a TFmodel from NumPy array files?

家住魔仙堡 提交于 2021-02-11 12:19:54

问题


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?


回答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.

class NumpyInitializer(tf.keras.initializers.Initializer):
    # custom class converting numpy arrays to tf's initializers 
    # used to initialize both kernel and bias
    def __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 

def restore_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
    """


    def file_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 = 0
        for 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 +=1
            print(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



来源:https://stackoverflow.com/questions/63304056/how-do-i-build-a-tfmodel-from-numpy-array-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!