Я использую новейшую версию R studio, 1.2.1335. Я пытаюсь использовать модель LSTM, используя сетку внутри скрипта R или PYthon внутри документа Rmarkdown, но оба возвращают ошибку.
Во-первых, моя попытка с сеткой:
library(reticulate)
use_condaenv('my_env')
SAMPLES=10000
A = 0.7
B = 10000.0
AMPU = 0.2
AMPN = 0.08
LAG = 5
tseq <- seq(0,0.1*pi,,(SAMPLES+LAG))
set.seed(1)
c.unif <- runif(SAMPLES+LAG)
c.norm <- rnorm(SAMPLES+LAG)
y1 <- A*sin(B*tseq)+c.unif*AMPU
y2 <- A*sin(B*tseq)+c.norm*AMPN
data <- cbind(y1[1:SAMPLES],y2[1:SAMPLES])
trgt <- cbind(y1[LAG:(SAMPLES+LAG-1)],y2[LAG:(SAMPLES+LAG-1)])
LEN = 8
SRATE = 1
STRIDE = 1
BATCH = 16
### TEST
np <- import("numpy")
keraspy <- import("keras")
pybuiltin <- import_builtins(convert = TRUE)
train_gen <- keraspy$preprocessing$sequence$TimeseriesGenerator(
data=data,
targets=trgt,
length=LEN,
sampling_rate = SRATE,
stride = STRIDE,
start_index = 1,
end_index = 9000,
shuffle = FALSE,
reverse = FALSE,
batch_size = BATCH
)
val_gen = keraspy$preprocessing$sequence$TimeseriesGenerator(
data=data,
targets=trgt,
length=LEN,
sampling_rate = SRATE,
stride= STRIDE,
start_index = 9001,
end_index = 10000,
shuffle = FALSE,
reverse = FALSE,
batch_size = BATCH
)
model = keraspy$models$Sequential()
model$add(keraspy$layers$Flatten(input_shape = c(pybuiltin$int(LEN), 2L)))
model$add(keraspy$layers$Dense(units = 32L, activation = "relu"))
model$add(keraspy$layers$Dense(units = 2L))
model$compile(optimizer = "rmsprop", loss = "mae")
stepsPerEpoch <- floor((train_gen$end_index - train_gen$start_index)/BATCH)
validationSteps <- floor((val_gen$end_index - val_gen$start_index)/BATCH)
model$fit_generator(
train_gen,
steps_per_epoch = pybuiltin$int(stepsPerEpoch),
epochs = pybuiltin$int(100),
validation_data = val_gen,
validation_steps = pybuiltin$int(validationSteps)
)
Эта ошибка возврата упоминается в заголовке:
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: 'float' object cannot be interpreted as an integer
Я также пытался оценить ту же модель внутри ноутбука R:
---
title: "R Notebook"
output: html_notebook
---
```{r}
SAMPLES=10000
A = 0.7
B = 10000.0
AMPU = 0.2
AMPN = 0.08
LAG = 5
tseq <- seq(0,0.1*pi,,(SAMPLES+LAG))
set.seed(1)
c.unif <- runif(SAMPLES+LAG)
c.norm <- rnorm(SAMPLES+LAG)
y1 <- A*sin(B*tseq)+c.unif*AMPU
y2 <- A*sin(B*tseq)+c.norm*AMPN
data <- cbind(y1[1:SAMPLES],y2[1:SAMPLES])
trgt <- cbind(y1[LAG:(SAMPLES+LAG-1)],y2[LAG:(SAMPLES+LAG-1)])
LEN = 8
SRATE = 1
STRIDE = 1
BATCH = 16
```
```{python}
import numpy as np
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense
import keras
import math
train_gen = TimeseriesGenerator(
data=r.data,
targets=r.trgt,
length=r.LEN,
sampling_rate = r.SRATE,
stride = r.STRIDE,
start_index = 1,
end_index = 9000,
shuffle = False,
reverse = False,
batch_size = r.BATCH
)
val_gen = TimeseriesGenerator(
data= r.data,
targets = r.trgt,
length= r.LEN,
sampling_rate = r.SRATE,
stride= r.STRIDE,
start_index = 9001,
end_index = 10000,
shuffle = False,
reverse = False,
batch_size = r.BATCH
)
math.floor((train_gen.end_index - train_gen.start_index)/int(r.BATCH))
train_gen.end_index
model = Sequential()
model.add(keras.layers.Flatten(input_shape = (int(r.LEN), 2)))
model.add(Dense(32, activation = "relu"))
model.add(Dense(2))
model.compile(optimizer = 'rmsprop', loss = 'mae')
model.fit_generator(
train_gen,
steps_per_epoch = math.floor((train_gen.end_index - train_gen.start_index)/float(r.BATCH)),
epochs = 100,
validation_data = val_gen,
validation_steps = math.floor((val_gen.end_index - val_gen.start_index)/float(r.BATCH))
)
```
но я получил ту же ошибку.
Если на последнем шаге я определяю числа как целые числа, я получаю сообщение об ошибке:
model.fit_generator(
train_gen,
steps_per_epoch = int(561),
epochs = int(100),
validation_data = val_gen,
validation_steps = int(61)
)
Я впервые использую python внутри Rstudio. Не очень перспективно ...