• keras第一课:Cifar分类


    from keras.datasets import cifar10  #keras自带mnist/cifar10/cifar100等数据集的演示功能
    import numpy as np
    #np.random.seed(10)  #随机数

    数据准备

    (x_img_train,y_label_train),(x_img_test,y_label_test)=cifar10.load_data()   

    x_img_train_normalize = x_img_train.astype('float32') / 255.0  #数据类型转换,然后归一化
    x_img_test_normalize = x_img_test.astype('float32') / 255.0

    from keras.utils import np_utils
    y_label_train_OneHot = np_utils.to_categorical(y_label_train)  #OneHot 编码
    y_label_test_OneHot = np_utils.to_categorical(y_label_test)

    建立模型

    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Activation, Flatten
    from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D

    model = Sequential()

    #卷积层

    model.add(Conv2D(filters=32,kernel_size=(3, 3),input_shape=(32, 32,3),activation='relu', padding='same'))
    model.add(Dropout(0.3))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    #卷积层

    model.add(Conv2D(filters=64, kernel_size=(3, 3),activation='relu', padding='same'))
    model.add(Dropout(0.3))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    #节点数据展开

    model.add(Flatten())
    model.add(Dropout(0.3))

    #全连接层,隐藏层

    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.3))

    #全连接层,输出层

    model.add(Dense(10, activation='softmax'))

    训练模型

    model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])

    train_history=model.fit(x_img_train_normalize, y_label_train_OneHot,validation_split=0.2,epochs=10, batch_size=128, verbose=1)

    评估模型的准确率

    scores = model.evaluate(x_img_test_normalize, y_label_test_OneHot, verbose=0)

    进行预测

    prediction=model.predict_classes(x_img_test_normalize)

    简单体会:keras很好用,一目了然。但是里面的参数怎么选择?这个还需要深入学习。

  • 相关阅读:
    yii2框架随笔9
    yii2源码学习笔记(五)
    yii2源码学习笔记(四)
    yii2源码学习笔记(三)
    yii2源码学习笔记(二)
    yii2源码学习笔记
    学习yii2.0框架阅读代码(一)
    (转)OAuth 2.0授权协议详解和流程
    (转)JavaScript 中对变量和函数声明的“提前(hoist)”
    JavaScript 中的算术运算
  • 原文地址:https://www.cnblogs.com/4c4853/p/9672574.html
Copyright © 2020-2023  润新知