简体   繁体   English

在 tensorflow 2.0.1 中保存和加载带有自定义层的 model

[英]Saving and loading a model with custom layers in tensorflow 2.0.1

class SimpleDense(Layer):

def __init__(self, units=1, name='SimpleDense',  **kwargs):
    '''Initializes the instance attributes'''
    super(SimpleDense, self).__init__()
    self.units = units
    super(SimpleDense, self).__init__(**kwargs)

def build(self, input_shape):
    '''Create the state of the layer (weights)'''
    # initialize the weights
    w_init = tf.random_normal_initializer()
    self.w = tf.Variable(name="kernel",
                         initial_value=w_init(shape=(input_shape[-1], self.units),
                                              dtype='float32'),
                         trainable=True)

    # initialize the biases
    b_init = tf.zeros_initializer()
    self.b = tf.Variable(name="bias",
                         initial_value=b_init(shape=(self.units,), dtype='float32'),
                         trainable=True)

def call(self, inputs):
    '''Defines the computation from inputs to outputs'''
    return tf.matmul(inputs, self.w) + self.b

def get_config(self):
    config = super().get_config().copy()
    config.update({
        'units': self.units,
    })
    return config 

my_layer = SimpleDense(units=1)
model = tf.keras.Sequential([my_layer])

xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)


model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(xs, ys, epochs=500, verbose=1)
tf.keras.models.save_model(model, os.path.join(model_path, 'my_model'),    save_format='h5')

cls.model = tf.keras.models.load_model(model_path + "/my_model", custom_objects={'SimpleDense': SimpleDense})

When I run the code above I keep getting this error when I try to load the model: ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.当我运行上面的代码时,当我尝试加载 model: ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

I have a constraint for using numpy==1.18.1, so I can't upgrade tensorflow, is there anyway to solve this issue or some sort of workaround?我有使用 numpy==1.18.1 的限制,所以我无法升级 tensorflow,有没有办法解决这个问题或某种解决方法? Thanks.谢谢。

Change your code to:将您的代码更改为:

class SimpleDense(Layer):

    def __init__(self, units=1, name='SimpleDense',  **kwargs):
        '''Initializes the instance attributes'''
        super(SimpleDense, self).__init__(name=name,**kwargs)
        self.units = units
        

    def build(self, input_shape):
        '''Create the state of the layer (weights)'''
        # initialize the weights
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(name="kernel",
                            initial_value=w_init(shape=(input_shape[-1], self.units),
                                                dtype='float32'),
                            trainable=True)

        # initialize the biases
        b_init = tf.zeros_initializer()
        self.b = tf.Variable(name="bias",
                            initial_value=b_init(shape=(self.units,), dtype='float32'),
                            trainable=True)

    def call(self, inputs):
        '''Defines the computation from inputs to outputs'''
        return tf.matmul(inputs, self.w) + self.b

    def get_config(self):
        config = super(SimpleDense, self).get_config()
        config.update({
            'units': self.units,
        })
        return config 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM