gpt4 book ai didi

r - 具有附加维度(子类)的二元分类

转载 作者:行者123 更新时间:2023-12-04 11:13:34 25 4
gpt4 key购买 nike

假设我有一个包含 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
)

错误:

Error in py_call_impl(callable, dots$args, dots$keywords):

ValueError: A target array with shape (10, 2) was passed for an output of shape (None, 3, 2) while using as loss binary_crossentropy. This loss expects targets to have the same shape as the output.

该错误与输入和输出数据的维度差异有关,但我不知道它应该是什么样子才正确。我的样本数据维度是 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
____________________________________________________________________

最佳答案

你的错误说你的模型输出形状是 (None, 3, 2) 但你给他输出形状 (10,2) (标签)。要解决此问题,您应该修复输入形状:您的输入 shpt 是 3,而不是 (3,2),因为您有 3 个特征并且您希望有 2 个输出。

改变这一行:

layer_dense(units = 2, activation = 'sigmoid', input_shape = c(3, 2))

为此:

layer_dense(units = 2, activation = 'sigmoid', input_shape = c(3))

我用 python 测试了它,它似乎运行良好:

import keras
model = keras.Sequential()
model.add(keras.layers.Dense(units = 2,activation="sigmoid",input_shape=(3,)))
model.summary()


Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_4 (Dense) (None, 2) 8
=================================================================
Total params: 8
Trainable params: 8
Non-trainable params: 0
_________________________________________________________________

关于r - 具有附加维度(子类)的二元分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60654677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com