i am trying to fit a model a using deep learning with kares to produce ANN model using the following code

Load libraries

library(keras)
library(lime)
library(tidyquant)
library(rsample)
library(recipes)
library(yardstick)
library(corrr)
> dataset<-read.csv(file.choose(), header=TRUE)
Error in file.choose() : file choice cancelled
>  dataset<-read.csv(file.choose(), header=TRUE)
> glimpse(dataset)
Observations: 2,538
Variables: 5
$ X         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56...
$ SessionID <int> 13307, 21076, 27813, 8398, 23118, 12256, 28799, 11457, 7542, 19261, 32716, 15088, 5623, 2916, 13679, 18061, 10417, 12601, 11103, 29218, 6626, 17057, 19944, 3074, 23073, 27199, 10053, 16373, 966, 22993, 10451, 2624...
$ Timestamp <fct> 2014-04-06T18:42:05.823Z, 2014-04-03T15:27:48.119Z, 2014-04-04T09:10:14.357Z, 2014-04-03T23:39:20.245Z, 2014-04-07T16:02:13.784Z, 2014-04-05T07:36:34.647Z, 2014-04-06T18:04:36.195Z, 2014-04-04T15:47:18.954Z, 2014-...
$ ItemID    <int> 214684513, 214718203, 214716928, 214826900, 214838180, 214717318, 214821307, 214537967, 214835775, 214706432, 214668590, 214639372, 214826606, 214667912, 214717007, 214821377, 214676957, 214579730, 214567410, 2148...
$ Price     <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
> dataset$Timestamp <-as.POSIXct(dataset$Timestamp, format = "%Y-%m-%dT%H:%M:%S")# date format
> str(dataset)
'data.frame':   2538 obs. of  5 variables:
 $ X        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ SessionID: int  13307 21076 27813 8398 23118 12256 28799 11457 7542 19261 ...
 $ Timestamp: POSIXct, format: "2014-04-06 18:42:05" "2014-04-03 15:27:48" "2014-04-04 09:10:14" "2014-04-03 23:39:20" ...
 $ ItemID   : int  214684513 214718203 214716928 214826900 214838180 214717318 214821307 214537967 214835775 214706432 ...
 $ Price    : int  0 0 0 0 0 0 0 0 0 0 ...
> dim(dataset)
[1] 2538    5
> set.seed(123)
>  smp_size <- floor(0.80 * nrow(dataset))
>  train_ind <- sample(seq_len(nrow(dataset)), size = smp_size)
>  trainset <- dataset[train_ind, ]
>  testset <- dataset[-train_ind, ]
> library(dplyr)
> trainset %>%
+     select(Price) %>%
+     mutate(
+         Price = Price %>% as.factor() %>% as.numeric()
+         ) %>%
+     correlate() %>%
+     focus(Price) %>%
+     fashion()

Correlation method: 'pearson'
Missing treated using: 'pairwise.complete.obs'

[1] rowname Price  
<0 rows> (or 0-length row.names)
> rec_obj <- recipe(Price ~ ., data = trainset) %>%
+     step_discretize(tenure, options = list(cuts = 6)) %>%
+     step_dummy(all_nominal(), -all_outcomes()) %>%
+     step_center(all_predictors(), -all_outcomes()) %>%
+     step_scale(all_predictors(), -all_outcomes()) %>%
+     prep(data = trainset)
Error in .f(.x[[i]], ...) : object 'tenure' not found
> rec_obj <- recipe(Price ~ ., data = trainset) %>%
+     step_discretize(SessionID, options = list(cuts = 6)) %>%
+     step_dummy(all_nominal(), -all_outcomes()) %>%
+     step_center(all_predictors(), -all_outcomes()) %>%
+     step_scale(all_predictors(), -all_outcomes()) %>%
+     prep(data = trainset)
Error: All columns selected for the step should be numeric
> str(dataset)
'data.frame':   2538 obs. of  5 variables:
 $ X        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ SessionID: int  13307 21076 27813 8398 23118 12256 28799 11457 7542 19261 ...
 $ Timestamp: POSIXct, format: "2014-04-06 18:42:05" "2014-04-03 15:27:48" "2014-04-04 09:10:14" "2014-04-03 23:39:20" ...
 $ ItemID   : int  214684513 214718203 214716928 214826900 214838180 214717318 214821307 214537967 214835775 214706432 ...
 $ Price    : int  0 0 0 0 0 0 0 0 0 0 ...
> rec_obj <- recipe(Price ~ ., data = trainset) %>%
+     step_discretize(SessionID) %>%
+     step_dummy(all_nominal(), -all_outcomes()) %>%
+     step_center(all_predictors(), -all_outcomes()) %>%
+     step_scale(all_predictors(), -all_outcomes()) %>%
+     prep(data = trainset)
Error: All columns selected for the step should be numeric
> rec_obj <- recipe(Price ~ ., data = trainset) %>%
+     prep(data = trainset)
> rec_obj
Data Recipe

Inputs:

      role #variables
   outcome          1
 predictor          4

Training data contained 2030 data points and no missing data.
> x_train_tbl <- bake(rec_obj, newdata = trainset)
> x_test_tbl  <- bake(rec_obj, newdata = testset)
> 
> glimpse(x_train_tbl)
Observations: 2,030
Variables: 5
$ X         <int> 730, 2000, 1038, 2239, 2384, 116, 1338, 2259, 1396, 1155, 2419, 1146, 1712, 1446, 260, 2271, 621, 107, 827, 2405, 2240, 1744, 1612, 2501, 1649, 1781, 1367, 1492, 726, 370, 2416, 2263, 1731, 1993, 62, 1196, 1898, 5...
$ SessionID <int> 14294, 6473, 23336, 9097, 10603, 11238, 449, 9307, 932, 31824, 10879, 1634, 3899, 1503, 5572, 9479, 9791, 4677, 8337, 10816, 9117, 4183, 3074, 12021, 3474, 4453, 708, 1983, 9413, 18759, 10866, 9401, 4069, 6409, 46...
$ Timestamp <dttm> 2014-04-01 14:25:41, 2014-04-01 19:33:15, 2014-04-04 13:27:15, 2014-04-05 16:20:44, 2014-04-07 20:09:20, 2014-04-02 18:52:28, 2014-04-01 15:42:00, 2014-04-07 19:42:19, 2014-04-04 14:09:41, 2014-04-04 06:02:38, 20...
$ ItemID    <int> 214821285, 214819547, 214833800, 214821285, 214753507, 214702852, 214832557, 214691383, 214819738, 214839311, 214552445, 214821275, 214832728, 214685792, 214511696, 214594678, 214839995, 214532070, 214635493, 2148...
$ Price     <int> 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, ...
> y_train_vec <- ifelse(pull(trainset, Price) == "1", 1, 0)
> y_test_vec  <- ifelse(pull(testset, Price) == "1", 1, 0)
> model_keras <- keras_model_sequential()
Using TensorFlow backend.
> 
> model_keras %>% 
+     # First hidden layer
+     layer_dense(
+         units              = 16, 
+         kernel_initializer = "uniform", 
+         activation         = "relu", 
+         input_shape        = ncol(x_train_tbl)) %>% 
+     # Dropout to prevent overfitting
+     layer_dropout(rate = 0.1) %>%
+     # Second hidden layer
+     layer_dense(
+         units              = 16, 
+         kernel_initializer = "uniform", 
+         activation         = "relu") %>% 
+     # Dropout to prevent overfitting
+     layer_dropout(rate = 0.1) %>%
+     # Output layer
+     layer_dense(
+         units              = 1, 
+         kernel_initializer = "uniform", 
+         activation         = "sigmoid") %>% 
+     # Compile ANN
+     compile(
+         optimizer = 'adam',
+         loss      = 'binary_crossentropy',
+         metrics   = c('accuracy')
+     )
> model_keras
Model
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Layer (type)                                                                                              Output Shape                                                                                  Param #                             
============================================================================================================================================================================================================================================
dense_1 (Dense)                                                                                           (None, 16)                                                                                    96                                  
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
dropout_1 (Dropout)                                                                                       (None, 16)                                                                                    0                                   
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
dense_2 (Dense)                                                                                           (None, 16)                                                                                    272                                 
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
dropout_2 (Dropout)                                                                                       (None, 16)                                                                                    0                                   
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
dense_3 (Dense)                                                                                           (None, 1)                                                                                     17                                  
============================================================================================================================================================================================================================================
Total params: 385
Trainable params: 385
Non-trainable params: 0
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________


> fit_keras <- fit(
+     object           = model_keras, 
+     x                = as.matrix(x_train_tbl), 
+     y                = y_train_vec,
+     batch_size       = 50, 
+     epochs           = 35,
+     validation_split = 0.30
+     )

but i got the following error

Train on 1421 samples, validate on 609 samples
Epoch 1/35
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: could not convert string to float: '2014-04-02 17:50:04'

Detailed traceback: 
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\models.py", line 1002, in fit
    validation_steps=validation_steps)
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\engine\training.py", line 1705, in fit
    validation_steps=validation_steps)
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\engine\training.py", line 1236, in _fit_loop
    outs = f(ins_batch)
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\backend\tensorflow_backend.py", line 2482, in __call__
    **self.session_kwargs)
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Users\ABRAR\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\python\client\session.py", line 1097, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)

how to solve this error?

  • Is timestamp variable necessary to be included in model? If not, you can remove it. If necessary, one possbile way is to convert it to unix timestamp format (seconds epoch). unixtimestamp.com – Ankit Seth Aug 10 at 4:31
  • timesteemp is necessary because i am working on e-visitor prediction that either the e-visitor will be a buyer or not and for this purpose timestamp is must. – maira khan Aug 10 at 4:50
  • can u plz provide me unix timestamp command used in r? – maira khan Aug 10 at 4:52
  • Did you solve your problem? Thanks – Dora Khiari Sep 12 at 15:30

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.