请有人能帮助我将此函数代码从pytorch转换为tensorflow 这些功能将添加为keras lambda层
这个想法是采用没有FC层的vgg或resnet预训练模型。将特征图简化为2D矩阵,然后使用这些矩阵平方根函数之一通过特定的梯度计算对其进行归一化。 然后最后将其送入softmax层。
原始作者没有使用keras,但是我必须使用它,并且我已经阅读到keras会自动执行反向传播。但是我想像原始作者那样做我的后援
# Compute error
def compute_error(A, sA):
normA = torch.sqrt(torch.sum(torch.sum(A * A, dim=1),dim=1))
error = A - torch.bmm(sA, sA)
error = torch.sqrt((error * error).sum(dim=1).sum(dim=1)) / normA
return torch.mean(error)
这是我的代码
#Compute Eror
def Compute_Error(A,sA):
Norm_A = tf.sqrt(tf.reduce_sum(tf.reduce_sum(A*A, axis=1),axis=1))
Error = A - tf.matmul(sA,sA)
Error = (tf.sqrt(tf.reduce_sum(tf.reduce_sum(Error*Error, axis=1),axis=1)))/Norm_A
return tf.reduce_mean(Error)
具有svd和lyapunov方程的矩阵平方根
# Forward + Backward via SVD decomposition
def sqrt_svd_lyap(A, dldz, dtype):
batchSize = A.data.shape[0]
dim = A.data.shape[1]
dlda = torch.zeros(batchSize, dim, dim).type(dtype)
sA = torch.zeros(batchSize, dim, dim).type(dtype)
for i in range(batchSize):
U, S, V = (A[i,:,:].data).svd()
sA[i,:,:] = (U.mm(S.diag().sqrt())).mm(V.t())
S = S.diag().sqrt().mm(torch.ones(dim, dim).type(dtype))
IU = U.t()
X = -U.mm(
((IU.mm(dldz[i,:,:].data)).mm(IU.t()))
/(S+S.t())
).mm(U.t());
dlda[i,:,:] = X
return sA, dlda, compute_error(A, Variable(sA, requires_grad=False))
这是我的代码
#Forward + Backward via SVD
def Sqrt_Root_SVD_Lyapunov(A):
BatchSize = A.shape[0]
Dim = A.shape[1]
#dlda = tf.zeros([BatchSize,Dim,Dim],tf.float32)
sA = tf.zeros([BatchSize,Dim,Dim],tf.float32)
output_list = []
for i in range(BatchSize):
S,U,V = tf.svd(A[i,:,:],compute_uv = True)
S = tf.where(tf.less(S, 1e-10), S, tf.sqrt(S))
SQRT = tf.matmul(U,(tf.matmul(tf.diag(tf.sqrt(S)),V, transpose_b = True)))
output_list.append(SQRT)
#S = tf.matmul(tf.diag(tf.sqrt(S)), (tf.ones([Dim,Dim],Type)))
#IU = tf.transpose(U)
#X = tf.matmul(tf.matmul(-U,(tf.matmul((tf.matmul(dldz,IU)),U)/(S+tf.transpose(S)))),IU)
#dlda [i,:,:] = X
sA= tf.stack(output_list)
return sA
具有Denman-Beavers迭代的矩阵平方根
# Forward via Denman-Beavers iterations
def sqrt_denman_beavers(A, numIters, dtype):
batchSize = A.data.shape[0]
dim = A.data.shape[1]
sA = torch.zeros(batchSize, dim, dim).type(dtype)
for n in range(batchSize):
Y = (A[n,:,:]).data
Z = torch.eye(dim, dim).type(dtype)
for i in range(numIters):
Y_ = 0.5*(Y + Z.inverse())
Z = 0.5*(Z + Y.inverse())
Y = Y_
sA[n,:,:] = Y
sA = Variable(sA, requires_grad=False)
error = compute_error(A,sA)
return sA, error
这是我的代码
#Forward via Denman-Beavers iterations
def Sqrt_Root_Denman_Beavers(A, Nb_Iterations, Type):
BatchSize = A.shape[0]
Dim = A.shape[1]
sA = tf.zeros([BatchSize,Dim,Dim],Type)
output_list = []
for i in range(BatchSize):
Y = A[i,:,:]
Z = tf.eye(20)
for j in range (Nb_Iterations):
Y_tmp = 0.5 * (Y + tf.linalg.inv(Z))
Z = 0.5 * (Z + tf.linalg.inv(Y))
Y = Y_tmp
output_list.append(Y)
sA = tf.stack(output_list)
Error = Compute_Error(A,sA)
return sA, Error
我想将这些功能之一添加为keras顺序模型中的lambda层。 训练模型时,我得到的精度不能超过0.40,所以我怀疑我的代码是错误的。