TensorFlow - 具有数千个标签的分类

TensorFlow - Classification with thousands of labels

我是 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

中的行数

谢谢!

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

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