r编程错误中的py_call_impl(可呼叫,点$ args,dots $ keywords):value eRror:数据基数模棱两可:x尺寸:13704 y尺寸:5710

发布于 2025-01-18 17:04:15 字数 1496 浏览 0 评论 0原文

我正在尝试使用R编程学习深度学习。但是卡住了这个错误。 py_call_impl中的错误(可呼叫,点$ args,点$关键字): ValueError:数据基数模棱两可: X尺寸:13704 Y尺寸:5710 确保所有阵列都包含相同数量的样本。

代码:

# Libraries required
install_tensorflow(method = "auto")
library(keras)
library(tensorflow)
library(caret)
library(reticulate)
library(stringr)

data <- fnd2
head(data)
x <- data[,c(2:4)]    
y <- data[,5]       

head(x)
head(y)

train_data_percentage <- 0.75
train_number <- round(nrow(x)*train_data_percentage)  
set.seed(1234)

index <- sample( c(1:nrow(data)), train_number, replace = F )

train_x <- as.matrix(x[index,])
train_y <- as.matrix(y[index])

test_x <- as.matrix(x[-index,])
test_y <- as.matrix(y[-index])

length(train_x); length(test_x); length(train_y); length(test_y)

table(train_y)
table(test_y)

# Padding sequences
train_x <- str_pad(train_x, width = 0, pad = " ")
test_x <- str_pad(test_x, width = 0, pad = " ")

# Model
model <- keras_model_sequential() 
model %>% 
  layer_embedding(input_dim = 500, output_dim = 32) %>%
  layer_simple_rnn(units = 32) %>%  
  layer_dense(units = 1, activation = "sigmoid")

# Compile
model %>% compile(optimizer = "rmsprop",
              loss = "binary_crossentropy",
              metrics = c("acc"))

# Fit model
history <- model %>% fit(train_x, train_y,
                     epochs = 25,
                     batch_size = 128,
                     validation_split = 0.2)

执行拟合模型部分后,此错误弹出。有人可以帮忙吗?谢谢..

I am trying to learn deep learning using R programming. But stuck with this error.
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Data cardinality is ambiguous:
x sizes: 13704
y sizes: 5710
Make sure all arrays contain the same number of samples.

Code:

# Libraries required
install_tensorflow(method = "auto")
library(keras)
library(tensorflow)
library(caret)
library(reticulate)
library(stringr)

data <- fnd2
head(data)
x <- data[,c(2:4)]    
y <- data[,5]       

head(x)
head(y)

train_data_percentage <- 0.75
train_number <- round(nrow(x)*train_data_percentage)  
set.seed(1234)

index <- sample( c(1:nrow(data)), train_number, replace = F )

train_x <- as.matrix(x[index,])
train_y <- as.matrix(y[index])

test_x <- as.matrix(x[-index,])
test_y <- as.matrix(y[-index])

length(train_x); length(test_x); length(train_y); length(test_y)

table(train_y)
table(test_y)

# Padding sequences
train_x <- str_pad(train_x, width = 0, pad = " ")
test_x <- str_pad(test_x, width = 0, pad = " ")

# Model
model <- keras_model_sequential() 
model %>% 
  layer_embedding(input_dim = 500, output_dim = 32) %>%
  layer_simple_rnn(units = 32) %>%  
  layer_dense(units = 1, activation = "sigmoid")

# Compile
model %>% compile(optimizer = "rmsprop",
              loss = "binary_crossentropy",
              metrics = c("acc"))

# Fit model
history <- model %>% fit(train_x, train_y,
                     epochs = 25,
                     batch_size = 128,
                     validation_split = 0.2)

After executing fit model part, this error pops up. Can someone please help? Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文