【R】KerasでDeep Learning

またまた、Kerasを使ってみます。とにかく、習うより慣れよの精神でどんどん数をこなしていこうと思います。

データは、渋谷駅前で働くデータサイエンティストのブログ様がまとめてくれたUCI ML Repositoryのオンラインニュース人気度をつかいます。そのもののデータをより使いやすくして修正していただいてあります。

library(tidyverse)
library(keras)

# データインポート
d_train <- read.csv('https://github.com/ozt-ca/tjo.hatenablog.samples/raw/master/r_samples/public_lib/jp/exp_uci_datasets/online_news_popularity/ONP_train.csv')
d_test <- read.csv('https://github.com/ozt-ca/tjo.hatenablog.samples/raw/master/r_samples/public_lib/jp/exp_uci_datasets/online_news_popularity/ONP_test.csv')

d_train.x <- as.matrix(d_train[,-59])
d_train.y <- as.matrix(d_train[,59])
d_test.x <- as.matrix(d_test[,-59])
d_test.y <- as.matrix(d_test[,59])

model <- keras_model_sequential()
model %>% layer_dense(units = 200, input_shape=58) %>%
  layer_activation(activation = 'sigmoid') %>%
  layer_dropout(rate = 0.2) %>%
  layer_dense(units = 50) %>%
  layer_activation(activation = 'sigmoid') %>%
  layer_dropout(rate = 0.2) %>%
  layer_dense(units = 44) %>%
  layer_activation(activation = 'sigmoid') %>%
  layer_dropout(rate = 0.2) %>%
  layer_dense(units = 1) %>%
  layer_activation(activation = 'linear') %>%
  compile(
    loss = 'mse',
    optimizer = optimizer_rmsprop(lr = 0.001)
  )

model %>% fit(d_train.x, d_train.y, epochs = 8, batch_size = 100)



> model %>% fit(d_train.x, d_train.y, epochs = 8, batch_size = 100)
Train on 34644 samples
Epoch 1/8
34644/34644 [==============================] - 2s 48us/sample - loss: 4.4122
Epoch 2/8
34644/34644 [==============================] - 1s 37us/sample - loss: 1.2599
Epoch 3/8
34644/34644 [==============================] - 2s 48us/sample - loss: 1.2174
Epoch 4/8
34644/34644 [==============================] - 1s 35us/sample - loss: 1.1671
Epoch 5/8
34644/34644 [==============================] - 1s 36us/sample - loss: 1.1364
Epoch 6/8
34644/34644 [==============================] - 1s 35us/sample - loss: 1.1372
Epoch 7/8
34644/34644 [==============================] - 1s 35us/sample - loss: 1.1243
Epoch 8/8
34644/34644 [==============================] - 1s 36us/sample - loss: 1.0993
pred_reg <- model %>% predict(d_test.x, batch_size=100)
library(Metrics)
rmse(pred_reg, d_test.y)
> rmse(pred_reg, d_test.y)
[1] 0.9412492

データをmaatrixにしないと、エラーになるところでつまずきました。。。

Add a Comment

メールアドレスが公開されることはありません。