2016-12-08 142 views
0

我是新來的機器學習和TensorFlow。我試圖訓練一個簡單的模型來識別性別。我使用身高,體重和鞋號的小數據集。但是,我在評估模型的準確性時遇到了一個問題。 這裏是整個代碼:TFLearn模型評估

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)總是返回1.0。如何使用TFLearn計算測試數據集的真實精度?

回答

3

你想在這種情況下進行二元分類。您的網絡設置爲執行線性迴歸。

首先,變換標籤(性別),以明確的功能:

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,而測試集的準確度會很差。

+0

謝謝,我已經意識到:) – Bolein95