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

173 天前
 usb7

大家好,

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

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

有意者,请留言或和我联系: tech2@usb7.net

1199 次点击
所在节点    外包
4 条回复
QuinceyWu
173 天前
# 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
173 天前
我看了下他们的主页,不是直接用源码安装上就可以了吗
treizeor
173 天前
chatgpt 一问就有很详细回答了
SunDoge
172 天前
有意

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1007683

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX