[英]Train for a parameter in the weight matrix in Tensorflow
我有一個神經網絡。 為簡單起見,只有一個層和權重矩陣為形狀的2-by-2
。 我需要網絡的輸出是輸入的旋轉版本,即矩陣應該是有效的旋轉矩陣。 我嘗試了以下方法:
def rotate(val):
w1 = tf.constant_initializer([[cos45, -sin45], [sin45, cos45]])
return tf.layers.dense(inputs=val, units=2, kernel_initializer=w1, activation=tf.nn.tanh)
在訓練時,我不想失去旋轉矩陣的屬性。 換句話說,我需要層僅估計矩陣中三角函數的角度(參數)。
我讀到kernel_constraint
可以通過規范化值在這方面提供幫助。 但是應用kernel_constraint
並不能保證對角線條目相等並且非對角線條目彼此為負(在這種情況下)。 一般來說,需要滿足的兩個屬性是,行列式應該是 1 和R^T*R = I
。
有沒有其他方法可以實現這一目標?
您可以定義自定義 Keras 層。 類似的東西:
from tensorflow.keras.layers import Layer
import tensorflow as tf
class Rotate(Layer):
def build(self, input_shape):
sh = input_shape[0]
shape = [sh, sh]
# Initial weight matrix
w = self.add_weight(shape=shape,
initializer='random_uniform')
# Set upper diagonal elements to negative of lower diagonal elements
mask = tf.cast(tf.linalg.band_part(tf.ones(shape), -1, 0), tf.float32)
w = mask * w
w -= tf.transpose(w)
# Set the same weight to the diagonal
diag_mask = 1 - tf.linalg.diag(tf.ones(sh))
w = diag_mask * w
diag_w = self.add_weight(shape=(1,),
initializer='random_uniform')
diagonal = tf.linalg.diag(tf.ones(sh)) * diag_w
self.kernel = w + diagonal
def call(self, inputs, **kwargs):
return tf.matmul(inputs, self.kernel)
請注意, self.kernel
的可學習權重self.kernel
具有以下方面: [[D, -L], [L, D]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.