我对TensorFlow非常陌生。我一直尝试使用TensorFlow创建一个函数,通过输入一个包含6个特征的向量来返回一个标签。
我有一个训练数据集,包含6个特征和1个标签。标签位于第一列:
309,3,0,2,4,0,6309,12,0,2,4,0,6309,0,4,17,2,0,6318,0,660,414,58,3,12311,0,0,414,58,0,2298,0,53,355,5,0,260,16,14,381,30,4,2312,0,8,8,13,0,3...
我有标签的索引,这是一个包含成千上万个名字的列表:
309,Joe318,Joey311,Bruce...
如何使用TensorFlow创建一个模型并进行训练,以便在给定没有第一列的向量时能够预测标签?
—
这是我尝试过的方法:
from __future__ import print_functionimport tflearnname_count = sum(1 for line in open('../../names.csv')) # this comes out to 24260 # Load CSV file, indicate that the first column represents labelsfrom tflearn.data_utils import load_csvdata, labels = load_csv('../../data.csv', target_column=0, categorical_labels=True, n_classes=name_count)# Build neural networknet = 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 modelmodel = 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]]) # 326print("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列(其中一个会是1,其余的都是0)。