首页 > 解决方案 > TypeError: “NoneType” object is not callable in Google Colab

问题描述

I am trying to train my model using Keras and TensorFlow.

Code where I'm getting the error.

def build_model():

# define the model, use pre-trained weights for image_net 
  base_model = InceptionV3(input_shape=(resized_height, resized_width, num_channel), weights='imagenet', include_top=False, pooling='avg')

  x = base_model.output
  # x = Dense(100, activation='relu')(x)
  # predictions = Dense(6, activation='sigmoid', name='final_classifier')(x)
  # model = Model(inputs = base_model.input, outputs= predictions)

  model = Sequential()
  # # model.add(LSTM(1024, return_sequences=False, kernel_initializer='he_normal', dropout=0.15, recurrent_dropout=0.15, implementation=2))
  model = Sequential()
  model.add(Dense(1024, activation='relu', input_shape=(51200,)))(x)
  model.add(Dropout(0.5))
  model.add(Dense(512, activation='relu'))(x)
  model.add(Dropout(0.5))
  model.add(Dense(256, activation='relu'))(x)
  model.add(Dropout(0.5))
  model.add(Dense(128, activation='relu'))(x)
  model.add(Dropout(0.5))
  model.add(Dense(64, activation='relu'))(x)
  model.add(Dropout(0.5))
  model.add(Dense(6, activation='softmax', name='final_classifier'))(x)
  
  return model

Build and Run the Model

model = build_model()
model_checkpoint = ModelCheckpoint(weight_file, monitor='val_loss', save_weights_only=False, save_best_only=True)

num_workers = 2

model.compile(optimizer=Adam(lr=initial_lr), loss='categorical_crossentropy', metrics=['accuracy'])

callbacks = [model_checkpoint, reduce_lr_on_plateau, tensor_board]

labels = labels_all
partition = partition_dict

model.fit_generator(generator=DataGenSequence(labels, partition['train'], current_state='train'),
                    steps_per_epoch=100,
                    epochs = 200,
                    verbose=1,
                    workers = num_workers,
                    callbacks=callbacks,
                    shuffle=False,
                    # maz_queue_size=32,
                    validation_data=DataGenSequence(labels, partition['valid'], current_state='validation'),
                    validation_steps=5
                    )

ERROR

enter image description here

Note: I am suffering from this error , I can't solved it and advanced thanks who are try to solve it and comment here for sharing the answer

标签: pythontensorflowkerasconv-neural-network

解决方案


Sequentail().add() does not have a return value or if speaking in Python: return None wich is an object of the Type NoneType. So when you are calling like this: Sequential().add()(x) you are calling the method .add() from the class Sequential and then you are trying to call its return value. This does not work since the return value is not a function but None from the NoneType.


推荐阅读