Download scripts/verify_finegrained_purity.py from zhangdebiao/rlclean: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/datasets/zhangdebiao/rlclean/resolve/main/scripts/verify_finegrained_purity.py
- Command line
-
hf download hf://datasets/zhangdebiao/rlclean/scripts/verify_finegrained_purity.py
-
curl -L -o verify_finegrained_purity.py https://huggingface.co/datasets/zhangdebiao/rlclean/resolve/main/scripts/verify_finegrained_purity.py
2.27 kB
| import numpy as np | |
| import pickle | |
| def verify(): | |
| idx_path = 'data/clean_subset_indices.npz' # 我们用 center-level 的 99% 那个文件 | |
| data_path = 'data/raw/fine_grained_processed/walker2d-medium-v2_independent_gaussian_p0.112.pkl' | |
| print(f"Loading Dataset: {data_path}") | |
| with open(data_path, 'rb') as f: | |
| data = pickle.load(f) | |
| print(f"Loading Indices: {idx_path}") | |
| idx_data = np.load(idx_path, allow_pickle=True) | |
| H = 5 | |
| # 提取所有 GT masks | |
| ms = np.concatenate([t['mask_s'] for t in data['trajectories']], axis=0).reshape(-1) | |
| ma = np.concatenate([t['mask_a'] for t in data['trajectories']], axis=0).reshape(-1) | |
| mr = np.concatenate([t['mask_r'] for t in data['trajectories']], axis=0).reshape(-1) | |
| # 整体 transition 是否脏 (s, a, r 任一脏即脏) | |
| mt = np.clip(ms + ma + mr, 0, 1) | |
| clean_indices_relative = idx_data['clean_indices'] | |
| # 转换为绝对索引 | |
| clean_indices_absolute = clean_indices_relative + H | |
| print(f"\n--- 细粒度掩码分析 ---") | |
| print(f"总 Transitions 数: {len(mt)}") | |
| print(f"提取的 Clean 子集大小: {len(clean_indices_absolute)}") | |
| # 在 Clean 子集中的 s, a, r 各自的脏数据量 | |
| dirty_s_in_clean = ms[clean_indices_absolute].sum() | |
| dirty_a_in_clean = ma[clean_indices_absolute].sum() | |
| dirty_r_in_clean = mr[clean_indices_absolute].sum() | |
| dirty_t_in_clean = mt[clean_indices_absolute].sum() | |
| total_clean = len(clean_indices_absolute) | |
| print(f"\n在被判定为 Clean 的 {total_clean} 个样本中:") | |
| print(f"状态 (S) 被真实加噪的数量: {int(dirty_s_in_clean)} (错误率: {dirty_s_in_clean/total_clean*100:.4f}%)") | |
| print(f"动作 (A) 被真实加噪的数量: {int(dirty_a_in_clean)} (错误率: {dirty_a_in_clean/total_clean*100:.4f}%)") | |
| print(f"奖励 (R) 被真实加噪的数量: {int(dirty_r_in_clean)} (错误率: {dirty_r_in_clean/total_clean*100:.4f}%)") | |
| # 计算 transition 级别的纯度 | |
| print(f"\n=> 任何一者(S/A/R)被加噪的总数: {int(dirty_t_in_clean)}") | |
| print(f"=> 细粒度联合纯度 (Transition完全干净): {(1 - dirty_t_in_clean/total_clean)*100:.4f}%") | |
| if __name__ == "__main__": | |
| verify() | |