Keras_demo

author: AGPC date: 2017-11-07 autosize: true

About this presentation

This presentation should be read after the ppt slides

You will see that Keras is like a toolbox.

Most tools were described in research papers and implemented in specific situation.

But given high resilience of the API, you can mix all types or layers and find new NN recipes.

Serendipity is indeed a frequent word in deep learning.

Install Keras for RStudio

install.packages("keras")
install_keras()

Error: Prerequisites for installing TensorFlow not available.

Please install the following Python packages before proceeding: pip, virtualenv

=> https://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/

Packages

[1] "check: knitr"
[1] "check: tidyverse"
[1] "check: keras"
[1] "check: plotly"
[1] "check: webshot"

Load data

load("house_price_data_for_keras.RData")
head(x_train)
     (Intercept)         Id MSSubClass MSZoningFV MSZoningRH MSZoningRL
1399           1 -0.0723789 -0.1678767          0          0          1
364            1 -1.3004471  2.4192855          0          0          0
269            1 -1.4131684 -0.6382698          0          0          0
243            1 -1.4440184 -0.1678767          0          0          0
626            1 -0.9895738 -0.8734664          0          0          1
345            1 -1.3229914  2.4192855          0          0          0
     MSZoningRM MSZoningNA LotFrontage     LotArea StreetPave StreetNA
1399          0          0 -0.39862212 -0.37633009          1        0
364           1          0 -2.06922221 -1.07621631          1        0
269           1          0  0.07257278 -0.41436739          1        0
243           1          0 -0.27011442 -0.65527025          1        0
626           0          0  0.75794718 -0.02131535          1        0
345           1          0 -1.42668371 -0.96058293          1        0
     AlleyPave AlleyNA LotShapeIR2 LotShapeIR3 LotShapeReg LotShapeNA
1399         0       1           0           0           1          0
364          0       1           0           0           1          0
269          0       1           0           0           1          0
243          0       1           0           0           1          0
626          0       1           0           0           0          0
345          0       1           0           0           1          0
     LandContourHLS LandContourLow LandContourLvl LandContourNA
1399              0              0              1             0
364               0              0              1             0
269               0              0              1             0
243               0              0              1             0
626               0              0              1             0
345               0              0              1             0
     UtilitiesNoSeWa UtilitiesNA LotConfigCulDSac LotConfigFR2
1399               0           0                0            0
364                0           0                0            0
269                0           0                0            0
243                0           0                0            0
626                0           0                0            0
345                0           0                0            0
     LotConfigFR3 LotConfigInside LotConfigNA LandSlopeMod LandSlopeSev
1399            0               1           0            0            0
364             0               1           0            0            0
269             0               1           0            0            0
243             0               0           0            0            0
626             0               0           0            0            0
345             0               1           0            0            0
     LandSlopeNA NeighborhoodBlueste NeighborhoodBrDale
1399           0                   0                  0
364            0                   0                  1
269            0                   0                  0
243            0                   0                  0
626            0                   0                  0
345            0                   0                  0
     NeighborhoodBrkSide NeighborhoodClearCr NeighborhoodCollgCr
1399                   0                   0                   0
364                    0                   0                   0
269                    0                   0                   0
243                    0                   0                   0
626                    0                   0                   0
345                    0                   0                   0
     NeighborhoodCrawfor NeighborhoodEdwards NeighborhoodGilbert
1399                   0                   0                   0
364                    0                   0                   0
269                    0                   0                   0
243                    0                   0                   0
626                    0                   0                   0
345                    0                   0                   0
     NeighborhoodIDOTRR NeighborhoodMeadowV NeighborhoodMitchel
1399                  0                   0                   0
364                   0                   0                   0
269                   1                   0                   0
243                   0                   0                   0
626                   0                   0                   0
345                   0                   1                   0
     NeighborhoodNAmes NeighborhoodNoRidge NeighborhoodNPkVill
1399                 1                   0                   0
364                  0                   0                   0
269                  0                   0                   0
243                  0                   0                   0
626                  1                   0                   0
345                  0                   0                   0
     NeighborhoodNridgHt NeighborhoodNWAmes NeighborhoodOldTown
1399                   0                  0                   0
364                    0                  0                   0
269                    0                  0                   0
243                    0                  0                   1
626                    0                  0                   0
345                    0                  0                   0
     NeighborhoodSawyer NeighborhoodSawyerW NeighborhoodSomerst
1399                  0                   0                   0
364                   0                   0                   0
269                   0                   0                   0
243                   0                   0                   0
626                   0                   0                   0
345                   0                   0                   0
     NeighborhoodStoneBr NeighborhoodSWISU NeighborhoodTimber
1399                   0                 0                  0
364                    0                 0                  0
269                    0                 0                  0
243                    0                 0                  0
626                    0                 0                  0
345                    0                 0                  0
     NeighborhoodVeenker NeighborhoodNA Condition1Feedr Condition1Norm
1399                   0              0               0              1
364                    0              0               0              1
269                    0              0               0              1
243                    0              0               0              1
626                    0              0               0              1
345                    0              0               0              1
     Condition1PosA Condition1PosN Condition1RRAe Condition1RRAn
1399              0              0              0              0
364               0              0              0              0
269               0              0              0              0
243               0              0              0              0
626               0              0              0              0
345               0              0              0              0
     Condition1RRNe Condition1RRNn Condition1NA Condition2Feedr
1399              0              0            0               0
364               0              0            0               0
269               0              0            0               0
243               0              0            0               0
626               0              0            0               0
345               0              0            0               0
     Condition2Norm Condition2PosA Condition2PosN Condition2RRAe
1399              1              0              0              0
364               1              0              0              0
269               1              0              0              0
243               1              0              0              0
626               1              0              0              0
345               1              0              0              0
     Condition2RRAn Condition2RRNn Condition2NA BldgType2fmCon
1399              0              0            0              0
364               0              0            0              0
269               0              0            0              0
243               0              0            0              0
626               0              0            0              0
345               0              0            0              0
     BldgTypeDuplex BldgTypeTwnhs BldgTypeTwnhsE BldgTypeNA
1399              0             0              0          0
364               0             1              0          0
269               0             0              0          0
243               0             0              0          0
626               0             0              0          0
345               0             0              1          0
     HouseStyle1.5Unf HouseStyle1Story HouseStyle2.5Fin HouseStyle2.5Unf
1399                0                0                0                0
364                 0                0                0                0
269                 0                1                0                0
243                 0                0                0                0
626                 0                1                0                0
345                 0                0                0                0
     HouseStyle2Story HouseStyleSFoyer HouseStyleSLvl HouseStyleNA
1399                0                0              0            0
364                 1                0              0            0
269                 0                0              0            0
243                 0                0              0            0
626                 0                0              0            0
345                 1                0              0            0
     OverallQual OverallCond   YearBuilt YearRemodAdd RoofStyleGable
1399 -0.77242013  -1.4055644 -0.70359076   -0.1083774              1
364  -0.06317371   2.1879039  0.02268699    1.0881187              1
269  -0.77242013   0.3911698 -1.03371701   -1.4005931              1
243  -0.77242013  -1.4055644 -2.35422201   -1.6398923              1
626  -0.06317371   0.3911698 -0.30743926   -1.0655742              0
345  -0.77242013  -2.3039314  0.15473749   -0.3955364              1
     RoofStyleGambrel RoofStyleHip RoofStyleMansard RoofStyleShed
1399                0            0                0             0
364                 0            0                0             0
269                 0            0                0             0
243                 0            0                0             0
626                 0            1                0             0
345                 0            0                0             0
     RoofStyleNA RoofMatlCompShg RoofMatlMembran RoofMatlMetal
1399           0               1               0             0
364            0               1               0             0
269            0               1               0             0
243            0               1               0             0
626            0               1               0             0
345            0               1               0             0
     RoofMatlRoll RoofMatlTar&Grv RoofMatlWdShake RoofMatlWdShngl
1399            0               0               0               0
364             0               0               0               0
269             0               0               0               0
243             0               0               0               0
626             0               0               0               0
345             0               0               0               0
     RoofMatlNA Exterior1stAsphShn Exterior1stBrkComm Exterior1stBrkFace
1399          0                  0                  0                  0
364           0                  0                  0                  0
269           0                  0                  0                  0
243           0                  0                  0                  0
626           0                  0                  0                  0
345           0                  0                  0                  0
     Exterior1stCBlock Exterior1stCemntBd Exterior1stHdBoard
1399                 0                  0                  0
364                  0                  0                  1
269                  0                  0                  0
243                  0                  0                  0
626                  0                  0                  0
345                  0                  1                  0
     Exterior1stImStucc Exterior1stMetalSd Exterior1stPlywood
1399                  0                  0                  0
364                   0                  0                  0
269                   0                  0                  0
243                   0                  0                  0
626                   0                  0                  0
345                   0                  0                  0
     Exterior1stStone Exterior1stStucco Exterior1stVinylSd
1399                0                 0                  1
364                 0                 0                  0
269                 0                 0                  1
243                 0                 0                  0
626                 0                 0                  0
345                 0                 0                  0
     Exterior1stWd Sdng Exterior1stWdShing Exterior1stNA
1399                  0                  0             0
364                   0                  0             0
269                   0                  0             0
243                   1                  0             0
626                   1                  0             0
345                   0                  0             0
     Exterior2ndAsphShn Exterior2ndBrk Cmn Exterior2ndBrkFace
1399                  0                  0                  0
364                   0                  0                  0
269                   0                  0                  0
243                   0                  0                  0
626                   0                  0                  0
345                   0                  0                  0
     Exterior2ndCBlock Exterior2ndCmentBd Exterior2ndHdBoard
1399                 0                  0                  0
364                  0                  0                  1
269                  0                  0                  0
243                  0                  0                  0
626                  0                  0                  0
345                  0                  1                  0
     Exterior2ndImStucc Exterior2ndMetalSd Exterior2ndOther
1399                  0                  0                0
364                   0                  0                0
269                   0                  0                0
243                   0                  0                0
626                   0                  0                0
345                   0                  0                0
     Exterior2ndPlywood Exterior2ndStone Exterior2ndStucco
1399                  0                0                 0
364                   0                0                 0
269                   0                0                 0
243                   0                0                 0
626                   0                0                 0
345                   0                0                 0
     Exterior2ndVinylSd Exterior2ndWd Sdng Exterior2ndWd Shng
1399                  0                  1                  0
364                   0                  0                  0
269                   1                  0                  0
243                   0                  1                  0
626                   0                  1                  0
345                   0                  0                  0
     Exterior2ndNA MasVnrTypeBrkFace MasVnrTypeNone MasVnrTypeStone
1399             0                 0              1               0
364              0                 1              0               0
269              0                 0              1               0
243              0                 0              1               0
626              0                 1              0               0
345              0                 0              1               0
     MasVnrTypeNA MasVnrArea ExterQualFa ExterQualGd ExterQualTA
1399            0 -0.5698929           0           0           1
364             0  2.2739587           0           0           1
269             0 -0.5698929           0           0           1
243             0 -0.5698929           0           0           1
626             0  0.8854900           0           0           1
345             0 -0.5698929           0           0           1
     ExterQualNA ExterCondFa ExterCondGd ExterCondPo ExterCondTA
1399           0           0           0           0           1
364            0           0           0           0           1
269            0           0           0           0           1
243            0           0           0           0           1
626            0           0           0           0           1
345            0           0           0           0           1
     ExterCondNA FoundationCBlock FoundationPConc FoundationSlab
1399           0                1               0              0
364            0                1               0              0
269            0                1               0              0
243            0                0               0              0
626            0                1               0              0
345            0                1               0              0
     FoundationStone FoundationWood FoundationNA BsmtQualFa BsmtQualGd
1399               0              0            0          0          0
364                0              0            0          0          0
269                0              0            0          0          0
243                0              0            0          0          0
626                0              0            0          0          0
345                0              0            0          0          1
     BsmtQualTA BsmtQualNA BsmtCondGd BsmtCondPo BsmtCondTA BsmtCondNA
1399          1          0          0          0          1          0
364           1          0          0          0          1          0
269           1          0          0          0          1          0
243           1          0          0          0          1          0
626           1          0          0          0          1          0
345           0          0          0          0          1          0
     BsmtExposureGd BsmtExposureMn BsmtExposureNo BsmtExposureNA
1399              0              0              1              0
364               0              0              1              0
269               0              0              1              0
243               0              0              1              0
626               0              0              1              0
345               0              0              1              0
     BsmtFinType1BLQ BsmtFinType1GLQ BsmtFinType1LwQ BsmtFinType1Rec
1399               0               0               0               1
364                0               0               0               0
269                0               0               0               0
243                0               0               0               0
626                0               0               0               0
345                0               0               0               1
     BsmtFinType1Unf BsmtFinType1NA  BsmtFinSF1 BsmtFinType2BLQ
1399               0              0 -0.57378627               1
364                0              0 -0.61329367               0
269                0              0 -0.08433346               0
243                1              0 -0.96886029               0
626                1              0 -0.96886029               0
345                0              0 -0.68572391               1
     BsmtFinType2GLQ BsmtFinType2LwQ BsmtFinType2Rec BsmtFinType2Unf
1399               0               0               0               0
364                0               0               0               1
269                0               0               1               0
243                0               0               0               1
626                0               0               0               1
345                0               0               0               0
     BsmtFinType2NA BsmtFinSF2   BsmtUnfSF TotalBsmtSF HeatingGasA
1399              0  1.7872797  0.26215347   0.3544337           1
364               0 -0.2930296 -0.54550236  -1.2904291           1
269               0  0.4457166 -0.79348683  -0.7073536           1
243               0 -0.2930296 -0.04725834  -1.1611088           1
626               0 -0.2930296  1.26319169   0.1457063           1
345               0  1.0780833 -0.87766504  -1.1701839           1
     HeatingGasW HeatingGrav HeatingOthW HeatingWall HeatingNA HeatingQCFa
1399           0           0           0           0         0           0
364            0           0           0           0         0           0
269            0           0           0           0         0           0
243            0           0           0           0         0           0
626            0           0           0           0         0           0
345            0           0           0           0         0           0
     HeatingQCGd HeatingQCPo HeatingQCTA HeatingQCNA CentralAirY
1399           1           0           0           0           1
364            1           0           0           0           1
269            0           0           0           0           1
243            1           0           0           0           0
626            0           0           1           0           1
345            0           0           1           0           1
     CentralAirNA ElectricalFuseF ElectricalFuseP ElectricalMix
1399            0               0               0             0
364             0               0               0             0
269             0               0               0             0
243             0               0               0             0
626             0               0               0             0
345             0               0               0             0
     ElectricalSBrkr ElectricalNA `1stFlrSF` `2ndFlrSF` LowQualFinSF
1399               0            0 -0.0601019  1.0065659   -0.1011797
364                1            0 -1.7243810  0.3907528   -0.1011797
269                1            0 -0.9725244 -0.7848906   -0.1011797
243                0            0 -0.6896225  0.5003862   -0.1011797
626                1            0 -0.1110752 -0.7848906   -0.1011797
345                1            0 -1.5893017  0.5587018   -0.1011797
      GrLivArea BsmtFullBath BsmtHalfBath  FullBath   HalfBath
1399  0.7968369    1.0864638   -0.2497667 -1.027187  1.2323877
364  -1.0152333   -0.8192577   -0.2497667 -1.027187  1.2323877
269  -1.4282351   -0.8192577   -0.2497667 -1.027187 -0.7561915
243  -0.1200666   -0.8192577   -0.2497667 -1.027187 -0.7561915
626  -0.7603183   -0.8192577   -0.2497667 -1.027187  1.2323877
345  -0.7682226   -0.8192577   -0.2497667 -1.027187  1.2323877
     BedroomAbvGr KitchenAbvGr KitchenQualFa KitchenQualGd KitchenQualTA
1399     0.169898   -0.2076629             0             0             1
364     -1.045622   -0.2076629             0             1             0
269     -1.045622   -0.2076629             0             0             1
243      0.169898   -0.2076629             0             0             1
626      0.169898   -0.2076629             0             0             1
345      0.169898   -0.2076629             0             0             1
     KitchenQualNA TotRmsAbvGrd FunctionalMaj2 FunctionalMin1
1399             0    0.3494857              0              1
364              0   -0.9249036              0              0
269              0   -1.5620983              0              0
243              0   -0.2877090              0              0
626              0   -0.9249036              0              0
345              0   -1.5620983              0              0
     FunctionalMin2 FunctionalMod FunctionalSev FunctionalTyp FunctionalNA
1399              0             0             0             0            0
364               0             0             0             1            0
269               0             0             0             1            0
243               0             0             0             1            0
626               0             0             0             1            0
345               0             0             0             1            0
     Fireplaces FireplaceQuFa FireplaceQuGd FireplaceQuPo FireplaceQuTA
1399 -0.9241529             0             0             0             0
364  -0.9241529             0             0             0             0
269   0.6235248             0             1             0             0
243  -0.9241529             0             0             0             0
626  -0.9241529             0             0             0             0
345  -0.9241529             0             0             0             0
     FireplaceQuNA GarageTypeAttchd GarageTypeBasment GarageTypeBuiltIn
1399             1                1                 0                 0
364              1                0                 0                 0
269              0                0                 0                 0
243              1                1                 0                 0
626              1                1                 0                 0
345              1                1                 0                 0
     GarageTypeCarPort GarageTypeDetchd GarageTypeNA GarageFinishRFn
1399                 0                0            0               0
364                  0                1            0               0
269                  0                1            0               0
243                  0                0            0               0
626                  0                0            0               0
345                  0                0            0               0
     GarageFinishUnf GarageFinishNA GarageCars GarageArea GarageQualFa
1399               1              0 -1.0065605 -1.0811522            0
364                1              0 -1.0065605 -0.9697289            0
269                0              0 -1.0065605  2.0944117            0
243                1              0 -1.0065605 -0.5611768            1
626                1              0  0.3064228 -0.1526247            0
345                1              0 -1.0065605 -0.6354590            0
     GarageQualGd GarageQualPo GarageQualTA GarageQualNA GarageCondFa
1399            0            0            1            0            0
364             0            0            1            0            0
269             0            0            0            0            0
243             0            0            0            0            0
626             0            0            1            0            0
345             0            0            1            0            0
     GarageCondGd GarageCondPo GarageCondTA GarageCondNA PavedDriveP
1399            0            0            1            0           0
364             0            0            1            0           0
269             0            0            0            0           0
243             0            0            1            0           0
626             0            0            1            0           0
345             0            0            1            0           0
     PavedDriveY PavedDriveNA WoodDeckSF OpenPorchSF EnclosedPorch
1399           1            0 -0.7406335  -0.7027224     2.2554811
364            1            0  1.2352358  -0.7027224    -0.3595391
269            1            0 -0.7406335  -0.3327658    -0.3595391
243            1            0 -0.7406335  -0.7027224     0.8390118
626            1            0 -0.7406335  -0.7027224    -0.3595391
345            1            0  0.6977993  -0.7027224    -0.3595391
     `3SsnPorch` ScreenPorch    PoolArea PoolQCFa PoolQCGd PoolQCNA
1399  -0.1033128  -0.2858865 -0.06313936        0        0        1
364   -0.1033128  -0.2858865 -0.06313936        0        0        1
269   -0.1033128  -0.2858865 -0.06313936        0        0        1
243   -0.1033128  -0.2858865 -0.06313936        0        0        1
626   -0.1033128   6.5665537 -0.06313936        0        0        1
345   -0.1033128  -0.2858865 -0.06313936        0        0        1
     FenceGdWo FenceMnPrv FenceMnWw FenceNA MiscFeatureOthr
1399         0          0         0       0               0
364          0          0         0       1               0
269          0          0         0       1               0
243          0          0         0       1               0
626          0          0         0       1               0
345          0          0         0       1               0
     MiscFeatureShed MiscFeatureTenC MiscFeatureNA     MiscVal     MoSold
1399               0               0             1 -0.08957661 -0.4468483
364                0               0             1 -0.08957661 -0.4468483
269                0               0             1 -0.08957661 -1.5519176
243                0               0             1 -0.08957661 -0.8152047
626                0               0             1 -0.08957661 -1.5519176
345                0               0             1 -0.08957661 -0.8152047
         YrSold SaleTypeCon SaleTypeConLD SaleTypeConLI SaleTypeConLw
1399  0.9180953           0             0             0             0
364   0.9180953           0             0             0             0
269   0.1576185           0             0             0             0
243  -1.3633351           0             0             0             0
626   1.6785721           0             0             0             0
345   1.6785721           0             0             0             0
     SaleTypeCWD SaleTypeNew SaleTypeOth SaleTypeWD SaleTypeNA
1399           0           0           0          1          0
364            0           0           0          1          0
269            0           0           0          1          0
243            0           0           0          1          0
626            0           0           0          1          0
345            0           0           0          1          0
     SaleConditionAdjLand SaleConditionAlloca SaleConditionFamily
1399                    0                   0                   0
364                     0                   0                   0
269                     0                   0                   0
243                     0                   0                   0
626                     0                   0                   0
345                     0                   0                   0
     SaleConditionNormal SaleConditionPartial SaleConditionNA train
1399                   1                    0               0     1
364                    1                    0               0     1
269                    1                    0               0     1
243                    1                    0               0     1
626                    1                    0               0     1
345                    1                    0               0     1
     GarageYrBlt_cut[-1.4121,-0.7865) GarageYrBlt_cut[-0.7865,-0.5128)
1399                                1                                0
364                                 0                                0
269                                 0                                0
243                                 0                                0
626                                 0                                1
345                                 0                                0
     GarageYrBlt_cut[-0.5128,-0.1999) GarageYrBlt_cut[-0.1999, 0.0738)
1399                                0                                0
364                                 1                                0
269                                 1                                0
243                                 0                                0
626                                 0                                0
345                                 0                                1
     GarageYrBlt_cut[ 0.0738, 0.6212) GarageYrBlt_cut[ 0.6212, 0.8558)
1399                                0                                0
364                                 0                                0
269                                 0                                0
243                                 0                                0
626                                 0                                0
345                                 0                                0
     GarageYrBlt_cut[ 0.8558, 1.0513) GarageYrBlt_cut[ 1.0513, 1.1295)
1399                                0                                0
364                                 0                                0
269                                 0                                0
243                                 0                                0
626                                 0                                0
345                                 0                                0
     GarageYrBlt_cut[ 1.1295, 8.9499] GarageYrBlt_cutNA
1399                                0                 0
364                                 0                 0
269                                 0                 0
243                                 0                 0
626                                 0                 0
345                                 0                 0
head(y_train)
    1399      364      269      243      626      345 
11.83501 11.67844 11.69941 11.27720 11.98293 11.35041 
head(x_test)
   (Intercept)        Id  MSSubClass MSZoningFV MSZoningRH MSZoningRL
3            1 -1.728788  0.06731988          0          0          1
4            1 -1.727601  0.30251644          0          0          1
9            1 -1.721669 -0.16787668          0          0          0
13           1 -1.716922 -0.87346638          0          0          1
18           1 -1.710990  0.77290957          0          0          1
19           1 -1.709803 -0.87346638          0          0          1
   MSZoningRM MSZoningNA LotFrontage     LotArea StreetPave StreetNA
3           0          0 -0.05593492  0.13717338          1        0
4           0          0 -0.39862212 -0.07837129          1        0
9           1          0 -0.78414521 -0.51326435          1        0
13          0          0  0.00000000  0.35500028          1        0
18          0          0  0.11540868  0.07897632          1        0
19          0          0 -0.14160672  0.44717732          1        0
   AlleyPave AlleyNA LotShapeIR2 LotShapeIR3 LotShapeReg LotShapeNA
3          0       1           0           0           0          0
4          0       1           0           0           0          0
9          0       1           0           0           1          0
13         0       1           1           0           0          0
18         0       1           0           0           1          0
19         0       1           0           0           1          0
   LandContourHLS LandContourLow LandContourLvl LandContourNA
3               0              0              1             0
4               0              0              1             0
9               0              0              1             0
13              0              0              1             0
18              0              0              1             0
19              0              0              1             0
   UtilitiesNoSeWa UtilitiesNA LotConfigCulDSac LotConfigFR2 LotConfigFR3
3                0           0                0            0            0
4                0           0                0            0            0
9                0           0                0            0            0
13               0           0                0            0            0
18               0           0                0            0            0
19               0           0                0            0            0
   LotConfigInside LotConfigNA LandSlopeMod LandSlopeSev LandSlopeNA
3                1           0            0            0           0
4                0           0            0            0           0
9                1           0            0            0           0
13               1           0            0            0           0
18               1           0            0            0           0
19               1           0            0            0           0
   NeighborhoodBlueste NeighborhoodBrDale NeighborhoodBrkSide
3                    0                  0                   0
4                    0                  0                   0
9                    0                  0                   0
13                   0                  0                   0
18                   0                  0                   0
19                   0                  0                   0
   NeighborhoodClearCr NeighborhoodCollgCr NeighborhoodCrawfor
3                    0                   1                   0
4                    0                   0                   1
9                    0                   0                   0
13                   0                   0                   0
18                   0                   0                   0
19                   0                   0                   0
   NeighborhoodEdwards NeighborhoodGilbert NeighborhoodIDOTRR
3                    0                   0                  0
4                    0                   0                  0
9                    0                   0                  0
13                   0                   0                  0
18                   0                   0                  0
19                   0                   0                  0
   NeighborhoodMeadowV NeighborhoodMitchel NeighborhoodNAmes
3                    0                   0                 0
4                    0                   0                 0
9                    0                   0                 0
13                   0                   0                 0
18                   0                   0                 0
19                   0                   0                 0
   NeighborhoodNoRidge NeighborhoodNPkVill NeighborhoodNridgHt
3                    0                   0                   0
4                    0                   0                   0
9                    0                   0                   0
13                   0                   0                   0
18                   0                   0                   0
19                   0                   0                   0
   NeighborhoodNWAmes NeighborhoodOldTown NeighborhoodSawyer
3                   0                   0                  0
4                   0                   0                  0
9                   0                   1                  0
13                  0                   0                  1
18                  0                   0                  1
19                  0                   0                  0
   NeighborhoodSawyerW NeighborhoodSomerst NeighborhoodStoneBr
3                    0                   0                   0
4                    0                   0                   0
9                    0                   0                   0
13                   0                   0                   0
18                   0                   0                   0
19                   1                   0                   0
   NeighborhoodSWISU NeighborhoodTimber NeighborhoodVeenker NeighborhoodNA
3                  0                  0                   0              0
4                  0                  0                   0              0
9                  0                  0                   0              0
13                 0                  0                   0              0
18                 0                  0                   0              0
19                 0                  0                   0              0
   Condition1Feedr Condition1Norm Condition1PosA Condition1PosN
3                0              1              0              0
4                0              1              0              0
9                0              0              0              0
13               0              1              0              0
18               0              1              0              0
19               0              0              0              0
   Condition1RRAe Condition1RRAn Condition1RRNe Condition1RRNn
3               0              0              0              0
4               0              0              0              0
9               0              0              0              0
13              0              0              0              0
18              0              0              0              0
19              1              0              0              0
   Condition1NA Condition2Feedr Condition2Norm Condition2PosA
3             0               0              1              0
4             0               0              1              0
9             0               0              1              0
13            0               0              1              0
18            0               0              1              0
19            0               0              1              0
   Condition2PosN Condition2RRAe Condition2RRAn Condition2RRNn
3               0              0              0              0
4               0              0              0              0
9               0              0              0              0
13              0              0              0              0
18              0              0              0              0
19              0              0              0              0
   Condition2NA BldgType2fmCon BldgTypeDuplex BldgTypeTwnhs BldgTypeTwnhsE
3             0              0              0             0              0
4             0              0              0             0              0
9             0              0              0             0              0
13            0              0              0             0              0
18            0              0              1             0              0
19            0              0              0             0              0
   BldgTypeNA HouseStyle1.5Unf HouseStyle1Story HouseStyle2.5Fin
3           0                0                0                0
4           0                0                0                0
9           0                0                0                0
13          0                0                1                0
18          0                0                1                0
19          0                0                1                0
   HouseStyle2.5Unf HouseStyle2Story HouseStyleSFoyer HouseStyleSLvl
3                 0                1                0              0
4                 0                1                0              0
9                 0                0                0              0
13                0                0                0              0
18                0                0                0              0
19                0                0                0              0
   HouseStyleNA OverallQual OverallCond  YearBuilt YearRemodAdd
3             0   0.6460727  -0.5071973  0.9800531    0.8488195
4             0   0.6460727  -0.5071973 -1.8590326   -0.6826955
9             0   0.6460727  -0.5071973 -1.3308306   -1.6398923
13            0  -0.7724201   0.3911698 -0.3074393   -1.0655742
18            0  -1.4816665  -0.5071973 -0.1423761   -0.8262750
19            0  -0.7724201  -0.5071973  1.0790910    0.9445391
   RoofStyleGable RoofStyleGambrel RoofStyleHip RoofStyleMansard
3               1                0            0                0
4               1                0            0                0
9               1                0            0                0
13              0                0            1                0
18              1                0            0                0
19              1                0            0                0
   RoofStyleShed RoofStyleNA RoofMatlCompShg RoofMatlMembran RoofMatlMetal
3              0           0               1               0             0
4              0           0               1               0             0
9              0           0               1               0             0
13             0           0               1               0             0
18             0           0               1               0             0
19             0           0               1               0             0
   RoofMatlRoll RoofMatlTar&Grv RoofMatlWdShake RoofMatlWdShngl RoofMatlNA
3             0               0               0               0          0
4             0               0               0               0          0
9             0               0               0               0          0
13            0               0               0               0          0
18            0               0               0               0          0
19            0               0               0               0          0
   Exterior1stAsphShn Exterior1stBrkComm Exterior1stBrkFace
3                   0                  0                  0
4                   0                  0                  0
9                   0                  0                  1
13                  0                  0                  0
18                  0                  0                  0
19                  0                  0                  0
   Exterior1stCBlock Exterior1stCemntBd Exterior1stHdBoard
3                  0                  0                  0
4                  0                  0                  0
9                  0                  0                  0
13                 0                  0                  1
18                 0                  0                  0
19                 0                  0                  0
   Exterior1stImStucc Exterior1stMetalSd Exterior1stPlywood
3                   0                  0                  0
4                   0                  0                  0
9                   0                  0                  0
13                  0                  0                  0
18                  0                  1                  0
19                  0                  0                  0
   Exterior1stStone Exterior1stStucco Exterior1stVinylSd
3                 0                 0                  1
4                 0                 0                  0
9                 0                 0                  0
13                0                 0                  0
18                0                 0                  0
19                0                 0                  1
   Exterior1stWd Sdng Exterior1stWdShing Exterior1stNA Exterior2ndAsphShn
3                   0                  0             0                  0
4                   1                  0             0                  0
9                   0                  0             0                  0
13                  0                  0             0                  0
18                  0                  0             0                  0
19                  0                  0             0                  0
   Exterior2ndBrk Cmn Exterior2ndBrkFace Exterior2ndCBlock
3                   0                  0                 0
4                   0                  0                 0
9                   0                  0                 0
13                  0                  0                 0
18                  0                  0                 0
19                  0                  0                 0
   Exterior2ndCmentBd Exterior2ndHdBoard Exterior2ndImStucc
3                   0                  0                  0
4                   0                  0                  0
9                   0                  0                  0
13                  0                  0                  0
18                  0                  0                  0
19                  0                  0                  0
   Exterior2ndMetalSd Exterior2ndOther Exterior2ndPlywood Exterior2ndStone
3                   0                0                  0                0
4                   0                0                  0                0
9                   0                0                  0                0
13                  0                0                  1                0
18                  1                0                  0                0
19                  0                0                  0                0
   Exterior2ndStucco Exterior2ndVinylSd Exterior2ndWd Sdng
3                  0                  1                  0
4                  0                  0                  0
9                  0                  0                  0
13                 0                  0                  0
18                 0                  0                  0
19                 0                  1                  0
   Exterior2ndWd Shng Exterior2ndNA MasVnrTypeBrkFace MasVnrTypeNone
3                   0             0                 1              0
4                   1             0                 0              1
9                   1             0                 0              1
13                  0             0                 0              1
18                  0             0                 0              1
19                  0             0                 0              1
   MasVnrTypeStone MasVnrTypeNA MasVnrArea ExterQualFa ExterQualGd
3                0            0  0.3334482           0           1
4                0            0 -0.5698929           0           0
9                0            0 -0.5698929           0           0
13               0            0 -0.5698929           0           0
18               0            0 -0.5698929           0           0
19               0            0 -0.5698929           0           0
   ExterQualTA ExterQualNA ExterCondFa ExterCondGd ExterCondPo ExterCondTA
3            0           0           0           0           0           1
4            1           0           0           0           0           1
9            1           0           0           0           0           1
13           1           0           0           0           0           1
18           1           0           0           0           0           1
19           1           0           0           0           0           1
   ExterCondNA FoundationCBlock FoundationPConc FoundationSlab
3            0                0               1              0
4            0                0               0              0
9            0                0               0              0
13           0                1               0              0
18           0                0               0              1
19           0                0               1              0
   FoundationStone FoundationWood FoundationNA BsmtQualFa BsmtQualGd
3                0              0            0          0          1
4                0              0            0          0          0
9                0              0            0          0          0
13               0              0            0          0          0
18               0              0            0          0          0
19               0              0            0          0          0
   BsmtQualTA BsmtQualNA BsmtCondGd BsmtCondPo BsmtCondTA BsmtCondNA
3           0          0          0          0          1          0
4           1          0          1          0          0          0
9           1          0          0          0          1          0
13          1          0          0          0          1          0
18          0          1          0          0          0          1
19          1          0          0          0          1          0
   BsmtExposureGd BsmtExposureMn BsmtExposureNo BsmtExposureNA
3               0              1              0              0
4               0              0              1              0
9               0              0              1              0
13              0              0              1              0
18              0              0              0              1
19              0              0              1              0
   BsmtFinType1BLQ BsmtFinType1GLQ BsmtFinType1LwQ BsmtFinType1Rec
3                0               1               0               0
4                0               0               0               0
9                0               0               0               0
13               0               0               0               0
18               0               0               0               0
19               0               1               0               0
   BsmtFinType1Unf BsmtFinType1NA  BsmtFinSF1 BsmtFinType2BLQ
3                0              0  0.09783956               0
4                0              0 -0.49477146               0
9                1              0 -0.96886029               0
13               0              0  0.64874834               0
18               0              1 -0.96886029               0
19               0              0  0.44901647               0
   BsmtFinType2GLQ BsmtFinType2LwQ BsmtFinType2Rec BsmtFinType2Unf
3                0               0               0               1
4                0               0               0               1
9                0               0               0               1
13               0               0               0               1
18               0               0               0               0
19               0               0               0               1
   BsmtFinType2NA BsmtFinSF2   BsmtUnfSF TotalBsmtSF HeatingGasA
3               0 -0.2930296 -0.28841755  -0.2989739           1
4               0 -0.2930296 -0.04725834  -0.6710532           1
9               0 -0.2930296  0.89007744  -0.2263730           1
13              0 -0.2930296 -0.87766504  -0.3171241           1
18              1 -0.2930296 -1.27580524  -2.3862480           1
19              0 -0.2930296 -0.21106459   0.1411687           1
   HeatingGasW HeatingGrav HeatingOthW HeatingWall HeatingNA HeatingQCFa
3            0           0           0           0         0           0
4            0           0           0           0         0           0
9            0           0           0           0         0           0
13           0           0           0           0         0           0
18           0           0           0           0         0           0
19           0           0           0           0         0           0
   HeatingQCGd HeatingQCPo HeatingQCTA HeatingQCNA CentralAirY
3            0           0           0           0           1
4            1           0           0           0           1
9            1           0           0           0           1
13           0           0           1           0           1
18           0           0           1           0           1
19           0           0           0           0           1
   CentralAirNA ElectricalFuseF ElectricalFuseP ElectricalMix
3             0               0               0             0
4             0               0               0             0
9             0               1               0             0
13            0               0               0             0
18            0               0               0             0
19            0               0               0             0
   ElectricalSBrkr ElectricalNA `1stFlrSF` `2ndFlrSF` LowQualFinSF
3                1            0 -0.6106138  1.2351632   -0.1011797
4                1            0 -0.5061185  0.9785744   -0.1011797
9                0            0 -0.3506499  0.9692439   -0.1011797
13               1            0 -0.6310031 -0.7848906   -0.1011797
18               1            0  0.3476847 -0.7848906   -0.1011797
19               1            0 -0.1161726 -0.7848906   -0.1011797
    GrLivArea BsmtFullBath BsmtHalfBath  FullBath   HalfBath BedroomAbvGr
3   0.5636589    1.0864638   -0.2497667  0.781232  1.2323877     0.169898
4   0.4273090    1.0864638   -0.2497667 -1.027187 -0.7561915     0.169898
9   0.5399458   -0.8192577   -0.2497667  0.781232 -0.7561915    -1.045622
13 -1.1634396    1.0864638   -0.2497667 -1.027187 -0.7561915    -1.045622
18 -0.4046229   -0.8192577   -0.2497667  0.781232 -0.7561915    -1.045622
19 -0.7642704    1.0864638   -0.2497667 -1.027187  1.2323877     0.169898
   KitchenAbvGr KitchenQualFa KitchenQualGd KitchenQualTA KitchenQualNA
3    -0.2076629             0             1             0             0
4    -0.2076629             0             1             0             0
9     4.4551678             0             0             1             0
13   -0.2076629             0             0             1             0
18    4.4551678             0             0             1             0
19   -0.2076629             0             1             0             0
   TotRmsAbvGrd FunctionalMaj2 FunctionalMin1 FunctionalMin2 FunctionalMod
3    -0.2877090              0              0              0             0
4     0.3494857              0              0              0             0
9     0.9866803              0              1              0             0
13   -1.5620983              0              0              0             0
18   -0.2877090              0              0              0             0
19   -0.2877090              0              0              0             0
   FunctionalSev FunctionalTyp FunctionalNA Fireplaces FireplaceQuFa
3              0             1            0  0.6235248             0
4              0             1            0  0.6235248             0
9              0             0            0  2.1712025             0
13             0             1            0 -0.9241529             0
18             0             1            0 -0.9241529             0
19             0             1            0 -0.9241529             0
   FireplaceQuGd FireplaceQuPo FireplaceQuTA FireplaceQuNA
3              0             0             1             0
4              1             0             0             0
9              0             0             1             0
13             0             0             0             1
18             0             0             0             1
19             0             0             0             1
   GarageTypeAttchd GarageTypeBasment GarageTypeBuiltIn GarageTypeCarPort
3                 1                 0                 0                 0
4                 0                 0                 0                 0
9                 0                 0                 0                 0
13                0                 0                 0                 0
18                0                 0                 0                 1
19                0                 0                 0                 0
   GarageTypeDetchd GarageTypeNA GarageFinishRFn GarageFinishUnf
3                 0            0               1               0
4                 1            0               0               1
9                 1            0               0               1
13                1            0               0               1
18                0            0               0               1
19                1            0               0               1
   GarageFinishNA GarageCars  GarageArea GarageQualFa GarageQualGd
3               0  0.3064228  0.62733835            0            0
4               0  1.6194060  0.78518802            0            0
9               0  0.3064228 -0.02263087            1            0
13              0 -1.0065605 -0.56117679            0            0
18              0  0.3064228  0.20021572            0            0
19              0  0.3064228  0.47877396            0            0
   GarageQualPo GarageQualTA GarageQualNA GarageCondFa GarageCondGd
3             0            1            0            0            0
4             0            1            0            0            0
9             0            0            0            0            0
13            0            1            0            0            0
18            0            1            0            0            0
19            0            1            0            0            0
   GarageCondPo GarageCondTA GarageCondNA PavedDriveP PavedDriveY
3             0            1            0           0           1
4             0            1            0           0           1
9             0            1            0           0           1
13            0            1            0           0           1
18            0            1            0           0           1
19            0            1            0           0           1
   PavedDriveNA  WoodDeckSF OpenPorchSF EnclosedPorch `3SsnPorch`
3             0 -0.74063351 -0.08119527    -0.3595391  -0.1033128
4             0 -0.74063351 -0.18478312     3.8743031  -0.1033128
9             0 -0.02932057 -0.70272237     2.8314081  -0.1033128
13            0  0.36585328 -0.70272237    -0.3595391  -0.1033128
18            0 -0.74063351 -0.70272237    -0.3595391  -0.1033128
19            0 -0.74063351  0.80670058    -0.3595391  -0.1033128
   ScreenPorch    PoolArea PoolQCFa PoolQCGd PoolQCNA FenceGdWo FenceMnPrv
3   -0.2858865 -0.06313936        0        0        1         0          0
4   -0.2858865 -0.06313936        0        0        1         0          0
9   -0.2858865 -0.06313936        0        0        1         0          0
13   2.8466576 -0.06313936        0        0        1         0          0
18  -0.2858865 -0.06313936        0        0        1         0          0
19  -0.2858865 -0.06313936        0        0        1         0          0
   FenceMnWw FenceNA MiscFeatureOthr MiscFeatureShed MiscFeatureTenC
3          0       1               0               0               0
4          0       1               0               0               0
9          0       1               0               0               0
13         0       1               0               0               0
18         0       1               0               1               0
19         0       1               0               0               0
   MiscFeatureNA     MiscVal      MoSold     YrSold SaleTypeCon
3              1 -0.08957661  1.02657749  0.1576185           0
4              1 -0.08957661 -1.55191764 -1.3633351           0
9              1 -0.08957661 -0.81520474  0.1576185           0
13             1 -0.08957661  1.02657749  0.1576185           0
18             0  0.79163250  1.39493394 -1.3633351           0
19             1 -0.08957661 -0.07849185  0.1576185           0
   SaleTypeConLD SaleTypeConLI SaleTypeConLw SaleTypeCWD SaleTypeNew
3              0             0             0           0           0
4              0             0             0           0           0
9              0             0             0           0           0
13             0             0             0           0           0
18             0             0             0           0           0
19             0             0             0           0           0
   SaleTypeOth SaleTypeWD SaleTypeNA SaleConditionAdjLand
3            0          1          0                    0
4            0          1          0                    0
9            0          1          0                    0
13           0          1          0                    0
18           0          1          0                    0
19           0          1          0                    0
   SaleConditionAlloca SaleConditionFamily SaleConditionNormal
3                    0                   0                   1
4                    0                   0                   0
9                    0                   0                   0
13                   0                   0                   1
18                   0                   0                   1
19                   0                   0                   1
   SaleConditionPartial SaleConditionNA train
3                     0               0     1
4                     0               0     1
9                     0               0     1
13                    0               0     1
18                    0               0     1
19                    0               0     1
   GarageYrBlt_cut[-1.4121,-0.7865) GarageYrBlt_cut[-0.7865,-0.5128)
3                                 0                                0
4                                 0                                0
9                                 0                                0
13                                0                                1
18                                0                                0
19                                0                                0
   GarageYrBlt_cut[-0.5128,-0.1999) GarageYrBlt_cut[-0.1999, 0.0738)
3                                 0                                0
4                                 0                                0
9                                 0                                0
13                                0                                0
18                                1                                0
19                                0                                0
   GarageYrBlt_cut[ 0.0738, 0.6212) GarageYrBlt_cut[ 0.6212, 0.8558)
3                                 0                                0
4                                 0                                1
9                                 0                                0
13                                0                                0
18                                0                                0
19                                0                                0
   GarageYrBlt_cut[ 0.8558, 1.0513) GarageYrBlt_cut[ 1.0513, 1.1295)
3                                 1                                0
4                                 0                                0
9                                 0                                0
13                                0                                0
18                                0                                0
19                                1                                0
   GarageYrBlt_cut[ 1.1295, 8.9499] GarageYrBlt_cutNA
3                                 0                 0
4                                 0                 0
9                                 0                 0
13                                0                 0
18                                0                 0
19                                0                 0
head(y_test)
       3        4        9       13       18       19 
12.31717 11.84940 11.77452 11.87757 11.40756 11.97666 

Define params

There is a slide dedicated to batch size selection

batch_size <- 4 #if batch_size is too low, it's like SGD, loss function is very unstable. if batch_size is too high, the gradient will be almost null after a very few iterations.
epochs <- 10

Design a neural network

model <- keras_model_sequential()

model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

Using TensorFlow backend. 2017-11-02 12:32:56.960563: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX

https://github.com/rstudio/keras/issues?q=is%3Aissue+is%3Aclosed

Choose loss function and optimizer

model %>% compile(
  loss = 'mean_squared_error',
  optimizer = 'adam',#try also sgd 
  metrics = c('mean_squared_error')
)

Fit the model and keep track of the optimization process

history <- model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  verbose = 2,
  validation_split = 0.3,callbacks=list(callback_tensorboard())
)

Evaluate the model

score <- model %>% evaluate(
  x_test, y_test,
  batch_size = batch_size,
  verbose = 1
)

print(paste0("RMSE on test set ",sqrt(score$mean_squared_error)))
[1] "RMSE on test set 0.367497673912869"

Encapsulate everything in a function

the function returns Model History Score (a list of 3 objects) MHS

source("run_model.R")
MHS <- build_model(x_train,y_train,x_test,y_test)
[1] "RMSE on test set 0.38902735047425"

Add layers

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

source("compile_evaluate_model.R")
MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch1.RData")
load("NN3Layer100relu80relu60relu_batch1.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-11

Add Dropout

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dropout(.7)%>%
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch1_dropout70.RData")
load("NN3Layer100relu80relu60relu_batch1_dropout70.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-13

Use alpha Dropout instead

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_alpha_dropout(.7)%>%
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch1_alphadropout70.RData")
load("NN3Layer100relu80relu60relu_batch1_alphadropout70.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-15

Use gaussian Dropout instead

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_gaussian_dropout(.7)%>%
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch4_Den1_gaussiandropout70_Act1.RData")

Den1 Act1 GDO “RMSE on test set 0.187559564216181”

Den1 GDO Act1 “RMSE on test set 0.158125156800799-0.166876005632993”

load("NN3Layer100relu80relu60relu_batch4_Den1_gaussiandropout70_Act1.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-17

Reduce overfitting - Gaussian Noise

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_gaussian_noise(stddev = 1) %>%
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch4_GN1_betweenDen1Act1.RData")

[1] “RMSE on test set 0.188398698260064” better !

[1] “RMSE on test set 0.250187371099559” if you put it between Dense 1 and Activation 1.

load("NN3Layer100relu80relu60relu_batch4_GN1.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-19

Activity Regularizer

Have a look at : https://keras.io/regularizers/

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>% 
  layer_activity_regularization(l1=0,l2=1)%>%
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch4_Den1_AR0100_Act1.RData")

Den1 AR7030 Act1 200 iter “RMSE on test set 0.269866256451131”

Den1 AR1000 Act1 500 iter “RMSE on test set 0.373504576873097 - 0.559613751871897”

Den1 AR0100 Act1 500 iter “RMSE on test set 0.234313469190436 - 0.264217724830065”

Act1 AR7030 Den2 “RMSE on test set 0.54332276985586”

load("NN3Layer100relu80relu60relu_batch4_Den1_AR0100_Act1.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-21

Batch Normalization

Have a look at : https://keras.io/regularizers/

There is a ton of paramaters : help(layer_batch_normalization)

model <- keras_model_sequential()
model %>%
  layer_dense(units = 100,input_shape = ncol(x_train)) %>%
  layer_batch_normalization()%>%
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN3Layer100relu80relu60relu_batch4_BN_betweenDen1Act1.RData")

BN_betweenDen1Act1 “RMSE on test set 0.256533278504706”

BN_betweenAct1Den2 “RMSE on test set 1.12236581801725”

Care with the interpretation of the documentation “Normalize the activations of the previous layer at each batch”

load("NN3Layer100relu80relu60relu_batch4_BN_betweenDen1Act1.RData")
plot(MHS[[2]])

plot of chunk unnamed-chunk-23

Mix GDO & GN & BN

batch_size <- 24 #if batch_size is too low, it's like SGD, loss function is very unstable. if batch_size is too high, the gradient will be almost null after a very few iterations.
epochs <- 1000
model <- keras_model_sequential()
model %>%
  layer_dense(units = 100, input_shape = ncol(x_train)) %>%
  layer_gaussian_dropout(.7)%>%
  layer_activation(activation = 'relu') %>% 
  layer_gaussian_noise(stddev = 1) %>%
  layer_batch_normalization()%>%
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 60) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 80) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 100) %>% 
  layer_activation(activation = 'relu') %>% 
  layer_dense(units=1)%>%
  layer_activation(activation = 'linear')

MHS <- compile_evaluate_model(model,x_train,y_train,x_test,y_test)
save(list="MHS",file="NN5Layer100relu80relu60relu80relu100relu_batch24_GDO70_Act1_GN1_BN.RData")

Den1 BN Act1 GN1 : “RMSE on test set 0.182853024983419”

Act1 GN1 BN : “RMSE on test set 0.21932565772734”

Den1 GDO70 Act 1 GN1 BN “RMSE on test set 0.184909205825903 - 0.14411899553965”

Impact of batch size

NN_list_batch_size <- c("NN5Layer100relu80relu60relu80relu100relu_batch2_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch4_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch6_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch8_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch10_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch12_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch16_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch20_GDO70_Act1_GN1_BN.RData"
                        ,"NN5Layer100relu80relu60relu80relu100relu_batch24_GDO70_Act1_GN1_BN.RData")
perf <- data.frame()

for (nm in NN_list_batch_size){
  print(nm)
  load(nm)
  batch_size <- regexpr(pattern = "batch[[:digit:]]*",nm)
  batch_size <- substr(nm,start = batch_size[1],stop=batch_size[1]+attr(batch_size,"match.length")-1)
  metrics <- MHS[[2]][2]$metrics
  sub_perf <- data.frame(iter=1:length(metrics$val_mean_squared_error),perf_train=metrics$mean_squared_error,perf_val=metrics$val_mean_squared_error,lr=metrics$lr,batch=batch_size)
  perf <- rbind(perf,sub_perf)
}

g <- ggplot(perf[perf$iter>10,])+geom_line(aes(x=iter,y=perf_train,color=batch))
g <- ggplotly(g)
htmlwidgets::saveWidget(as_widget(g), file = "gg_perf_train.html")

g <- ggplot(perf[perf$iter>10,])+geom_line(aes(x=iter,y=perf_val,color=batch))
g <- ggplotly(g)
htmlwidgets::saveWidget(as_widget(g), file = "gg_perf_val.html")

g <- ggplot(perf[perf$iter>10,])+geom_line(aes(x=iter,y=lr,color=batch))
g <- ggplotly(g)
htmlwidgets::saveWidget(as_widget(g), file = "gg_lr.html")

Callbacks

Early stop

Reduce learning rate

Tensorboard

history <- model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  verbose = 2,
  validation_split = 0.3,
  callbacks=list(callback_tensorboard(log_dir = "logs/",write_graph = TRUE,write_images = TRUE,write_grads = TRUE,),
                 callback_early_stopping(monitor = "val_loss",patience=100,mode = "min"),
                 callback_reduce_lr_on_plateau(monitor = "val_loss", factor = 0.8,patience=10,verbose=1)))

Tensorboard

docker exec -it keras_demo pip install tensorboard

Run it inside the docker, then need to forward it to your browser… docker exec -it keras_demo tensorboard –logdir=logs

then http://172.17.0.2:6006 this IP is local (inside the container)

Run it locally from the saved logs tensorboard –logdir=~/Documents/training\ hackathon\ 2017/logs

then http://localhost:6006

Word embedding

Example here : https://keras.rstudio.com/articles/examples/imdb_lstm.html data prep : https://github.com/wush978/FeatureHashing/blob/master/vignettes/SentimentAnalysis.Rmd

load("text_and_value_data.RData")#this dataset was created in tm section after text cleaning



sample_train=sample(1:nrow(tm_data),round(0.7*nrow(tm_data)))
x_train <- tm_data[sample_train,]$Description
y_train <- log(tm_data[sample_train,]$Value)
x_test <- tm_data[-sample_train,]$Description
y_test <- log(tm_data[-sample_train,]$Value)


# x_train <- pad_sequences(x_train, maxlen = maxlen)
# x_test <- pad_sequences(x_test, maxlen = maxlen)

model <- keras_model_sequential()
model %>%
  layer_embedding(input_dim = max_features, output_dim = 128) %>% 
  layer_lstm(units = 64, dropout = 0.2, recurrent_dropout = 0.2) %>% 
  layer_dense(units = 1, activation = 'linear')

# Try using different optimizers and different optimizer configs
model %>% compile(
  loss = 'binary_crossentropy',
  optimizer = 'adam',
  metrics = c('accuracy')
)

cat('Train...\n')
model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = 15,
  validation_data = list(x_test, y_test)
)

scores <- model %>% evaluate(
  x_test, y_test,
  batch_size = batch_size
)

cat('Test score:', scores[[1]])
cat('Test accuracy', scores[[2]])
library(keras)

max_features <- 2000
batch_size <- 32

# Cut texts after this number of words (among top max_features most common words)
maxlen <- 80  

cat('Loading data...\n')
imdb <- dataset_imdb(num_words = max_features)
x_train <- imdb$train$x
y_train <- imdb$train$y
x_test <- imdb$test$x
y_test <- imdb$test$y

cat(length(x_train), 'train sequences\n')
cat(length(x_test), 'test sequences\n')

cat('Pad sequences (samples x time)\n')
x_train <- pad_sequences(x_train, maxlen = maxlen)
x_test <- pad_sequences(x_test, maxlen = maxlen)
cat('x_train shape:', dim(x_train), '\n')
cat('x_test shape:', dim(x_test), '\n')

cat('Build model...\n')
model <- keras_model_sequential()
model %>%
  layer_embedding(input_dim = max_features, output_dim = 128) %>% 
  layer_lstm(units = 64, dropout = 0.2, recurrent_dropout = 0.2) %>% 
  layer_dense(units = 1, activation = 'sigmoid')

# Try using different optimizers and different optimizer configs
model %>% compile(
  loss = 'binary_crossentropy',
  optimizer = 'adam',
  metrics = c('accuracy')
)

cat('Train...\n')
model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = 15,
  validation_data = list(x_test, y_test)
)

scores <- model %>% evaluate(
  x_test, y_test,
  batch_size = batch_size
)

cat('Test score:', scores[[1]])
cat('Test accuracy', scores[[2]])

Practice

Fine tune Keras NN layers & parameters

Check the documentation and try some other layers

Try a simple NN on Seattle data

Try word embedding techniques