属性错误:模块"keras.layers"没有属性"正则化程序"

qgelzfjb  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(131)

我第一次尝试从Git运行Python项目,在安装了所有需要的库之后,我启动了程序,但收到了以下错误:

属性错误:模块“keras.layers”没有属性“正则化器”

资料来源:

from keras import layers, models, optimizers
from keras import backend as K

class Actor:
    '''
    Actor(policy) Model
    '''
    def __init__(self, state_size, action_size):
        self.state_size = state_size
        self.action_size = action_size

        self.build_model()
    
    def build_model(self):
        states = layers.Input(shape=(self.state_size, ), name='states')

        net = layers.Dense(units=16, kernel_regularizer=layers.regularizers.l2(1e-6))(states)
        net = layers.BatchNormalization()(net)
        net = layers.Activation('relu')(net)
        net = layers.Dense(units=32, kernel_regularizer=layers.regularizers.l2(1e-6))(net)
        net = layers.BatchNormalization()(net)
        net = layers.Activation('relu')(net)

        actions = layers.Dense(units=self.action_size, activation='softmax', name='actions')(net)

        self.model = models.Model(inputs=states, outputs=actions)

        action_gradients = layers.Input(shape=(self.action_size, ))
        loss = K.mean(-action_gradients * actions)

        optimizer = optimizers.Adam(lr=.0001)
        updates_op = optimizer.get_updates(params=self.model.trainable_weights, loss=loss)
        self.train_fn = K.function(
            inputs = [self.model.input, action_gradients, K.learning_phase()],
            outputs = [],
            updates = updates_op
        )
2w3rbyxf

2w3rbyxf1#

我认为程序是用TensorFlow版本1和keras版本1编写的。这样编写可能会解决问题
net = layers.Dense(32, kernel_regularizer=tensorflow.keras.regularizers.L1(1e-6))(states),但会出现很多错误。因为使用了一些在后续版本中已删除的功能,例如

optimizer.get_updates

不再工作。所以,最好使用更高版本

相关问题