NTM Memory System OpenClaw Skill

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

v1.0.0 Recently Updated Updated 2 wk ago

Installation

clawhub install ntm-memory-system

Requires npm i -g clawhub

97

Downloads

0

Stars

0

current installs

0 all-time

1

Versions

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

Statistics

Downloads 97
Stars 0
Current installs 0
All-time installs 0
Versions 1
Comments 0
Created Mar 19, 2026
Updated Mar 19, 2026

Latest Changes

v1.0.0 · Mar 19, 2026

基于 Neural Turing Machines 的外部记忆系统

Quick Install

clawhub install ntm-memory-system
EU Made in Europe

Chat with 100+ AI Models in one App.

Use Claude, ChatGPT, Gemini alongside with EU-Hosted Models like Deepseek, GLM-5, Kimi K2.5 and many more.