V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
usb7
V2EX  ›  外包

请人付费将基于 tinygrad 的最简单的深度学习模型部署在 Ubuntu 的电脑 CPU 上运行

  •  
  •   usb7 · 112 天前 · 1153 次点击
    这是一个创建于 112 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,

    关于 tinygrad: https://tinygrad.org/

    我想请人研究一下 tinygrad ,基于 tinygrad 的最简单的深度学习模型部署在 Ubuntu 的电脑 CPU 上运行。

    有意者,请留言或和我联系: [email protected]

    4 条回复    2024-01-12 08:41:32 +08:00
    QuinceyWu
        1
    QuinceyWu  
       112 天前
    # 0
    ##安装 python 和 pip
    ```bash
    sudo apt update
    sudo apt install python3 python3-pip
    ```
    ##安装 tinygrad
    ```bash
    git clone https://github.com/geohot/tinygrad.git
    cd tinygrad
    pip3 install -r requirements.txt
    ```
    ##测试安装
    ```bash
    python3 -m unittest
    ```

    # 1
    ##创建模型

    ```python
    from tinygrad.tensor import Tensor

    class SimpleNN:
    def __init__(self, input_size, num_classes):
    # 随机初始化权重
    self.w1 = Tensor.random(input_size, 64)
    self.w2 = Tensor.random(64, num_classes)

    def forward(self, x):
    # 简单的前向传播
    x = x.dot(self.w1).relu()
    x = x.dot(self.w2).logsoftmax()
    return x

    def parameters(self):
    # 返回模型参数
    return [self.w1, self.w2]
    ```

    ## 准备数据
    ```python
    import numpy as np

    X = np.random.randn(10, 784)
    Y = np.zeros((10, 10))
    Y[np.arange(10), np.random.randint(0, 10, size=10)] = 1
    ```

    ## 训练模型
    ```python
    def train(model, X, Y, lr=0.001, epochs=100):
    # 将 numpy 数组转换为 Tensor
    inputs = Tensor(X)
    targets = Tensor(Y)

    for i in range(epochs):
    # 前向传播
    out = model.forward(inputs)

    # 计算损失(交叉熵)
    loss = out.mul(targets).mean()
    print(f"Epoch {i}, Loss: {loss.data}")

    # 反向传播
    model.zero_grad()
    loss.backward()

    # 更新权重
    for param in model.parameters():
    param.data -= lr * param.grad

    def main():
    model = SimpleNN(784, 10)
    train(model, X, Y)

    if __name__ == "__main__":
    main()
    ```

    # 2
    ## 执行脚本 开始训练过程 看到每个 epoch 的损失输出到终端
    paopjian
        2
    paopjian  
       112 天前
    我看了下他们的主页,不是直接用源码安装上就可以了吗
    treizeor
        3
    treizeor  
       112 天前
    chatgpt 一问就有很详细回答了
    SunDoge
        4
    SunDoge  
       111 天前 via Android
    有意
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2216 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 02:22 · PVG 10:22 · LAX 19:22 · JFK 22:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.