【问题标题】:Reduce weights for wrongly predicted class减少错误预测类别的权重
【发布时间】:2019-04-20 02:57:39
【问题描述】:

我目前正在开发一个简单的预测系统,在该系统中向用户询问一系列是/否问题,并根据他们的回答,预训练模型 (MLPClassifier) 预测一个类别并询问用户是否预测是正确的。我不确定这是否可能,但我希望随后改变预训练模型的权重(以某种在线学习方式),以便网络(在该会话中)不会预测相同以后上课。目前,我只是将错误的响应添加到字典中,如果网络预测该类已经在列入黑名单的类集中,它会被忽略,但我觉得必须有比这更好的方法!我的分类器代码是:

mlp = MLPClassifier(hidden_layer_sizes=(128,), max_iter=500, alpha=1e-4,
                    solver='sgd', verbose=10, tol=1e-4, random_state=1,
                    learning_rate_init=.1, )
x_train, x_test, y_train, y_test = train_test_split(df.values[:, 0:8], df.label_idx, test_size=0.33,
                                                    random_state=42)

预测的代码是:

def receive_input():
responses = []
bad_guesses = []
print("Answer questions (Yes/No) or enter END to make prediction")
count = 0
while count < len(questions):
    print(questions[count])
    response = input().lower().strip()
    if response == 'end':
        break
    elif response == 'yes':
        responses.append(1)
    elif response == 'no':
        responses.append(0)
    else:
        print('Invalid Input')
        continue
    count += 1

    padded_responses = np.pad(np.array(responses), (0, 8 - len(responses)), 'constant', constant_values=(0, -1))
    prob_pred = mlp.predict_proba(padded_responses.reshape(1, -1)).flatten()
    index = np.argmax(prob_pred)
    best_score = prob_pred[index]
    guess = labels[index]
    if best_score > 0.8 and guess not in bad_guesses:
        print('Early guess is: ' + labels[index] + ' is this right ? (Yes/No)')
        correct = input()
        if correct == 'Yes':
            break
        elif correct == 'No':
            bad_guesses.append(labels[index])

pred = mlp.predict(np.array(responses).reshape(1, -1))
print('Prediction is: ' + labels[pred[0]])

【问题讨论】:

  • 太棒了,看起来可行,谢谢
  • 是的,听起来不错

标签: machine-learning scikit-learn neural-network


【解决方案1】:

mlp.coefs_ 为您提供了一个列表,其中ith 元素表示与层i 对应的权重矩阵

此外,mlp.intercepts_ 为您提供了一个列表,其中ith 元素表示与层i + 1 对应的偏置向量

所以你可以试试看这些属性是否可以改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多