Giter Site home page Giter Site logo

tensor's Introduction

Tensor Library for C++

Goal

To create a modern c++ tensor library for instructional purposes.

Why c++?

The c++11 language is a very different than c++98. The soon to be finished c++20 language has continued in that vein, and has added a large set of features that make c++ a good choice for both math and ML applications.

Why not use Python?

It's not possible to write the library entirely in Python, so some compiled language has to be used. So the question is what does Python provide that isn't already available in c++? Conventional wisdom is that Python is easy to use and c++ is difficult. And while to some extent that's true, modern c++ has simplified a lot of aspects of the language, to the point that if you're not writing a library, often the code itself is often close to what you would write in Python.

For example here is some simple code in Python:

# create a 2d tensor
a = np.array([[1, 2, 3], [4, 5, 6])
print("a:\n{}".format(a))

# slicing the tensor with a range
b = a[0:2, 1:3]
print("b:\n{}".format(b))

# broadcast
c = np.array([[1], [2]])
print("c:\n{}".format(c))

d = a + c
print("d:\n{}".format(d))

and the corresponding code for the tensor library:

// create a 2d tensor
auto a = tensor({{1, 2, 3}, {4, 5, 6}});
fmt::print("a:\n{}\n", a);
//[[  1,   2,   3],
// [  4,   5,   6]]

// slicing the tensor with a range
Tensor<int> b = a[{0, 2}][{1, 3}];
fmt::print("b: \n{}\n", b);
// [[  2,   3],
//  [  5,   6]]

// broadcasting also works
auto c = tensor({{1}, {2}});
fmt::print("c:\n{}\n", c);
// [[  1],
//  [  2]]

auto d = a + c;
fmt::print("d: \n{}\n", d);
// [[  2,   3,   4],
//  [  6,   7,   8]]

Additionally, c++ has features that python doesn't have:

  1. Strong typing: It's easy to know the type of every variable. For large projects this can radically decrease development time. Additionally, IDEs can help automatically detect errors for you.
  2. Compiled: A compiled language allows you to catch many errors before you run. Especially in cases where you may train a model for long periods of time, it's better to know your code doesn't have errors.
  3. Simplicity: If you need to develop new features, you will either have to write bridge code to Python, or use a library that automates that. Additionally, this makes it more difficult to both understand how the code works, as well as debug your code.

tensor's People

Contributors

abeschneider avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.