0xZohar commited on
Commit
aaa20ea
·
verified ·
1 Parent(s): 27bc948

Add code/cube3d/training/norm_rotation.py

Browse files
code/cube3d/training/norm_rotation.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ # 读取文件内容
4
+ with open("rotation_matrix_300_round1_filter1_error_log.txt", "r") as file:
5
+ lines = file.readlines()
6
+
7
+ # 初始化一个列表用于存储归一化后的矩阵
8
+ normalized_matrices = []
9
+
10
+ # 逐行处理文件中的矩阵
11
+ i = 0
12
+ while i < len(lines):
13
+ # 查找每个错误信息行
14
+ if lines[i].startswith("Error with rotation matrix:"):
15
+ matrix = []
16
+ i += 1 # 跳到矩阵数据的第一行
17
+ # 收集矩阵数据,直到遇到分隔行
18
+ while i < len(lines) and lines[i].strip() != "--------------------------------------------------":
19
+ row = lines[i].strip().replace("[", "").replace("]", "") # 清理矩阵行数据
20
+ matrix.append([float(x) for x in row.split()]) # 转换为浮动数值
21
+ i += 1
22
+ matrix = np.array(matrix) # 转换为 numpy 数组
23
+
24
+ # 归一化操作:计算矩阵的 Frobenius 范数
25
+ norm = np.linalg.norm(matrix)
26
+ normalized_matrix = matrix / norm # 归一化
27
+
28
+ normalized_matrices.append(normalized_matrix)
29
+
30
+ i += 1 # 继续处理下一个矩阵
31
+
32
+ # 将归一化后的矩阵保存到一个新文件
33
+ with open("normalized_rotation_matrix_log.txt", "w") as file:
34
+ for norm_matrix in normalized_matrices:
35
+ file.write("Normalized rotation matrix:\n")
36
+ for row in norm_matrix:
37
+ file.write(f"{row}\n")
38
+ file.write("-" * 50 + "\n")
39
+
40
+ print("归一化后的矩阵已保存到 normalized_rotation_matrix_log.txt")