Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not squeeze dim[1], expected a dimension of 1, got 2

I have very simple input: Points, and I am trying to classify whether they are in some region or not. So my training data is of the shape (1000000, 2), which is an array of the form:
[ [x1,y1], [x2,y2],... ]
My labels are of a similar form (Shaped (10000, 2)):
[ [1,0], [0,1], [0,1],... ]
([0,1]means the point is in the region, [1,0] means it isn't)

My model is set up this way:

import tensorflow as tf
from tensorflow import keras
import numpy as np

# Reads the points and labels from .csv format files
train_data = np.genfromtxt('data/train_data.csv', delimiter=',')
train_labels = np.genfromtxt('data/train_labels.csv', delimiter=',')

model = keras.models.Sequential()
model.add(keras.layers.Dense(128, activation='relu', input_shape=(2,)))
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(2, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels, epochs=1, batch_size=100, verbose=1) # ERROR

Notice that the input shape is (2,), meaning (according to the reference) that the model would expect arrays of the form (*, 2).

I am getting the error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 2

and I have no idea why. Any suggestions?

Stacktrace:

Traceback (most recent call last):
  File "C:/Users/omer/Desktop/Dots/train.py", line 25, in <module>
    model.fit(train_data, train_labels, epochs=1, batch_size=100, verbose=1)
  File "C:\Users\omer\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 880, in fit
    validation_steps=validation_steps)
  File "C:\Users\omer\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 329, in model_iteration
    batch_outs = f(ins_batch)
  File "C:\Users\omer\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\backend.py", line 3076, in __call__
    run_metadata=self.run_metadata)
  File "C:\Users\omer\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:\Users\omer\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 2
     [[{{node metrics/acc/Squeeze}}]]
like image 476
Omer Lubin Avatar asked Jan 26 '23 11:01

Omer Lubin


1 Answers

Your labels are of the wrong shape. See the documentation:

When using the sparse_categorical_crossentropy loss, your targets should be integer targets. If you have categorical targets, you should use categorical_crossentropy

So you need to convert your labels to integers:

train_labels = np.argmax(train_labels, axis=1)
like image 118
IonicSolutions Avatar answered Jan 30 '23 06:01

IonicSolutions