TensorFlow - 分类有数千个标签

时间:2017-07-23 13:25:02

标签: machine-learning tensorflow artificial-intelligence tflearn

我对TensorFlow很新。我一直在尝试使用TensorFlow来创建一个函数,我给它一个带有6个特征的向量并获得一个标签。

我有6个功能和1个标签的训练数据集。标签位于第一列:

309,3,0,2,4,0,6
309,12,0,2,4,0,6
309,0,4,17,2,0,6
318,0,660,414,58,3,12
311,0,0,414,58,0,2
298,0,53,355,5,0,2
60,16,14,381,30,4,2
312,0,8,8,13,0,3
...

我有标签的索引,这是一个包含成千上万个名字的列表:

309,Joe
318,Joey
311,Bruce
...

如果没有第一列的矢量,如何使用TensorFlow创建模型并训练它以便能够预测标签?

-

这就是我的尝试:

from __future__ import print_function
import tflearn

name_count = sum(1 for line in open('../../names.csv'))  # this comes out to 24260 

# Load CSV file, indicate that the first column represents labels
from tflearn.data_utils import load_csv
data, labels = load_csv('../../data.csv', target_column=0,
                    categorical_labels=True, n_classes=name_count)


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

# Define model
model = tflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

# Predict 
pred = model.predict([[218,5,124,26,0,3]])  # 326
print("Name:", pred[0][1])

它基于https://github.com/tflearn/tflearn/blob/master/tutorials/intro/quickstart.md
我收到错误:

ValueError: Cannot feed value of shape (16, 24260) for Tensor u'TargetsData/Y:0', which has shape '(?, 2)'

24260是names.csv中的行数

谢谢!

1 个答案:

答案 0 :(得分:1)

net = tflearn.fully_connected(net, 2, activation='softmax')

看起来说你有2个输出类,但实际上你有24260. 16是你的小批量的大小,所以你有16行24260列(其中一个24260将是1,其他将是全0)。