Measure Zero


  • About

  • Quotes

  • Notes

  • Sitemap

  • Search

Huggingface Transformers Trainer as a general PyTorch trainer

2023-01-04 | ~ | Machine Learning

受 这篇 启发, 自定义 Huggingface Transformers Trainer 做通用训练器.

模型定义照常.

import torch.nn as nn

class Model(nn.Module):
    def forward(self, inputs):
        ...
        return logits

自定义损失函数. 损失函数要么写在模型的 forward 里 (Huggingface 的写法), 要么继承 Trainer 类, 覆写 compute_loss.

import transformers

class MyTrainer(transformers.Trainer):
    def compute_loss(self, model, inputs, return_outputs=False):
        labels = inputs.pop('labels')
        logits = model(**inputs)
        # loss_fct = nn.CrossEntropyLoss()
        loss_fct = nn.BCEWithLogitsLoss()
        loss = loss_fct(logits, labels)
        # TODO: tested only with `return_outputs=False`
        return (loss, {'logits': logits}) if return_outputs else loss
Read more »

Notes on Distributed Data

2023-01-01 | ~ | Tech
  • Kleppmann, M. (2017). Designing data-intensive applications: The big ideas behind reliable, scalable, and maintainable systems. “ O’Reilly Media, Inc.”.

There are various reasons why you might want to distribute a database across multiple machines:

  • Scalability
  • Fault tolerance/high availability
  • Latency

Replication

Replication means keeping a copy of the same data on multiple machines that are connected via a network. All of the difficulty in replication lies in handling changes to replicated data.

  • 使数据和用户地理邻近 (减少延迟)
  • 系统部分故障也能继续工作 (提高可用性)
  • 横向扩展处理读取请求 (提高读取吞吐量)

In this chapter we will assume that your dataset is so small that each machine can hold a copy of the entire dataset.

Read more »

GPT-1 到 ChatGPT 简介

2022-12-24 | ~ | Machine Learning

总体时间线参考 这里.

GPT-1~3

GPT-1

Our system works in two stages; first we train a transformer model on a very large amount of data in an unsupervised manner — using language modeling as a training signal — then we fine-tune this model on much smaller supervised datasets to help it solve specific tasks.

We trained a 12-layer decoder-only transformer with masked self-attention heads (768 dimensional states and 12 attention heads).

GPT 全称 generative pre-training, 就是预训练 + 微调. 时间顺序从前到后依次是, GPT-1, BERT, GPT-2.

来自 BERT 论文

Read more »

[Walkthrough] Install and set up Redis

2022-12-19 | ~ 2023-01-04 | Tech

Check system info.

cat /etc/os-release

Follow a walkthrough listed here. The below works on CentOS 7.

Read more »

Atypical: Season 4 Episode 1 Transcript

2022-12-18 | ~ | Language

一时兴起的听写, 不保证对. 听了个开头 (然后看完了), Sam 的台词最容易听.

Read more »

正则灾难性回溯与防御方法

2022-12-10 | ~ | Tech

灾难性回溯

考虑用正则表达式 (x+x+)+y 匹配字符串 xxxxxxxxxxy (10 个 x). 显然正则可以改写为 xx+y 或者 x{2,}y, 但先不管这点, 该例旨在展示匹配机制.

  • 第一个 x+ 匹配 10 个 x, 第二个 x+ 没匹配上; 于是第一个 x+ 回溯 到匹配 9 个 x, 第二个 x+ 匹配一个 x. 至此 group 1 x+x+ 完成了一次匹配.
  • Group 1 尝试重复, 但失败了, 于是 (x+x+)+ 完成了一次匹配. 接下来匹配 y, 完成匹配.

删除字符串结尾的 y 使得正则匹配失败会导致 灾难性回溯 (catastrophic backtracking).

Read more »

RabbitMQ 消息确认机制

2022-11-23 | ~ | Tech

生产者先把消息发到 exchange (很多个), exchange 决定把消息分发到哪些 queue (很多个), 再从 queue 发到消费者.

确认机制分为两部分, exchange 确认收到 publisher 的消息, consumer 确认收到 queue 的消息. 来自官方文档 Consumer Acknowledgements and Publisher Confirms — RabbitMQ

Read more »

《看不见的女性》笔记

2022-11-13 | ~ | Reading
  • 卡罗琳·克里亚多·佩雷斯. (2022, Aug). 看不见的女性. 新星出版社.

原书还有副标题, Invisible Women: Data Bias in a World Designed for Men. 书中陈列了大量数据和例子展示这一点.

论及性别数据缺口, 最重要的观点之一是它通常不带恶意, 甚至并非有意. 事实恰恰相反: 性别数据缺口完全是一种存在了数千年的思考方式的产物, 因此也可说源于不思考. 双重的不思考: 男人不言而喻, 女人不被提及. 因为当我们说到人类, 总的来说, 我们指的是男人.

三个主题: 女性的身体, 女性无偿看护的负担, 和男性对女性的暴力.

将人类 (human) 默认为男性, “不加说明就是男性”. 比如众所周知, 很多职业需要在前面额外加个 “女” 以示区分, 以及很多语言单词里也有默认性别. 有趣的是, Unicode 将 “跑步者” emoji 加入编码后, 大多数平台使用的图标是男性跑步者, 直到 2016 年 Unicode 区分了两性跑步者. 虽然这些平台想设计出 “中性” 的跑者形象, 但多数人还是会把它看作男性. (国足指男国家队.)

影视作品中女性出现比例也显著小于男性: 儿童电视角色中女性非人类角色, 电影主演, 出场时间 (只有当主角是女性时, 两性出场频率才大体一样, 而不是女性更多) 等. 不平衡还表现在雕像, 钞票, 新闻媒体报道上等.

这种完全由男性主导的文化带来的后果是, 男性的经验和视角被视为普遍, 而女性的被视为小众.

Read more »

轻量级篇章事件抽取: PTPCG

2022-11-09 | ~ 2023-01-14 | Machine Learning

起点是 ChFinAnn 数据集, 和它的 SOTA 榜单. 其中

  • Zhu, T., Qu, X., Chen, W., Wang, Z., Huai, B., Yuan, N. J., & Zhang, M. (2021). Efficient Document-level Event Extraction via Pseudo-Trigger-aware Pruned Complete Graph. arXiv preprint arXiv:2112.06013. [Code]

看起来很不错, 模型比其他人轻量很多, 训练资源要求少 (对比 Doc2EDAG 等涉及到动态建立图神经网络的巨大计算资源需求), 推理速度也快, 效果和其他人相当.

Read more »

SQLAlchemy 简要

2022-10-29 | ~ | Tech
import urllib.parse

import sqlalchemy

password = ...
# escaping special characters such as @ signs
password = urllib.parse.quote_plus(password)
url = f'dialect+driver://username:{password}@host:port/database'
engine = sqlalchemy.create_engine(url)  # Engine object

with engine.connect() as conn:
    results = conn.execute('SELECT * FROM students')
    for row in results:
        print(row)
Read more »
1 … 3 4 5 … 18
Shiina

Shiina

知乎 豆瓣 bangumi Instagram Weibo
Creative Commons
RSS
© 2019 - 2025   Shiina   CC BY-NC-ND 4.0
RSS  
Powered by Jekyll
 
Theme NexT.Mist