/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
python/torch_refs/torch_grad.py
44 строки
745 B
kolkir
Refactor python folder structure
01 мар 2025, 22:16
01 мар 2025, 22:16
ef25f05
Код
Авторство
О чём код?
import torch def plus(x, y): return x + y def minus(x, y): return x - y def mul(x, y): return x * y def div(x, y): return x / y def show_grad(x_val, y_val, op): x = torch.tensor(x_val, requires_grad=True) y = torch.tensor(y_val, requires_grad=True) z = op(x, y) print(f"z={z}") z.mean().backward() print(f"x grad = {x.grad}") print(f"y grad = {y.grad}") print("add") show_grad([1.0], [2.0], plus) show_grad([1.0, 3.0], [2.0, 4.0], plus) print("minus") show_grad([1.0], [2.0], minus) show_grad([1.0, 3.0], [2.0, 4.0], minus) print("mul") show_grad([2.0], [1.5], mul) show_grad([2.0, 3.0], [2.0, 1.5], mul) print("div") show_grad([2.0], [1.5], div) show_grad([2.0, 3.0], [2.0, 1.5], div)