gpt4 book ai didi

tensorflow - Keras 模型保存错误 : TypeError: get_config() missing 1 required positional argument: 'self'

转载 作者:行者123 更新时间:2023-12-03 17:08:43 25 4
gpt4 key购买 nike

我正在运行 keras tensorflow 2.0 并试图创建一个模型,训练它,然后保存它。我的代码如下(简化):

    model = Model()

model.save("saved_models/model1", overwrite=True, save_format="tf")

在这里, Model()是一个子类 tf.keras.Model 的类并实现了 call(inputs)功能。

模型类(在下面请求)如下所示:

class Model(tf.keras.Model):
def __init__(self, loadFile: str = None):
super(Model, self).__init__()

#TODO: transfer from above into here V
self.conv1 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(10, 30),
strides=(1, 30),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)

self.pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
strides=(2, 1),
padding="same",
data_format="channels_last")

self.conv2 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(4, 1),
strides=(2, 1),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)

self.pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 1),
strides=(2, 1),
padding="same",
data_format="channels_last")

self.flattened = tf.keras.layers.Flatten(data_format="channels_last")

self.fcl1 = tf.keras.layers.Dense(units=500,
activation=tf.keras.activations.softmax,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)

self.fcl2 = tf.keras.layers.Dense(units=4,
activation=tf.keras.activations.softmax,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal,
bias_initializer=tf.keras.initializers.zeros)

if loadFile is not None:
self.load_weights(loadFile)

def call(self, inputs, training: bool = False):
conv1 = self.conv1(inputs)
pool1 = self.pool1(conv1)
conv2 = self.conv2(pool1)
pool2 = self.pool2(conv2)

flattened = self.flattened(pool2)
fcl1 = self.fcl1(flattened)
return self.fcl2(fcl1)

当我运行上述程序时,我收到以下警告:
W tensorflow/python/util/util.cc:280] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.

随后出现以下错误:
File "C:/Users/lavre/Desktop/Programming/Python/MyProject/neural_net/model.py", line 155, in <module>
model.save(value, overwrite=True, save_format="tf")
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1213, in save
saving.save_model(self, filepath, overwrite, include_optimizer, save_format)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\save.py", line 106, in save_model
saved_model.save(model, filepath, overwrite, include_optimizer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model.py", line 1492, in save
save_lib.save(model, filepath)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 849, in save
saveable_view, asset_info.asset_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 604, in _serialize_object_graph
_write_object_proto(obj, obj_proto, asset_file_def_index)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\saved_model\save.py", line 643, in _write_object_proto
metadata=obj._tracking_metadata)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2195, in _tracking_metadata
metadata['config'] = self.get_config()
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\layers\convolutional.py", line 252, in get_config
'kernel_initializer': initializers.serialize(self.kernel_initializer),
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\initializers.py", line 166, in serialize
return serialize_keras_object(initializer)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 140, in serialize_keras_object
instance.get_config())
TypeError: get_config() missing 1 required positional argument: 'self'

为什么我收到这个错误?还有第一个警告是什么意思?

最佳答案

更改您的初始化程序部分,您可以这样做:

self.conv1 = tf.keras.layers.Conv2D(filters=10,
kernel_size=(10, 30),
strides=(1, 30),
padding="same",
data_format="channels_last",
activation=tf.keras.activations.relu,
use_bias=True,
kernel_initializer=tf.keras.initializers.glorot_normal(),
bias_initializer=tf.keras.initializers.zeros())

像这样更改其他层,这对我的代码很有用。

关于tensorflow - Keras 模型保存错误 : TypeError: get_config() missing 1 required positional argument: 'self' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com