Модель TF2 из Numpy массивов - PullRequest
1 голос
/ 05 августа 2020

Я столкнулся с проблемой воспроизвести TF2. модель из numpy массивов (матрица смещения и матрица весов для каждого слоя). Чтобы избежать путаницы, матрица смещения для согласованности файла numpy представляет собой 2D-матрицу с одним столбцом. В этом посте показано, как я это сделал.

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 

В моем каталоге у меня было 4 numpy файлов (уровень 1 - L1 и уровень 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 результат функции:

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
_________________________________________________________________

Я надеюсь, что этот пост будет полезен для всех, поскольку он мой первый.

Удачного кодирования: D

...