main.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <algorithm>
  2. #include <traph/core/tensor.h>
  3. #include <traph/tensor/tensor.h>
  4. #include <traph/nn/arithmetic.h>
  5. #include <traph/core/variable.h>
  6. #include <traph/nn/variable.h>
  7. #include <iostream>
  8. int main()
  9. {
  10. /*
  11. traph::Tensor<float> a = traph::zeros<float>({4, 3});
  12. traph::Tensor<float> w = traph::ones<float>({3, 5});
  13. traph::Tensor<float> result = traph::matmul(a, w);
  14. */
  15. // traph::Tensor<float> result2 = traph::add(a, 1.f);
  16. /*
  17. traph::Tensor<traph::f32> a = traph::zeros<traph::f32>({ 5000, 5000 });
  18. traph::Tensor<traph::f32> b = traph::zeros<traph::f32>({ 5000, 5000 });
  19. traph::Tensor<traph::f32> c = traph::matmul(a, b);
  20. */
  21. /*
  22. auto a = traph::Variable<traph::f32>({2, 3});
  23. auto c = traph::mul(traph::mul(a, a), 3);
  24. auto out = traph::mean(c);
  25. out.backward();
  26. */
  27. /*
  28. traph::Tensor<float> a = traph::ones<float>({ 10000, 10000 });
  29. auto b = a.sum();
  30. std::cout << b;
  31. */
  32. // auto a = traph::Variable<traph::f32>({ 2, 3 });
  33. /*
  34. auto a = traph::ones<traph::f32>({ 2,3,2 });
  35. a->requires_grad_(true);
  36. auto b = traph::sin<traph::f32>(a);
  37. auto c = traph::ones<traph::f32>({ 2,3,2 });
  38. c->requires_grad_(true);
  39. auto d = traph::add<traph::f32>(b, c);
  40. auto e = traph::sum<traph::f32>(d);
  41. e->backward();
  42. std::cout << a->grad()->to_string();
  43. */
  44. auto a = traph::ones<traph::f32>({ 2,3 });
  45. a->requires_grad_(true);
  46. auto b = traph::ones<traph::f32>({ 3,2 });
  47. b->requires_grad_(true);
  48. auto c = traph::matmul(a, b);
  49. auto d = traph::sum(c);
  50. d->backward();
  51. std::cout << a->grad()->to_string();
  52. return 0;
  53. }