• 8.优化器


    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,Adam
     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 # 创建模型,输入784个神经元,输出10个神经元
    15 model = Sequential([
    16         Dense(units=10,input_dim=784,bias_initializer='one',activation='softmax')
    17     ])
    18 
    19 # 定义优化器
    20 sgd = SGD(lr=0.2)
    21 adam = Adam(lr=0.001) 
    22 
    23 # 定义优化器,loss function,训练过程中计算准确率
    24 model.compile(
    25     optimizer = adam,
    26     loss = 'categorical_crossentropy',
    27     metrics=['accuracy'],
    28 )
    29 
    30 # 训练模型
    31 model.fit(x_train,y_train,batch_size=32,epochs=10)
    32 
    33 # 评估模型
    34 loss,accuracy = model.evaluate(x_test,y_test)
    35 
    36 print('
    test loss',loss)
    37 print('accuracy',accuracy)

  • 相关阅读:
    Linux-1-用户管理
    接口相关资料整理
    JPype1使用总结
    1.django项目的创建(在CMD中)
    公有云、私有云和混合云的区别
    接口测试1.测试用例要点与模板
    PostMan Test 的脚本scripts编写方法
    Tensorflow RNN中的坑
    2019年终总结
    tensorflow中一种融合多个模型的方法
  • 原文地址:https://www.cnblogs.com/liuwenhua/p/11566991.html
Copyright © 2020-2023  润新知