0

我正在使用 kerasR 和 Tensorflow 包在 R 中构建神经网络。任务:二元分类(默认预测) ANN 结构为 (23, 16, 8, 4, 1)。

我的代码如下:

#------------------------------------------------------------------------
#------------------------------------------------------------------------
## 1. PACKAGES INSTALLATION AND ACTIVATION

#install.packages(kerasR)
#install.packages(tensorflow)
#install.packages(httr)
#install.packages(RCurl)
#install.packages(readxl)
#install.packages(remotes)
#remotes::install_github("rstudio/tensorflow")
library(tensorflow)
#install_tensorflow(version = "2.0.0")
library(keras)
library(kerasR)
library(httr)
library(RCurl)
library(readxl)

#------------------------------------------------------------------------
## 2. DATA PREPROCESSING
# Get data
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/00350/default%20of%20credit%20card%20clients.xls"
GET(url, write_disk("default.xls", overwrite=TRUE))
dataset <- read_xls('default.xls', sheet = 1, skip = 1)
# Drop the index column and rename the target variable column 
dataset$ID <- NULL
colnames(dataset)[24] <- "default"
# Remove missing values
dataset <- na.omit(dataset)
with(dataset, sum(is.na(default)))

#------------------------------------------------------------------------
## 3. DATA SPLIT: TRAIN DATASET AND TEST DATASET 
# Random sampling, create training (80%) and test set (20%)
set.seed(80)
samplesize = 0.80 * nrow(dataset)
index = sample( seq_len ( nrow ( dataset ) ), size = samplesize )
X_train = as.matrix(dataset[index , 1:23])
Y_train = as.matrix(dataset[index , 24])
X_test = as.matrix(dataset[-index , 1:23])
Y_test = as.matrix(dataset[-index , 24])


#------------------------------------------------------------------------
## 4. DATA SCALING 
scaled_X_train = scale(X_train)
scaled_X_test = scale(X_test)
Y_train <- to_categorical(Y_train, 2)

#------------------------------------------------------------------------
## 5. TRAIN NN MODEL
# Construct the NN

mod <- Sequential()

mod$add(Dense(units = 16, input_shape = dim(X_train)[2]))
mod$add(Activation("relu"))
mod$add(Dropout(0.25))

mod$add(Dense(units = 8))
mod$add(Activation("relu"))
mod$add(Dropout(0.25))

mod$add(Dense(units = 4))
mod$add(Activation("relu"))
mod$add(Dropout(0.25))

mod$add(Dense(1))

#Regularisation
#mod$add(ActivityRegularization(l1 = 1, l2 = 0))

mod$add(Activation("sigmoid"))

opt<-optimizer_adam( lr= 0.0001 , decay = 0, clipnorm = 1 )
keras_compile(mod,  loss = 'binary_crossentropy', optimizer = opt, metrics = "binary_accuracy")

#Train the NN
keras_fit(mod, X_train, Y_train,
          batch_size = 16, epochs = 20,
          verbose = 0, validation_split = 0.1)

output

#------------------------------------------------------------------------

我得到如下错误:

py_call_impl(callable, dots$args, dots$keywords) 中的错误:ValueError:在用户代码中:

/home/hang-nguyen1/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
    outputs = self.distribute_strategy.run(
/home/hang-nguyen1/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/hang-nguyen1/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/home/hang-nguyen1/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
    return fn(*args, **kwargs)
/home/hang-nguyen1/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-pac

输出错误:找不到对象“输出”

谁能帮我解释一下这个错误?太感谢了!!!

4

0 回答 0