Noisy Netを実装したDQNで強化学習を行っています。
学習済みモデルの評価を行っていると、Inputは同じデータなのに対して異なるQ値のリストをOutputしていました。
Noisy Netは評価時にもノイズが乗った行動をとってしまうのでしょうか?もしくは評価時は特別な処理を入れる必要があるのでしょうか。
python
1class NoisyDense(Layer): 2 def __init__(self, 3 units=7, 4 sigma_init=0.02, 5 activation=None, 6 use_bias=True, 7 kernel_initializer='glorot_uniform', 8 bias_initializer='zeros', 9 kernel_regularizer=None, 10 bias_regularizer=None, 11 activity_regularizer=None, 12 kernel_constraint=None, 13 bias_constraint=None, 14 **kwargs): 15 if 'input_shape' not in kwargs and 'input_dim' in kwargs: 16 kwargs['input_shape'] = (kwargs.pop('input_dim'),) 17 super(NoisyDense, self).__init__(**kwargs) 18 self.units = units 19 self.sigma_init = sigma_init 20 self.activation = activations.get(activation) 21 self.use_bias = use_bias 22 self.kernel_initializer = initializers.get(kernel_initializer) 23 self.bias_initializer = initializers.get(bias_initializer) 24 self.kernel_regularizer = regularizers.get(kernel_regularizer) 25 self.bias_regularizer = regularizers.get(bias_regularizer) 26 self.activity_regularizer = regularizers.get(activity_regularizer) 27 self.kernel_constraint = constraints.get(kernel_constraint) 28 self.bias_constraint = constraints.get(bias_constraint) 29 30 def build(self, input_shape): 31 assert len(input_shape) >= 2 32 self.input_dim = input_shape[-1] 33 34 self.kernel_shape = tf.constant((self.input_dim, self.units)) 35 self.bias_shape = tf.constant((self.units, )) 36 37 self.kernel = self.add_weight(shape=(self.input_dim, self.units), 38 initializer=self.kernel_initializer, 39 name='kernel', 40 regularizer=self.kernel_regularizer, 41 constraint=self.kernel_constraint) 42 43 self.sigma_kernel = self.add_weight(shape=(self.input_dim, self.units), 44 initializer=initializers.Constant(value=self.sigma_init), 45 name='sigma_kernel' 46 ) 47 48 if self.use_bias: 49 self.bias = self.add_weight(shape=(self.units,), 50 initializer=self.bias_initializer, 51 name='bias', 52 regularizer=self.bias_regularizer, 53 constraint=self.bias_constraint) 54 self.sigma_bias = self.add_weight(shape=(self.units,), 55 initializer=initializers.Constant(value=self.sigma_init), 56 name='sigma_bias') 57 else: 58 self.bias = None 59 self.epsilon_bias = None 60 61 self.epsilon_kernel = K.zeros(shape=(self.input_dim, self.units)) 62 self.epsilon_bias = K.zeros(shape=(self.units,)) 63 64 self.sample_noise() 65 super(NoisyDense, self).build(input_shape) 66 67 def call(self, X): 68 perturbation = self.sigma_kernel * K.random_uniform(shape=self.kernel_shape) 69 perturbed_kernel = self.kernel + perturbation 70 output = K.dot(X, perturbed_kernel) 71 if self.use_bias: 72 bias_perturbation = self.sigma_bias * K.random_uniform(shape=self.bias_shape) 73 perturbed_bias = self.bias + bias_perturbation 74 output = K.bias_add(output, perturbed_bias) 75 if self.activation is not None: 76 output = self.activation(output) 77 return output 78 79 def compute_output_shape(self, input_shape): 80 assert input_shape and len(input_shape) >= 2 81 assert input_shape[-1] 82 output_shape = list(input_shape) 83 output_shape[-1] = self.units 84 return tuple(output_shape) 85 86 def sample_noise(self): 87 K.set_value(self.epsilon_kernel, np.random.normal(0, 1, (self.input_dim, self.units))) 88 K.set_value(self.epsilon_bias, np.random.normal(0, 1, (self.units,))) 89 90 def remove_noise(self): 91 K.set_value(self.epsilon_kernel, np.zeros(shape=(self.input_dim, self.units))) 92 K.set_value(self.epsilon_bias, np.zeros(shape=self.units,))
あなたの回答
tips
プレビュー