Argumente `target` und" output "müssen den gleichen Rang haben (NDIM). Empfangen: target.shape = (keine,), output.shape Python

Python-Programme
Anonymous
 Argumente `target` und" output "müssen den gleichen Rang haben (NDIM). Empfangen: target.shape = (keine,), output.shape

Post by Anonymous »

Ich habe versucht, ein neuronales Netzwerk zu erstellen, das Bilder identifizieren kann. Wenn ich aber versuche, mein Modell anzupassen, erhalte ich den folgenden Fehler: < /p>
ValueError Traceback (most recent call last)
in ()
1 # Train the model
----> 2 history = model.fit(training, batch_size=batch_size, epochs=epoch,validation_data=testing)

1 frames
/usr/local/lib/python3.11/dist-packages/keras/src/backend/tensorflow/nn.py in categorical_crossentropy(target, output, from_logits, axis)
651 )
652 if len(target.shape) != len(output.shape):
--> 653 raise ValueError(
654 "Arguments `target` and `output` must have the same rank "
655 "(ndim). Received: "

ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(None,), output.shape=(None, 9)
< /code>
Ich erstelle es in Zusammenarbeit und der Code, den ich verwende, ist Folgendes: < /p>
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

from google.colab import drive
from google.colab.patches import cv2_imshow
drive.mount('/content/drive')

img_height = 180
img_width = 180
batch_size = 32
epoch = 20
< /code>
normalization_layer = tf.keras.layers.Rescaling(1./255)
normalized_ds = training.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
print(np.min(first_image), np.max(first_image))
< /code>
AUTOTUNE = tf.data.AUTOTUNE

training = training.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
testing = testing.cache().prefetch(buffer_size=AUTOTUNE)
< /code>
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical

num_classes = 9

model = Sequential([
Conv2D(32, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(256, activation='relu'),
Dense(num_classes, activation='softmax')
])
model.build(input_shape=(batch_size, img_height, img_width, 3))
< /code>
# Compilação do modelo
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

< /code>
history = model.fit(training, batch_size=batch_size, epochs=epoch,validation_data=testing)
< /code>
The Training and Testing variables are prefetchdirectories created using
tf.keras.utils.image_dataset_from_directory
< /code>
How do I fix this error.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post