rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
833 stars 282 forks source link

AttributeError in timeseries_generator() #335

Closed Maiae closed 6 years ago

Maiae commented 6 years ago

Installed v 2.1.5 and when running timeseries_generator() I get:

Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'keras.preprocessing.sequence' has no attribute 'TimeseriesGenerator'

Full code below:

library(tensorflow)
library(keras)

data <- matrix(1:50, nrow = 50, ncol = 1)
targets <- matrix(1:50, nrow = 50, ncol = 1)

data_gen <- timeseries_generator(data, targets, length = 10, sampling_rate = 2, batch_size = 2)

At this point I get:

Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'keras.preprocessing.sequence' has no attribute 'TimeseriesGenerator'

jjallaire commented 6 years ago

This is likely b/c you have an older version of the underlying Keras library installed (timeseries_generator was added only very recently. What is the output of:

keras:::keras_version()

You can update your version of Keras with:

library(keras)
install_keras()
Maiae commented 6 years ago

Thanks, it works. My underlying Keras library was v2.0.5. After running install_keras() I am now on v2.1.5 and timeseries_generator() works.

Closing this now.

westland commented 6 years ago

timeseries_generator() might still have a problem in the Ubuntu 16.04 installation of keras:::keras_version() ‘2.1.6’ ... I ran similar code, and this terminated the R session with the error:

2018-05-03 11:40:22.638687: W tensorflow/core/framework/op_kernel.cc:1318] OP_REQUIRES failed at conv_ops.cc:379 : Invalid argument: Computed output size would be negative: -4 [input_size: 0, effective_filter_size: 5, stride: 1] Error in py_call_impl(callable, dots$args, dots$keywords) : InvalidArgumentError: Computed output size would be negative: -4 [input_size: 0, effective_filter_size: 5, stride: 1] [[Node: conv1d_2/convolution/Conv2D = Conv2D[T=DT_FLOAT, _class=["loc:@train...propFilter"], data_format="NHWC", dilations=[1, 1, 1, 1], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](conv1d_2/convolution/ExpandDims, conv1d_2/convolution/ExpandDims_1)]]

Caused by op u'conv1d_2/convolution/Conv2D', defined at: File "/home/chris/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/keras/models.py", line 522, in add output_tensor = layer(self.outputs[0]) File "/home/chris/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/keras/engine/topology.py", line 619, in call output = self.call(inputs, **kwargs) File "/home/chris/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/keras/layers/convolutional.py", line 160, in call dilation_rate=self.dilation_rate[0]) File

Here is the code:

devtools::install_github("rstudio/keras") library(keras) install_keras()

data <- matrix(1:681, nrow = 681, ncol = 1) trgt <- matrix(1:681, nrow = 681, ncol = 1)

lngth = 10
samp_rate = 2
strd = 1
btch_sz = 2
val_steps <- (nrow(data) - 401 - lngth) / btch_sz

train_gen = timeseries_generator( data=data, targets=trgt, length=lngth, sampling_rate = samp_rate, stride = strd,
start_index = 1, end_index = 400, shuffle = FALSE, reverse = FALSE, batch_size = btch_sz )

val_gen = timeseries_generator( data=data, targets=trgt, length=lngth, sampling_rate = samp_rate, stride= strd,
start_index = 401, end_index = NULL, shuffle = FALSE, reverse = FALSE, batch_size = btch_sz )

model <- keras_model_sequential() %>% layer_conv_1d(filters = 32, kernel_size = 5, activation = "relu", input_shape = list(NULL, dim(data)[[-1]])) %>% layer_max_pooling_1d(pool_size = 3) %>% layer_conv_1d(filters = 32, kernel_size = 5, activation = "relu") %>% layer_gru(units = 32, dropout = 0.1, recurrent_dropout = 0.5) %>% layer_dense(units = 1)

model %>% compile( optimizer = optimizer_rmsprop(), loss = "mae", metrics = c("mae") )

model %>% fit_generator( train_gen, steps_per_epoch = 10, epochs = 10, validation_data = val_gen, validation_steps = val_steps
)