我正在从Rbloggers复制示例,但是train函数会导致错误:
py_call_impl(可调用,dots $ args,dots $ keywords)错误: ValueError:列dtype和SparseTensors dtype必须兼容。键:ALUMNUS_IND,列dtype:,张量dtype:
该示例的原始代码没有ALUMNUS_IND的“ dtype = tf $ int32”,但是会导致相同的错误消息。有没有办法强制使用int32或完成火车功能的解决方案是什么?
tensorflow软件包是1.9
library(readr)
library(dplyr)
library(tensorflow)
library(tfestimators)
donor_data <- read_csv("https://www.dropbox.com/s/ntd5tbhr7fxmrr4/DonorSampleDataCleaned.csv?raw=1")
my_mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
donor_data <- donor_data %>%
mutate_if(is.numeric,
.funs = funs(
ifelse(is.na(.),
median(., na.rm = TRUE),
.))) %>%
mutate_if(is.character,
.funs = funs(
ifelse(is.na(.),
my_mode(.),
.)))
predictor_cols <- c("MARITAL_STATUS", "GENDER",
"ALUMNUS_IND", "PARENT_IND",
"WEALTH_RATING", "PREF_ADDRESS_TYPE")
# Convert feature to factor
donor_data <- mutate_at(donor_data,
.vars = predictor_cols,
.funs = as.factor)
feature_cols <- feature_columns(
column_indicator(
column_categorical_with_vocabulary_list(
"MARITAL_STATUS",
vocabulary_list = unique(donor_data$MARITAL_STATUS))),
column_indicator(
column_categorical_with_vocabulary_list(
"GENDER",
vocabulary_list = unique(donor_data$GENDER))),
column_indicator(
column_categorical_with_vocabulary_list(
"ALUMNUS_IND",
vocabulary_list = unique(donor_data$ALUMNUS_IND),
dtype = tf$int32)),
column_indicator(
column_categorical_with_vocabulary_list(
"PARENT_IND",
vocabulary_list = unique(donor_data$PARENT_IND))),
column_indicator(
column_categorical_with_vocabulary_list(
"WEALTH_RATING",
vocabulary_list = unique(donor_data$WEALTH_RATING))),
column_indicator(
column_categorical_with_vocabulary_list(
"PREF_ADDRESS_TYPE",
vocabulary_list = unique(donor_data$PREF_ADDRESS_TYPE))),
column_numeric("AGE"))
row_indices <- sample(1:nrow(donor_data),
size = 0.8 * nrow(donor_data))
donor_data_train <- donor_data[row_indices, ]
donor_data_test <- donor_data[-row_indices, ]
donor_pred_fn <- function(data) {
input_fn(data,
features = c("AGE", "MARITAL_STATUS",
"GENDER", "ALUMNUS_IND",
"PARENT_IND", "WEALTH_RATING",
"PREF_ADDRESS_TYPE"),
response = "DONOR_IND")
}
classifier <- dnn_classifier(
feature_columns = feature_cols,
hidden_units = c(80, 40, 30),
n_classes = 2,
label_vocabulary = c("N", "Y"))
train(classifier,
input_fn = donor_pred_fn(donor_data_train))
答案 0 :(得分:0)
注释以下代码以将其修复-
# Convert feature to factor
donor_data <- mutate_at(donor_data,.vars = predictor_cols,
.funs = as.factor)
答案 1 :(得分:0)
RDRR给出了很好的意见。请将.funs = as.factor
更改为.funs = as.character
。
# Convert feature to character
donor_data <- mutate_at(donor_data,
.vars = predictor_cols,
.funs = as.character)