Git Product home page Git Product logo

keras's Introduction

R interface to Keras

R-CMD-check CRAN_Status_Badge license

Keras is a high-level neural networks API developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research. Keras has the following key features:

  • Allows the same code to run on CPU or on GPU, seamlessly.

  • User-friendly API which makes it easy to quickly prototype deep learning models.

  • Built-in support for convolutional networks (for computer vision), recurrent networks (for sequence processing), and any combination of both.

  • Supports arbitrary network architectures: multi-input or multi-output models, layer sharing, model sharing, etc. This means that Keras is appropriate for building essentially any deep learning model, from a memory network to a neural Turing machine.

See the package website at https://keras.posit.co for complete documentation.

keras's People

Contributors

16eagle avatar atroiano avatar ax3man avatar brianrice2 avatar cdk29 avatar christophersparling avatar dfalbel avatar edoffagne avatar edward22 avatar gsteinbu avatar ivokwee avatar janlauge avatar javierluraschi avatar jemus42 avatar jjallaire avatar jspiewak avatar kevinykuo avatar martinstuder avatar muschellij2 avatar osorensen avatar pitakakariki avatar rajshah4 avatar rsangole avatar statist-bhfz avatar t-kalinowski avatar terrytangyuan avatar tomazweiss avatar topepo avatar turgut090 avatar yulijia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras's Issues

problem with parallelization in kerasR

Suppose that we want to train several models in parallel using kerasR. When we train them sequentially with simple lapply, everything works fine. But when we use parallel::mclapply, the program gets stuck and nothing happens. Here is a minimal working example (on a side note, I know that I need to reinitialize the model weights before training it again, this is a minimal example).

reticulate::use_python("~/anaconda/bin")
library(kerasR)

logcat <- function(...) {
  cat(paste0(format(Sys.time(), "%H:%M:%OS3"), "  ", ..., "\n"))
}

# ---------------------------------------------------------------------------- #
# Define data
# ---------------------------------------------------------------------------- #

num_observations <- 100
num_features <- 4
features <- matrix(runif(num_observations * num_features), ncol = num_features)
observations <- sample(0:1, num_observations, replace = TRUE)
observation_categories <- kerasR::to_categorical(observations)

# ---------------------------------------------------------------------------- #
# Train model
# ---------------------------------------------------------------------------- #

trainModel <- function(iter, features, observation_categories){
  
  # define model
  logcat("define model ", iter)
  model <- Sequential()
  model$add(Dense(units = 20, input_shape = num_features))
  model$add(Activation("relu"))
  model$add(Dense(ncol(observation_categories)))
  model$add(Activation("softmax"))
  
  # compile model
  logcat("compile model ", iter)
  kerasR::keras_compile(model, loss = 'categorical_crossentropy', 
    optimizer = Adam(), metrics = "categorical_accuracy")
  
  # fit model
  logcat("fit model ", iter)
  keras_fit(model, features, observation_categories, epochs = 10, 
    batch_size = 10, verbose = 0)
  
  # model weights
  model$get_weights()
}

num_models <- 4

## using lapply
# model_weights <- lapply(seq(num_models), trainModel, features, 
#   observation_categories)

## using parallel::mclapply
model_weights <- parallel::mclapply(seq(num_models), trainModel, features, 
  observation_categories, mc.cores = 2, mc.silent = FALSE)

attempt to make fit pure functional

We can check for the python h5py module and if it's available use it to clone the compiled model. This will likely migrate to protobufs in the TF integrated version.

Error in image_load ... The Pillow Python package is required to load images

I've got the following errors;

> img2keras <- image_load("data/elephant.jpg", target_size = c(639, 639))
Error in image_load("data/elephant.jpg", target_size = c(639, 639)) :
The Pillow Python package is required to load images

From the windows cmd...
C:\Users\victor>pip install Pillow
Requirement already satisfied: Pillow in c:\programdata\anaconda3\lib\site-packages
Requirement already satisfied: olefile in c:\programdata\anaconda3\lib\site-packages (from Pillow)

I'd like to know how to configure image_load function for R to recognize Python Pillow library.

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

`Matrix products: default

locale:
[1] LC_COLLATE=Korean_Korea.949 LC_CTYPE=Korean_Korea.949 LC_MONETARY=Korean_Korea.949
[4] LC_NUMERIC=C LC_TIME=Korean_Korea.949 `

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] jpeg_0.1-8 dplyr_0.7.1 purrr_0.2.2.2 readr_1.1.1 tidyr_0.6.3 tibble_1.3.3
[7] ggplot2_2.2.1 tidyverse_1.1.1 keras_0.3.11

loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 cellranger_1.1.0 compiler_3.4.0 plyr_1.8.4 bindr_0.1
[6] forcats_0.2.0 tools_3.4.0 lubridate_1.6.0 jsonlite_1.5 nlme_3.1-131
[11] gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1 rlang_0.1.1.9000 psych_1.7.5
[16] parallel_3.4.0 haven_1.0.0 bindrcpp_0.2 xml2_1.1.1 httr_1.2.1
[21] stringr_1.2.0 hms_0.3 grid_3.4.0 reticulate_0.9.0.9000 glue_1.1.0
[26] R6_2.2.2 readxl_1.0.0.9000 foreign_0.8-68 modelr_0.1.0 reshape2_1.4.2
[31] magrittr_1.5 scales_0.4.1 rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5
[36] colorspace_1.3-2 tensorflow_0.9.0.9000 stringi_1.1.5 lazyeval_0.2.0.9000 munsell_0.4.3
[41] broom_0.4.2

float32 to float64

Hi,

If GPU acceleration expects float32 instead of float64 while R-64bit is used, is there an elegant way recommended to convert a 64 bit R object to 32 bit Python one?

Many thanks!

Plot cuts off validation points

My validation points are not shown in the plots. This may be because the y-axis starts from a higher value.

> history
Trained on 109,892 samples, validated on 12,211 samples (batch_size=128, epochs=25)
Final epoch (plot to see history):
     acc: 0.8822
    loss: 0.3102
val_loss: 1.389
 val_acc: 0.6713

I tried using ylim attribute but it doesnt seem to work

> plot(history, ylim=c(0,.9))

image

tensorboard() not working within RStudio

Hi,

First of all many thanks for making it so much easier to run deep learning in R.
I was able to run bunch of examples just fine but for some reason was not able to run tensorboard().

So I did pip install tensorboard and then from command line ran tensorboard --logdir=path/to/mydir and it started up just fine.

Not sure if there is any special setup I have to do to be able to utilize tensorboard() within RStudio?

Here is small snapshot of error I saw within RStudio

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

Traceback (most recent call last):
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/bin/tensorboard", line 7, in <module>
    from tensorflow.tensorboard.tensorboard import main
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: dlopen(/Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 10): Library not loaded: @rpath/libcublas.8.0.dylib
  Referenced from: /Users/tarunparmar/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so
  Reason: image not found

Best Regards,
Tarun

clear_session() missing?

Hello,

I train a lot of models and run in the same problem as is described in
keras-team/keras#3579

i.e. my tensorflow is getting cluttered and the newer models need more time. The solution in Python would be to use

from keras import backend as K
K.clear_session()

I can't seem to find a corresponding function within

https://rstudio.github.io/keras/reference/index.html

Is there a more elegant solution to this problem as loading/restarting several R-Sessions?

Greetings and thank you,
Martin

normalize_input undefined

Passing a list of inputs to a model returns an error that "normalize_input" is undefined.

The function appears to have been renamed "normalize" and redefining it by hand appears to fix the issue with no side effects "normalize_input=normalize"

extract parameters from individual layers

I have a CNN model, the configuration of whose individual layers I am trying to extract. I am trying to look for the R equivalent of model.layers[0].get_config()

I tried using model$get_layer(index = as.integer(1)) %>% get_config(), which returns Dict(19 items) of class "python.builtin.dict" "python.builtin.object". How do I access the individual parameters from this Dictionary?

Regression test with Boston Housing data results in 0 accuracy

library(keras)

cat('Loading data...\n')
bh <- dataset_boston_housing(path = "boston_housing.npz", seed = 113L,test_split = 0.2)
x_train <- bh$train$x
y_train <- bh$train$y
x_test <- bh$test$x
y_test <- bh$test$y

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

model <- keras_model_sequential()

model %>%
layer_dense(units = 13, input_shape = 13) %>%
layer_activation(activation = 'relu') %>%
layer_dense(units = 1)

compile model

model %>% compile(
loss = loss_mean_squared_error,
optimizer = optimizer_adam(),
metrics = c('accuracy')
)

train and evaluate

model %>% fit(
x_train,
y_train,
batch_size = 5,
epochs = 100,
verbose = 1,
validation_data = list(x_test, y_test)
)
scores <- model %>% evaluate(
x_test, y_test, verbose = 0
)

cat('Test loss:', scores[[1]], '\n')
cat('Test accuracy:', scores[[2]], '\n')

Output:
Test loss: 71.86488
Test accuracy: 0

Creating custom activation functions

Hi,

first of all thanks for porting Keras to R, this is extremely useful :-)

From looking into the source code I would have expected the following to work (this is just a minimal example, I actually want to define my own activation function using the backend provided tensor operations):


dummy_custom_activation <- function(x) {
  activations_linear(x)
}
attr(dummy_custom_activation, "py_function_name") <- "dummy_custom_activation"

model <- keras_model_sequential()
model %>% 
  layer_dense(units = 1, activation = dummy_custom_activation, input_shape = 1)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  Evaluation error: unused argument (list()).

Setting activation to 'dummy_custom_activation' also does not work.

faster RCNN

I'd like to know if there is the faster RCNN with R-Keras. Please let me know. Thank you.

Error when defining `optimizer` on Windows

This is the minimal example:

library(keras)

# create model
model <- keras_model_sequential()

# add layers and compile the model
model %>% 
  layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% 
  layer_dense(units = 1, activation = 'sigmoid')

optimizer <- optimizer_rmsprop(lr = 0.01)

model %>% compile(
    optimizer = optimizer,
    loss = 'binary_crossentropy',
    metrics = c('accuracy')
  )

# Generate dummy data
data <- matrix(runif(1000*100), nrow = 1000, ncol = 100)
labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1)

# Train the model, iterating on the data in batches of 32 samples
model %>% fit(data, labels, epochs=10, batch_size=32)

And the traceback:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
 TypeError: unorderable types: NoneType() > int()

Detailed traceback: 
 File "C:\Users\DANIEL~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 835, in fit
   initial_epoch=initial_epoch)
 File "C:\Users\DANIEL~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1457, in fit
   self._make_train_function()
 File "C:\Users\DANIEL~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 995, in _make_train_function
   self._collected_trainable_weights, self.constraints, self.total_loss)
 File "C:\Users\DANIEL~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\keras\python\keras\optimizers.py", line 217, in get_updates
   grads = self.get_gradients(loss, params)
 File "C:\Users\DANIEL~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tens 

When I set optimizer to "rmsprop" directly, it works fine.
This error only happened on Windows.

My session Info:

R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] keras_0.3.2

loaded via a namespace (and not attached):
[1] compiler_3.4.0        magrittr_1.5          R6_2.2.1              tools_3.4.0           Rcpp_0.12.10          reticulate_0.8        tensorflow_0.7.0.9008 jsonlite_1.4         

And

> library(tensorflow)
> tf$VERSION
[1] "1.1.0"

Support for Theano backend

Is it easy to add support for Theano or CNTK backends?

I've had a go with this package on Linux and absolutely loved it. Now I am on Windows and don't really want to bloat the computer with another Anaconda environment just to run Python 3.5 with tensorflow.

Please feel free to simply say no and I will use tensorflow. CNTK looks quite interesting for the future though, so would be cool to try it in R.

R output in case it is helpful:

library(keras)
model <- keras_model_sequential()
#> Error: Installation of TensorFlow not found.
#> 
#> Python environments searched for 'tensorflow' package:
#>  C:\Users\haydenr\Documents\Anaconda3\python.exe
#> 
#> You can install TensorFlow using the install_tensorflow() function.
model <- keras_model_sequential()
#> Error: Python module tensorflow.contrib.keras.python.keras was not found.
#> 
#> Detected Python configuration:
#> 
#> python:         C:\Users\haydenr\DOCUME~1\ANACON~1\python.exe
#> libpython:      C:/Users/haydenr/DOCUME~1/ANACON~1/python36.dll
#> pythonhome:     C:\Users\haydenr\DOCUME~1\ANACON~1
#> version:        3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]
#> Architecture:   64bit
#> numpy:          C:\Users\haydenr\DOCUME~1\ANACON~1\lib\site-packages\numpy
#> numpy_version:  1.12.1
#> tensorflow:     [NOT FOUND]

And Python output:

(C:\Users\haydenr\Documents\Anaconda3) C:\Users\haydenr>python
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import Sequential
Using Theano backend.
C:\Users\haydenr\Documents\Anaconda3\lib\site-packages\theano\gpuarray\dnn.py:135: UserWarning: Your cuDNN version is more recent than Theano. If you encounter problems, try updating Theano or downgrading cuDNN to version 5.1.
  warnings.warn("Your cuDNN version is more recent than "
Using cuDNN version 6021 on context None
Mapped name None to device cuda: GeForce GTX 960M (0000:01:00.0)
>>> model = Sequential()
>>> model
<keras.models.Sequential object at 0x000001AED520B940>
>>>

Thanks heaps,
Hayden

use fit_generator

Is there a concrete example of using fit_generator in rstudio/keras? It requires to provide a generator and I don't know if it is possible to write a generator in R. Any suggestions would be appreciated.

Python module tensorflow.contrib.keras.python.keras was not found.

Hi,

I installed Keras and Tensorflow GPU version on Ubuntu 16.
While trying to load the MNIST example it returns: "Python module tensorflow.contrib.keras.python.keras was not found."

Do you might know what the issue is?

I have added:

export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_HOME}/lib64
PATH=${CUDA_HOME}/bin:${PATH}
export PATH

to my .profile file.

Code:

library(keras)
library(tensorflow)
data<-dataset_mnist()
Error: Python module tensorflow.contrib.keras.python.keras was not found.

Detected Python configuration:

python: /home/blackfish/.virtualenvs/r-tensorflow/bin/python
libpython: /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome: /usr:/usr
virtualenv: /home/blackfish/.virtualenvs/r-tensorflow/bin/activate_this.py
version: 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
numpy: /home/blackfish/.virtualenvs/r-tensorflow/local/lib/python2.7/site-packages/numpy
numpy_version: 1.13.0
tensorflow: /home/blackfish/.virtualenvs/r-tensorflow/local/lib/python2.7/site-packages/tensorflow

python versions found:
/home/blackfish/.virtualenvs/r-tensorflow/bin/python
/home/blackfish/tensorflow/bin/python
/usr/bin/python
/usr/bin/python3

Creating Custom Metrics

Hey hey,

I'm trying to implement a custom metric that returns the mean response for the top 1.5% observations when sorting by predicted response. (see function below)

When running with the two lines commented out I get an index error so I tried to convert 'y_true' and 'y_pred' to numeric vectors, but it seems they can't be coerced as is.

Any idea how to get this working?

 customMetric <- function( y_true, y_pred ){
      #y_true <- as.numeric(y_true)
      #y_pred <- as.numeric(y_pred)
      return ( mean( y_true[ y_pred >= quantile(y_pred, .985) ] ) )
    }

Fatal error with conv layers in Windows 10 with GPU

I keep getting a fatal error when running convolutional layers. I am able to build models with dense, dropout, activation, and simple_rnn layers. I've included information on the R session, the model, and GPU. I've successfully installed CUDA 8.0 and cudnn 5.1 for CUDA 8.0.

PS. I am quite excited that you brought Keras and Tensorflow to R!!! Thanks!

sessionInfo()

R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] keras_0.3.6
loaded via a namespace (and not attached):
[1] compiler_3.4.0 magrittr_1.5 R6_2.2.1 tools_3.4.0 withr_1.0.2 Rcpp_0.12.10
[7] reticulate_0.8 memoise_1.1.0 tensorflow_0.8.2 jsonlite_1.5 digest_0.6.12 devtools_1.12.0

Model

Layer (type) Output Shape Param #
conv2d_1 (Conv2D) (None, 26, 26, 32) 320
conv2d_2 (Conv2D) (None, 24, 24, 64) 18496
max_pooling2d_1 (MaxPooling2D) (None, 12, 12, 64) 0
dropout_1 (Dropout) (None, 12, 12, 64) 0
flatten_1 (Flatten) (None, 9216) 0
dense_1 (Dense) (None, 128) 1179776
dropout_2 (Dropout) (None, 128) 0
dense_2 (Dense) (None, 10) 1290
===
Total params: 1,199,882.0
Trainable params: 1,199,882.0
Non-trainable params: 0.0

Device 0: "GeForce GTX 970"

CUDA Driver Version / Runtime Version 8.0 / 8.0
CUDA Capability Major/Minor version number: 5.2
Total amount of global memory: 4096 MBytes (4294967296 bytes)
(13) Multiprocessors, (128) CUDA Cores/MP: 1664 CUDA Cores
GPU Max Clock rate: 1178 MHz (1.18 GHz)
Memory Clock rate: 3505 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 1835008 bytes

Run "keras/docs/articles/examples/cifar10_cnn.R" Failed

Hi ,
I try to run keras/docs/articles/examples/cifar10_cnn.R to make sure all R/Python packages are installed OK.

When I try keras/docs/articles/examples/cifar10_cnn.R, I got the following Error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: init() got an unexpected keyword argument 'zca_epsilon'

May I have some hints?

Thanks in advance.

install_tensorflow() does not work inside a proxy environment

While installing tensorflow using the command install_tensorflow() I get the following error. My environment is Windows_7, R 3.4.0.

No matching distribution found for tensorflow
Error: Error 1 occurred installing tensorflow package
In addition: Warning message:
running command '"C:\PROGRA~1\Python35\/Scripts/pip.exe" install --upgrade --ignore-installed "tensorflow" "h5py" "pyyaml" "requests" "Pillow"' had status 1 

How can I get over this? I think this is a proxy issue.

FYI - Generally while installing using pip from command prompt I set proxy the following way before using pip comman..

set https_proxy=user:password@url/:port

tensor argument in input_layer throws error

I can't figure out how to utilize the tensor argument in input_layer. My assumption was that I could provide a matrix/array, which would obviate the need to provide the shape. However, I keep getting the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

Sample code:

input_matrix <- matrix(1:4, ncol = 2, nrow = 2)
input <- layer_input(tensor = input_matrix)

load_model_hdf5 errors on compile keyword

Trying to load a model using load_model_hdf5("mypath") fails with:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: load_model() got an unexpected keyword argument 'compile'

Running keras:::keras$models$load_model("mypath") appears to work great.

(Thanks for this awesome package!)

Example in the documentation does not work

I tried to run the example that you can find at https://rstudio.github.io/keras/ (Getting started: 30 seconds to Keras):

library(keras)
model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 512, activation = 'relu', input_shape = c(784)) %>% 
  layer_dropout(rate = 0.2) %>% 
  layer_dense(units = 512, activation = 'relu') %>%
  layer_dropout(rate = 0.2) %>%
  layer_dense(units = 10, activation = 'softmax')

But I get the error:

Error in keras_model_sequential(): could not find function "keras_model_sequential"

'application_resnet()' example fails with TensorFlow keras implementation

Given this code:

# Sys.setenv(KERAS_IMPLEMENTATION = "keras")
library(keras)

# instantiate the model
model <- application_resnet50(weights = 'imagenet')

# load the image
img_path <- "elephant.jpg"
img <- image_load(img_path, target_size = c(224, 224))
x <- image_to_array(img)

# ensure we have a 4d tensor with single element in the batch dimension,
# the preprocess the input for prediction using resnet50
dim(x) <- c(1, dim(x))
x <- imagenet_preprocess_input(x)

# make predictions then decode and print them
preds <- model %>% predict(x)
imagenet_decode_predictions(preds, top = 3)[[1]]

TensorFlow Keras

> imagenet_decode_predictions(preds, top = 3)[[1]]
  class_name           class_description score
1  n02098286 West_Highland_white_terrier     1
2  n15075141               toilet_tissue     0
3  n02319095                  sea_urchin     0

Stand-alone Keras

> imagenet_decode_predictions(preds, top = 3)[[1]]
  class_name class_description     score
1  n02504458  African_elephant 0.4545720
2  n01871265            tusker 0.3656018
3  n02504013   Indian_elephant 0.1791181

Any idea what might be going on here?

How do we shape data for multivariate RNN and LSTM?

I can't find any examples beyond uni-variate sequential problems.

I have 110 input features across two sequential time periods (t0, t1) and I'm predicting a binary outcome in t2.

Two things are unclear for me:
How do I shape the data fed to Keras?
How do I set the parameters in the layers to accept these data?

Are we to use a 3 dimensional array with time as the third dimension?
Are we to simply use a matrix where the first 110 columns correspond to t0, the next 110 correspond to t1 and the last column is the t2 binary outcome?

error when applying TimeDistributed to Input layer

Working on this PR (#11) I'm getting a strange error when we attempt to apply time_distributed to the input layer:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: as_list() is not defined on an unknown TensorShape. 

I've reduced the code to a much simpler variation which demonstrates the Python version working and the equivalent R version (using raw reticulate rather than our high level keras wrappers) yielding the same error:

library(keras)
library(reticulate)

py_run_string("
# alias layer functions
import tensorflow.contrib.keras as keras
from keras.layers import Input, TimeDistributed, LSTM

# define layers
x = Input(shape=(28, 28, 1))
encoded_rows = TimeDistributed(LSTM(128))(x)
")


# alias layer functions
keras <- keras:::keras
Input <- keras$layers$Input
TimeDistributed <- keras$layers$TimeDistributed
LSTM <- keras$layers$LSTM

# define layers
x <- Input(shape = c(28L,28L,1L))
lstm <- LSTM(units = 128L)
td_lstm <- TimeDistributed(lstm)
encoded_rows <- td_lstm(x)

@terrytangyuan or @dfalbel Is there anything you are aware of which might be triggering this error? I'm guessing some combination of Python language "tricks" which elude our interop layer or perhaps just an error in the way our interop layer handles some particular situation.

Save model object natively in R

Similar to apache/mxnet#362, it would be critical to be able to store the model object in a native R format (instead of JSON, HD5, or some other external file format).

Basically, without something like this, we can't embed the model into other R objects. The only other option that I can think of would be to save the JSON or similar as a text string, but that is pretty kludgy.

install error

ImportError: No module named 'tensorflow.contrib.keras'
Error: package or namespace load failed for ‘keras’

to_categorical() throws "IndexError:"

I have a matrix like this

> summary(as.factor(as.numeric(trainTransformed$waitCategory)))
   1    2    3    4    5    6    7    8    9 
1069   56   98   49   10    1    4    1    2 

y_train= matrix(as.numeric(trainTransformed$waitCategory))

As you can see there are only 9 classes. However whenever I try one-hot encoding I get the following error

> one_hot_labels <- to_categorical(y_train, num_classes =9)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  IndexError: index 9 is out of bounds for axis 1 with size 9

Detailed traceback: 
  File "C:\PROGRA~1\Python35\lib\site-packages\tensorflow\contrib\keras\python\keras\utils\np_utils.py", line 41, in to_categorical
    categorical[np.arange(n), y] = 1

This error disappears when I use num_classes=10 instead of 9. However it creates a matrix with 10 columns, which is not what I want.

I think this issue is related to this

Variational Autoencoder (VAE) Implementation using Keras in R

I am trying to developing VAE models using keras in R. It seems that all the VAE-relevant implementations using keras are done in Python. Even though there are some example python codes, I have not got any luck in implementing them in R.

I think it would be great if there are some tutorials or example codes on this topic. Any help will be much appreciated.

Best regards
Fan

shape of inputs /outputs for sequence to sequence model ?

Hi guys,
I am trying to learn a model to label some parts of sequences.
I would like the model to do something like: [64,34,77,33,88] -> [0,0,1,1,0]
So, in input I have a matrix of sequences (1 sequence per row) produced with pad_sequenceswhich looks like this:

int [1:21885, 1:30] 21 21 1506 28 102 21 61 224 15 15 ...

and the output is also a matrix of sequences produced by pad_sequences:

int [1:21885, 1:30] 0 0 1 0 0 0 1 1 0 0 ...

here is the code to simulate the shape of inputs/outputs I use:

input_length = 30
n_sample = 5
vocab_size = 100
quest_train <- matrix(floor(runif(input_length*n_sample, 1,vocab_size)), ncol = input_length)
tag_train <- matrix(sample(c(0,1), size = input_length*n_sample, replace = T), ncol = input_length)
input_dim = vocab_size
embed_dim = 50


model <- keras_model_sequential()
model %>%
  layer_embedding(input_dim = input_dim,
                output_dim = embed_dim) %>%
  layer_dropout(rate = 0.2) %>%
  layer_lstm(units = 128, return_sequences = T) %>%
  layer_dropout(rate = 0.5) %>% 
  time_distributed(layer_dense(units = 2, activation = 'softmax'))

model %>%
  compile(loss = 'categorical_crossentropy', 
          optimizer = 'adam', 
          metrics = c('accuracy'))

model %>% fit(quest_train,  
             tag_train, 
              batch_size = 16 ,
              epochs = 10, 
              shuffle = TRUE)

But when I try to run this I get this error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Error when checking model target: expected time_distributed_23 to have 3 dimensions, but got array with shape (5, 30)

So I tried to convert the output vector to a list of 2 dimensional matrices with to_categorical
like this:

tags_train_cat <- lapply(1:nrow(tag_train), function(x) (to_categorical(tag_train[x,])))

then my new target looks like this:

List of 5
 $ : num [1:30, 1:2] 1 1 1 1 1 0 1 1 1 1 ...
 $ : num [1:30, 1:2] 1 1 1 1 1 0 0 1 1 1 ...
 $ : num [1:30, 1:2] 0 1 1 1 1 1 1 1 1 1 ...
 $ : num [1:30, 1:2] 1 1 1 1 1 1 0 0 0 0 ...
...

but know I get this error:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of X arrays

So, my question is: What do I do wrong ?

R keras somewhat slower than python keras

Hi @jjallaire, first of all thanks for awesome R keras package, now R will be on par with Python for deep learning :)

I've been toying around and it seems the "same" code runs slower in R. On the MNIST dataset about 5sec/epoch for R and 3sec/epoch for Python with TF backend on Tesla K80 GPUs on AWS p2.xlarge instance.

I would think once training started, runtime should be the same, and R profiler shows all time is spent in .Call. Any ideas why the difference?

R code here
Python code here

Keras library : Installation failed

Hi

I am a newbie to Deep-learning & trying to install Keras library into R-Studio but with below error,

> library(devtools)
> install_github("rstudio/keras")
Downloading GitHub repo rstudio/keras@master
from URL https://api.github.com/repos/rstudio/keras/zipball/master
Installing keras
Installation failed: missing value where TRUE/FALSE needed
Error in if (file.exists(dest) && file.mtime(dest) > file.mtime(lib) &&  : 
  missing value where TRUE/FALSE needed

If I try installing from R-GUI then,

> install_github('rstudio/keras')
Downloading GitHub repo rstudio/keras@master
from URL https://api.github.com/repos/rstudio/keras/zipball/master
Installing keras
Installing 1 package: R6
Installing package into ‘C:/Users/'username'/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
Installation failed: missing value where TRUE/FALSE needed
Downloading GitHub repo rstudio/reticulate@master
from URL https://api.github.com/repos/rstudio/reticulate/zipball/master
Installing reticulate
Installation failed: missing value where TRUE/FALSE needed
Downloading GitHub repo rstudio/tensorflow@master
from URL https://api.github.com/repos/rstudio/tensorflow/zipball/master
Installing tensorflow
Installation failed: missing value where TRUE/FALSE needed
"C:/PROGRA~1/R/R-34~1.0/bin/x64/R" --no-site-file --no-environ --no-save  \
  --no-restore --quiet CMD INSTALL  \
  "C:/Users/'username'/AppData/Local/Temp/RtmpyYqQzk/devtools33f466c72026/rstudio-keras-887dd15"  \
  --library="C:/Users/'username'/Documents/R/win-library/3.4"  \
  --install-tests 

* installing *source* package 'keras' ...
** R
** inst
** tests
** preparing package for lazy loading
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : 
  namespace 'reticulate' 0.8.0.9005 is being loaded, but >= 0.8.0.9007 is required
ERROR: lazy loading failed for package 'keras'
* removing 'C:/Users/'username'/Documents/R/win-library/3.4/keras'
Installation failed: Command failed (1)

Please help me to fix this!!!

ERROR: dependency 'tensorflow' is not available for package 'keras'

I am trying to install keras from my Rstudio and R version 3.4.0 Patched , and i get this error--

devtools::install_github("rstudio/keras")

Output

  • installing source package 'reticulate' `...
    ** libs

*** arch - i386
c:/Rtools/mingw_32/bin/g++ -I"C:/PROGRA1/R/R-341.0PA/include" -DNDEBUG -I"C:/Users/hp/Documents/R/win-library/3.4/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
In file included from RcppExports.cpp:4:0:
reticulate_types.h:7:18: fatal error: Rcpp.h: No such file or directory
#include <Rcpp.h>
^
compilation terminated.
make: *** [RcppExports.o] Error 1
Warning: running command 'make -f "C:/PROGRA1/R/R-341.0PA/etc/i386/Makeconf" -f "C:/PROGRA1/R/R-341.0PA/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="reticulate.dll" OBJECTS="RcppExports.o interrupt.o libpython.o python.o"' had status 2
ERROR: compilation failed for package 'reticulate'

  • removing 'C:/Users/hp/Documents/R/win-library/3.4/reticulate'
  • restoring previous 'C:/Users/hp/Documents/R/win-library/3.4/reticulate'

installing source package 'tensorflow' ...
** R
** inst
** tests
** preparing package for lazy loading
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
namespace 'reticulate' 0.8 is being loaded, but >= 0.8.0.9003 is required
ERROR: lazy loading failed for package 'tensorflow'

  • removing 'C:/Users/hp/Documents/R/win-library/3.4/tensorflow'
    Installation failed: Command failed (1)
    "C:/PROGRA1/R/R-341.0PA/bin/x64/R" --no-site-file --no-environ --no-save
    --no-restore --quiet CMD INSTALL
    "C:/Users/hp/AppData/Local/Temp/Rtmpm6jYvs/devtools277cf242bcc/rstudio-keras-e79e54d"
    --library="C:/Users/hp/Documents/R/win-library/3.4" --install-tests

**ERROR: dependency 'tensorflow' is not available for package 'keras'

  • removing 'C:/Users/hp/Documents/R/win-library/3.4/keras'
    Installation failed: Command failed (1)`**

getting gradients

I am trying to get the value of the gradient (of the weights wrt to the loss) as R matrix. I got so far:

K <- backend()
wt <- model$trainable_weights[c(1,3,5)]
gr <- K$gradients(model$total_loss, wt)
dim(K$eval(wt[[1]]))
dim(K$eval(gr[[1]]))

K$eval(wt[[1]]) works but the last K$eval(gr[[1]]) throws these errors:

2017-07-26 20:40:03.651904: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,4000] has negative dimensions
2017-07-26 20:40:03.651978: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,4000] has negative dimensions

Anyone knows what to do?

Thanks,

Ivo

dataset_mnist occurs error

mnist <- dataset_mnist()
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.npz: None -- unknown url type: https

Detailed traceback: 
  File "/home/jjwkdl/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/tensorflow/contrib/keras/python/keras/datasets/mnist.py", line 37, in load_data
    path, origin='https://s3.amazonaws.com/img-datasets/mnist.npz')
  File "/home/jjwkdl/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/tensorflow/contrib/keras/python/keras/utils/data_utils.py", line 221, in get_file
    raise Exception(error_msg.format(origin, e.errno, e.reason))

Rstduio Server 1.0.143
Ubuntu 16.04 LTS

Error with CNTK backend

Hi,

I am trying to use keras with CNTK backend. Things work well in Python, meaning I can import Keras and it loads CNTK module successfully. When switching to R session, while I can load 'keras' library (
I have set environment variables, KERAS_BACKEDN='cntk' and KERAS_PYTHON='<path_to_my_python_bin>', before loading keras R library) but when I did 'backend()', it popped up error as following:

Error in py_module_import(module, convert = convert) : 
  ImportError: cannot import name 'HTTPSHandler'

Detailed traceback: 
  File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/__init__.py", line 3, in <module>
    from . import activations
  File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/activations.py", line 4, in <module>
    from . import backend as K
  File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/backend/__init__.py", line 77, in <module>
    from .cntk_backend import *
  File "/anaconda/envs/py35/lib/python3.5/site-packages/keras/backend/cntk_backend.py", line 2, in <module>
    import cntk as C
  File "/anaconda/envs/py35/lib/python3.5/site-packages/cntk/__init__.py", line 34, in <module>
    from .sample_installer import install_samples
  File "/anaconda/envs/py35/lib/python3.5/site-packages/cntk/sample_installer.py", line 13, in <module>
    import pip
  File "/anaconda/envs/py35/lib/python3.5/site-packages/pip/__init__.py", line 16, in <module>
    from pip.vcs import git, mercurial,
In addition: Warning message:
In stop(e, call. = FALSE) : additional arguments ignored in stop()
  • My OS is Linux Ubuntu 16.04.2 LTS.
  • Python 3.5.
  • R 3.3.3

Any advice? Many thanks!

ERROR: dependency 'processx' is not available for package 'tensorflow'

Command : devtools::install_github("rstudio/keras")

Downloading GitHub repo rstudio/keras@master
from URL https://api.github.com/repos/rstudio/keras/zipball/master
Installing keras
Downloading GitHub repo rstudio/tensorflow@master
from URL https://api.github.com/repos/rstudio/tensorflow/zipball/master
Installing tensorflow
"C:/PROGRA1/MIE74D1/RCLIEN1/R_SERVER/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL
"C:/Users/../AppData/Local/Temp/RtmpUrkTr6/devtools1d1c25a770c6/rstudio-tensorflow-8795bd8"
--library="C:/Users/../Documents/R/win-library/3.3" --install-tests

ERROR: dependency 'processx' is not available for package 'tensorflow'
removing 'C:/Users/girivr/Documents/R/win-library/3.3/tensorflow'
Error: Command failed (1)

Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'NoneType' object has no attribute 'write'

Hi all, I'm running the code below in R using the keras package.

library(keras)
use_condaenv('py35',required=TRUE)
reticulate::py_config()
tf_config()

model <- keras_model_sequential()
# add layers and compile the model
model %>% 
  layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% 
  layer_dense(units = 1, activation = 'sigmoid') %>% 
  compile(
    optimizer = 'rmsprop',
    loss = 'binary_crossentropy',
    metrics = c('accuracy')
  )

# Generate dummy data
data <- matrix(runif(1000*100), nrow = 1000, ncol = 100)
labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1)

# Train the model, iterating on the data in batches of 32 samples
model %>% fit(data, labels, epochs=20, batch_size=32)

# output predictions for first 10 rows
model %>% predict(data[1:10,]) %>% round()

# session info
devtools::session_info()

When I get to the line fitting the model (model %>% fit(data, labels, epochs=20, batch_size=32)), my script produces the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  AttributeError: 'NoneType' object has no attribute 'write'

Detailed traceback: 
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 835, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1494, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1144, in _fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end
    callback.on_batch_end(batch, logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callback

Strangely, when I compile/knit this as a report, everything works fine the FIRST time but then the error recurs. Any ideas what could be causing this?

Here's the output from py_config, tf_config, and session_info.

> reticulate::py_config()
python:         C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
libpython:      C:/Users/kdpsingh/AppData/Local/conda/conda/envs/py35/python35.dll
pythonhome:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35
version:        3.5.3 |Anaconda 4.4.0 (64-bit)| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow

python versions found: 
 C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
 C:\PROGRA~3\ANACON~1\python.exe

> tf_config()
TensorFlow v1.1.0 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow)
Python v3.5 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe)

> devtools::session_info()
Session info ---------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.2 (2016-10-31)
 system   x86_64, mingw32             
 ui       RStudio (1.0.136)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2017-06-06                  

Packages -------------------------------------------------------------------------------------
 package    * version date       source                             
 backports    1.0.4   2016-10-24 CRAN (R 3.3.2)                     
 devtools     1.12.0  2016-06-24 CRAN (R 3.3.3)                     
 digest       0.6.10  2016-08-02 CRAN (R 3.3.2)                     
 evaluate     0.10    2016-10-11 CRAN (R 3.3.2)                     
 htmltools    0.3.5   2016-03-21 CRAN (R 3.3.2)                     
 jsonlite     1.5     2017-06-01 CRAN (R 3.3.3)                     
 keras      * 0.3.6   2017-06-05 Github (rstudio/keras@023d109)     
 knitr        1.15.1  2016-11-22 CRAN (R 3.3.2)                     
 magrittr     1.5     2014-11-22 CRAN (R 3.3.2)                     
 memoise      1.0.0   2016-01-29 CRAN (R 3.3.3)                     
 R6           2.2.1   2017-05-10 CRAN (R 3.3.3)                     
 Rcpp         0.12.7  2016-09-05 CRAN (R 3.3.2)                     
 reticulate   0.8     2017-05-22 CRAN (R 3.3.3)                     
 rmarkdown    1.3     2016-12-21 CRAN (R 3.3.2)                     
 rprojroot    1.1     2016-10-29 CRAN (R 3.3.2)                     
 rstudioapi   0.6     2016-06-27 CRAN (R 3.3.3)                     
 stringi      1.1.2   2016-10-01 CRAN (R 3.3.2)                     
 stringr      1.1.0   2016-08-19 CRAN (R 3.3.2)                     
 tensorflow   0.8.2   2017-06-05 Github (rstudio/tensorflow@bace720)
 withr        1.0.2   2016-06-20 CRAN (R 3.3.3)  

reproducible results

Hi everyone!

Each time I train a model on the same data set, I receive the different result. I already set the seed set.seed(1) but it does not work. How can I make the results the same for each run?
Thank you very much!

library(keras)
# Generate dummy data
set.seed(1)
data <- matrix(runif(1000*100), nrow = 1000, ncol = 100)
labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1)

# repeat running the code from this:
model <- keras_model_sequential()

# add layers and compile the model
model %>% 
    layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% 
    # layer_dropout(rate = 0.25) %>%
    # layer_batch_normalization() %>%
    layer_dense(units = 1, activation = 'relu') %>% 
    compile(
        optimizer = 'adam',
        loss = 'mean_squared_error',
        metrics = 'mean_squared_error'
    )

# Train the model, iterating on the data in batches of 32 samples
set.seed(1)
model %>% fit(data, labels, epochs=10, batch_size=32)

Vectorization requires large memory in lstm_text_generation example

Hi,

I am experimenting with char-rnn and I am using text data (encoding = 'UTF-8') from here.(Alice in Wonderland Book)

I removed all the text before header and after THE END.

HEADER:
*** START OF THIS PROJECT GUTENBERG EBOOK ALICE'S ADVENTURES IN WONDERLAND ***
.
.
story
.
.
***THE END***

While trying out the lstm_text_generation.R example; I am having issue while vectorization.

The line X <- array(0, dim = c(length(dataset$sentece), maxlen, length(chars))) takes 5.1 Gb of space.
Is there any alternate solution to this?

When I run the for loop for converting char to int I get following error:
Error: cannot allocate vector of size 5.1 Gb

Following is my complete code till vectorization (which is exactly same as in example)

library(keras)
library(tokenizers)
library(stringr)
library(purr)

### Parameters ####################################################################

max.len <- 100

### Data preparation ##############################################################

raw_text <- readLines("data/short_text.txt", encoding = "UTF-8") %>%
            str_to_lower() %>%
            str_c(collapse = "\n") %>%
            tokenize_characters(strip_non_alphanum = F, simplify = T)

print(sprintf("Corpus Length: %d ", length(raw_text)))
            
chars <- raw_text %>%
            unique() %>%
            sort()

print(sprintf("Characters: %d ", length(chars)))

dataset <- map(
      seq(1, length(raw_text)-max.len, by = 1), ~list(sentence = raw_text[.x:(.x+max.len-1)], next_char = raw_text[.x+max.len])
)

dataset <- transpose(dataset)

# vectorization

x <- array(0, dim = c(length(dataset$sentence), max.len))
y <- array(0, dim = c(length(dataset$sentence), length(chars)))

for(i in 1:length(dataset$sentece)){
      
      x[i,] <- sapply(chars, function(x){
            as.integer(x == dataset$sentece[[i]])
      })
      
      y[i,] <- as.integer(chars == dataset$next_char[[i]])
      
}

Support for Sparse Matrices

Currently the x object accepted by the fit function is a

Vector, matrix, or array of training data...

It would be great if there is also support for sparse matrices, as these X matrices can grow very large on disk, although they can be very sparse. For example, the Nietzsche example runs fine on my 32GB RAM laptop, but I run into RAM problems even with twice as many rows. And this is a classic case for a sparse matrix.

Unexpected behavior of layer_permute

See the minimal example:

placeholder <- layer_input(shape = c(10, 9))
permute_r <- layer_permute(dims = c(2,1))
permute_r(placeholder)
# Error in py_call_impl(callable, dots$args, dots$keywords) : 
#   ValueError: Input 0 is incompatible with layer permute_7: expected ndim=2, found ndim=3
permute <- tf$contrib$keras$layers$Permute(dims = c(2L,1L))
permute(placeholder)
# Tensor("permute_8/transpose:0", shape=(?, 9, 10), dtype=float32)

The same example can be found on keras docs: https://keras.io/layers/core/#permute

Pad sequences fails when word count is equal to one

I am working on an Embedding layer and discovered after creating the train set with texts_to_sequences(), pad_sequences() will fail on observations which contain only one word. Example and sessionInfo() below.

library(tidyverse)
library(stringr)
library(keras)
set.seed(1234)

--from Green Eggs and Ham

df <- data_frame(comment = c("I would not like them",
"Here or there.",
"I would not like them",
"Anywhere"),
y = as.numeric(c(0,
1,
0,
1)))

word_count <- function(x) {
cnt <- length(str_split(x, " ")[[1]])
cnt
}

map_dbl(df$comment, ~ word_count(.x))
max_len <- 10
max_features <- 25

tokenizer <- text_tokenizer(num_words = max_features)
tokenizer$fit_on_texts(df$comment)
x_train <- tokenizer$texts_to_sequences(df$comment)

-- blows up on this step with a single word in the comment
x_train <- pad_sequences(x_train, maxlen = max_len)
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: sequences must be a list of iterables. Found non-iterable: 8

Detailed traceback:
File "/Users/v800655/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/preprocessing/sequence.py", line 64, in pad_sequences
'Found non-iterable: ' + str(x))

sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.5

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] keras_0.3.14 stringr_1.2.0 dplyr_0.7.1 purrr_0.2.2.2 readr_1.1.1
[6] tidyr_0.6.3 tibble_1.3.3 ggplot2_2.2.1 tidyverse_1.1.1

loaded via a namespace (and not attached):
[1] Rcpp_0.12.12 cellranger_1.1.0 plyr_1.8.4 bindr_0.1
[5] forcats_0.2.0 tools_3.3.2 packrat_0.4.8-12 jsonlite_1.5
[9] lubridate_1.6.0 nlme_3.1-128 gtable_0.2.0 lattice_0.20-34
[13] pkgconfig_2.0.1 rlang_0.1.1 psych_1.7.3.21 parallel_3.3.2
[17] haven_1.0.0 bindrcpp_0.2 xml2_1.1.1 httr_1.2.1
[21] hms_0.3 grid_3.3.2 reticulate_0.9.0.9004 glue_1.1.1.9000
[25] R6_2.2.2 readxl_1.0.0 foreign_0.8-67 reshape2_1.4.2
[29] modelr_0.1.0 magrittr_1.5 tfruns_0.9 scales_0.4.1
[33] rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5 colorspace_1.3-2
[37] tensorflow_0.9.0.9010 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3
[41] broom_0.4.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.