3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

R言語でもディープラーニングがしたい!

Posted at

#nnetを使う

library(nnet)
#irisをtestとtrainに分ける

samples <- sample(1:150,75)
iris.train <- iris[samples,]
iris.test  <- iris[-samples,1:4]
actual_species <- iris[-samples,5]

iris.nnet <- nnet(Species~., size=3, decay=0.1,maxit=100,data=iris.train)
iris.nnet

結果は

a 4-3-3 network with 27 weights
inputs: Sepal.Length Sepal.Width Petal.Length Petal.Width 
output(s): Species 
options were - softmax modelling  decay=0.1

モデルのオプションは、
sizeが中間層
decayが過学習抑制のための値
maxitが繰り返し回数

作ったモデルを図で確認する

library(NeuralNetTools)
plotnet(iris.nnet)

4444433.png

#予測

予測値は本当にあっているのか比較して確かめる

pred_species <- round(predict(iris.nnet,iris.test))
colSums(pred_species)
table(actual_species)

maxitを10回で実行したとき

nnet(Species~., size=3, decay=0.1,maxit=10,data=iris.train)
# weights:  27
initial  value 74.140168 
iter  10 value 36.933697
final  value 36.933697 
stopped after 10 iterations

colSums(pred_species)
    setosa versicolor  virginica 
        28         37          6 
table(actual_species)
actual_species
    setosa versicolor  virginica 
        27         16         32 

100回のモデル

nnet(Species~., size=3, decay=0.1,maxit=100,data=iris.train)
# weights:  27
initial  value 125.135244 
iter  10 value 68.201740
iter  20 value 41.303200
iter  30 value 19.560457
iter  40 value 18.588992
iter  50 value 16.462453
iter  60 value 16.399883
final  value 16.399882 
converged

colSums(pred_species)
    setosa versicolor  virginica 
        27         14         33 
table(actual_species)
actual_species
    setosa versicolor  virginica 
        27         16         32 

60回くらいで収束してしまう。
一問missなので、なかなか良いモデルなのでは?

#以上

ついでにneuralnetのパッケージも使ってみます
モデルの作成と図にする

library("neuralnet")

network = neuralnet(Species~., hidden=5, err.fct = "ce", linear.output = FALSE,data=iris.train)
plot(network)

66665.png

pred_neuralnet <- round(predict(network, iris.test))
pred_neuralnet <- data.frame(setosa=pred_neuralnet[,1],versicolor=pred_neuralnet[,2],virginica=pred_neuralnet[,3])

colSums(pred_neuralnet)
table(actual_species)

結果の確認

colSums(pred_neuralnet)
    setosa versicolor  virginica 
        27         14         34 
table(actual_species)
actual_species
    setosa versicolor  virginica 
        27         16         32 

こちらもそこそこ予測できていそう。

#今回の参照サイト

https://logics-of-blue.com/time-series-analysis-by-nnet/

https://www1.doshisha.ac.jp/~mjin/R/Chap_23/23.html
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?