通过网上对开源代码与文章的阅读,总有一些让我眼前一亮的用法或技巧。平时总是想着以后在找,后来就再也没有找到了。从今以后对这些东西要第一时间记录,以后自己融合的时候方便一些。
1. pytorch 在模型训练中动态调整学习率:
手动修改学习率太浪费时间,通过对问题的搜索,找到动态调整学习率的方法
参考文章[pytorch]动态调整学习率 - wildkid1024 - 博客园 (cnblogs.com)
其中选用的方式为:根据loss更新学习率。
首先我使用的是根据loss更新,其实按道理应该是根据valid反馈的数据来更新,但是某种情况我这里测试做的太慢了,就拿train每个epoch的loss来实验一下,按道理会导致过拟合。。。。
通过optimizer.param_groups:获取LR打印看看有没有变化 -_- || 初学小菜鸟,欢迎大家提出建议。
optimizer = optim.SGD(model.parameters(), lr=learning_rate,momentum=0.9)
#min 值在patience次数内没有更小 就更新学习率 。 学习率*=factor。 verbose这个触发之后会有相应打印
#threshold 关注超过阈值的变化,cooldown 触发后等几轮才能在触发,min-lr 最小界限,差异小于eps 忽略更新
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min',factor=0.5, verbose=1, patience=3, min_lr=0.0001,eps=1e-4 )
.....
.....
.....
#我写的是在每个epoch完成后获取loss 传入scheduler
print("epoch :",epoch_id,' loss: ',epoch_loss)
scheduler.step(epoch_loss)
'''
Starting epoch: 13
epoch : 13 loss: 32.1612434387207
Starting epoch: 14
epoch : 14 loss: 38.54547309875488
Epoch 14: reducing learning rate of group 0 to 5.0000e-02.
'''
学习率动态调整成功。
2. 抽取文本数据中被标注的数据
已经写过n多次这种代码了,每次用又不知道在哪个文件夹里,头疼。。。记录一下
实体在文本中已被<e1></e1>标注。
import re
#文本文件存储路径
file = open("./triple.txt", encoding='utf-8')
while 1:
line = file.readline()
if not line:
break
e1 = re.findall('<e1>(.*?)</e1>', line)
print(e1)
3 查询出单张数据表中(以某两列值为组合)重复的行
也不难,就是浪费了一会时间,但是以后肯定还会有类似操作,记录一下 ||—_—
第一个是只展示那些有重复的其中一行:
SELECT * from (select num,entity_one,entity_two from entity_relation group by entity_one,entity_two having count(*)>1) b order BY b.entity_one,b.entity_two
第二个是展示那些有重复的所有的重复行(并根据条件排序):
SELECT * from entity_relation WHERE (entity_one,entity_two) in (select entity_one,entity_two from entity_relation group by entity_one,entity_two having count(*)>1) order BY entity_one,entity_two
包括excel中 的查询 某个值在不在某一列中的函数公式:
=IF(COUNTIF(B:B,A1)=0,A1,"")
这里是如果A1不在B列中,输出A1的值。
有一堆的数据想把它拼成 sql的in 条件
file= open("./id.txt", encoding='utf-8')
condition='('
i=1
while 1:
line = file.readline()
if not line:
break
line_s = line.split()
if i ==1:
condition=condition+line_s[0]
i=0
else:
condition = condition + ","+ line_s[0]
print(condition+")")
'''(550,18807,18841,551,553,545,17472,17463,581,18497,510,18496,18511,18499,18514,18452,18500,18508,18513,552,549,524,348,548,18884,554,540,17577,17999,508,582,17568,18045,18061,17583,18048,18064,18002,18049,18057,18063,523,18676,18691,18694,18679,18688,18693,18632,18680,558,642,17641,17640,543,579,17642,507,17768,544,17784,578,17957,17971,17959,17974,17960,17968,17973,17913,535,18870,534,18860,18908,18913,560,557,561,559,19211,18732,18786,527,19317,19313,538,592,593,19314,18782,18842,529,19139,19274,18966,18974,18803,530,18719,18723,18804,18801,18802,18891,17678,17661,18267,511,566,564,565,65,18329,506,18316,18331,567,18319,18334,18272,18320,18328,18333,546,17608,580,18226,509,18225,18241,18228,18244,17604,18182,18229,18237,18243,532,18812,18997,533,17428,17816,17389,502,17887,17439,17438,17868,17882,17871,17885,17823,17872,17880,17884,17436,19254,19126,525,19287,537,591,19294,19293,528,19290,19291,19235,19284,19252,536,18984,19006,18983,556,19240,555,570,17546,17547,572,573,18586,505,539,18585,18601,571,18588,18604,18542,18589,18597,18603,541,577,576,18135,18151,18138,18154,18092,18139,18147,18153,17732,542,18359,503,17705,574,575,18406,18421,563,18409,18424,17751,18362,18410,18418,18423,531,18925,526,19288,19289,18977,19001,19000,19002,18777,18772,18778,18776,18819,18769,18773,18775,18780,18840,18836,18810,18831,18834,18835,547,504,569,568,562,19257,19208,18994)'''