TensorFlow无法保存缺少自变量的模型

时间:2019-04-27 17:23:09

标签: python tensorflow keras

请帮助我进行以下操作。我似乎无法保存模型。如您所见,我确实引用了Sequential()方法的实例

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=[timePortion,1 ]))
model.add(tf.keras.layers.Conv1D(kernel_size=timePortion,
                                 filters=1000, 
                                 strides=1,
                                 use_bias=False,
                                 activation="relu",
                                 kernel_initializer=tf.keras.initializers.VarianceScaling))
model.summary()
model.add(tf.keras.layers.Dropout(rate=0.2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(32,
                                activation='relu', 
                                kernel_initializer=tf.keras.initializers.VarianceScaling))
model.add(tf.keras.layers.Dense(8,
                                activation='relu', 
                                kernel_initializer=tf.keras.initializers.VarianceScaling))
model.add(tf.keras.layers.Dense(1, 
                                kernel_initializer=tf.keras.initializers.VarianceScaling))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),
              loss="mean_squared_error", 
              metrics=["accuracy","mae"])
filepath = "model.h5"
model.fit(inputs,
          labels,
          steps_per_epoch=1,
          epochs=2,
          shuffle=False, 
          verbose=1)
tf.keras.models.save_model(model,
                           filepath, 
                           overwrite=True,
                           include_optimizer=True)

我在将模型保存到Jupyter笔记本电脑时遇到问题。该文件实际上已创建,但随后出现此错误。这很奇怪,因为我正在引用模型实例。

TypeError: get_config() missing 1 required positional argument: 'self'

1 个答案:

答案 0 :(得分:1)

问题在于内核初始化程序无法序列化,因为您尚未实例化它。要实例化它,请添加圆括号()

kernel_initializer=tf.keras.initializers.VarianceScaling()