TfLearn 混淆矩阵训练因 std::bad_alloc 异常终止

在使用 TFLearn 创建卷积神经网络时,遇到如何生成混淆矩阵的问题。我目前的代码如下所示:

 from __future__ import division, print_function, absolute_import    import tflearn    from tflearn.layers.core import input_data, dropout, fully_connected    from tflearn.layers.conv import conv_2d, max_pool_2d    from tflearn.layers.normalization import local_response_normalization    from tflearn.layers.estimator import regression    from sklearn.metrics import confusion_matrix    import h5py    hdf5Test = h5py.File('/path', 'r')    X = hdf5Test['X']    Y = hdf5Test['Y']    # 构建卷积网络    network = input_data(shape=[None, 240, 320, 3], name='input')    network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")    network = max_pool_2d(network, 2)    network = local_response_normalization(network)    network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")    network = max_pool_2d(network, 2)    network = local_response_normalization(network)    network = fully_connected(network, 128, activation='tanh')    network = dropout(network, 0.8)    network = fully_connected(network, 256, activation='tanh')    network = dropout(network, 0.8)    network = fully_connected(network, 2, activation='softmax')    network = regression(      network,      optimizer='sgd',      learning_rate=0.01,      loss='categorical_crossentropy',      name='target'    )    # 训练    model = tflearn.DNN(network, tensorboard_verbose=0)    model.load('/path.tflearn')    predictions = model.predict(X)    print(confusion_matrix(Y, predictions))

每次尝试运行这段代码时,我都会收到以下错误信息:

terminate called after throwing an instance of ‘std::bad_alloc’ what(): std::bad_alloc Aborted (core dumped)

任何建议都将非常有帮助,我刚开始使用 TFLearn。


回答:

最后,发现问题出在我试图预测的数据大小。我通过将其插入循环中来解决这个问题:

# 预测类别predictions = []count = 0length = len(X)for line in X:  print('Line ' + str(count) + ' of ' + str(length))  tmp = model.predict_label([line])  predictions.append(tmp[0])  count += 1

通过一些格式化处理,我随后能够使用 Sklearn 生成混淆矩阵:

predictedClasses = np.argmin(predictions, axis=1)actualClasses = np.argmax(Y, axis=1)print(confusion_matrix(actualClasses, predictedClasses))

这种方法对我有效,可能对你也有用… 我认为 TFLearn 应该考虑一种简化的方法来生成混淆矩阵,以便其他人不会遇到同样的问题。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注