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})
当我运行上面的代码时,当我试图加载模型时,我一直得到这个错误:ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
我有一个约束使用numpy==1.18.1,所以我不能升级tensorflow,无论如何有解决这个问题或某种变通方法?谢谢。
修改代码为:
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