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)
    
    trainX = trainX.astype('float32')
    testX = testX.astype('float32')
    trainX /= 255.0
    testX /= 255.0
    
    trainY = keras.utils.to_categorical(trainY, num_classes)
    testY = keras.utils.to_categorical(testY, num_classes)
    # 2. 定义模型。
    # 定义两个输入。
    input1 = Input(shape=(784,), name = "input1")
    input2 = Input(shape=(10,), name = "input2")
    
    # 定义第一个输出。
    x = Dense(1, activation='relu')(input1)
    output1 = Dense(10, activation='softmax', name = "output1")(x)
    
    # 定义第二个输出。
    y = keras.layers.concatenate([x, input2])
    output2 = Dense(10, activation='softmax', name = "output2")(y)
    
    model = Model(inputs=[input1, input2], outputs=[output1, output2])
    
    # 定义损失函数、优化函数和评测方法。
    model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(),loss_weights = [1, 0.1],metrics=['accuracy'])
    # 3. 模型训练。
    model.fit([trainX, trainY], [trainY, trainY],batch_size=128,epochs=20,validation_data=([testX, testY], [testY, testY]))

  • 相关阅读:
    Ubuntu 或 UbuntuKyLin14.04 Unity桌面側边栏和顶层菜单条显示异常解决方法
    关于程序猿的几个阶段!
    独立开发人员低成本推广APP的18条技巧
    Effective C++ 条款27
    OpensStack instance debug
    OpenStackCLI调试及术语识记
    OpenStack术语名词及问题调试
    apacheOfbiz
    obiz
    How to run OFBiz as a Service on linux
  • 原文地址:https://www.cnblogs.com/tszr/p/12096581.html
Copyright © 2011-2022 走看看