Jeffery Dough
Jeffery Dough

Reputation: 5

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

I'm trying to get into machine learning and I've decided on using tflearn for a start. I used tflearn's quickstart guide to get the basics and tried using that neural network for a task I've set myself: Predicting the age of abalones from their dimensions. For this I downloaded the according dataset as .csv from the UCI repository. The table is in this format:

SEX|LENGTH|DIAMETER|HEIGHT|WHOLE WEIGHT|SHUCKED WEIGHT|VISCERA WEIGHT|SHELL WEIGHT|RINGS

Since the age is the same as the number of rings, I imported the .csv like this:

data, labels = load_csv("abalone.csv", categorical_labels=False, has_header=False)

The task is to predict the number of rings based on the data, so I set up my input layer like this:

net = tflearn.input_data(shape=[None, 8])

Added four hidden layers with the default linear activation function:

net = tflearn.fully_connected(net, 320)
net = tflearn.fully_connected(net, 200)
net = tflearn.fully_connected(net, 200)
net = tflearn.fully_connected(net, 320)

And an output layer with one node since there is only one result (no. of rings):

net = tflearn.fully_connected(net, 1, activation="sigmoid")
net = tflearn.regression(net)

Now I initialize the model but during training the above error occurs:

model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=1000, show_metric=True, batch_size=1600)

The entire exception:

Traceback (most recent call last):
  File "D:\OneDrive\tensornet.py", line 34, in <module>
    model.fit(data, labels, n_epoch=1000, show_metric=True, batch_size=1600)
  File "C:\Python3\lib\site-packages\tflearn\models\dnn.py", line 215, in fit
    callbacks=callbacks)
  File "C:\Python3\lib\site-packages\tflearn\helpers\trainer.py", line 333, in fit
    show_metric)
  File "C:\Python3\lib\site-packages\tflearn\helpers\trainer.py", line 774, in _train
    feed_batch)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
    run_metadata_ptr)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 944, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1600,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)'

From what I understand, the exception occurs when trying to fit my labels (which are a 1600x1 Tensor) with my output layer. But I don't know how to fix this.

Upvotes: 0

Views: 1121

Answers (1)

mangate
mangate

Reputation: 648

You need to add another axis to the labels so they'll have a (1600,1) shape instead of (1600,)

The simplest way to do it is like this:

 labels = labels[:, np.newaxis]

Upvotes: 1

Related Questions