简体   繁体   English

Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)'

[英]Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)'

I'm having trouble while visualizing a trained tensorflow model to compare the training loss, accuracy against the validation data with the following block of code:我在可视化经过训练的 tensorflow model 以使用以下代码块将训练损失、准确性与验证数据进行比较时遇到问题:

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig('./foo.png')
plt.show()

But it's throwing the error: ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)但它抛出了错误: ValueError: x and y must have the same first dimension, but have shapes (20,) and (1,)

The entire code:整个代码:

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os
import matplotlib.pyplot as plt
import numpy as np

import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

URL = r'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'

zip_dir = tf.keras.utils.get_file('cats_and_dogs_filtered.zip', origin=URL, extract=True)

zip_dir_base = os.path.dirname(zip_dir)

base_dir = os.path.join(os.path.dirname(zip_dir), 'cats_and_dogs_filtered')
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

train_cats_dir = os.path.join(train_dir, 'cats')
train_dogs_dir = os.path.join(train_dir, 'dogs')
validation_cats_dir = os.path.join(validation_dir, 'cats')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

num_cats_tr = len(os.listdir(train_cats_dir))
num_dogs_tr = len(os.listdir(train_dogs_dir))

num_cats_val = len(os.listdir(validation_cats_dir))
num_dogs_val = len(os.listdir(validation_dogs_dir))

total_train = num_cats_tr+num_dogs_tr
total_validation = num_cats_val+num_dogs_val

print(total_train)
print(total_validation)

BATCH_SIZE = 100
IMG_SHAPE = 150

train_image_generator = ImageDataGenerator(rescale=1./255)
validation_image_generator = ImageDataGenerator(rescale=1./255)

train_data_gen = train_image_generator.flow_from_directory(batch_size=BATCH_SIZE, 
directory=train_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE), class_mode='binary')

val_data_gen = validation_image_generator.flow_from_directory(batch_size=BATCH_SIZE, 
directory=validation_dir, shuffle=True, target_size=(IMG_SHAPE,IMG_SHAPE), 
class_mode='binary')

sample_training_images, _ = next(train_data_gen)

def plotImages(image_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip(image_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()

plotImages(sample_training_images[:5])

model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150,150,3)),
                               tf.keras.layers.MaxPooling2D(2,2),
                               
                               tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
                               tf.keras.layers.MaxPooling2D(2,2),
                               
                               tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
                               tf.keras.layers.MaxPooling2D(2,2),
                               
                               tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
                               tf.keras.layers.MaxPooling2D(2,2),
                               
                               tf.keras.layers.Flatten(),
                               tf.keras.layers.Dense(512, activation='relu'),
                               tf.keras.layers.Dense(2)
                               ])

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

model.summary()

EPOCHS = 20

history = model.fit_generator(train_data_gen, steps_per_epoch=int(np.ceil(total_train/float(BATCH_SIZE))), epochs=EPOCHS, validation_data=val_data_gen, validation_steps=int(np.ceil(total_validation/float(BATCH_SIZE))))

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig('./foo.png')
plt.show()

Had the same issue.有同样的问题。 According to my repro, problem was history.history['val_accuracy'] had a single value, hence the error.根据我的复制,问题是 history.history['val_accuracy'] 有一个值,因此错误。 To resolve it, i tired with kernel_initializer='random_normal' in Dense layer while preparing the model.为了解决它,我在准备 model 时厌倦了密集层中的 kernel_initializer='random_normal'。

.
.
model.add(Dense(64, activation='relu', kernel_initializer='random_normal'))
.
.

The above plotting code shows these graphs with my test data.上面的绘图代码用我的测试数据显示了这些图表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题 - ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem Matplotlib 中的 Plot K-Means:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (10,) 和 (1,) - Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,) Matplotlib:ValueError:x 和 y 必须具有相同的第一维 - Matplotlib: ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM