gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:50:55 24 4
gpt4 key购买 nike

我对 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)。

关于machine-learning - TensorFlow - 具有数千个标签的分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45265584/

24 4 0