Search code examples
machine-learningtensorflowartificial-intelligencetflearn

TensorFlow - Classification with thousands of labels


I'm very new to TensorFlow. I've been trying use TensorFlow to create a function where I give it a vector with 6 features and get back a label.

I have a training data set in the form of 6 features and 1 label. The label is in the first column:

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
...

I have the index for the labels which is a list of thousand and thousands of names:

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

How do I create a model and train it using TensorFlow to be able to predict the label, given a vector without the first column?

--

This is what I tried:

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])

It's based on https://github.com/tflearn/tflearn/blob/master/tutorials/intro/quickstart.md
I get the error:

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

24260 is the number of lines in names.csv

Thank you!


Solution

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

    looks to be saying you have 2 output classes, but in reality you have 24260. 16 is the size of your minibatch, so you have 16 rows of 24260 columns (one of these 24260 will be a 1, the others will be all 0s).