我正在使用 keras 构建多输出分类模型。我的数据集是这样的
[x1,x2,x3,x4,y1,y2,y3]
x1,x2,x3 是特征,y1,y2,y3 是标签,y1,y2,y3 是多类。
我已经建立了一个模型(我忽略了一些隐藏层):
def baseline_model(input_dim=23,output_dim=3):
model_in = Input(shape=(input_dim,))
model = Dense(input_dim*5,kernel_initializer='uniform',input_dim=input_dim)(model_in)
model = Activation(activation='relu')(model)
model = Dropout(0.5)(model)
...................
model = Dense(output_dim,kernel_initializer='uniform')(model)
model = Activation(activation='sigmoid')(model)
model = Model(model_in,model)
model.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
return model
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用keras的方法使其支持分类:
estimator = KerasClassifier(build_fn=baseline_model)
estimator.fit()
estimator.predict(df[0:10])
Run Code Online (Sandbox Code Playgroud)
但我发现结果不是多输出的,只输出一维。
[0,0,0,0,0,0,0,0,0,0]
那么对于多输出分类问题,我们能不能使用KerasClassifier函数来学习呢?
multilabel-classification deep-learning keras tensorflow multiclass-classification
在我的实验中,我试图训练一个神经网络来检测患者是否表现出症状 A、B、C、D。我的数据由每位患者的不同角度照片以及他们是否表现出症状 A、B、C、D 组成。
现在,在 pytoch 中,我正在使用 MSELoss 并将测试误差计算为分类总数中正确分类的总数。我想这太天真了,甚至是不恰当的。
测试误差计算的示例如下:假设我们有 2 名患者,每人都有两张图像。那么总共会有 16 个分类(1 个分类代表患者 1 是否有照片 1 中的症状 A、B、C、D 等)。如果模型正确预测照片 1 中的患者 1 表现出症状 A,那么正确分类的总数就会增加 1。
multilabel-classification deep-learning multiclass-classification pytorch loss-function
我试图了解在数据集不平衡的情况下,AUC 为何是比分类准确性更好的指标。
假设数据集包含 3 个类别的 1000 个示例,如下所示:
a = [[1.0, 0, 0]]*950 + [[0, 1.0, 0]]*30 + [[0, 0, 1.0]]*20
Run Code Online (Sandbox Code Playgroud)
显然,这个数据是不平衡的。
一个幼稚的策略是预测属于第一类的每个点。
假设我们有一个具有以下预测的分类器:
b = [[0.7, 0.1, 0.2]]*1000
Run Code Online (Sandbox Code Playgroud)
使用列表中的真实标签a
和列表中的预测b
,分类精度为 0.95。
因此,人们会认为该模型确实在分类任务上表现良好,但这并不是因为该模型正在预测一个类别中的每个点。
因此,建议使用 AUC 指标来评估不平衡的数据集。如果我们使用 TF Keras AUC
指标
预测 AUC ,我们会得到 ~0.96。
如果我们通过设置 来使用 sklearn f1-score指标来预测 f1-score ,我们会得到 0.95。 b=[[1,0,0]]*1000
现在我有点困惑,因为所有指标(准确度、AUC 和 f1-score)都显示出很高的价值,这意味着该模型非常擅长预测任务(但这里的情况并非如此)。
我在这里遗漏了哪一点以及我们应该如何解释这些价值观?
谢谢。
python scikit-learn auc multiclass-classification tensorflow2.0
我已经训练了一个模型,想要计算几个重要的指标,例如accuracy
、、 和。precision
recall
f1 score
我遵循的过程是:
from pyspark.ml.classification import LogisticRegression
lr = LogisticRegression(featuresCol='features',labelCol='label')
lrModel = lr.fit(train)
lrPredictions = lrModel.transform(test)
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.evaluation import BinaryClassificationEvaluator
eval_accuracy = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
eval_precision = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="precision")
eval_recall = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="recall")
eval_f1 = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="f1Measure")
eval_auc = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol="prediction")
accuracy = eval_accuracy.evaluate(lrPredictions)
precision = eval_precision.evaluate(lrPredictions)
recall = eval_recall.evaluate(lrPredictions)
f1score = eval_f1.evaluate(lrPredictions)
auc = eval_accuracy.evaluate(lrPredictions)
Run Code Online (Sandbox Code Playgroud)
然而,它只能计算accuracy
和auc
,而不能计算其他三个。这里我应该修改什么?
machine-learning pyspark apache-spark-ml multiclass-classification
我的模型是用这段代码编译的
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
Run Code Online (Sandbox Code Playgroud)
在训练过程中,我遇到了这个错误
tensorflow.python.framework.errors_impl.InvalidArgumentError:收到标签值 5,该值超出了 [0, 5) 的有效范围。
我的标签是1,2,3,4,5
哪个[1,5]
不是[0, 5)
。如何为该模型设置标签?
machine-learning keras tensorflow multiclass-classification tf.keras
我正在尝试使用 Cifar10 数据集计算多类分类问题的 f1 分数。我正在从 sklearn 库导入 f1 指标。但是我不断收到以下错误消息:
ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets
Run Code Online (Sandbox Code Playgroud)
下面是我在验证集上测试模型的函数。有人能够解释在执行多类分类时如何计算 f1 吗?我很困惑。
@torch.no_grad()
def valid_function(model, optimizer, val_loader):
model.eval()
val_loss = 0.0
val_accu = 0.0
f_one = []
for i, (x_val, y_val) in enumerate(val_loader):
x_val, y_val = x_val.to(device), y_val.to(device)
val_pred = model(x_val)
loss = criterion(val_pred, y_val)
val_loss += loss.item()
val_accu += accuracy(val_pred, y_val)
f_one.append(f1_score(y_val.cpu(), val_pred.cpu()))
val_loss /= len(val_loader)
val_accu /= len(val_loader)
print('Val Loss: %.3f | Val Accuracy: %.3f'%(val_loss,val_accu))
return …
Run Code Online (Sandbox Code Playgroud) metrics machine-learning conv-neural-network multiclass-classification
keras ×2
tensorflow ×2
auc ×1
metrics ×1
pyspark ×1
python ×1
pytorch ×1
scikit-learn ×1
tf.keras ×1