keras 未定义“BatchNormalization”

o7jaxewo  于 2023-03-08  发布在  其他
关注(0)|答案(4)|浏览(264)

尝试训练鲁棒CNN模型,其定义如下:

from keras.datasets import cifar10
from keras.utils import np_utils
from keras import metrics
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, LSTM, merge
from keras.layers import BatchNormalization
from keras import metrics
from keras.losses import categorical_crossentropy
from keras.optimizers import SGD
import pickle
import matplotlib.pyplot as plt
import numpy as np 
from keras.preprocessing.image import ImageDataGenerator
from keras import layers
from keras.callbacks import EarlyStopping


def Robust_CNN():
    
    model = Sequential()
    model.add(Conv2D(256, (3, 3), activation='relu', padding='same', init='glorot_uniform', input_shape=(2,128,1)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(1, 2), padding='valid',  data_format=None))
    model.add(layers.Dropout(.3))
    model.add(Conv2D(128, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
    model.add(layers.Dropout(.3))
    model.add(Conv2D(64, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
    model.add(layers.Dropout(.3))
    model.add(Conv2D(64, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
    model.add(layers.Dropout(.3))
    model.add(Flatten())
    model.add(Dense(128, activation='relu', init='he_normal'))
    model.add(BatchNormalization())
    model.add(Dense(11, activation='softmax', init='he_normal'))
    
    return model

但是,在尝试执行此操作时,我收到一个NameError,指出name 'BatchNormalization'未定义。完整的错误消息如下所示:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-8084d29438f8> in <module>
     55 # >>>>>>>>>>>>>>>>>>>>> choose a model by un-commenting only one of the three <<<<<<<<<<<<<<<<<<<<<<<<<<<
     56 #xx_shape = (2,128,1)
---> 57 models = Robust_CNN()
     58 #models = CLDNN()
     59 #models = resnet(xx_shape)

~\AppData\Local\Programs\Python\Python37\Scripts\FYP\Optimizing-Modulation-Classification-with-Deep-Learning-master\Optimizing-Modulation-Classification-with-Deep-Learning-master\Robust_CNN Model\model.py in Robust_CNN()
     19 def Robust_CNN():
     20 
---> 21     model = Sequential()
     22     model.add(Conv2D(256, (3, 3), activation='relu', padding='same', init='glorot_uniform', input_shape=(2,128,1)))
     23     model.add(BatchNormalization())

NameError: name 'BatchNormalization' is not defined

即使我已经导入了BatchNormalization,似乎也弄不明白为什么会出现这种情况。

vlju58qv

vlju58qv1#

首先从tensorflow.keras.layers导入BatchNormalization,然后运行代码:

from tensorflow.keras.layers import BatchNormalization
ebdffaop

ebdffaop2#

把这个加到你的代码里-
from tensorflow.keras.layers import BatchNormalization

wb1gzix0

wb1gzix03#

# import BatchNormalization
from keras.layers.normalization import BatchNormalization
e1xvtsh3

e1xvtsh34#

导入批次规范化

从tensorflow .keras.layers导入批次规格化

相关问题