gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:32:16 26 4
gpt4 key购买 nike

在使用 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))

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

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

对于 TFLearn 来说,任何建议都很棒。

最佳答案

最后,它发现这是由于我试图预测的数据大小所致。我通过将其插入循环来修复此问题:

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

关于machine-learning - TfLearn 混淆矩阵训练在 std::bad_alloc 上终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45314813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com