什么是大模型
大模型 Large Language Model 缩写 LLM
通常是指那些具有大量参数和复杂结构的机器学习模型。这些模型的特点包含高参数数量、高计算复杂性和高数据消耗。它通过对大量数据的学习,能够实现对复杂任务的高效处理
大家熟悉的ChatGTP就是一个基于大模型(GTP-3.5, GTP-4)的对话产品,国内外常用的大模型有以下:
大模型能做什么
我们多多少少应该都和上述一些大模型对话产品聊过天,但是,千万别以为大模型只是聊天机器人,他的能量,远不止于此,它还可以写文案,写小说,写PPT,画图(midjunery,stable diffusion),做视频,编代码(通义灵码),等等
简单来说:
- 大模型就是一个函数,给输入,生成输出
- 任何可以用语言描述的问题,都可以输入给大模型,就能生成问题的结果
它可能解决一切问题,所以是通用人工智能,也就是AGI(Artificial General Intelligence)
AGI时代,AI将无处不在,主流预测AGI时代将在3-5年内来临
大模型现况
当前已经有很多企业在引入大模型来降本增效,期望利用各种AI自动化代替人工,裁员降本或提高效率。自媒体上也有各种神化AI或各种岗位会被AI取代的传言来制造焦虑,实际上目前通用人工智能还不能达到老板们所期待的高度,是无法完成一个完整的工作链路的,更实际的是提高某个生产环节的效率
大模型是怎么生成结果的
通俗原理
根据上下文,猜下一个词的概率
略深一点的通俗原理
大模型的两个核心过程:
训练
- 大模型阅读了人类说过的所有的话。这就是机器学习
- 训练过程会把不同 token 同时出现的概率存入神经网络文件。保存的数据就是参数,也叫权重
推理
- 我们给推理程序若干 token,程序会加载大模型权重,算出概率最高的下一个 token 是什么
- 用生成的 token,再加上上文,就能继续生成下一个 token。以此类推,生成更多文字
再深一层的原理
这套生成机制的内核叫「Transformer 架构」。Transformer 仍是主流,但其实已经不是最先进的了。
附上transformer的代码,仅有短短400多行!你敢信
import os
import time
import math
import json
import joblib
import random
import argparse
import numpy as np
import tensorflow as tf
from tqdm import tqdm
from functools import partial
from sklearn.utils import shuffle
from sklearn.metrics import accuracy_score
from opt import adam, warmup_cosine, warmup_linear, warmup_constant
from datasets import rocstories
from analysis import rocstories as rocstories_analysis
from text_utils import TextEncoder
from utils import encode_dataset, flatten, iter_data, find_trainable_variables, convert_gradient_to_tensor, shape_list, ResultLogger, assign_to_gpu, average_grads, make_path
def gelu(x):
return 0.5*x*(1+tf.tanh(math.sqrt(2/math.pi)*(x+0.044715*tf.pow(x, 3))))
def swish(x):
return x*tf.nn.sigmoid(x)
opt_fns = {
'adam':adam,
}
act_fns = {
'relu':tf.nn.relu,
'swish':swish,
'gelu':gelu
}
lr_schedules = {
'warmup_cosine':warmup_cosine,
'warmup_linear':warmup_linear,
'warmup_constant':warmup_constant,
}
def _norm(x, g=None, b=None, e=1e-5, axis=[1]):
u = tf.reduce_mean(x, axis=axis, keep_dims=True)
s = tf.reduce_mean(tf.square(x-u), axis=axis, keep_dims=True)
x = (x - u) * tf.rsqrt(s + e)
if g is not None and b is not None:
x = x*g + b
return x
def norm(x, scope, axis=[-1]):
with tf.variable_scope(scope):
n_state = shape_list(x)[-1]
g = tf.get_variable("g", [n_state], initializer=tf.constant_initializer(1))
b = tf.get_variable("b", [n_state], initializer=tf.constant_initializer(0))
return _norm(x, g, b, axis=axis)
def dropout(x, pdrop, train):
if train and pdrop > 0:
x = tf.nn.dropout(x, 1-pdrop)
return x
def mask_attn_weights(w):
n = shape_list(w)[-1]
b = tf.matrix_band_part(tf.ones([n, n]), -1, 0)
b = tf.reshape(b, [1, 1, n, n])
w = w*b + -1e9*(1-b)
return w
def _attn(q, k, v, train=False, scale=False):
w = tf.matmul(q, k)
if scale:
n_state = shape_list(v)[-1]
w = w*tf.rsqrt(tf.cast(n_state, tf.float32))
w = mask_attn_weights(w)
w = tf.nn.softmax(w)
w = dropout(w, attn_pdrop, train)
a = tf.matmul(w, v)
return a
def split_states(x, n):
x_shape = shape_list(x)
m = x_shape[-1]
new_x_shape = x_shape[:-1]+[n, m//n]
return tf.reshape(x, new_x_shape)
def merge_states(x):
x_shape = shape_list(x)
new_x_shape = x_shape[:-2]+[np.prod(x_shape[-2:])]
return tf.reshape(x, new_x_shape)
def split_heads(x, n, k=False):
if k:
return tf.transpose(split_states(x, n), [0, 2, 3, 1])
else:
return tf.transpose(split_states(x, n), [0, 2, 1, 3])
def merge_heads(x):
return merge_states(tf.transpose(x, [0, 2, 1, 3]))
def conv1d(x, scope, nf, rf, w_init=tf.random_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(0), pad='VALID', train=False):
with tf.variable_scope(scope):
nx = shape_list(x)[-1]
w = tf.get_variable("w", [rf, nx, nf], initializer=w_init)
b = tf.get_variable("b", [nf], initializer=b_init)
if rf == 1: #faster 1x1 conv
c = tf.reshape(tf.matmul(tf.reshape(x, [-1, nx]), tf.reshape(w, [-1, nf]))+b, shape_list(x)[:-1