在Rstudi中使用python时,“float”对象不能解释为整数错误

2024-10-02 20:37:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是R studio的最新版本1.2.1335。我尝试在R脚本中使用网状结构使用LSTM模型,或者在Rmarkdown文档中使用PYthon,但是都返回了一个错误。你知道吗

首先,我对网状结构的尝试:

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)) 
)
```

但我也犯了同样的错误。你知道吗

如果我在最后一步将数字定义为整数,我会得到smae错误:

model.fit_generator(
  train_gen,
  steps_per_epoch = int(561),
  epochs = int(100),
  validation_data = val_gen,
  validation_steps = int(61) 
)

这是我第一次在Rstudio中使用python。不是很有希望。。。你知道吗


Tags: importdataindexmodellenbatchtrainval