zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然TensorFlow高层封装:Keras-返回值

    # 1. 数据预处理。
    import keras
    from keras.models import Model
    from keras.datasets import mnist
    from keras.layers import Input, Dense
    from tflearn.layers.core import fully_connected
    
    num_classes = 10
    img_rows, img_cols = 28, 28
     
    # 通过Keras封装好的API加载MNIST数据。
    (trainX, trainY), (testX, testY) = mnist.load_data()
    trainX = trainX.reshape(trainX.shape[0], img_rows * img_cols)
    testX = testX.reshape(testX.shape[0], img_rows * img_cols)
     
    # 将图像像素转化为0到1之间的实数。
    trainX = trainX.astype('float32')
    testX = testX.astype('float32')
    trainX /= 255.0
    testX /= 255.0
     
    # 将标准答案转化为需要的格式(one-hot编码)。
    trainY = keras.utils.to_categorical(trainY, num_classes)
    testY = keras.utils.to_categorical(testY, num_classes)
    # 2. 通过返回值的方式定义模型。
    inputs = Input(shape=(784,))
    
    x = Dense(500, activation='relu')(inputs)
    predictions = Dense(10, activation='softmax')(x)
    
    model = Model(inputs=inputs, outputs=predictions)
    model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(),metrics=['accuracy'])
    # 3. 训练模型。
    model.fit(trainX, trainY,batch_size=32,epochs=10,validation_data=(testX, testY))

     

  • 相关阅读:
    数据库事务的四大特性以及事务的隔离级别
    informer使用示例
    Linux内存、Swap、Cache、Buffer详细解析
    浏览器访问百度的整个过程
    安装zookeeper
    配置java环境
    promethues开发
    go mod常用操作说明
    redis使用基础
    channel的声明和使用
  • 原文地址:https://www.cnblogs.com/tszr/p/12096370.html
Copyright © 2011-2022 走看看