如何从NumPy数组文件构建TFmodel? [英] How do I build a TFmodel from NumPy array files?

查看:70
本文介绍了如何从NumPy数组文件构建TFmodel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含NumPy数组文件的目录: bias1.npy,kernel1.npy,bias2.npy,kernel2.npy .如何建立一个使用这些数组作为内核和层偏差的TF模型?

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?

推荐答案

为避免numpy文件的一致性造成的混淆偏差矩阵,是一列的2D矩阵.这篇文章展示了我是如何根据numpy的权重和偏差来重现tf的模型的.

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 

在我的目录中,我有4个numpy文件(第1层-L1和第2层-L2):

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)

调用该函数将导致:

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.

快乐编码:D

这篇关于如何从NumPy数组文件构建TFmodel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆