/
vuron
/
adept
Обзор
Документация
Войти
/
vuron
/
adept
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
with_cpu
python/cpp_bindings/variable_py.cpp
30 строк
1 KB
kolkir
Fix training issues
06 мар 2025, 23:54
06 мар 2025, 23:54
efe26e8
Код
Авторство
О чём код?
#include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <adept/autograd/autograd.hpp> namespace py = pybind11; using namespace adept; void bind_variable(py::module& m) { py::class_<Variable>(m, "Variable") .def(py::init<const Tensor&, bool>(), py::arg("tensor"), py::arg("requires_grad") = true) .def("clone", &Variable::clone) .def("backward", &Variable::backward) .def("data", [](const Variable& self) { return self.data(); }) .def("data", [](Variable& self) { return self.data(); }) .def("grad", [](const Variable& self) { return self.grad(); }) .def("grad", [](Variable& self) { return self.grad(); }) .def("zero_grad", &Variable::zero_grad) .def("__repr__", &Variable::to_string) .def("sum", [](const Variable& self) { return sum(self); }) .def("mean", [](const Variable& self) { return mean(self); }) .def("dot", [](const Variable& self, const Variable& other) { return matmul(self, other); }) .def("__add__", [](const Variable& self, const Variable& other) { return self + other; }) .def("__sub__", [](Variable& self, const Variable& other) { return self - other; }) .def("__mul__", [](Variable& self, const Variable& other) { return self * other; }) .def("__truediv__", [](Variable& self, const Variable& other) { return self / other; }) .def("squeeze", &Variable::squeeze) .def("unsqueeze", &Variable::unsqueeze); }