天天看點

keras 11_加載模型

keras 11_加載模型

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.optimizers import SGD

# 載入資料
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("x_train.shape:", x_train.shape, "y_train.shape:",
      y_train.shape)  # x_train.shape: (60000, 28, 28) y_train.shape: (60000,)

# 将(60000,28,28)轉換為(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1) / 255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0

# 将輸出資料标簽轉換為one-hot編碼的格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 載入模型
model = load_model("mnist.h5")

# 評估模型
loss,accuracy = model.evaluate(x_test, y_test)

print("loss:",loss,"accuracy:",accuracy)
           

繼續閱讀