zoukankan      html  css  js  c++  java
  • 11.模型载入

    1 import numpy as np
    2 from keras.datasets import mnist
    3 from keras.utils import np_utils
    4 from keras.models import Sequential
    5 from keras.layers import Dense
    6 from keras.optimizers import SGD
    7 from keras.models import load_model
     1 # 载入数据
     2 (x_train,y_train),(x_test,y_test) = mnist.load_data()
     3 # (60000,28,28)
     4 print('x_shape:',x_train.shape)
     5 # (60000)
     6 print('y_shape:',y_train.shape)
     7 # (60000,28,28)->(60000,784)
     8 x_train = x_train.reshape(x_train.shape[0],-1)/255.0
     9 x_test = x_test.reshape(x_test.shape[0],-1)/255.0
    10 # 换one hot格式
    11 y_train = np_utils.to_categorical(y_train,num_classes=10)
    12 y_test = np_utils.to_categorical(y_test,num_classes=10)
    13 
    14 # 载入模型
    15 model = load_model('model.h5')
    16 
    17 # 评估模型
    18 loss,accuracy = model.evaluate(x_test,y_test)
    19 
    20 print('
    test loss',loss)
    21 print('accuracy',accuracy)

    # 训练模型
    model.fit(x_train,y_train,batch_size=64,epochs=2)
    
    # 评估模型
    loss,accuracy = model.evaluate(x_test,y_test)
    
    print('
    test loss',loss)
    print('accuracy',accuracy)

    # 保存参数,载入参数
    model.save_weights('my_model_weights.h5')
    model.load_weights('my_model_weights.h5')
    # 保存网络结构,载入网络结构
    from keras.models import model_from_json
    json_string = model.to_json()
    model = model_from_json(json_string)
    print(json_string)

  • 相关阅读:
    构造并判断二叉搜索树-js
    构造树并判断是否对称
    旋转图像
    螺旋矩阵
    链表实现快速排序
    任务调度器
    队列的实现
    最大矩形
    棒球比赛
    复原IP地址
  • 原文地址:https://www.cnblogs.com/liuwenhua/p/11567043.html
Copyright © 2011-2022 走看看