【问题标题】:TensorFlow - Classification with thousands of labelsTensorFlow - 具有数千个标签的分类
【发布时间】:2017-07-23 13:25:01
【问题描述】:

我对 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 中的行数

谢谢!

【问题讨论】:

  • 你已经尝试了什么?您是在问“监督学习是如何工作的?”或者“我如何编写代码来使用 tensorflow 进行监督学习?”还是您担心要分类的类别数量?你看过分层 softmax 吗?
  • @RyanStout 已编辑,谢谢!由于我看到的所有监督学习示例只有大约 2 或 3 个标签,我什至不确定应该使用哪种方法。但我的问题是如何用代码解决这个问题。

标签: machine-learning tensorflow artificial-intelligence tflearn


【解决方案1】:
net = tflearn.fully_connected(net, 2, activation='softmax')

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2020-09-04
    • 2018-06-11
    • 2017-07-22
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2017-09-18
    相关资源
    最近更新 更多