type
status
date
slug
summary
tags
category
icon
password

01 正确认识 ChatGPT

正確認識ChatGPT

  • ChatGPT真正做的事:文字接龍
    • 非常复杂——1700亿个以上的参数
    • notion image
    • 训练与测试的区别

Chat-GPT背后的关键技术:

  • 預训练(自督导式学习、基石模型)与督导式学习
notion image
notion image
  • 几个待解决的问题
    • 如何精准对 ChatGPT 提出需求? “催眠“(Prompting)
    • 如何更正错误?Neural Editing
    • 如何侦测 AI 生成的物件?
    • 不小心揭露秘密? Machine Unlearning

GPT 社会化的过程

论文:Training language models to follow instructionswith human feedback

作业

Tutorial

CoLAB

  • Similar to JupyterNootbook
    • +code
      • python
      • Shell script
    • +text
      • Markdown

Magic Commands

  • 惊叹号!
    • Starts a shell
    • Does the operations
    • Kills that shell
  • 百分号%
    • “%” affects the process associated with the notebok

Check GPU Type

notion image

Useful Linux Commands(in Colab)

  • ls: List all files in the current directory
  • Ls -l:List all files in the current directory with more details
  • pwd : Output the working directory
  • mkdir <name>: Creat a directory <name>
  • cd <name>: change directory

Pytorch Tutorial 1

An machine learning framework in Python.
  • Two main features:
    • N-dimensional Tensor computation (like NumPy) on GPUs
    • Automatic differentiation for training deep neural networks
    • Prerequisites

    • Basic python3
    • Deep Learning Basics
    • NumPy
    • Training & Testing Neural Networks in Pytorch

    • 训练一个模型之前需要选好:
      • 模型的框架
      • Loss Function
      • Optimization Algorithm
    • 不停地训练与验证
      • notion image
    • 准备好数据集,喂给模型
    • notion image

      Step1️⃣ Dataset & DataLoader

    • Dataloader会去搓这个dataset,每搓一次就会吐出来一个sample和它对应的期望值,将一个sample和期望值集合成一个batch。batchsize指的是一组搓多少次
      • notion image
        notion image
      notion image

      Tips: Tensors

    • 所谓的Tensor,就是n维矩阵。
    • notion image
    • Check size of each dimension by .shape
      • Note: dim in PyTorch == axis in NumPy

      🎺 Create a Tensor

      notion image

      🎺 Common Operations

    • Addition
      • Subtraction
        • Power
          • Summation
            • Mean
              • Transpose 交换两维
                • Squeze 去掉长度为1的那一维 & Unsqueeze
                  • notion image
                    notion image
                • Cat 将几个矩阵连起来
                  • notion image

                  🎺 Data Type

                • Using different data type for model and data will cause errors!
                  • notion image

                  🎺 Pytorch v.s. Numpy

                • Similar Attributes
                  • notion image
                • Same Functions
                  • notion image

                  🎺 Device

                • Tensors use CPU by default
                  • Use .to() to move tensors to appropriate devices
                    • CPU
                      • GPU
                    • Check if your computer has NVIDIA GPU
                      • Specify Multiple GPUs: cuda:0 cuda:1 cuda2....
                      • 🎺 Gradient Calculation 梯度计算

                        notion image

                    Step2️⃣ Define Neural Network

                    前面講解了如何把data給load進來,並存為tensor的形式;學習了如何操作這些tensor;接下來就開始確認modle的架構了。
                    notion image

                    🎺 Linear Layer:Fully-Connected Layer

                    notion image
                    • 乘上一个矩阵 weight ,加上一个矩阵 bias
                    notion image
                    notion image
                    在这个例子中,我们会给这个32*32的矩阵左乘一个64*32的Weight,再加上一个64*64的bias:

                    🎺 Not-Linear Activation Function

                    • Sigmoid Activation
                      • notion image
                    • ReLU Activation
                      • notion image
                    🎺 Build ur own Neural Network
                    • You need to overwrite class Model:

                    Step3️⃣ Choose Loss Function

                    notion image
                    • What is Loss Function?
                      • Compute the differece between output_tensor and the Expect_tensor
                      • Some Loss Functions
                        • Mean Squared ERROR (for regression tasks)
                          • Cross Entropy (for classification tasks)

                          Step4️⃣ Optimization Algorithm

                          • 选择一个适当的最佳化算法。在pyTorch中有torch.optim库来让你挑选一个。
                            • Gradient-based optimization algorithms that adjust network parameters to reduce error.
                          • SGD随机梯度下降算法(Stochastic Gradient Desent)
                            • 决定好一个适当的Optimization算法后,需要对每一batch的数据做:
                              • Call optimizer.zero_grad() to reset gradients of model parameters
                              • Call loss.backward() to backpropagate gradients of prediction loss
                              • Call optimizer.step() to adjust model parameter

                          Step5️⃣ Entire Procedure

                          notion image

                          🎺 Neural Network Training Setup

                          🎺Neural Network Training Loop

                          🎺Neural Network Validation Loop

                          🎺Neural Network Testing Loop

                          ⚠️Notice

                          • model.eval()
                            • Changes behavior of some model layers, 比如 dropout 和 batch normalization
                          • with torch.no_grad()
                            • 在 validation 和 testing 的時候,我們並不希望模型從這些數據中學習,此時此刻,關掉gradient就一勞永逸了。

                          Save/Load models

                          訓練好模型後,就需要把模型保存起來,以供後續load調用
                          • Save
                            • Load

                              More about PyTorch

                              • torchAudio
                              • torchText
                              • torchVision
                              • skorch

                              Pytorch Tutorial 2

                              Documentation

                              学会它的重要途径是他官网的手册
                              https://pytorch.org/docs/stable/
                              • torch.nn -> Neural Network
                              • torch.optim -> Optimization Algorithms
                              • torch.utils.data -> Dataset, DataLoader
                              • For example: torch.max
                              notion image
                              notion image
                              notion image
                              notion image

                              Common ERROR

                              • Tensors on different device
                                • You should promise they are on both GPU or CPU
                              • Tensor运算需要遵循线性代数的规则!
                              • CUDA Out of Memory
                                • Reduce Batch Size!
                              • Mismatched Tensor Type
                                • notion image

                              02 机器学习基本概念介绍

                              机器学习的基本概念

                              • 机器学习 约等于 机器自动找一个函数
                                • 根据函数的输出来做分类:
                                  • Regression回归:函数的输出是一个数值
                                  • Classification分类:函数的输出是一个类别(选择题)
                                  • notion image
                                    notion image
                                  • Structured Learning(又叫做生成式学习Generative Learning )
                                    • 机器生成有结构的物件(影像、文字)
                                    • ChatGPT是哪一类呢?
                                      本质上是“文字接龙”——做选择题:使用分类
                                      从使用者的感受来说,是生成式学习;综合起来,也就是把生成式学习拆解成多个分类问题

                              找出函数的三步骤

                              notion image

                              设定范围

                              • 定出候选函数的集合。所谓“候选函数的集合,其实就是”Model“
                              • 深度学习中的类神经网路的结构(如 CNN、RNN、Transformer等等)指的就是不同的候选函数的集合
                              notion image

                              设定标准

                              • 定出“评价函数好坏的标准”。所谓“评价函数好坏的标准”,就是“Loss”,Loss越小函数越好。
                              • 将函数的输出与标准参考值相比较
                                • notion image
                              notion image

                              达成目标

                              • 找出最好的函数——最佳化Optimization
                                • 选出 Loss最小的那一个函数
                                • notion image
                                • 一般而言,集合H中会有无穷多个待选函数。对每一个就求Loss并比较的工作量将是不可接受的,因此这里会有一些高级的最佳化算法
                                  • notion image

                              达成目标

                              • 很多时候,最佳化算法其实并不能找到最优解。我们只能期待它越快,选出的Loss越低越好
                              • 超参数
                                • 需要先设定Learning Rate、Batch Size、How to init等参数,提供给Optimization Algorithm。
                                • 为了和神经网路内部的参数做出区分,这些参数叫做超参数 Hyperparameter。在训练一个模型的时候就需要人工一点一点调试它。
                              notion image

                              设定标准

                              • 在这一个环节,我们分成两个部分:Training和Testing
                                • 在Training环节表现比较好的函数,不一定在Testing上也表现良好,这时候可以考虑引入Regularization
                                • notion image

                              设定范围

                              • 很有技巧性。在第一步就需要预先筛掉那些不如意的函数集合
                              • 范围越大、所需要的训练资料也要越多,效果才会好
                              notion image
                              notion image
                              notion image

                              生成式学习的两种策略

                              各个击破还是一次到位?
                              • 生成有结构的复杂物件
                                • notion image
                                • 还有生成影片(https://arxiv.org/abs/2210.02303)、生成语言(https://arxiv.org/abs/2301.13662)、生成声音(https://arxiv.org/abs/2301.12503)

                              AutoRegressive,AR Model —— 逐个击破

                              • 一个一个小单元生成,例如ChatGPT一个字一个字的接龙、图像一个像素一个像素的生成
                                • notion image

                              NotAotoRegressive, NAR Model —— 一次到位

                              • 预先定好要生成多少内容,再一次性生成出来
                                • notion image
                              • 各个击破,更类似于串行;一次到位更像是并行计算。因此,后者常常更快,常用于生成影像
                              notion image
                              • 在生成品质上,一般前者更佳,,常用来生成语言
                                • notion image

                                  二者结合

                                  notion image
                                  notion image

                              03 机器如何生成文句

                              对于大语言模型的两种不同期待

                              FineTune v.s. Prompt
                              notion image

                              期待一:成为专才 · 解决某一特定任务

                              notion image

                              期待二:成为通才

                              notion image
                              • 现在的GPT即是一个“通才”,我们将一个通用的LLM作为初始化模型,通过一些小型的改造,这样就不用调太多参数了

                              Finetune v.s. Prompt

                              1. Finetuning(微调):Finetuning 是指在一个预训练的语言模型基础上,在特定的任务或领域上进行额外的训练。预训练的语言模型通常是在大规模的文本数据上进行训练,以学习语言的统计规律和语义表示。然而,当需要解决特定任务时,如文本分类、命名实体识别等,可以使用 finetuning 的方法进一步调整模型的参数,以使其适应特定的任务需求。Finetuning 的过程涉及将特定任务的训练数据与预训练模型的参数一起输入到模型中,并通过反向传播和梯度下降等方法来更新模型参数,使其适应特定任务的数据。Finetuning 可以提高模型在特定任务上的性能和泛化能力。
                              1. Prompting(提示):Prompting 是指在使用语言模型进行生成或问答任务时,为模型提供一个提示或问题来引导其生成结果。通过给定一个明确的提示文本,模型可以更好地理解用户的意图,并生成与提示相关的响应。Prompting 在对话系统、文本生成、机器翻译等任务中广泛应用。合理设计的提示文本可以提高模型生成结果的质量和相关性。例如,在问答任务中,给定一个问题作为提示,模型可以生成与问题相关的答案。在生成文本任务中,给定一个开头或上下文作为提示,模型可以继续生成与之相关的文本内容。
                              Prompting 的设计可以根据具体任务和需求进行灵活调整,包括选择合适的提示文本、控制生成的风格和内容等。通过合理设计的提示,可以提高语言模型的交互性和生成能力。

                              对預训练模型做改造

                              Head 外挂

                              notion image

                              Finetune 微调

                              • 微调大语言模型本身的参数
                              notion image

                              Adapter

                              • 不动LLM的参数,只调整Adapter的参数
                              notion image

                              如何训练一个“通才”

                              • 机器要学会阅读“题目叙述”或者“题目范例”
                                • notion image

                              Prompting——In-context Learning

                              notion image
                              notion image
                              • 一开始,机器拿到输入,完全不知道要输出什么;这时候,我们给一些例子,包含输入与期望的输出;机器学习过这些内容后,便能给出符合规定的回答
                                • Rethinking the Role of Demonstrations: What makes In-Context Learning Work?
                                  Ref: arxiv.org
                              notion image
                              • 机器本来就会做情感分析,只是需要通过一些例子来告诉机器:你需要做情感分析
                              notion image
                              • 你会发现,对于In-context Learning来说,大幅提升训练用例效果不佳

                              Prompting——Instruction Learning

                              直接用預训练的一个文字接龙的模型来给他一些instruction,它给出的输出很不尽人意的。这时候我们需要进行instruct-tuning的微调,它才能看懂人类给的instruction
                              • instruction - tuning
                                • notion image
                                  notion image
                                • 给出“翻译”和“摘要”两个instruction,给出对应的正确回答用作训练;给出完全不同的instruction:编修,看他能否给出人类期望的输出。训练和测试用的instruction是完全不同的,不然就成in-context learning了

                              Chain of Thought (CoT) Prompting

                              • 在原来的prompt中,添加推理的过程
                              notion image
                              notion image
                              • Self-consistency: 让它一个人做多次,自己和自己对答案
                              notion image
                              notion image

                              用机器自动找Prompt

                              让机器自己催眠自己
                              notion image
                              • Soft Prompt——直接使用向量来代替原来的instruction进行训练,可以看做是放了一个Adapter在input部分
                              notion image
                              • Using Reinforcement Learning
                              https://arxiv.org/abs/2206.03931
                              notion image
                              • 直接使用LM来找Prompt
                              • 把训练用例直接丢给GPT,让他去想对应的Instruction
                              https://arxiv.org/abs/2211.01910
                              notion image

                              04 生成式学习的各种议题:多样性、评量方式、其他挑战

                              大模型+大资料 = 神奇结果

                              notion image
                              Scaling Laws for Neural Language Models (https://arxiv.org/abs/2001.08361)

                              Emergent Ability 大模型的顿悟时刻

                              notion image
                              • 尝试让不同大小的模型来解决八个不同的问题
                                • 虚线代表随机乱猜的结果
                                • 差不多在模型大小增加到10B的时候,Accuracy有显著增加
                              https://arxiv.org/pdf/2206.07682.pdf
                               
                              notion image
                              • 一些Prompt的技巧,比如chain of thought,也只是在大模型上有显著的效果
                              https://arxiv.org/pdf/2206.07682.pdf
                              notion image
                              • Calibration——语言模型知不知道自己不知道某个东西?
                              notion image
                              notion image

                              模型越大,结果越差的情况

                              • U-Shaped?
                              • 会不会是因为大模型还不够大呢?
                              • 可能是因为模型一知半解
                              notion image
                              notion image
                              • 这些具有“陷阱”的问题中,大模型反而容易因为一知半解掉进坑里——模型还不够大

                              还能不能更大?

                              notion image

                              大资料的重要性

                              notion image
                              • 对于一个大模型,它不仅仅需要学习语言上的语法知识、还需要学习一些知识来让它学会世界的物理常识
                              https://arxiv.org/abs/2011.04946
                              横轴为训练资料的Token数量

                              资料本身的前处理 DataPreparetion

                              notion image

                              资源有限

                              • 是优先增大模型还是优先增大训练资料呢?
                              学而不思&思而不学?
                              notion image
                              • 如今的模型已经足够大,更需要的是更多的训练资料
                              • 一种少量资料快速提高正确率的方法:Instruction-tuning
                              notion image
                              notion image

                              KNN LM

                              notion image
                              传统的LM
                              • 输入一个Sequence 台湾大,输出一个向量h
                              • 这个向量再过下LinearTransform和Softmax即可得到下一个字的概率分布
                              KNN LM
                              • 对于KNN LM,我们依旧是同上输出一个向量h,但是我们通过比较Test时输出的h与Train时输出的h的相似度,将相似度最高的h.train作为h.test
                              notion image
                              • 优点:能将大部分用于Test的数据给Train用,这样一来能增加很多训练数据
                              • 缺点:使用时需要现场计算大量的Distance,会很浪费书简

                              05 图像生成模型

                              Denosing Diffusion Probabilistic Model

                              Reverse Process

                              Firstly, sample a pure noise. Then denoise step by step.
                              notion image
                              The times of "denoise" has been predefined.
                              As to the details, the "denoise" part has two inputs: the image and a number which denotes the sequence of process. The number tells the "Denoise" how "serious" the noise is.
                              notion image
                              Inside of the Denoise, there is a "Noise Predicter" which predicts the noise of the input image. Then, it cuts the predicted noise off from the input image to denoise.
                              notion image

                              How to train it? -----Forward Process

                              notion image
                              Add noise to the image artially. Thus we can have a set of inputs and corresponding ground truth.

                              Methodology

                              notion image

                              Text to image

                              Simply add the text to the inputs of the denoise.
                              notion image
                              notion image

                              Stable Diffusion

                              Framework

                              Three utilities: Text Encoder, Generation Model, Decoder.
                              notion image
                              notion image

                              Text Encoder

                              Text Encoder is of vital importance.
                              notion image

                              DALL-E series

                              notion image

                              Imagen

                              notion image

                              Fréchet Inception Distance (FID)

                              notion image
                              Calculate the Fréchet distance between the two Guassians.
                              【NIPS2017】Attention Is All You Need【python】"__main__"
                              Loading...
                              目录
                              0%
                              公告
                              🎉Crystal PuNK’s blog🎉
                              --- 正在施工 ---
                              👏欢迎comment👏
                               
                               
                               
                              目录
                              0%