我试图定义张量流中的损失,其中包含正则化项。但是,这不是标准的l2或l1规范。这是关于权重的Mahalanobis范数,具有协方差矩阵P.
例如,假设我有一个包含以下权重(tf.Variables)的两层神经网络:W1,b1,W2,b2。为了计算Mahalanobis范数,我需要将所有权重连接成一个向量,让我们称之为“#ta; theta"其中theta = [W1(:),b1(:),W2(:),b2(:)] ^ T。然后我想表演" theta ^ T P theta"。
我的问题是:如何更改tf.Variable的形状,同时保留以下属性:
我添加了一些代码来说明我的问题。我如何定义" theta"作为(W1,b1,W2,b2)的连接向量,具有上述特性?
我搜索了我的问题的答案,但我看到的所有正则化都是关于权重的l2或l1规范,而不是需要连接权重的东西,就像我的情况一样。
谢谢!
def create_model_trainable(self):
"""
Defines the trained network
:return:
"""
self.q_values = self.neural_net(self.input_placeholder)
self.selected_q = tf.reduce_sum(tf.mul(self.q_values, self.actions_placeholder), 1)
theta = ???
covariance = self.create_covariance_parameters()
regularization = self.regularization(covariance, theta)
self.loss = tf.reduce_sum(tf.square(self.labels_placeholder - self.selected_q)) + regularization
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
self.train_op = optimizer.minimize(self.loss)
def neural_net(self, input_obs):
"""
Defines the architecture of the neural network.
:param input_obs: input_placeholder for inputs
:return:
out: tf Tensor of full structure of the network
"""
with tf.name_scope("Layer1"):
W1shape = [self.observation_shape, self.hidden_size]
W1 = tf.get_variable("W1", shape=W1shape)
b_shape = [1, self.hidden_size]
b1 = tf.get_variable("b1", shape=b_shape, initializer=tf.constant_initializer(0.0))
with tf.name_scope("OutputLayer"):
Ushape = [self.hidden_size, self.num_actions]
U = tf.get_variable("U", shape=Ushape)
b3shape = [1, self.num_actions]
b3 = tf.get_variable("b3", shape=b3shape, initializer=tf.constant_initializer(0.0))
xW = tf.matmul(input_obs, W1)
h = tf.tanh(tf.add(xW, b1))
hU = tf.matmul(h, U)
out = tf.add(hU, b3)
return out