使用python keras训练CNN时发生AttributeError

时间:2019-12-18 13:43:45

标签: python tensorflow machine-learning keras deep-learning

我正在尝试构建CNN,但出现属性错误  AttributeError:“ ProgbarLogger”对象没有属性“ log_values”

这是代码

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(16,(3, 3), activation = 'relu', padding = 'valid', input_shape = (28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size = (2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(100, activation = 'relu'))
model.add(tf.keras.layers.Dense(10, activation = 'softmax'))

# learning rule
optimizer = tf.keras.optimizers.SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)

# Loss function
model.compile(loss = 'categorical_crossentropy',
              optimizer = optimizer,
              metrics = ['accuracy'])
print(model.summary())


history = History()
model.fit(X_train, Y_train, batch_size = 64, epochs = 20, validation_split = 0.1, callbacks = [history])
score = model.evaluate(X_test, Y_test, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
plt.plot(history.history['accuracy'], '-r', label= "Training")
plt.plot(history.history['val_accuracy'], '-b', label= "Validation")
plt.xlabel('Epoch #')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

这是错误。我正在使用keras的更新版本并出现错误。我是机器学习的新手,将不胜感激

  

runfile('C:/ Users / mwaqa / Desktop / Spyder / E7 Q1.py',   wdir ='C:/ Users / mwaqa / Desktop / Spyder')       训练数据=(60000,28,28)(60000,)       测试数据=(10000,28,28)(10000,)       型号:“ sequential_29”       _________________________________________________________________       图层(类型)输出形状参数#
      ================================================== ===============       conv2d_25(Conv2D)(无,26,26,16)160
      _________________________________________________________________       max_pooling2d_23(MaxPooling(None,13,13,16)0
      _________________________________________________________________       flatten_23(Flatten)(None,2704)0
      _________________________________________________________________       density_49(Dense)(None,100)270500
      _________________________________________________________________       density_50(Dense)(None,10)1010
      ================================================== ===============       参数总计:271,670       可训练的参数:271,670       不可训练的参数:0       _________________________________________________________________       没有       训练54000个样本,验证6000个样本       时代1/20       追溯(最近一次通话):

  File "C:/Users/mwaqa/Desktop/Spyder/E7 Q1.py", line 57, in <module>
    model.fit(X_train, Y_train, batch_size = 64, epochs = 20, validation_split = 0.1, callbacks = [history])


AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
     
    

Blockquote

  

1 个答案:

答案 0 :(得分:-1)

我也遇到了同样的错误 发生此错误的原因是,您可能无法读取训练数据集或验证数据集的总数。并尝试将此总数除以可用batch_size(32/64)。 在我的情况下,由于以下原因导致出现错误 即我有png格式的图像数据集,我尝试读取jpg格式的所有图像,但是没有jpg格式的图像,那么您将检索的图像总数为0和0/32(样本/批量大小),这意味着训练数据集和验证数据集中没有可用的图像,因此AttributeError:'ProgbarLogger'对象没有属性'log_values' 所以我将代码更改了

no_of_sample = glob(train_path + '/*/*.jpg')

no_of_sample = glob(train_path + '/*/*.png')