Python TF2来自Numpy数组的模型

Python TF2来自Numpy数组的模型,python,numpy,tensorflow,model,tensorflow2.0,Python,Numpy,Tensorflow,Model,Tensorflow2.0,我遇到这个问题是为了复制TF2。来自numpy阵列的模型(每个层的偏差矩阵和权重矩阵)。为避免混淆,numpy文件一致性的偏差矩阵是一列2D矩阵。这篇文章展示了我是如何做到的 class NumpyInitializer(tf.keras.initializers.Initializer): # custom class converting numpy arrays to tf's initializers # used to initialize both kernel a

我遇到这个问题是为了复制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 
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):

调用函数的结果如下:

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
_________________________________________________________________
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

@Filip Kubacki,感谢您的解决方案。为了社区的利益,我在这里提供解决方案(答案部分)从NumPy数组(即权重和偏差)文件构建Tensorflow模型

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

调用函数的结果如下:

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
_________________________________________________________________
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
_________________________________________________________________

@菲利普·库巴基,谢谢你的解决方案。为了社区的利益,我在这里提供解决方案(答案部分)从NumPy数组(即权重和偏差)文件构建Tensorflow模型

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

调用函数的结果如下:

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
_________________________________________________________________
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
_________________________________________________________________

您应该将问题替换为问答格式,而不是文章格式。将问题分为问题部分和答案部分,回答你的问题并接受它。在问题部分给出更多细节,比如为什么这么难或者什么会导致错误,并确保它不是其他问题的重复。谢谢你的帖子。正如Sajan points所建议的,请将其编辑为一个问题,然后将解决方案添加为答案,这样可以使人们更容易找到它,并理解您发布的内容是一个解决方案。例如,您可以发布类似“如何从NumPy数组文件构建Keras模型?”的内容,以及类似“我有一个包含NumPy数组文件的目录,如
bias1.npy
kernel1.npy
bias2.npy
,等等。如何构建使用这些数组作为权重的Keras模型?”谢谢。我已经改了。正确的帖子可以在这里找到:你应该用问答格式代替文章格式。将问题分为问题部分和答案部分,回答你的问题并接受它。在问题部分给出更多细节,比如为什么这么难或者什么会导致错误,并确保它不是其他问题的重复。谢谢你的帖子。正如Sajan points所建议的,请将其编辑为一个问题,然后将解决方案添加为答案,这样可以使人们更容易找到它,并理解您发布的内容是一个解决方案。例如,您可以发布类似“如何从NumPy数组文件构建Keras模型?”的内容,以及类似“我有一个包含NumPy数组文件的目录,如
bias1.npy
kernel1.npy
bias2.npy
,等等。如何构建使用这些数组作为权重的Keras模型?”谢谢。我已经改了。正确的帖子可以在这里找到: