TfLearn 混淆矩阵训练在 std::bad_alloc 上终止

Posted

技术标签:

【中文标题】TfLearn 混淆矩阵训练在 std::bad_alloc 上终止【英文标题】:TfLearn Confusion Matrix training terminated on std::bad_alloc 【发布时间】:2018-01-01 01:25:54 【问题描述】:

在使用 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']

    # Building convolutional network
    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'
    )

    # Training
    model = tflearn.DNN(network, tensorboard_verbose=0)
    model.load('/path.tflearn')

    predictions = model.predict(X)
    print(confusion_matrix(Y, predictions))

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

在抛出 'std::bad_alloc' 的实例后调用终止 什么():std::bad_alloc 中止(核心转储)

任何建议都会很棒,对于 TFLearn 来说是新的。

【问题讨论】:

你能添加一些或你的数据,或代码来生成相同形状的合成数据吗?包含 MWE 的问题更容易回答。 你也可以包括完整的堆栈跟踪,它可能更容易诊断问题来自哪里。 我使用的数据集是 pitt.edu/~emotion/um-spread.htm 。每张图像在 3 个通道上为 240*320。从数据集中随机抽取 9679 张图像进行测试。 @ncfirth 这是给我的完整堆栈跟踪。 【参考方案1】:

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

# Predict Classes
predictions = []
count = 0
length = 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 应该研究一种简化的方法来生成混淆矩阵,这样其他人就不会遇到同样的问题。

【讨论】:

以上是关于TfLearn 混淆矩阵训练在 std::bad_alloc 上终止的主要内容,如果未能解决你的问题,请参考以下文章

sklearn:绘制跨训练+测试集组合的混淆矩阵

如何使用混淆矩阵计算自定义训练的 spacy ner 模型的整体准确性?

XGB模型训练报错 terminate called after throwing an instance of ‘std::bad_alloc‘ what()

XGB模型训练报错 terminate called after throwing an instance of ‘std::bad_alloc‘ what()

混淆矩阵值错误:找到样本数量不一致的输入变量:[3, 360]

如何打印大维度的混淆矩阵