Showing posts with label Deep learning. Show all posts
Showing posts with label Deep learning. Show all posts

Saturday, March 2, 2019

Deep learning

Deep learning is a subject of importance right from image detection to speech recognition and AI-related activity

> library(neuralnet)
> library(ISLR)
Error in library(ISLR) : there is no package called ‘ISLR’
> install.packages("ISLR")
> library(ISLR)
Warning message:
package ‘ISLR’ was built under R version 3.5.2
> library(ISLR)
> data = College

Image detection

> View(data)
>
> max_data <- apply(data[,2:18], 2, max)
> min_data <- apply(data[,2:18], 2, min)
> data_scaled <- scale(data[,2:18],center = min_data, scale = max_data - min_data)
>
> Private = as.numeric(College$Private)-1
> data_scaled = cbind(Private,data_scaled)
> index = sample(1:nrow(data),round(0.70*nrow(data)))
>
> train_data <- as.data.frame(data_scaled[index,])
> test_data <- as.data.frame(data_scaled[-index,])
>
> n = names(train_data)
>
> f <- as.formula(paste("Private ~", paste(n[!n %in% "Private"], collapse = " + ")))
>
> deep_net = neuralnet(f,data=train_data,hidden=c(5,3),linear.output=F)
> plot(deep_net)
deep learning,machine learning,what is deep learning,deep learning tutorial,learning,deep learning ai,deep learning python,deep learning training,introduction to deep learning,deep learning tutorial for beginners,mit deep learning,deep learning nlp,udemy deep learning,mario deep learning,caffe deep learning,learn deep learning,python deep learning,o que é deep learning,deep learning matlab

> predicted_data <- compute(deep_net,test_data[,2:18])
>
> print(head(predicted_data$net.result))
                          [,1]
Alaska Pacific University    1
Albertson College            1
Allegheny College            1
Amherst College              1
Andrews University           1
Antioch University           1
> predicted_data$net.result <- sapply(predicted_data$net.result,round,digits=0)
> table(test_data$Private,predicted_data$net.result)
 
      0   1
  0  54  10
  1  10 159
> install.packages("h2o")
Installing package into ‘C:/Users/ADMIN/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/h2o_3.22.1.1.zip'
Content type 'application/zip' length 122132920 bytes (116.5 MB)
downloaded 116.5 MB

package ‘h2o’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\ADMIN\AppData\Local\Temp\Rtmpof4ihy\downloaded_packages
> library(h2o)

----------------------------------------------------------------------

Your next step is to start H2O:
    > h2o.init()

For H2O package documentation, ask for help:
    > ??h2o

After starting H2O, you can use the Web UI at http://localhost:54321
For more information visit http://docs.h2o.ai

----------------------------------------------------------------------


Attaching package: ‘h2o’

The following objects are masked from ‘package:stats’:

    cor, sd, var

The following objects are masked from ‘package:base’:

    %*%, %in%, &&, ||, apply, as.factor,
    as.numeric, colnames, colnames<-, ifelse,
    is.character, is.factor, is.numeric, log,
    log10, log1p, log2, round, signif, trunc

Warning message:
package ‘h2o’ was built under R version 3.5.2
> library(h2o)
> c1=h2o.init(max_mem_size = "2G",
+             nthreads = 2,
+             ip = "localhost",
+             port = 54321)

H2O is not running yet, starting it now...

Note:  In case of errors look at the following log files:
    C:\Users\ADMIN\AppData\Local\Temp\Rtmpof4ihy/h2o_ADMIN_started_from_r.out
    C:\Users\ADMIN\AppData\Local\Temp\Rtmpof4ihy/h2o_ADMIN_started_from_r.err

java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Starting H2O JVM and connecting: .. Connection successful!

R is connected to the H2O cluster:
    H2O cluster uptime:         8 seconds 865 milliseconds
    H2O cluster timezone:       Asia/Kolkata
    H2O data parsing timezone:  UTC
    H2O cluster version:        3.22.1.1
    H2O cluster version age:    2 months and 1 day
    H2O cluster name:           H2O_started_from_R_ADMIN_unb933
    H2O cluster total nodes:    1
    H2O cluster total memory:   1.78 GB
    H2O cluster total cores:    2
    H2O cluster allowed cores:  2
    H2O cluster healthy:        TRUE
    H2O Connection ip:          localhost
    H2O Connection port:        54321
    H2O Connection proxy:       NA
    H2O Internal Security:      FALSE
    H2O API Extensions:         Algos, AutoML, Core V3, Core V4
    R Version:                  R version 3.5.0 (2018-04-23)

> data(iris)
> summary(iris)
  Sepal.Length       Sepal.Width        Petal.Length
 Min.   :4.300000   Min.   :2.000000   Min.   :1.000
 1st Qu.:5.100000   1st Qu.:2.800000   1st Qu.:1.600
 Median :5.800000   Median :3.000000   Median :4.350
 Mean   :5.843333   Mean   :3.057333   Mean   :3.758
 3rd Qu.:6.400000   3rd Qu.:3.300000   3rd Qu.:5.100
 Max.   :7.900000   Max.   :4.400000   Max.   :6.900
  Petal.Width             Species
 Min.   :0.100000   setosa    :50
 1st Qu.:0.300000   versicolor:50
 Median :1.300000   virginica :50
 Mean   :1.199333               
 3rd Qu.:1.800000               
 Max.   :2.500000               
> iris_d1 <- h2o.deeplearning(1:4,5,
+                             as.h2o(iris),hidden=c(5,5),
+                             export_weights_and_biases=T)
  |==============================================| 100%
  |==============================================| 100%
>
> iris_d1
Model Details:
==============

H2OMultinomialModel: deeplearning
Model ID:  DeepLearning_model_R_1551499097956_1
Status of Neuron Layers: predicting Species, 3-class classification, multinomial distribution, CrossEntropy loss, 73 weights/biases, 4.6 KB, 1,500 training samples, mini-batch size 1
  layer units      type dropout       l1       l2
1     1     4     Input  0.00 %       NA       NA
2     2     5 Rectifier  0.00 % 0.000000 0.000000
3     3     5 Rectifier  0.00 % 0.000000 0.000000
4     4     3   Softmax      NA 0.000000 0.000000
  mean_rate rate_rms momentum mean_weight weight_rms
1        NA       NA       NA          NA         NA
2  0.003087 0.003858 0.000000    0.055145   0.563931
3  0.001105 0.000828 0.000000    0.148419   0.333557
4  0.001324 0.000642 0.000000   -0.028787   1.654691
  mean_bias bias_rms
1        NA       NA
2  0.852716 0.826724
3  1.062501 0.273368
4  0.001277 0.103763


H2OMultinomialMetrics: deeplearning
** Reported on training data. **
** Metrics reported on full training frame **

Training Set Metrics:
=====================

Extract training frame with `h2o.getFrame("iris_sid_a830_1")`
MSE: (Extract with `h2o.mse`) 0.06891309264
RMSE: (Extract with `h2o.rmse`) 0.2625130333
Logloss: (Extract with `h2o.logloss`) 0.2637877632
Mean Per-Class Error: 0.07333333333
Confusion Matrix: Extract with `h2o.confusionMatrix(<model>,train = TRUE)`)
=========================================================================
Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
           setosa versicolor virginica  Error
setosa         50          0         0 0.0000
versicolor      7         40         3 0.2000
virginica       0          1        49 0.0200
Totals         57         41        52 0.0733
                 Rate
setosa     =   0 / 50
versicolor =  10 / 50
virginica  =   1 / 50
Totals     = 11 / 150

Hit Ratio Table: Extract with `h2o.hit_ratio_table(<model>,train = TRUE)`
=======================================================================
Top-3 Hit Ratios:
  k hit_ratio
1 1  0.926667
2 2  1.000000
3 3  1.000000
> plot(iris_d1)
>
> h2o.weights(iris_d1, matrix_id=1)
   Sepal.Length    Sepal.Width  Petal.Length
1  0.2409946620  0.35679787397 -1.1170697212
2  0.6530707479  0.67512649298  0.2999281287
3 -0.1507256031  0.44363185763  0.5350508094
4  0.6421241760  0.03761408478 -0.1086364165
5 -0.2883346975 -0.27492222190  0.1764629483
    Petal.Width
1 -1.4844481945
2  0.2284315377
3  0.7389914393
4  0.0453807041
5 -0.5465673208

[5 rows x 4 columns]
> h2o.weights(iris_d1, matrix_id=2)
             C1             C2            C3
1 -0.2234841734 -0.01124510914 0.43649563193
2 -0.1895816028  0.21325439215 0.79128360748
3 -0.3035679460 -0.55753493309 0.48393419385
4  0.1758618206 -0.35445415974 0.36051633954
5  0.3964880109  0.23581361771 0.04750946537
             C4             C5
1 0.58218020201  0.02807313576
2 0.08547680080 -0.19191685319
3 0.45826247334  0.19889642298
4 0.08671515435  0.68862718344
5 0.20328296721  0.06957834214

[5 rows x 5 columns]
> h2o.weights(iris_d1, matrix_id=3)
             C1           C2           C3            C4
1 -0.6639142036 -2.032027721 -2.748629332  0.7574009895
2 -0.6556437612 -0.368837446 -1.755167603  0.5674321651
3  2.5461874008 -1.720250487  2.794062376 -1.1157760620
            C5
1 1.7997418642
2 1.4643006325
3 0.6993095279

[3 rows x 5 columns]
> h2o.biases(iris_d1, vector_id=1)
            C1
1 2.4911530990
2 0.6355656164
3 0.4266453373
4 0.3115003118
5 0.3987136025

[5 rows x 1 column]
> h2o.biases(iris_d1, vector_id=2)
            C1
1 0.7540707757
2 1.1464952188
3 0.7208821239
4 1.2750280645
5 1.4160310664

[5 rows x 1 column]
> h2o.biases(iris_d1, vector_id=3)
              C1
1  0.09313390534
2  0.05446989759
3 -0.14377270651

[3 rows x 1 column]
>
> plot(as.data.frame(h2o.weights(iris_d1, matrix_id=1))[,1])

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...