• 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)

  • 相关阅读:
    Git配置SSH访问GitHub
    vue 관련
    node
    关于CheckBox和EditText在ListView里多布局的处理
    百度地图定位
    java常用简单正则表达式写法
    Android二维码开源项目zxing编译
    Andrew XUtils的session获得和cookieStore使用
    常用易忘知识点
    替换Fragment 报错 The specified child already has a parent. You must call removeView()
  • 原文地址:https://www.cnblogs.com/liuwenhua/p/11567043.html
Copyright © 2020-2023  润新知