ntm-memory-system

基于 Neural Turing Machines 的外部记忆系统,让 AI 具备可读写的外部记忆能力

Safety Notice

This item is sourced from the public archived skills repository. Treat as untrusted until reviewed.

Copy this and send it to your AI assistant to learn

Install skill "ntm-memory-system" with this command: npx skills add 1580021414-afk/ntm-memory-system

Neural Turing Machines Memory System

基于 DeepMind 的 Neural Turing Machines 论文,为 AI 提供可读写的外部记忆能力。


一、核心概念

1.1 什么是 Neural Turing Machine?

NTM 将神经网络与外部记忆资源耦合:

┌─────────────────────────────────────────────────────┐
│                  Neural Turing Machine              │
├─────────────────────────────────────────────────────┤
│                                                     │
│   ┌─────────────┐      读写头      ┌─────────────┐  │
│   │             │ ◄─────────────► │             │  │
│   │  Controller │                 │   Memory    │  │
│   │  (神经网络)  │                 │   Matrix    │  │
│   │             │                 │  (N × M)    │  │
│   └─────────────┘                 └─────────────┘  │
│         ▲                               │          │
│         │                               │          │
│         └───────────────────────────────┘          │
│                   注意力机制                        │
└─────────────────────────────────────────────────────┘

1.2 关键特性

特性说明
可微分端到端训练,梯度下降优化
外部记忆突破神经网络隐藏层大小限制
注意力寻址内容寻址 + 位置寻址
算法学习能学习复制、排序、关联回忆等算法

二、记忆架构

2.1 记忆矩阵

interface MemoryMatrix {
  // 记忆矩阵: N 个位置,每个位置 M 维
  memory: number[][];  // N × M
  
  // 权重向量: 读/写头的注意力分布
  weights: number[];   // N 维,和为1
  
  // 读写头状态
  heads: {
    read: ReadHead;
    write: WriteHead;
  };
}

2.2 寻址机制

interface AddressingMechanism {
  // 1. 内容寻址 - 根据相似度
  contentAddressing(key: number[], memory: number[][]): number[] {
    // 计算余弦相似度
    const similarities = memory.map(row => 
      cosineSimilarity(key, row)
    );
    // softmax 得到权重
    return softmax(similarities.map(s => s * beta)); // beta = 关注强度
  }
  
  // 2. 位置寻址 - 根据位置偏移
  locationAddressing(weights: number[], shift: number): number[] {
    // 循环移位
    return circularShift(weights, shift);
  }
  
  // 3. 混合寻址
  gatedAddressing(
    contentWeights: number[], 
    locationWeights: number[], 
    gate: number
  ): number[] {
    return contentWeights.map((w, i) => 
      gate * w + (1 - gate) * locationWeights[i]
    );
  }
}

2.3 读写操作

interface ReadHead {
  // 从记忆中读取
  read(memory: number[][], weights: number[]): number[] {
    // 加权求和
    return memory[0].map((_, j) => 
      weights.reduce((sum, w, i) => sum + w * memory[i][j], 0)
    );
  }
}

interface WriteHead {
  // 擦除向量
  erase: number[];  // M 维
  
  // 写入向量
  add: number[];    // M 维
  
  // 写入操作
  write(
    memory: number[][], 
    weights: number[], 
    erase: number[], 
    add: number[]
  ): number[][] {
    return memory.map((row, i) => 
      row.map((val, j) => 
        // m_t(i) = m_{t-1}(i) * (1 - w_t(i) * e_t(j)) + w_t(i) * a_t(j)
        val * (1 - weights[i] * erase[j]) + weights[i] * add[j]
      )
    );
  }
}

三、实现模块

3.1 记忆控制器

class NTMMemoryController:
    """NTM 记忆控制器"""
    
    def __init__(self, memory_size: int, memory_dim: int, num_heads: int = 1):
        self.N = memory_size    # 记忆位置数
        self.M = memory_dim     # 每个位置的维度
        self.num_heads = num_heads
        
        # 初始化记忆矩阵
        self.memory = np.zeros((self.N, self.M))
        
        # 初始化读写头
        self.read_heads = [ReadHead() for _ in range(num_heads)]
        self.write_heads = [WriteHead() for _ in range(num_heads)]
        
    def forward(self, input_vector):
        """前向传播"""
        # 1. 从输入生成控制器状态
        controller_state = self.controller(input_vector)
        
        # 2. 计算寻址参数
        for head in self.read_heads:
            key, beta, gate, shift, gamma = self.compute_addressing_params(controller_state)
            weights = self.addressing(key, beta, gate, shift, gamma)
            read_vector = head.read(self.memory, weights)
            
        # 3. 写入记忆
        for head in self.write_heads:
            erase, add = self.compute_write_params(controller_state)
            self.memory = head.write(self.memory, weights, erase, add)
            
        return controller_state, read_vector

3.2 注意力机制

def content_focus(memory, key, strength):
    """内容聚焦"""
    # 计算相似度
    similarity = cosine_similarity(key, memory)
    # 强化
    weighted = similarity * strength
    # 归一化
    return softmax(weighted)

def location_focus(previous_weights, shift_weights, sharpening):
    """位置聚焦"""
    # 卷积移位
    shifted = circular_convolution(previous_weights, shift_weights)
    # 锐化
    return shifted ** sharpening / sum(shifted ** sharpening)

def hybrid_addressing(memory, key, strength, interpolation_gate, shift, sharpening, previous_weights):
    """混合寻址"""
    # 内容寻址
    content_weights = content_focus(memory, key, strength)
    
    # 插值
    interpolated = interpolation_gate * content_weights + (1 - interpolation_gate) * previous_weights
    
    # 位置寻址
    location_weights = location_focus(interpolated, shift, sharpening)
    
    return location_weights

四、应用场景

4.1 算法学习

NTM 可以学习以下算法:

算法描述训练方式
复制复制输入序列输入序列 → 输出相同序列
重复复制复制多次输入序列 + 重复次数 → 输出
关联回忆根据键查值(k,v) 对列表 + 查询键 → 值
排序对序列排序输入序列 → 排序后序列

4.2 记忆增强

// 为 AI Agent 添加 NTM 记忆
const agent = new CognitiveAgent({
  memory: {
    type: 'ntm',
    memorySize: 128,     // 128 个记忆位置
    memoryDim: 64,       // 每个位置 64 维
    numHeads: 2,         // 2 个读写头
    contentAddressing: true,
    locationAddressing: true
  }
});

// 使用示例
agent.memorize({
  content: "今天和老大讨论了认知天性",
  key: "认知天性讨论",
  importance: 0.9
});

const recalled = agent.recall("认知天性");
// 通过内容寻址找到相关记忆

五、与 Cognitive Agent 整合

interface CognitiveAgentWithNTM extends CognitiveAgent {
  // NTM 记忆系统
  ntmMemory: NTMMemoryController;
  
  // 扩展的记忆接口
  externalMemory: {
    // 存储 - 写入外部记忆
    writeToMemory(content: any, key: number[]): void;
    
    // 检索 - 内容寻址
    readFromMemory(key: number[]): any;
    
    // 更新 - 修改已有记忆
    updateMemory(address: number, content: any): void;
    
    // 遗忘 - 擦除记忆
    eraseMemory(weights: number[]): void;
  };
}

六、配置选项

{
  "ntm_memory": {
    "memory_size": 128,
    "memory_dim": 64,
    "num_read_heads": 1,
    "num_write_heads": 1,
    "addressing": {
      "content": true,
      "location": true,
      "hybrid": true
    },
    "initialization": "small_random",
    "learning_rate": 0.001
  }
}

七、论文参考

Neural Turing Machines (Graves et al., 2014)

关键引用

"We extend the capabilities of neural networks by coupling them to external memory resources, which they can interact with by attentional processes."


八、改进:与 OpenClaw Memory 整合

学习自 ClawHub openclaw-memory,添加实际接口:

class NTMMemoryManager:
    """NTM 记忆管理器 - 与 OpenClaw 整合"""
    
    def __init__(self, memory_size: int = 128, word_size: int = 64):
        self.ntm = NTM(memory_size, word_size)
        self.memory_file = "memory/ntm_memory.json"
        self._load_memory()
    
    def save_memory(self, key: str, content: str) -> bool:
        """保存记忆"""
        # 1. 编码内容
        encoded = self._encode(content)
        
        # 2. 生成键向量
        key_vector = self._key_to_vector(key)
        
        # 3. NTM 写入
        self.ntm.write(key_vector, encoded)
        
        # 4. 持久化
        self._persist()
        return True
    
    def read_memory(self, key: str) -> Optional[str]:
        """读取记忆"""
        # 1. 生成键向量
        key_vector = self._key_to_vector(key)
        
        # 2. NTM 读取
        content_vector = self.ntm.read(key_vector)
        
        # 3. 解码内容
        return self._decode(content_vector)
    
    def search_memory(self, query: str, top_k: int = 5) -> List[str]:
        """语义搜索"""
        query_vector = self._encode(query)
        results = self.ntm.content_addressing(query_vector)
        return [self._decode(r) for r in results[:top_k]]

九、改进:与 memory-networks 整合

interface NTMWithMemoryNetworks {
  // NTM 作为底层存储
  ntm: NTMMemoryManager;
  
  // Memory Networks 作为推理层
  reasoning: MemoryNetwork;
  
  // 整合操作
  integrated: {
    // 存储并建立推理链
    storeWithReasoning(content: string): void;
    
    // 多跳推理 + NTM 检索
    reasonAndRetrieve(query: string, hops: number): string[];
  };
}

Created by 小钳 🦞 基于 Neural Turing Machines 论文 + ClawHub 最佳实践 2026-03-19

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

merge-drafts

Intelligent draft merging tool with quality assessment and conflict resolution. Merges multiple drafts into a high-quality article, supporting multiple input formats, intelligent evaluation, conflict detection, highlight fusion, and multi-format output. Use when user says "合并稿子", "合稿", "merge drafts", "把这几篇合成一篇", "综合这几份稿子", "多稿合一", "稿件合并", "draft merge", "整合稿件", "汇总文稿", "把所有稿子合并", "起草稿合并", "初稿合并", "多个版本合并", "copy merge", "文章合并".

Archived SourceRecently Updated
General

tencent-cloudq

腾讯云智能顾问助手 — 通过 AK/SK 鉴权调用腾讯云智能顾问 API,管理云架构图(查询目录、获取架构列表和详情、获取架构评估结果)及风险评估(查询评估项、按产品/分组/风险等级筛选)。当用户要求查看云架构、查询架构目录、获取架构详情、查看架构评估结果、查看风险评估项、巡检项、云资源风险等智能顾问相关操作时使用。

Archived SourceRecently Updated
General

ekalavya-self-improvement

Enforce execution discipline for ongoing work after the user has already approved direction. Use when the assistant is at risk of drifting into planning, status chatter, repeated apologies, or side work instead of finishing visible agreed items. Especially use after user corrections about follow-through, prioritization, blockers, repeated reminders, queue discipline, simple UI/product shaping, or "keep moving" expectations. Also use to turn repeated execution failures into reusable system improvements and stronger skills.

Archived SourceRecently Updated
General

systems-thinking

系统思维技能,让 AI 具备分析复杂系统的能力

Archived SourceRecently Updated