时间:2024年4月2日-4月6日, 4月11日-4月12日
前一个文件见boml项目学习
x# 重新整理后将代码分为两大类:cpu 和 gpu_Colab
bash <(curl -sSL "https://raw.githubusercontent.com/Cz1544252489/DailyWork/main/shell%20script/BLO_cpu.sh")
bash <(curl -sSL "https://github.com/Cz1544252489/DailyWork/raw/main/shell%20script/BLO_gpu_Colab.sh")
使用GitHub项目 学习双层优化以下内容是基于 vis-opt-group/BDA脚本Data_hyper_cleaning.py脚本Few_shot.py其他安装jupyter notebook忽略部分警告F1的含义使用matplotlib可视化结果保存为pickle格式关于json管理包jq的使用pickle包的使用一个tools文件BDA项目中无法使用GPU的问题提高GPU使用效率
前言:由于现在对原boml项目有一定的了解,发现当前的BDA项目在原来的项目基础上有一定程度的精简。对于数据的获取问题上有了更加明确的用于处理数据清晰问题的代码,进而进一步分析该代码。
在数据超清洗问题上,BDA中提供了一个交互函数来添加参数,相比于原项目中使用json数据文档的方式要更方便一点。这一点可以在后期自己写代码时利用上,以增加代码使用的便捷性。
基于运行情况,该项目也可以在之前搭建的环境下使用GPU进行运算。
一些使用例子
xxxxxxxxxx
python Data_hyper_cleaning.py -d=omniglot -mbs=20 -nmi=500
# 输出太频繁且没有最后的总结
python Data_hyper_cleaning.py -d=omniglot -mbs=20 -nmi=500 -log=Ture -pi=10 -si=50 -te=250
# 此处pi 和si似乎不是针对当前输出的 (使用Colab T4)
python Data_hyper_cleaning.py -d=omniglot -mbs=5 -nmi=500 -log=Ture -pi=10 -si=50 -te=250
# 此处pi 和si似乎不是针对当前输出的 (使用Colab T4)
python Data_hyper_cleaning.py -etes=10 -mbs=5 -nmi=200 -md=Aggr -log=True -pi=10 -si=50 -te=100
python Data_hyper_cleaning.py -etes=10 -mbs=5 -nmi=200 -log=True -pi=100 -si=100
python Data_hyper_cleaning.py -etes=10 -mbs=5 -nmi=200 -md=Aggr -log=True -pi=100 -si=100 -Notes='Aggr'
##########################################################
#
# 以下为更加系统性的测试
#
#############################################################
# 针对数据集的结果
python Data_hyper_cleaning.py -d='omniglot' -Notes='omni' -nmi=100 -log=True -pi=50 -si=100
python Data_hyper_cleaning.py -d='miniimagenet' -Notes='mini' -nmi=100 -log=True -pi=50 -si=100
python Data_hyper_cleaning.py -d='omniglot' -md='Aggr' -Notes='omni-Aggr' -nmi=100 -log=True -pi=50 -si=100
python Data_hyper_cleaning.py -d='miniimagenet' -md='Aggr' -Notes='mini-Aggr' -nmi=100 -log=True -pi=50 -si=100
##
inner_method='Aggr'
outer_method='Reverse'
python Data_hyper_cleaning.py -i_d=$inner_method -o_d=$outer_method -nmi=200 -log=True -pi=50 -si=100 -Notes='Aggr_Reverse'
python tools.py -m='plot_pic' -fi='Aggr_Reverse.pickle' -fo='Aggr_Reverse.png' -py='train_train' && rm temp.json
#######################################
# scp root@x.x.x.x:~/BDA/test_script/Aggr_Reverse.png ./
######################################
# 使用T4 平均每次迭代0.25s,5000次大约需要21分钟(实际用时远超21分钟)
python Data_hyper_cleaning.py -mbs=10 -i_d='Aggr' -o_d='Reverse' -nmi=2000 -log=True -pi=100 -si=100 -Notes='Aggr_Reverse_mbs10_nmi2000_pi100_si_100'
python tools.py -m='plot_pic' -fi='Aggr_Reverse_mbs10_nmi2000_pi100_si_100.pickle' -fo='Aggr_Reverse_mbs10_nmi2000_pi100_si_100.png' -py='train_train' && rm temp.json
python Data_hyper_cleaning.py -mbs=10 -i_d='Trad' -o_d='Reverse' -nmi=2000 -log=True -pi=100 -si=100 -Notes='Trad_Reverse_mbs10_nmi2000_pi100_si_100'
python tools.py -m='plot_pic' -fi='Trad_Reverse_mbs10_nmi2000_pi100_si_100.pickle' -fo='Trad_Reverse_mbs10_nmi2000_pi100_si_100.png' -py='train_train' && rm temp.json
python Data_hyper_cleaning.py -mbs=30 -i_d='Trad' -o_d='Reverse' -nmi=1000 -log=True -Notes='Trad_Reverse_mbs30_nmi1000'
python tools.py -m='plot_pic' -fi='Trad_Reverse_mbs30_nmi1000.pickle' -fo='Trad_Reverse_mbs30_nmi1000.png' -py='train_train' && rm temp.json
python Data_hyper_cleaning.py -mbs=10 -i_d='Trad' -o_d='Reverse' -nmi=1000 -log=True -ld='logs/' -Notes='Trad_Reverse_mbs50_nmi1000' && python tools.py -m='plot_pic' -fi='Trad_Reverse_mbs50_nmi1000.pickle' -fo='Trad_Reverse_mbs50_nmi1000.png' -py='train_train' && rm temp.json
xxxxxxxxxx
参考1:https://blog.csdn.net/time_forgotten/article/details/104792200
参考2:https://cloud.tencent.com/developer/news/1307462
参考3(似乎无效):https://zhuanlan.zhihu.com/p/116733375
参考4(似乎无效):https://blog.csdn.net/weixin_45459911/article/details/108796503
参考5:https://blog.csdn.net/a1456123a/article/details/115498412
参考6:https://github.com/tensorflow/tensorflow/issues/30308#issuecomment-513467525
xxxxxxxxxx
# 使用以下方式运行
python -W ignore file.py
xxxxxxxxxx
# 或者在Python文件前加上
import warnings
warnings.filterwarnings("ignore")
# 或者 针对固定类型的警告 可选:`DeprecationWarning`,`FutureWarning`
warnings.filterwarnings("ignore",category=FutureWarning)
xxxxxxxxxx
# 根据参考5中的方法 执行下面代码可以消除来自tensorflow的警告
sed -i "5itf.get_logger().setLevel('ERROR')" ~/BDA/boml/extension.py
xxxxxxxxxx
# 根据参考6中的方法结局XLA未使用的问题
export TF_XLA_FLAGS=--tf_xla_cpu_global_jit
xxxxxxxxxx
# 整个过程可以使用以下代码解决
# 前两行用于清楚来自numpy的警告
sed -i "1iimport warnings" ~/BDA/test_script/Data_hyper_cleaning.py
sed -i "2iwarnings.filterwarnings('ignore',category=FutureWarning)" ~/BDA/test_script/Data_hyper_cleaning.py
# 后一行用消除来自tensorflow的警告
sed -i "5itf.get_logger().setLevel('ERROR')" ~/BDA/boml/extension.py
F1的使用来自于python包 scikit
维基百科地址:https://en.wikipedia.org/wiki/F-score
Compute the F1 score, also known as balanced F-score or F-measure.
The F1 score can be interpreted as a harmonic mean of the precision and recall, where an F1 score reaches its best value at 1 and worst score at 0. The relative contribution of precision and recall to the F1 score are equal. The formula for the F1 score is:
Where TP is the number of true positives, FN is the number of false negatives, and FP is the number of false positives. F1 is by default calculated as 0.0 when there are no true positives, false negatives, or false positives.
关于真假,正反例问题看下表:
真实\预测 | 正例 | 反例 |
---|---|---|
正例 | TP(真正例) | FN(假反例) |
反例 | FP(假正例) | TN(真反例) |
真假取决于 预测和真实是否相同;正反取决于预测值的正反。
参考页面1:https://matplotlib.org/stable/users/getting_started/
参考页面2:https://blog.csdn.net/LOVEmy134611/article/details/117572333
xxxxxxxxxx
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(-6, 6, 1024)
y = np.sinc(x)
plt.plot(x, y)
plt.savefig('sinc.png', c = 'c')
##
import json
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
with open('file.json','r') as file:
data = json.load(file)
x = np.array(data['iterations'])
y = np.array(data['test_test'])
plt.plot(x, y)
plt.savefig('test_test_to_time.png', c = 'c')
关于保存文件格式为pickle的相关内容:https://blog.csdn.net/m0_38132177/article/details/122196583
xxxxxxxxxx
import pickle
file = open('Notes.pickle','rb')
info = pickle.load(file)
print(info)
一段将pickle文件转换为 json文件的代码(chatGPT生成):
xxxxxxxxxx
import pickle
import json
import numpy as np
# 替换为你的Pickle文件路径
pickle_file_path = 'Notes_L4.pickle'
# 替换为你想要保存的JSON文件路径
json_file_path = 'file.json'
# 定义一个函数,用于处理不可直接序列化的类型
def default(obj):
if isinstance(obj, np.float32):
return float(obj)
raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
# 步骤1: 从Pickle文件读取数据
with open(pickle_file_path, 'rb') as pickle_file:
data = pickle.load(pickle_file)
# 步骤2: 将数据转换为JSON格式
# 使用default函数来处理float32等不可直接序列化的类型
json_data = json.dumps(data, indent=4, default=default)
# 步骤3: 将JSON数据写入到文件
with open(json_file_path, 'w') as json_file:
json_file.write(json_data)
print(f"已将Pickle数据转换为JSON格式,并保存到 '{json_file_path}'")
参考页面1:https://zhuanlan.zhihu.com/p/606945462
参考页面1:https://blog.csdn.net/weixin_43625577/article/details/86699789
xxxxxxxxxx
import pickle
import json
import numpy as np
import argparse
from scipy.io import savemat
import matplotlib.pyplot as plt
import matplotlib as mpl
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--method', type=str, default='pic2json', metavar='STRING',
help='pic2json, pic2mat or plot.')
parser.add_argument('-fi', '--filename_input', type=str, default='Notes.pickle', metavar='STRING',
help='filename of input.')
parser.add_argument('-fo', '--filename_output', type=str, default='file.json', metavar='STRING',
help='filename of output.')
parser.add_argument('-px', '--plot_x', type=str, default='iterations', metavar='STRING',
help='plot for x.')
parser.add_argument('-py', '--plot_y', type=str, default='test_test', metavar='STRING',
help='plot for y, including train_train, train_test, test_test, valid_test, inner_losses, outer_losses, F1_score_micro etc.')
args = parser.parse_args()
if args.method=='pic2json':
# 定义一个函数,用于处理不可直接序列化的类型
def default(obj):
if isinstance(obj, np.float32):
return float(obj)
raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
# 步骤1: 从Pickle文件读取数据
with open(args.filename_input, 'rb') as pickle_file:
data = pickle.load(pickle_file)
# 步骤2: 将数据转换为JSON格式
# 使用default函数来处理float32等不可直接序列化的类型
json_data = json.dumps(data, indent=4, default=default)
# 步骤3: 将JSON数据写入到文件
with open(args.filename_output, 'w') as json_file:
json_file.write(json_data)
print(f"已将Pickle数据转换为JSON格式,并保存到 '{args.filename_output}'")
if args.method=='pic2mat':
with open(args.filename_input, 'rb') as pickle_file:
data = pickle.load(pickle_file)
mat_data = {'data': data}
savemat(args.filename_output, mat_data)
print(f"已将Pickle数据转换为MATLAB的.mat格式,并保存到 '{args.filename_output}'")
if args.method=='plot':
with open(args.filename_input,'r') as file:
data = json.load(file)
x = np.array(data[args.plot_x])
y = np.array(data[args.plot_y])
plt.plot(x, y)
plt.savefig(args.filename_output)
print(f"已将'{args.filename_input}'中数据{args.plot_x}(x)-{args.plot_y}(y)的线图,并保存到 '{args.filename_output}'")
xxxxxxxxxx
# 初步判断为未使用以下代码:
source ~/.bashrc
# 由于并未安装tensorflow-gpu
# 可以使用以下代码安装
wget https://files.pythonhosted.org/packages/32/67/559ca8408431c37ad3a17e859c8c291ea82f092354074baef482b98ffb7b/tensorflow_gpu-1.14.0-cp37-cp37m-manylinux1_x86_64.whl
pip install tensorflow_gpu-1.14.0-cp37-cp37m-manylinux1_x86_64.whl
参考链接1:https://blog.csdn.net/u012856866/article/details/104921811