gpt4 book ai didi

python - Tensorflow 和 Keras 无法加载 .ckpt 保存

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:53 25 4
gpt4 key购买 nike

所以我使用 ModelCheckpoint 回调来保存我正在训练的模型的最佳时期。它保存没有错误,但是当我尝试加载它时,出现错误:

2019-07-27 22:58:04.713951: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open C:\Users\Riley\PycharmProjects\myNN\cp.ckpt: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?

我试过使用绝对/完整路径,但没有成功。我确定我可以使用 EarlyStopping,但我仍然想了解为什么会出现错误。这是我的代码:

from __future__ import absolute_import, division, print_function

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import datetime
import statistics

(train_images, train_labels), (test_images, test_labels) = np.load("dataset.npy", allow_pickle=True)

train_images = train_images / 255
test_images = test_images / 255

train_labels = list(map(float, train_labels))
test_labels = list(map(float, test_labels))
train_labels = [i/10 for i in train_labels]
test_labels = [i/10 for i in test_labels]

'''
model = keras.Sequential([
keras.layers.Flatten(input_shape=(128, 128)),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(1)
])

'''

start_time = datetime.datetime.now()

model = keras.Sequential([
keras.layers.Conv2D(32, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=(128, 128, 1)),
keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
keras.layers.Dropout(0.2),
keras.layers.Conv2D(64, (5, 5), activation='relu'),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Dropout(0.2),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(1000, activation='relu'),
keras.layers.Dense(1)

])

model.compile(loss='mean_absolute_error',
optimizer=keras.optimizers.SGD(lr=0.01),
metrics=['mean_absolute_error', 'mean_squared_error'])

train_images = train_images.reshape(328, 128, 128, 1)
test_images = test_images.reshape(82, 128, 128, 1)

model.fit(train_images, train_labels, epochs=100, callbacks=[keras.callbacks.ModelCheckpoint("cp.ckpt", monitor='mean_absolute_error', save_best_only=True, verbose=1)])

model.load_weights("cp.ckpt")

predictions = model.predict(test_images)

totalDifference = 0
for i in range(82):
print("%s: %s" % (test_labels[i] * 10, predictions[i] * 10))
totalDifference += abs(test_labels[i] - predictions[i])

avgDifference = totalDifference / 8.2

print("\n%s\n" % avgDifference)
print("Time Elapsed:")
print(datetime.datetime.now() - start_time)

最佳答案

TLDR;您正在保存整个模型,同时尝试仅加载权重,这不是它的工作方式。

解释

您的模型的 fit :

model.fit(
train_images,
train_labels,
epochs=100,
callbacks=[
keras.callbacks.ModelCheckpoint(
"cp.ckpt", monitor="mean_absolute_error", save_best_only=True, verbose=1
)
],
)

作为save_weights=False默认为 ModelCheckpoint ,您正在将整个模型保存到 .ckpt .

顺便说一句。文件应命名为 .hdf5.hf5因为它是 Hierarchical Data Format 5 .由于 Windows 不是扩展不可知的,如果 tensorflow,您可能会遇到一些问题。/keras依赖于此操作系统上的扩展。

另一方面,您只加载模型的权重,而文件包含整个模型:

model.load_weights("cp.ckpt")

Tensorflow 的检查点 ( .cp ) 机制不同于 Keras 的 ( .hdf5 ),因此请注意这一点(有计划将它们更紧密地集成,请参阅 herehere)。

解决方案

因此,要么像现在一样使用回调,但是使用model.load("model.hdf5")或添加 save_weights_only=True ModelCheckpoint 的参数:

model.fit(
train_images,
train_labels,
epochs=100,
callbacks=[
keras.callbacks.ModelCheckpoint(
"weights.hdf5",
monitor="mean_absolute_error",
save_best_only=True,
verbose=1,
save_weights_only=True, # Specify this
)
],
)

你可以使用你的 model.load_weights("weights.hdf5") .

关于python - Tensorflow 和 Keras 无法加载 .ckpt 保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237671/

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