Проблема Keras с несовместимым слоем

avatar
user3547425
8 августа 2021 в 23:19
80
1
1

Я новичок в машинном обучении и пытаюсь изучить несколько примеров. Я работаю с базой данных fashion_mnist, и у меня есть код ниже. Я получаю сообщение об ошибке в этой строке:

history = model.fit(train_X, train_y, epochs = 10, validation_data = (valid_X, valid_y))

The error is the following: 

ValueError: Вход 0 слоя sequence_5 несовместим с слой: ожидаемый min_ndim=4, найденный ndim=3. Полная форма получена: [Нет, 28, 28]

Что я делаю не так, как это исправить?
Заранее спасибо.

Ниже приведен полный исходный код:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
#from tensorflow import keras
#import keras
from keras.layers import Dense

fashion_mnist = tf.keras.datasets.fashion_mnist
# Contrary to Scikit_Learn, Keras images are 28x28 array rather than a 1D array of size 784
# Pixels intensities are integers (0 to 255) rather than floats (0.0 to 255.0)

fashion_mnist.load_data()  # Dataset already split in Training and Testing

# Dataset already split in Training and Testing

(train_X, train_y), (test_X, test_y) = fashion_mnist.load_data()  

print(train_X.shape)
print(train_y.shape)
print(test_X.shape)
print(test_y.shape)
# Adding 'valid' sample and Scaling the data (intensity of pixels from 0 to 1)

valid_X, train_X = train_X[:5000], train_X[5000:]
valid_y, train_y = train_y[:5000], train_y[5000:]

print(train_X.shape)
print(train_y.shape)
print(test_X.shape)
print(test_y.shape)
print(valid_X.shape)
print(valid_y.shape)
# Dataset already split in Training and Testing
train_X[0]

# Labeling the 'y' data for the 1st image

y_modalities = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
print(train_y[0])
print(y_modalities[train_y[0]])


image_1 = train_X[0]

plt.imshow(image_1, cmap = 'binary')
print('Class number:', train_y[0])
print('Label is:', y_modalities[train_y[0]])

image_2 = train_X[1]

plt.imshow(image_2, cmap = 'binary')
print('Class number:', train_y[1])
print('Label is:', y_modalities[train_y[1]])

image_3 = train_X[2]

plt.imshow(image_3, cmap = 'binary')
print('Class number:', train_y[2])
print('Label is:', y_modalities[train_y[2]])

plt.figure(figsize = (10, 10))
for i in range(25):
    plt.subplot(5, 5, i+1)
    plt.subplot(5, 5, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_X[i], cmap = plt.cm.binary)
    plt.xlabel(y_modalities[train_y[i]])
plt.show()

# Adding 'valid' sample and Scaling the data (intensity of pixels from 0 to 1)

train_X , valid_X, test_X = train_X /255.0, valid_X/255.0, test_X/255.0
print("after validation")
print(train_X.shape)
print(valid_X.shape)
print(test_X.shape)

# Scaled 1 image array

train_X[0].round(2)

train_y

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64, kernel_size = 3,  activation = 'relu', padding = 'same', input_shape = [28, 28, 1]),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(128, kernel_size = 3, activation = 'relu', padding = 'same'),
    tf.keras.layers.Conv2D(128, kernel_size = 3, activation = 'relu', padding = 'same'),
    tf.keras.layers.MaxPooling2D(2),
    tf.keras.layers.Conv2D(256, kernel_size = 3, activation = 'relu', padding = 'same'),
    tf.keras.layers.Conv2D(256, kernel_size = 3, activation = 'relu', padding = 'same'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation = 'relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(64, activation = 'relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(100, activation = 'relu'),
    tf.keras.layers.Dense(10, activation = 'softmax')
])

# Built-in loss functions can be passed via their string identifier for a CLASSIFIER model

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

model.summary()


history = model.fit(train_X, train_y, epochs = 10, validation_data = (valid_X, valid_y))


pd.DataFrame(history.history).plot(figsize = (8, 5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show

model.evaluate(test_X, test_y)
new_X = test_X[:9]
prob_y = model.predict(new_X)
prob_y
y_pred = model.predict_classes(new_X)
y_pred
np.array(y_modalities)[y_pred]
for i in range(9):
    plt.subplot(330 + 1 + i)
    x_i = test_X[i].reshape(28, 28)      # Replacing train_X with test_X
    plt.imshow(x_i, cmap = 'binary')
Источник

Ответы (1)

avatar
Abhishek Prajapat
9 августа 2021 в 06:07
1

Это означает, что форма ваших изображений не совпадает с требуемым вводом, и, следовательно, вы должны изменить форму ввода таким образом, а остальная часть кода такая же.

(train_X, train_y), (test_X, test_y) = fashion_mnist.load_data()
train_X = train_X.reshape(-1, 28, 28, 1)
test_X = test_X.reshape(-1, 28, 28, 1)

Кроме того, вы должны изменить функцию потерь на 'SparseCategorialCrossentropy'

model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(), optimizer = 'adam', metrics = ['accuracy'])