简体   繁体   中英

Getting an error while plotting after training the model

After training the model in CNN while plotting the model_history i am facing a value error called 'All arrays must be of the same length'.Please assist me on how to fix this.

This is the training model:

train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
                        input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=1e-4),
              metrics=['acc'])


history = model.fit(
      train_generator,
      steps_per_epoch=62.5,
      epochs=100,
      validation_data=validation_generator,
      validation_steps=50)

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

The code with error snippet is as follows:

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

This is the error I am getting:

ValueError                                Traceback (most recent call last)
<ipython-input-18-47d552496333> in <module>
----> 1 pd.DataFrame(history.history).plot(figsize=(8, 5))
      2 plt.grid(True)
      3 plt.gca().set_ylim(0, 1)
      4 plt.show()

~\anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    612         elif isinstance(data, dict):
    613             # GH#38939 de facto copy defaults to False only in non-dict cases
--> 614             mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
    615         elif isinstance(data, ma.MaskedArray):
    616             import numpy.ma.mrecords as mrecords

~\anaconda3\lib\site-packages\pandas\core\internals\construction.py in dict_to_mgr(data, index, columns, dtype, typ, copy)
    460         # TODO: can we get rid of the dt64tz special case above?
    461 
--> 462     return arrays_to_mgr(
    463         arrays, data_names, index, columns, dtype=dtype, typ=typ, consolidate=copy
    464     )

~\anaconda3\lib\site-packages\pandas\core\internals\construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity, typ, consolidate)
    115         # figure out the index, if necessary
    116         if index is None:
--> 117             index = _extract_index(arrays)
    118         else:
    119             index = ensure_index(index)

~\anaconda3\lib\site-packages\pandas\core\internals\construction.py in _extract_index(data)
    621             lengths = list(set(raw_lengths))
    622             if len(lengths) > 1:
--> 623                 raise ValueError("All arrays must be of the same length")
    624 
    625             if have_dicts:

ValueError: All arrays must be of the same length

Snap of the error received :

Please try again plotting the model learning history using below code, after training the model:

import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(history.history['acc'], label='accuracy')
plt.plot(history.history['val_acc'], label = 'val_accuracy')
plt.grid(True)
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')

Please let us know if issue still persists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM