TFLearn模型评估

我刚开始学习机器学习和TensorFlow。我尝试训练一个简单的模型来识别性别。我使用了身高、体重和鞋码的小数据集。然而,我在评估模型准确性时遇到了问题。以下是完整的代码:

import tflearnimport tensorflow as tfimport numpy as np# [height, weight, shoe_size]X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],     [190, 90, 47], [175, 64, 39], [177, 70, 40], [159, 55, 37], [171, 75, 42],     [181, 85, 43], [170, 52, 39]]# 0 - for female, 1 - for maleY = [1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0]data = np.column_stack((X, Y))np.random.shuffle(data)# Split into train and test setX_train, Y_train = data[:8, :3], data[:8, 3:]X_test, Y_test = data[8:, :3], data[8:, 3:]# Build neural networknet = tflearn.input_data(shape=[None, 3])net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 1, activation='linear')net = tflearn.regression(net, loss='mean_square')# fix for tflearn with TensorFlow 12:col = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)for x in col:    tf.add_to_collection(tf.GraphKeys.VARIABLES, x)# Define modelmodel = tflearn.DNN(net)# Start training (apply gradient descent algorithm)model.fit(X_train, Y_train, n_epoch=100, show_metric=True)score = model.evaluate(X_test, Y_test)print('Training test score', score)test_male = [176, 78, 42]test_female = [170, 52, 38]print('Test male: ', model.predict([test_male])[0])print('Test female:', model.predict([test_female])[0])

尽管模型的预测并不是很准确

Test male:  [0.7158362865447998]Test female: [0.4076206684112549]

model.evaluate(X_test, Y_test)总是返回1.0。如何使用TFLearn计算测试数据集上的真实准确率?


回答:

在这种情况下,你想要进行二元分类。你的网络被设置为执行线性回归。

首先,将标签(性别)转换为分类特征:

from tflearn.data_utils import to_categoricalY_train = to_categorical(Y_train, nb_classes=2)Y_test = to_categorical(Y_test, nb_classes=2)

你的网络的输出层需要两个输出单元来预测你想要的两个类别。此外,激活函数需要是softmax以进行分类。tf.learn的默认损失是交叉熵,默认度量是准确率,因此这是正确的设置。

# Build neural networknet = tflearn.input_data(shape=[None, 3])net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 2, activation='softmax')net = tflearn.regression(net)

输出现在将是一个包含每个性别概率的向量。例如:

[0.991, 0.009] #female

请注意,使用你这么小的数据集,网络会严重过拟合。这意味着在训练过程中准确率会接近1,而在测试集上的准确率会相当差。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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