在训练模型后绘制时出现错误



在CNN中训练模型后,同时绘制model_history,我面临一个称为'所有数组必须具有相同长度'的值错误。请帮助我如何解决这个问题。

这是训练模型:

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()

包含错误片段的代码如下:

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

这是我得到的错误:

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()
~anaconda3libsite-packagespandascoreframe.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
~anaconda3libsite-packagespandascoreinternalsconstruction.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     )
~anaconda3libsite-packagespandascoreinternalsconstruction.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)
~anaconda3libsite-packagespandascoreinternalsconstruction.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

收到错误的快照:

请在训练模型后,使用下面的代码再次尝试绘制model learning history:

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')

如果问题仍然存在,请告诉我们。

相关内容

  • 没有找到相关文章

最新更新