<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【小白學(xué)習(xí)keras教程】二、基于CIFAR-10數(shù)據(jù)集訓(xùn)練簡(jiǎn)單的MLP分類模型

          共 6718字,需瀏覽 14分鐘

           ·

          2021-09-04 15:30

          「@Author:Runsen」

          分類任務(wù)的MLP

          • 當(dāng)目標(biāo)(「y」)是離散的(分類的)
          • 對(duì)于損失函數(shù),使用交叉熵;對(duì)于評(píng)估指標(biāo),通常使用accuracy

          數(shù)據(jù)集描述

          • CIFAR-10數(shù)據(jù)集包含10個(gè)類中的60000個(gè)圖像—50000個(gè)用于培訓(xùn),10000個(gè)用于測(cè)試
          • 有關(guān)更多信息,請(qǐng)參閱官方文檔
          from tensorflow.keras.datasets import cifar10
          from tensorflow.keras.utils import to_categorical
          # load data and flatten X data to fit into MLP
          (x_train, y_train), (x_test, y_test) = cifar10.load_data()
          x_train, x_test = x_train.reshape(x_train.shape[0], -1), x_test.reshape(x_test.shape[0], -1)
          y_train, y_test = to_categorical(y_train), to_categorical(y_test)
          print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

          1.創(chuàng)建模型

          • 與回歸模型相同-使用Sequentia()
          model = Sequential()

          1-1.添加層

          • Keras層可以「添加」到模型中
          • 添加層就像一個(gè)接一個(gè)地堆疊樂(lè)高積木
          • 應(yīng)注意的是,由于這是一個(gè)分類問(wèn)題,應(yīng)添加sigmoid層(針對(duì)多類問(wèn)題的softmax)
          • 文檔:https://keras.io/layers/core/
          # Keras model with two hidden layer with 10 neurons each 
          model.add(Dense(50, input_shape = (x_train.shape[-1],)))    # Input layer => input_shape should be explicitly designated
          model.add(Activation('sigmoid'))
          model.add(Dense(50))                         # Hidden layer => only output dimension should be designated
          model.add(Activation('sigmoid'))
          model.add(Dense(50))                         # Hidden layer => only output dimension should be designated
          model.add(Activation('sigmoid'))
          model.add(Dense(10))                          # Output layer => output dimension = 1 since it is regression problem
          model.add(Activation('sigmoid'))
          # This is equivalent to the above code block
          model.add(Dense(50, input_shape = (x_train.shape[-1],), activation = 'sigmoid'))
          model.add(Dense(50, activation = 'sigmoid'))
          model.add(Dense(50, activation = 'sigmoid'))
          model.add(Dense(10, activation = 'sigmoid'))

          1-2.模型編譯

          • Keras模型應(yīng)在培訓(xùn)前“編譯”
          • 應(yīng)指定損失類型(函數(shù))和優(yōu)化器
          • 文檔(優(yōu)化器):https://keras.io/optimizers/
          • 文檔(損失):https://keras.io/losses/
          from tensorflow.keras import optimizers
          sgd = optimizers.SGD(lr = 0.01)    # stochastic gradient descent optimizer
          model.compile(optimizer = sgd, loss = 'categorical_crossentropy', metrics = ['accuracy'])

          模型摘要

          model.summary()
          Model: "sequential"
          _________________________________________________________________
          Layer (type)                 Output Shape              Param #   
          =================================================================
          dense (Dense)                (None50)                153650    
          _________________________________________________________________
          activation (Activation)      (None50)                0         
          _________________________________________________________________
          dense_1 (Dense)              (None50)                2550      
          _________________________________________________________________
          activation_1 (Activation)    (None50)                0         
          _________________________________________________________________
          dense_2 (Dense)              (None50)                2550      
          _________________________________________________________________
          activation_2 (Activation)    (None50)                0         
          _________________________________________________________________
          dense_3 (Dense)              (None10)                510       
          _________________________________________________________________
          activation_3 (Activation)    (None10)                0         
          =================================================================
          Total params: 159,260
          Trainable params: 159,260
          Non-trainable params: 0
          _________________________________________________________________

          2.訓(xùn)練

          • 使用提供的訓(xùn)練數(shù)據(jù)訓(xùn)練模型
          model.fit(x_train, y_train, batch_size = 128, epochs = 50, verbose = 1)

          3.評(píng)估

          • Keras模型可以用evaluate()函數(shù)計(jì)算
          • 文檔:https://keras.io/metrics/
          results = model.evaluate(x_test, y_test)
          在這里插入圖片描述
          print(model.metrics_names)     # list of metric names the model is employing
          print(results)                 # actual figure of metrics computed
          瀏覽 106
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91影院理论推荐手机在线观看 | 天天操天天天 | 国产精品18欠久久久久久 | 九九九九精品视频 | 在线视频精品播放 |