0xZohar's picture
Add code/cube3d/training/norm_rotation.py
aaa20ea verified
import numpy as np
# 读取文件内容
with open("rotation_matrix_300_round1_filter1_error_log.txt", "r") as file:
lines = file.readlines()
# 初始化一个列表用于存储归一化后的矩阵
normalized_matrices = []
# 逐行处理文件中的矩阵
i = 0
while i < len(lines):
# 查找每个错误信息行
if lines[i].startswith("Error with rotation matrix:"):
matrix = []
i += 1 # 跳到矩阵数据的第一行
# 收集矩阵数据,直到遇到分隔行
while i < len(lines) and lines[i].strip() != "--------------------------------------------------":
row = lines[i].strip().replace("[", "").replace("]", "") # 清理矩阵行数据
matrix.append([float(x) for x in row.split()]) # 转换为浮动数值
i += 1
matrix = np.array(matrix) # 转换为 numpy 数组
# 归一化操作:计算矩阵的 Frobenius 范数
norm = np.linalg.norm(matrix)
normalized_matrix = matrix / norm # 归一化
normalized_matrices.append(normalized_matrix)
i += 1 # 继续处理下一个矩阵
# 将归一化后的矩阵保存到一个新文件
with open("normalized_rotation_matrix_log.txt", "w") as file:
for norm_matrix in normalized_matrices:
file.write("Normalized rotation matrix:\n")
for row in norm_matrix:
file.write(f"{row}\n")
file.write("-" * 50 + "\n")
print("归一化后的矩阵已保存到 normalized_rotation_matrix_log.txt")