TFLearn 模型评估
TFLearn model evaluation
我是机器学习和 TensorFlow 的新手。我正在尝试训练一个简单的模型来识别性别。我使用身高、体重和鞋码的小型数据集。但是,我在评估模型的准确性时遇到了问题。
这是完整的代码:
import tflearn
import tensorflow as tf
import 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 male
Y = [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 set
X_train, Y_train = data[:8, :3], data[:8, 3:]
X_test, Y_test = data[8:, :3], data[8:, 3:]
# Build neural network
net = 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 model
model = 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)
总是returns1.0
。如何使用 TFLearn 计算测试数据集的真实精度?
你想在这种情况下进行二元分类。您的网络设置为执行线性回归。
首先,将标签(性别)转换为分类特征:
from tflearn.data_utils import to_categorical
Y_train = to_categorical(Y_train, nb_classes=2)
Y_test = to_categorical(Y_test, nb_classes=2)
您的网络的输出层需要两个输出单元用于您要预测的两个 类。此外,激活需要是 softmax 才能进行分类。 tf.learn 默认损失是交叉熵,默认指标是准确率,所以这已经是正确的了。
# Build neural network
net = 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,而在您的测试集上的准确度将很差。
我是机器学习和 TensorFlow 的新手。我正在尝试训练一个简单的模型来识别性别。我使用身高、体重和鞋码的小型数据集。但是,我在评估模型的准确性时遇到了问题。 这是完整的代码:
import tflearn
import tensorflow as tf
import 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 male
Y = [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 set
X_train, Y_train = data[:8, :3], data[:8, 3:]
X_test, Y_test = data[8:, :3], data[8:, 3:]
# Build neural network
net = 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 model
model = 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)
总是returns1.0
。如何使用 TFLearn 计算测试数据集的真实精度?
你想在这种情况下进行二元分类。您的网络设置为执行线性回归。
首先,将标签(性别)转换为分类特征:
from tflearn.data_utils import to_categorical
Y_train = to_categorical(Y_train, nb_classes=2)
Y_test = to_categorical(Y_test, nb_classes=2)
您的网络的输出层需要两个输出单元用于您要预测的两个 类。此外,激活需要是 softmax 才能进行分类。 tf.learn 默认损失是交叉熵,默认指标是准确率,所以这已经是正确的了。
# Build neural network
net = 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,而在您的测试集上的准确度将很差。