小周带你读论文-2之“草履虫都能看懂的Transformer老活儿新整“Attention is all you need(1)

本文是对经典论文《Attention is all you need》的个人解读,旨在以简单易懂的方式介绍Transformer架构。文章指出Transformer解决了传统Seq2Seq模型的时序计算问题,通过对比GPT和BERT的训练方式,探讨了为何Decoder-only架构逐渐成为主流,并讨论了Decoder-only的表达能力和优势。此外,还涉及了低秩问题和零样本测试,证明Decoder-only模型的优越性。

这论文其实也不用多说了,我相信百分之70以上我的读者读过

      但是还是老规矩 1,2,3 上链接

      1706.03762.pdf (arxiv.org)

      《Attention is all you need》我如果干讲这个可能有点枯燥,毕竟好多人看过,但是这个论文又是玩LLM不可能跨过的一篇文章,所以我站在我的角度夹带点私货来对这个论文做一些个人解读,保证你们看到一篇不一样的,更丰富内容的“Attention is all you need”

       我的目的就是一定要让大家明白,所以会讲的很细,希望出一个能让草履虫都能看懂的Transformer论文解析

       我就只沾一部分原文就是background:

Background The goal of reducing sequential computation also forms the foundation of the Extended Neural GPU [16], ByteNet [18] and ConvS2S [9], all of which use convolutional neural networks as basic building block, computing hidden representations in parallel for all input and output positions. In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions, linearly for ConvS2S and logarithmically for ByteNet. This makes it more difficult to learn dependencies between distant positions [12]. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention as described in section 3.2. Self-attention, sometimes called intra-attention is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence. Self-attention has been used successfully in a variety of tasks including reading comprehension, abstractive summarization, textual entailment and learning task-independent sentence representations [4, 27, 28,

<think>我们被要求详细解释PHP封装协议,从基础到精通,使用简单易懂的语言。根据引用[1]和[3],我们知道PHP在Web开发中用于处理动态内容,并且经常需要与文件系统或网络资源交互。封装协议(Wrapper)是PHP中一个重要的特性,它允许我们像操作普通文件一样操作各种资源(如HTTP、FTP、压缩文件等)。计划回答结构:1.什么是封装协议?(基础)2.内置的封装协议有哪些?(列举并简要说明)3.如何使用封装协议?(包括示例)4.自定义封装协议(进阶)5.封装协议的应用场景注意:引用中并没有直接提到封装协议,但提到了PHP处理数据、存储和取,封装协议正是处理这些资源访问的一种机制。我们将按照上述结构进行解释,并尽量使用简单易懂的语言和示例。</think>### PHP封装协议详解:从入门到精通 #### 一、基础概念(什么是封装协议?) 封装协议(Wrapper)是PHP中用于**统一访问不同资源**的抽象层。它允许开发者使用标准文件函数(如`fopen()`, `file_get_contents()`)处理各种资源类型,包括: - 本地文件系统(`file://`) - HTTP/HTTPS网页(`http://`) - FTP服务器(`ftp://`) - 压缩文件(`zip://`) - 数据流(`php://`)等 核心原理:通过**协议前缀**(如`http://`)告诉PHP使用对应的协议处理器解析资源路径,实现**统一操作接口**[^1]。 --- #### 二、常用内置封装协议详解 1. **`file://` (默认协议)** 访问本地文件系统: ```php $content = file_get_contents('file:///var/www/data.txt'); // 等效于(默认省略协议): $content = file_get_contents('/var/www/data.txt'); ``` 2. **`http://` & `https://`** 获取网络资源: ```php $html = file_get_contents('https://www.example.com/api'); // 发送参数的GET请求: $data = file_get_contents('http://api.com?user=john&page=2'); ``` 3. **`php://` (PHP流协议)** - `php://input`:取原始POST数据 ```php $rawData = file_get_contents('php://input'); ``` - `php://output`:直接写入输出缓冲 ```php fwrite(fopen('php://output', 'w'), 'Hello World'); ``` - `php://temp`:临时数据存储(内存不足时转存磁盘) ```php $temp = fopen('php://temp', 'r+'); fwrite($temp, 'Temporary data'); ``` 4. **`zip://`** 直接取压缩包内文件: ```php // 取 archive.zip 中 config.ini 的内容 $content = file_get_contents('zip://archive.zip#config.ini'); ``` 5. **`data://`** 嵌入Base64或URL编码数据: ```php $img = file_get_contents('data:image/png;base64,iVBORw0KGgo...'); ``` --- #### 三、高级应用:自定义封装协议 通过`stream_wrapper_register()`创建自定义协议处理器: ```php class MyWrapper { public function stream_open($path, $mode, $options, &$opened_path) { // 解析路径并建立连接 $this->position = 0; return true; } public function stream_read($count) { // 返回自定义数据 $data = "Custom data chunk"; $this->position += strlen($data); return $data; } } // 注册 myproto:// 协议 stream_wrapper_register('myproto', 'MyWrapper'); // 使用自定义协议 $data = file_get_contents('myproto://example-data'); ``` **关键方法**: - `stream_open()`:打开资源 - `stream_read()`/`stream_write()`:写数据 - `stream_close()`:关闭资源 - `stream_seek()`:随机访问 --- #### 四、封装协议的应用场景 1. **统一资源访问** 用相同代码处理本地文件和网络资源: ```php function getContent($path) { return file_get_contents($path); // 自动识别协议 } ``` 2. **安全控制** 通过`allow_url_fopen`和`allow_url_include`配置限制危险协议访问。 3. **数据处理管道** 结合`stream_filter_append()`实现实时数据转换: ```php $stream = fopen('http://example.com/data.csv', 'r'); stream_filter_append($stream, 'convert.iconv.UTF-8/ASCII'); // 字符编码转换 ``` --- #### 五、安全注意事项 1. **禁用危险协议** 在`php.ini`中设置: ```ini allow_url_include = Off // 禁止直接包含远程代码 allow_url_fopen = On // 按需开启网络访问 ``` 2. **验证用户输入路径** 防止路径注入攻击: ```php $userPath = $_GET['path']; if (!preg_match('/^[a-z0-9:\/\.\-]+$/i', $userPath)) { die('Invalid path!'); } ``` > **最佳实践**:优先使用PHP内置协议(如`php://input`),避免直接操作`$_POST`处理二进制数据[^3]。 --- ### 相关问题 1. PHP封装协议与cURL在处理HTTP请求时有何性能差异? 2. 如何通过`php://filter`实现文件内容实时加密? 3. 自定义封装协议时如何实现目录遍历功能? 4. 为什么`allow_url_include`默认关闭?它可能引发哪些安全问题? [^1]: PHP封装协议允许统一访问不同资源类型,简化开发流程。 [^3]: PHP内置协议如`php://input`能安全处理原始数据流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值