【发布时间】:2020-03-12 12:59:43
【问题描述】:
假设我有一个包含 10 个观察值的数组(属于 A 类或 B 类)、5 列和 2 个子类(C、D)作为附加维度,我想做一个二元分类(到 A 类或 B 类)在 Keras R 中。在这种情况下,网络架构应该是什么样的?
library("keras")
df = data.frame(class = c(rep("A", 10), rep("B", 10)),
subclass = rep(c("C", "D"), 10),
feature1 = rnorm(20),
feature2 = rnorm(20),
feature3 = rnorm(20))
df1 = df[df$subclass == "C", ]
df2 = df[df$subclass == "D", ]
df_list = list(df1, df2)
build_model = function() {
model = keras_model_sequential()
model %>%
# input_shape is 3 features and 2 subclasses
layer_dense(units = 2, activation = 'sigmoid', input_shape = c(3, 2))
model %>%
compile(
optimizer = "adam",
loss = "binary_crossentropy",
metrics = list("accuracy")
)
}
# one hot encoding to A, B classes
labels = to_categorical(as.integer(df_list[[1]][, "class"]) - 1)
# drop factor columns
data = lapply(df_list, function(x) x[, -(1:2)])
# convert to array
data_array = array(unlist(c(data[[1]], data[[2]])), dim = c(10, 3, 2))
model = build_model()
# error appears in the following function:
history = model %>% fit(
x = data_array,
y = labels
)
错误:
py_call_impl 中的错误(可调用,dots$args,dots$keywords):
ValueError: 一个形状为 (10, 2) 的目标数组被传递给一个输出 形状 (None, 3, 2),同时用作损失
binary_crossentropy。这 loss 期望目标具有与输出相同的形状。
该错误与输入和输出数据的维度之间的差异有关,但我不知道它应该是什么样子正确。我的样本数据维度是 10 个观察值、3 个特征和 2 个子类。
型号信息:
Model: "sequential"
____________________________________________________________________
Layer (type) Output Shape Param #
====================================================================
dense (Dense) (None, 3, 2) 6
====================================================================
Total params: 6
Trainable params: 6
Non-trainable params: 0
____________________________________________________________________
【问题讨论】:
-
到底是什么问题?
-
代码中哪里出现了错误?请记住minimal reproducible example 指南中的最小部分——这是您自己调试的良好第一步,并且可以让其他人更轻松地帮助您
-
我根据给定的指南更正了问题
标签: r tensorflow keras