pytorch实现施密特正交化

pytorch实现施密特正交
对于⼀个给定的pytorch张量,如果想要对这⾥⾯的每⼀个⾏向量进⾏施密特正交化,可以使⽤下⾯的代码:
import numpy as np
from sympy.matrices import Matrix, GramSchmidt
import torch
functional as F
def orthogo_tensor(x):
m, n = x.size()
x_np = x.t().numpy()
matrix =[Matrix(col)for col in x_np.T]
gram = GramSchmidt(matrix)
ort_list =[]
for i in range(m):
vector =[]
for j in range(n):
vector.append(float(gram[i][j]))
ort_list.append(vector)
ort_list = np.mat(ort_list)
ort_list = torch.from_numpy(ort_list)
ort_list = F.normalize(ort_list,dim=1)
return ort_list
x = torch.randn(4,6)
x = orthogo_tensor(x)
print(x)
print(x.matmul(x.t()))
程序运⾏结果如下所⽰:
tensor([[-0.1633,0.5601,-0.6081,-0.2640,-0.4215,-0.2061],
[0.0386,-0.0236,-0.0674,0.4131,-0.5576,0.7154],
[-0.3210,0.6896,0.5430,-0.1018,0.1794,0.2898],
一叶荻[-0.3445,-0.3674,0.3962,-0.5217,-0.5563,-0.0886]],
dtype=torch.float64)
淳于髡tensor([[1.0000e+00,-3.6843e-09,1.5446e-08,-7.1455e-08],
易切削不锈钢>四川康定地震[-3.6843e-09,1.0000e+00,-2.1965e-08,-3.5087e-08],
[1.5446e-08,-2.1965e-08,1.0000e+00,-2.0042e-07],
性之初[-7.1455e-08,-3.5087e-08,-2.0042e-07,1.0000e+00]],
dtype=torch.float64)blc

本文发布于:2024-09-23 02:20:51,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/437370.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:正交   康定   结果
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议