<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # R 中的 Keras API 我們在第 3 章中了解了 Keras API。在 R 中,此 API 使用`keras` R 包實現。 `keras` R 軟件包實現了 Keras Python 接口的大部分功能,包括順序 API 和函數式 API。 作為示例,我們提供了 MLP 模型的演練,用于在以下鏈接中對來自 MNIST 數據集的手寫數字進行分類: [https://keras.rstudio.com/articles/examples/mnist_mlp.html](https://keras.rstudio.com/articles/examples/mnist_mlp.html) 。 您可以按照 Jupyter R 筆記本中的代碼`ch-17c_Keras_in_R`。 1. 首先,加載庫: ```r library(keras) ``` 1. 定義超參數: ```r batch_size <- 128 num_classes <- 10 epochs <- 30 ``` 1. 準備數據: ```r # The data, shuffled and split between train and test sets c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist() x_train <- array_reshape(x_train, c(nrow(x_train), 784)) x_test <- array_reshape(x_test, c(nrow(x_test), 784)) # Transform RGB values into [0,1] range x_train <- x_train / 255 x_test <- x_test / 255 cat(nrow(x_train), 'train samples\n') cat(nrow(x_test), 'test samples\n') # Convert class vectors to binary class matrices y_train <- to_categorical(y_train, num_classes) y_test <- to_categorical(y_test, num_classes) ``` 注釋是不言自明的:數據從 Keras 數據集庫加載,然后轉換為 2D arrray 并歸一化為[0,1]范圍。 1. 定義模型: ```r model <- keras_model_sequential() model %>% layer_dense(units=256,activation='relu',input_shape=c(784)) %>% layer_dropout(rate = 0.4) %>% layer_dense(units = 128, activation = 'relu') %>% layer_dropout(rate = 0.3) %>% layer_dense(units = 10, activation = 'softmax') summary(model) model %>% compile( loss = 'categorical_crossentropy', optimizer = optimizer_rmsprop(), metrics = c('accuracy') ) ``` 1. 定義和編譯順序模型。我們得到的模型定義如下: ```r _____________________________________________________ Layer (type) Output Shape Param # ===================================================== dense_26 (Dense) (None, 256) 200960 _____________________________________________________ dropout_14 (Dropout) (None, 256) 0 _____________________________________________________ dense_27 (Dense) (None, 128) 32896 _____________________________________________________ dropout_15 (Dropout) (None, 128) 0 _____________________________________________________ dense_28 (Dense) (None, 10) 1290 ===================================================== Total params: 235,146 Trainable params: 235,146 Non-trainable params: 0 ``` 1. 訓練模型: ```r history <- model %>% fit( x_train, y_train, batch_size = batch_size, epochs = epochs, verbose = 1, validation_split = 0.2 ) plot(history) ``` 擬合函數的輸出存儲在歷史對象中,其包含來自訓練周期的損失和度量值。繪制歷史對象中的數據,結果如下: ![](https://img.kancloud.cn/93/f3/93f3dc15b5dbaf685aa13d65bcdd33f4_840x840.png)Training and Validation Accuracy (y-axis) in Epochs (x-axis) 1. 評估模型: ```r score <- model %>% evaluate( x_test, y_test, verbose = 0 ) # Output metrics cat('Test loss:', score[[1]], '\n') cat('Test accuracy:', score[[2]], '\n') ``` 輸出如下: ```r Test loss: 0.1128517 Test accuracy: 0.9816 ``` 太酷!! 在以下鏈接中查找更多關于 Keras in R 的示例:[https://keras.rstudio.com/articles/examples/index.html ](https://keras.rstudio.com/articles/examples/index.html) 有關 Keras R 軟件包的更多文檔可在以下鏈接中找到:[https://keras.rstudio.com/reference/index.html](https://keras.rstudio.com/reference/index.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看