Search code examples
pythonmachine-learningtensorflowtflearn

Python Tflearn - ValueError: Cannot feed value of shape (16, 1) for Tensor u'InputData/X:0', which has shape '(?, 2)'


Being new to machine learning and tflearn/tensorflow I was trying to follow the quickstart tutorial of tflearn (the titanic one).

Modifying it to suit my need I got to this code:

from __future__ import print_function

import numpy as np
import tflearn

# Load CSV file, indicate that the first column represents labels
from tflearn.data_utils import load_csv
data, labels = load_csv('nowcastScaled.csv', target_column=1, n_classes=2)

# Preprocessing function
def preprocess(data):
    return np.array(data, dtype=np.float32)

# Preprocess data
data = preprocess(data)

# Build neural network
net = tflearn.input_data(shape=[None, 2])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy')

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

But I got this error:

ValueError: Cannot feed value of shape (16, 1) for Tensor u'InputData/X:0', which has shape '(?, 2)'

My csv file is composed of 2 column, one being an index (number of the entry, since it's only a test I limited myself to 100 entry) and the other being a congestion score (what I'm trying to predict, between 0 and 200), both are numerical values.

I kinda understand that I'm trying to feed it a bad value (or at least something he's not waiting for) but I don't see how to correct it.


Solution

  • Adding

    data = np.reshape(data, (-1, 2))
    

    Corrected my problem but gave me the same error but with Y this time so I did:

    labels = np.reshape(labels, (-1, 2))
    

    both before regression and it seem to have done the trick. I don't know if it is the best or even a good way to do it but for now I managed to got ride of the error.