我正在尝试使用Python中的LightGBM为多类分类问题(3个类)建模分类器.我使用了以下参数.
params = {'task': 'train',
'boosting_type': 'gbdt',
'objective': 'multiclass',
'num_class':3,
'metric': 'multi_logloss',
'learning_rate': 0.002296,
'max_depth': 7,
'num_leaves': 17,
'feature_fraction': 0.4,
'bagging_fraction': 0.6,
'bagging_freq': 17}
Run Code Online (Sandbox Code Playgroud)
数据集的所有分类特征都是用标签编码的LabelEncoder.我跑完后训练模型cv,eartly_stopping如下图所示.
lgb_cv = lgbm.cv(params, d_train, num_boost_round=10000, nfold=3, shuffle=True, stratified=True, verbose_eval=20, early_stopping_rounds=100)
nround = lgb_cv['multi_logloss-mean'].index(np.min(lgb_cv['multi_logloss-mean']))
print(nround)
model = lgbm.train(params, d_train, num_boost_round=nround)
Run Code Online (Sandbox Code Playgroud)
训练结束后,我用这样的模型进行预测,
preds = model.predict(test)
print(preds)
Run Code Online (Sandbox Code Playgroud)
我有一个嵌套数组作为这样的输出.
[[ 7.93856847e-06 9.99989550e-01 2.51164967e-06]
[ 7.26332978e-01 1.65316511e-05 2.73650491e-01]
[ 7.28564308e-01 8.36756769e-06 2.71427325e-01]
...,
[ 7.26892634e-01 1.26915179e-05 2.73094674e-01]
[ 5.93217601e-01 2.07172044e-04 4.06575227e-01]
[ 5.91722491e-05 9.99883828e-01 …Run Code Online (Sandbox Code Playgroud) python machine-learning predict multiclass-classification lightgbm
一般说明: 我的代码工作正常,但结果是有线的。我不知道问题出在
我为这个错误苦苦挣扎了几个星期,到目前为止我已经改变了损失函数、优化器、数据生成器等,但我无法解决它。我很感激任何帮助。如果以下信息还不够,请告诉我。
研究领域: 我正在使用tensorflow、keras进行多类分类。该数据集有 36 个二元人类属性。我使用了resnet50,然后对于身体的每个部分(头部,上半身,下半身,鞋子,配件),我都在网络中添加了一个单独的分支。该网络有 1 个输入图像,带有 36 个标签和 36 个输出节点(具有 sigmoid 激活的 36 个定义层)。
问题: 问题是 keras 报告的准确性很高,但大多数输出的 f1-score 非常低或为零(即使我在编译网络时使用 f1-score 作为指标,f1-socre验证非常糟糕)。
a训练结束后,当我在预测模式下使用网络时,对于某些类,它始终返回一/零。这意味着网络无法学习(即使我使用加权损失函数或焦点损失函数。)
为什么奇怪呢?因为,最先进的方法即使在第一个 epoch 之后也会报告较高的 f1 分数(例如https://github.com/chufengt/iccv19_attribute,我已在我的 PC 中运行它并在一个 epoch 后获得了良好的结果)。
部分代码:
print("setup model ...")
input_image = KL.Input(args.img_input_shape, name= "input_1")
C1, C2, C3, C4, C5 = resnet_graph(input_image, architecture="resnet50", stage5=False, train_bn=True)
output_layers = merged_model (input_features=C4)
model = Model(inputs=input_image, outputs=output_layers, name='SoftBiometrics_Model')
...
print("model compiling ...")
OPTIM = optimizers.Adadelta(lr=args.learning_rate, rho=0.95)
model.compile(optimizer=OPTIM, loss=binary_focal_loss(alpha=.25, gamma=2), metrics=['acc',get_f1]) …Run Code Online (Sandbox Code Playgroud) 我正在学习使用Keras设计卷积神经网络.我开发了一个使用VGG16作为基础的简单模型.我在数据集中有大约6类图像.以下是我的模型的代码和说明.
model = models.Sequential()
conv_base = VGG16(weights='imagenet' ,include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
conv_base.trainable = False
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(6, activation='sigmoid'))
Run Code Online (Sandbox Code Playgroud)

以下是编译和拟合模型的代码:
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
model.summary()
callbacks = [
EarlyStopping(monitor='acc', patience=1, mode='auto'),
ModelCheckpoint(monitor='val_loss', save_best_only=True, filepath=model_file_path)
]
history = model.fit_generator(
train_generator,
steps_per_epoch=10,
epochs=EPOCHS,
validation_data=validation_generator,
callbacks = callbacks,
validation_steps=10)
Run Code Online (Sandbox Code Playgroud)
这是用于预测新图像的代码
img = image.load_img(img_path, target_size=(IMAGE_SIZE, IMAGE_SIZE))
plt.figure(index)
imgplot = plt.imshow(img)
x = image.img_to_array(img)
x = x.reshape((1,) + x.shape)
prediction = model.predict(x)[0]
# print(prediction)
Run Code Online (Sandbox Code Playgroud)
通常,model.predict()方法可以预测多个类.
[0 1 1 0 0 0]
Run Code Online (Sandbox Code Playgroud)
我有一些问题
deep-learning conv-neural-network keras multiclass-classification
我想知道如何使用 sklearn 运行多类、多标签、序数分类。我想预测目标群体的排名,范围从某一位置最普遍的群体 (1) 到最不普遍的群体 (7)。我似乎无法正确处理。你能帮我一下吗?
# Random Forest Classification
# Import
import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split
from sklearn.metrics import make_scorer, accuracy_score, confusion_matrix, f1_score
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
# Import dataset
dataset = pd.read_excel('alle_probs_edit.v2.xlsx')
X = dataset.iloc[:,4:-1].values
Y = dataset.iloc[:,-1].values
# Split in Train and Test
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42 )
# Scaling the features (alle Variablen auf …Run Code Online (Sandbox Code Playgroud) python ordinal scikit-learn multilabel-classification multiclass-classification
我正在开展一个分类项目,一个结果可能属于多个类别。例如,结果可能属于 A、B 和/或 C 类;例如,A、B、A&B、A&C、B&C 等。但是,我想预测一个类别的概率。例如,P(A)=结果包含 A 类的概率;例如,Pr(A)+Pr(A&B)+Pr(A&C)+Pr(A&B&C)。
我更喜欢使用 LightGBM。我的问题是:
multilabel-classification multiclass-classification lightgbm
您好,我的训练数据中标签中有很多缺失值,例如单个标签可以具有以下值:
[nan, 0, 0, nan, 1, 0]
Run Code Online (Sandbox Code Playgroud)
我想训练一个忽略 nan 值的分类模型。目前我已将 nan 值填充为 -1,并尝试对其进行切片。掩码不起作用,因为分类交叉熵仍然考虑到它
ix = tf.where(tf.not_equal(y_true, -1))
true = tf.gather(y_true, ix)
pred = tf.gather(y_pred, ix)
return keras.objectives.categorical_crossentropy(true, pred)
Run Code Online (Sandbox Code Playgroud)
是我到目前为止所能想到的,但它有错误
InvalidArgumentError (see above for traceback): Incompatible shapes: [131] vs. [128]
[[Node: mul_1 = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Mean, _recv_dense_3_sample_weights_0/_13)]]
Run Code Online (Sandbox Code Playgroud)
有谁知道如何处理这个问题?
python missing-data keras tensorflow multiclass-classification
我知道您可以为不平衡的数据集设置scale_pos_weight。然而,如何处理不平衡数据集中的多分类问题。我已经浏览过https://datascience.stackexchange.com/questions/16342/unbalanced-multiclass-data-with-xgboost/18823,但不太明白如何在 Dmatrix 中设置权重参数。
有人可以详细解释一下吗?
我只是尝试制作我的第一个ML.NET项目,之前我使用 Azure ML、可视化界面、Python 等构建了该项目,但现在我想使用C#来完成。
我正在遵循本教程,但具有完全不同的数据集和目的。
数据集有很多额外的列,但我的数据模型如下所示(指向数据集中列的索引):
using Microsoft.ML.Data;
namespace ML_Net
{
public class Earthquake
{
[LoadColumn(1)]
public int geo_level_1_id { get; set; }
[LoadColumn(2)]
public int geo_level_2_id { get; set; }
[LoadColumn(3)]
public int geo_level_3_id { get; set; }
[LoadColumn(4)]
public int count_floors_pre_eq { get; set; }
[LoadColumn(5)]
public int age { get; set; }
[LoadColumn(6)]
public int area { get; set; }
[LoadColumn(7)]
public int height { get; set; }
[LoadColumn(8)]
public int …Run Code Online (Sandbox Code Playgroud) keras ×3
python ×3
lightgbm ×2
tensorflow ×2
xgboost ×2
c# ×1
missing-data ×1
ml.net ×1
ordinal ×1
predict ×1
scikit-learn ×1