-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdemo.py
39 lines (31 loc) · 1.07 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Copyright (c) 2022-2024 yewentao256
Licensed under the MIT License.
"""
import microtorch
def test_main(device: str = "cpu") -> None:
observation = microtorch.rand(256, device, requires_grad=True)
target = microtorch.rand(256, device)
params = []
for i in range(4):
params.append(microtorch.rand(256, device, requires_grad=True))
def model(x: microtorch.Tensor) -> microtorch.Tensor:
x = x * params[0]
x = x + params[1]
x = x * params[2]
x = x + params[3]
return x
# Create a simple optimizer
optimizer = microtorch.SGDOptimizer(params, 0.1)
# Optimize the model for 50 iterations
for i in range(50):
optimizer.zero_grad()
prediction = model(observation)
loss = microtorch.sum(microtorch.square(prediction - target))
loss.backward()
optimizer.step()
print(f"Iter: {i}, Loss: {loss[0]}")
if __name__ == "__main__":
device = "cuda" if microtorch.cuda.is_cuda_available() else "cpu"
print(f"using `{device}` to test main")
test_main(device)