Qiitaからとってきたコードですが activationsがないといわれます バージョンの違いでしょうか?
https://qiita.com/pocokhc/items/fc00f8ea9dca8f8c0297#noisy-network
自分のKeras は2.3.1
class NoisyDense(Layer): def __init__(self, units, sigma_init=0.02, activation=None, use_bias=True, kernel_initializer='he_normal', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs): if 'input_shape' not in kwargs and 'input_dim' in kwargs: kwargs['input_shape'] = (kwargs.pop('input_dim'),) super(NoisyDense, self).__init__(**kwargs) self.units = units self.sigma_init = sigma_init self.activation = activations.get(activation) self.use_bias = use_bias self.kernel_initializer = initializers.get(kernel_initializer) self.bias_initializer = initializers.get(bias_initializer) self.kernel_regularizer = regularizers.get(kernel_regularizer) self.bias_regularizer = regularizers.get(bias_regularizer) self.activity_regularizer = regularizers.get(activity_regularizer) self.kernel_constraint = constraints.get(kernel_constraint) self.bias_constraint = constraints.get(bias_constraint) def build(self, input_shape): assert len(input_shape) >= 2 self.input_dim = input_shape[-1] self.kernel = self.add_weight(shape=(self.input_dim, self.units), initializer=self.kernel_initializer, name='kernel', regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) self.sigma_kernel = self.add_weight(shape=(self.input_dim, self.units), initializer=initializers.Constant(value=self.sigma_init), name='sigma_kernel' ) if self.use_bias: self.bias = self.add_weight(shape=(self.units,), initializer=self.bias_initializer, name='bias', regularizer=self.bias_regularizer, constraint=self.bias_constraint) self.sigma_bias = self.add_weight(shape=(self.units,), initializer=initializers.Constant(value=self.sigma_init), name='sigma_bias') else: self.bias = None self.epsilon_bias = None self.epsilon_kernel = K.zeros(shape=(self.input_dim, self.units)) self.epsilon_bias = K.zeros(shape=(self.units,)) self.sample_noise() super(NoisyDense, self).build(input_shape) def call(self, X): perturbation = self.sigma_kernel * self.epsilon_kernel perturbed_kernel = self.kernel + perturbation output = K.dot(X, perturbed_kernel) if self.use_bias: bias_perturbation = self.sigma_bias * self.epsilon_bias perturbed_bias = self.bias + bias_perturbation output = K.bias_add(output, perturbed_bias) if self.activation is not None: output = self.activation(output) return output def compute_output_shape(self, input_shape): assert input_shape and len(input_shape) >= 2 assert input_shape[-1] output_shape = list(input_shape) output_shape[-1] = self.units return tuple(output_shape) def sample_noise(self): K.set_value(self.epsilon_kernel, np.random.normal(0, 1, (self.input_dim, self.units))) K.set_value(self.epsilon_bias, np.random.normal(0, 1, (self.units,))) def remove_noise(self): K.set_value(self.epsilon_kernel, np.zeros(shape=(self.input_dim, self.units))) K.set_value(self.epsilon_bias, np.zeros(shape=self.units,))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-12-fc968c7c32fe> in <module> 269 # [4.2]Qネットワークとメモリ、Actorの生成-------------------------------------------------------- 270 #前処理 --> 271 mainQN = QNetwork(learning_rate=learning_rate,action_size=2) # メインのQネットワーク 272 targetQN = QNetwork(learning_rate=learning_rate,action_size=2) # 価値を計算するQネットワーク 273 # plot_model(mainQN.model, to_file='Qnetwork.png', show_shapes=True) # Qネットワークの可視化 <ipython-input-12-fc968c7c32fe> in __init__(self, learning_rate, state_size, action_size) 131 self.v=NoisyDense(1, #Velu 132 use_bias=True,kernel_initializer='he_normal',bias_initializer='zeros',kernel_constraint=max_norm(2.), --> 133 bias_constraint=max_norm(2.))(self.v) 134 self.dv=Dense(16,activation='relu' 135 ,use_bias=True,kernel_initializer='he_normal',bias_initializer='zeros',kernel_constraint=max_norm(2.), <ipython-input-12-fc968c7c32fe> in __init__(self, units, sigma_init, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs) 37 self.units = units 38 self.sigma_init = sigma_init ---> 39 self.activation = activations.get(activation) 40 self.use_bias = use_bias 41 self.kernel_initializer = initializers.get(kernel_initializer) NameError: name 'activations' is not defined
回答1件
あなたの回答
tips
プレビュー