Issue
I'm currently working on a simple neural network using Keras, and I'm running into a problem with my labels. The network is making a binary choice, and as such, my labels are all 1s and 0s. My data is composed of a 3d NumPy array, basically pixel data from a bunch of images. Its shape is (560, 560, 32086). However since the first two dimensions are only pixels, I shouldn't assign a label to each one so I tried to make the label array with the shape (1, 1, 32086) so that each image only has 1 label. However when I try to compile this with the following code:
model = Sequential(
[
Rescaling(1.0 / 255),
Conv1D(32, 3, input_shape=datax.shape, activation="relu"),
Dense(750, activation='relu'),
Dense(2, activation='sigmoid')
]
)
model.compile(optimizer=SGD(learning_rate=0.1), loss="binary_crossentropy", metrics=['accuracy'])
model1 = model.fit(x=datax, y=datay, batch_size=1, epochs=15, shuffle=True, verbose=2)
I get this error "ValueError: Data cardinality is ambiguous: x sizes: 560 y sizes: 1 Make sure all arrays contain the same number of samples." Which I assume means that the labels have to be the same size as the input data, but that doesn't make sense for each pixel to have an individual label.
The data is collected through a for loop looping through files in a directory and reading their pixel data. I then add this to the NumPy array and add their corresponding label to a label array. Any help in this problem would be greatly appreciated.
Solution
The ValueError
occurs because the first dimension is supposed to be the number of samples and needs to be the same for x
and y
. In your example that is not the case. You would need datax
to to have shape (32086, 560, 560)
and datay
should be (32086,)
.
Have a look at this example and note how the 60000 training images have shape (60000, 28, 28)
.
Also I suspect a couple more mistakes have sneaked into your code:
- Are you sure you want a
Conv1D
layer and notConv2D
? Maybe this example would be informative. - Since you are using binary crossentropy loss your last layer should only have one output instead of two.
Answered By - sply88
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.