Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

python - 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:UsersomerAppDataLocalProgramsPythonPython37libsite-packagesensorflowpythonkerasengineraining.py", line 880, in fit
    validation_steps=validation_steps)
  File "C:UsersomerAppDataLocalProgramsPythonPython37libsite-packagesensorflowpythonkerasengineraining_arrays.py", line 329, in model_iteration
    batch_outs = f(ins_batch)
  File "C:UsersomerAppDataLocalProgramsPythonPython37libsite-packagesensorflowpythonkerasackend.py", line 3076, in __call__
    run_metadata=self.run_metadata)
  File "C:UsersomerAppDataLocalProgramsPythonPython37libsite-packagesensorflowpythonclientsession.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:UsersomerAppDataLocalProgramsPythonPython37libsite-packagesensorflowpythonframeworkerrors_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}}]]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...