AttributeError: 'list' object has no attribute 'set_model'

我与影子孤独终老i 提交于 2020-01-02 18:38:31

问题


I'm trying to use early stopping and Model check points to save the best model while training a deep convolution neural network. However, I get the following error:

 callback.set_model(model)
 AttributeError: 'list' object has no attribute 'set_model'

My code so far is:

model = Sequential()

###First block
model.add(Conv2D(100,kernel_size = (3,3),activation = 'relu',padding = 'same',input_shape=(12,11,1)))
model.add(Conv2D(100,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.20))


###Second block

model.add(Conv2D(128,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(Conv2D(128,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.10))


model.add(Flatten())

#model.add(Dense(100,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dense(1000,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dropout(0.30))
model.add(Dense(500,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dropout(0.10))
#model.add(Dense(500,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
#model.add(Dropout(0.15))

model.add(Dense(5,activation = 'softmax')) 

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


earlystop = [EarlyStopping(monitor='val_acc', min_delta=0.001, patience=5,
                          verbose=1, mode='auto')]


outputModel = 'outputModel'

model_json = model.to_json()
with open(outputModel+".json", "w") as json_file:
    json_file.write(model_json)
modWeightsFilepath=outputModel+"_weights.hdf5"
checkpoint = ModelCheckpoint(modWeightsFilepath, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='auto')

callbacks_list = [earlystop,checkpoint]


history = model.fit(x_train, Y,
      batch_size=100, ##number of observations per batch
      epochs=100, ##Number of epochs
      callbacks = callbacks_list,
      verbose=1,
      shuffle = True,
      validation_split=0.2) ###Data for evaluation

I don't know what I'm doing wrong. I read that ModelCheckPoint and earlystopping should be given as a list which is why I explicitly make it as:

  callbacks_list = [earlystop,checkpoint]

Help will be appreciated.


回答1:


You are correct about callbacks but earlystop is already a list here. Remove the brackets around [EarlyStopping(..)] to fix the problem.



来源:https://stackoverflow.com/questions/53063339/attributeerror-list-object-has-no-attribute-set-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!