Search code examples
python-3.xcsvmachine-learningtensorflowtflearn

tflearn DNN model gives TargetsData/Y:0 error


I get the following error...

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

I understand that this has to do with the shape of my Y variable which in this case is the variable labels, but I'm not sure how to change the shape to make my model work.

Basically, I have a CSV file which I saved into a variable using pandas...

data = pd.read_csv('Speed Dating Data.csv')

After some preprocessing, I decided to extract my target class as so...

# Target label used for training
labels = np.array(data["age"], dtype=np.float32)

Next I removed this column from my data variable...

# Data for training minus the target label.
data = np.array(data.drop("age", axis=1), dtype=np.float32)

Then I decided to setup my model...

net = tflearn.input_data(shape=[None, 32])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 16, activation='softmax')
net = tflearn.regression(net)

# Define model.
model = tflearn.DNN(net)

model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

If I run this, I get the error above. Since my labels seems to be (16,) but I need it to be (?, 16), I tried the following code...

labels = labels[np.newaxis, :]

But this gives yet another error. I think I am unsure as to what form my target class, labels, is supposed to be. How can I fix this?


Solution

  • Reshape your label according to follows,

    label= np.reshape(label,(-1,16)) # since you have 16 classes
    

    which reshape the label to (?,16).

    Hope this helps.

    Updated according to your Requirement. And added comments to changes.

    labels = np.array(data["age"], dtype=np.float32)
    label= np.reshape(label,(-1,1)) #reshape to [6605,1]
    
    data = np.array(data.drop("age", axis=1), dtype=np.float32)
    
    net = tflearn.input_data(shape=[None, 32])
    net = tflearn.fully_connected(net, 32)
    net = tflearn.fully_connected(net, 32)
    net = tflearn.fully_connected(net, 1, activation='softmax') #Since this is a regression problem only one output
    net = tflearn.regression(net)
    
    # Define model.
    model = tflearn.DNN(net)
    
    model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)