How can I create a scalar in ATen?
I know that I can use double variables, but it’s rather inconvenient to write inner products as
// A and B are ATen (n, ) tensors
double inner_prod = 0.0;
for (idx = 0; idx < n; ++idx) {
inner_prod += *(A[idx] * B[idx]).data<double>();
}
in this particular case, could use dot or matmul.
e.g. in python
>>> g=torch.manual_seed(0)
>>> a=torch.rand(3)
>>> b=torch.rand(3)
>>> torch.matmul(a,b)
tensor(0.3578)
>>> torch.dot(a,b)
tensor(0.3578)
in c++, maybe something like:
#include “ATen/ATen.h”
using namespace at;
int main(void) {
Generator &gen = globalContext().defaultGenerator(Backend::CPU);
gen.manualSeed(0);
auto a=CPU(kFloat).rand(3);
auto b=CPU(kFloat).rand(3);
auto c=matmul(a,b);
auto d=dot(a,b);
std::cout <<“Dimension of c:” << c.dim() << “, value:” << c.toCDouble() << std::endl;
std::cout <<“Dimension of d:” << d.dim() << “, value:” << d.toCDouble() << std::endl;
}
Dimension of c:0, value:0.357792
Dimension of d:0, value:0.357792