python加载模型包占用内存多大,python - 在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})

当我运行上面的代码时,尝试加载模型时ValueError: Attempt to convert a value (None) with an unsupported type () to a Tensor.出现此错误: ValueError: Attempt to convert a value (None) with an unsupported type () to a Tensor.

我有使用numpy == 1.18.1的约束,所以我无法升级tensorflow,是否有解决此问题的方法或某种解决方法? 谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值