当前位置:网站首页>6 custom layer

6 custom layer

2022-06-26 15:58:00 X1996_

The custom layer name should not be the same as the self-contained layer name

from sklearn import datasets
import tensorflow as tf
import numpy as np

iris = datasets.load_iris()
data = iris.data
labels = iris.target

#  Define a full connectivity layer 
class MyDense(tf.keras.layers.Layer):
    def __init__(self, units=32, **kwargs):
        self.units = units
        super(MyDense, self).__init__(**kwargs)
    
    # build Methods are generally defined as Layer Parameters that need to be trained 
    # trainable=True  Get involved in training  False  Don't take part in training 
    # name Need to name , Otherwise, an error will occur when saving the model 
    def build(self, input_shape): 
        self.w = self.add_weight(shape=(input_shape[-1], self.units),
                                 initializer='random_normal',
                                 trainable=True,
                                 name='w')
        self.b = self.add_weight(shape=(self.units,),
                                 initializer='random_normal',
                                 trainable=True,
                                 name='b')
        super(MyDense,self).build(input_shape) #  It's equivalent to setting self.built = True
    
    #call Methods generally define forward propagation operation logic ,__call__ Method calls it . 
    def call(self, inputs): 
        return tf.matmul(inputs, self.w) + self.b
    
    # If you want a custom Layer adopt Functional API  When combined into a model, you can serialize , You need to customize it get_config Method .
    #  Model cannot be saved without definition 
    def get_config(self):  
        config = super(MyDense, self).get_config()
        config.update({
    'units': self.units})
        return config


#  Functional programming 
inputs = tf.keras.Input(shape=(4,))
x = MyDense(units=16)(inputs) #  The number of neurons is set to 16
x = tf.nn.tanh(x) #  The full connection layer is followed by an activation function 
x = tf.keras.layers.Dense(8)(x)
x = tf.nn.relu(x)
x = MyDense(units=3)(x) # Three categories 
predictions = tf.nn.softmax(x)
model = tf.keras.Model(inputs=inputs, outputs=predictions)

#  Upset 
data = np.concatenate((data,labels.reshape(150,1)),axis=-1)
np.random.shuffle(data)

labels = data[:,-1]
data = data[:,:4]

# Optimizer  Adam
# Loss function   Cross entropy loss function 
# Evaluation function  #acc
model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

#  Training 
model.fit(data, labels, batch_size=32, epochs=100,shuffle=True)

Show network structure

model.summary()

Save the model

model.save('keras_model_tf_version.h5')

Load model predictions

#  Add the custom layer name to the dictionary before loading the model 
#  Need to put MyDense The network can be defined only when it is written 
_custom_objects = {
    
    "MyDense" : MyDense
}

new_model = tf.keras.models.load_model("keras_model_tf_version.h5",custom_objects=_custom_objects)

y_pred = new_model.predict(data)
np.argmax(y_pred,axis=1)
原网站

版权声明
本文为[X1996_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261529517212.html