够快!爆火的ChatGPT等价开源项目来了,网友:我担心跑不起来
- - 机器之心最近一段时间,由 OpenAI 开发的 AI 聊天机器人程序 ChatGPT 横扫各大 AI 社区,大家对它的热情只增不减,不断挖掘其潜力. 有些研究者坐不住了,开始琢磨怎样才能开发个等同于 ChatGPT 的开源软件. 还没有行动的小伙伴这次参考示例来了,下面我们将要介绍的这个项目(PaLM + RLHF)就实现了这样的功能.
感兴趣的小伙伴不妨一试。
不过该项目目前只包含训练架构和代码,没有预先训练好的权重。在使用说明上,文档也显示必须先要训练 PaLM。
$ pip install palm-rlhf-pytorch
import torch
from palm_rlhf_pytorch import PaLM, RewardModel
palm = PaLM(
num_tokens = 20000,
dim = 512,
depth = 12,
causal = False
)
reward_model = RewardModel(
palm,
num_binned_output = 5 # say rating from 1 to 5
).cuda()
# mock data
seq = torch.randint(0, 20000, (1, 1024)).cuda()prompt_mask = torch.zeros(1, 1024).bool().cuda() # which part of the sequence is prompt, which part is response
labels = torch.randint(0, 5, (1,)).cuda()
# train
loss = reward_model(seq, prompt_mask = prompt_mask, labels = labels)loss.backward()
# after much training
reward = reward_model(seq, prompt_mask = prompt_mask)
最后将 transformer 和奖励模型传递给 RLHFTrainer。
import torch
from palm_rlhf_pytorch import PaLM, RewardModel, RLHFTrainer
# load your pretrained palm
palm = PaLM(
num_tokens = 20000,
dim = 512,
depth = 12
).cuda()
palm.load('./path/to/pretrained/palm.pt')
# load your pretrained reward model
reward_model = RewardModel(
palm,
num_binned_output = 5
).cuda()
reward_model.load('./path/to/pretrained/reward_model.pt')
# ready your list of prompts for reinforcement learning
prompts = torch.randint(0, 256, (50000, 512)).cuda() # 50k prompts
# pass it all to the trainer and train
trainer = RLHFTrainer(
palm = palm,
reward_model = reward_model,
prompt_token_ids = prompts
)
trainer.train(num_episodes = 50000)
# then, if it succeeded...
# generate say 10 samples and use the reward model to return the best one
answer = trainer.generate(2048, prompt = prompts[0], num_samples = 10) # (<= 2048,)
更多细节内容请参阅原项目。