Working with Tensors

Let's set the stage by talking about exactly what a Tensor is. To do so, we should also talk a little bit about vectors and matrices as well. You can skip this section if you are already familiar, but it is short and if you already know about matrices and vectors, who knows, you might remember something you've forgotten! So go ahead and read it anyway!

Now, before we talk, let me show you a graphic that may make things a tad easier to visualize:

A vector is an array of numbers, as you can see here:

A matrix is a grid of n x m numbers, a two-dimensional array. We can do all kinds of neat operations on a matrix, such as addition and subtraction, so long as the sizes are compatible:

We can multiply matrices if we so desire, like this:

And matrices can be added together, like this:

In both cases, we are working within a two-dimensional space. So, what can we do if our requirement is to work out of an n dimensional space to where n > 2? This where Tensors come in.

A Tensor is basically a matrix but is not two-dimensional (although it could be). It could be a three-dimensional matrix (a vector is a tensor is a matrix) or some incredibly crazy dimension that we do not yet know how to visualize. And to show you how powerful Tensors really are, a tensor can be covariant in one dimension and contravariant in another. The dimension of a tensor is usually called its rank.

More formally, a tensor is really what is called a mathematical entity, which lives inside a structure and interacts with other entities inside that structure. If one of the entities gets transformed, the tensor must obey what is referred to as a related transformation rule. This is really what differentiates a matrix from a tensor. The tensor must allow the entities to shift around when transformations occur.

Now that we've got that all squared away and under our belts, let's look at how we can work with Tensors by walking through a bit of example code:

void BasicVariables ()
{
Console.WriteLine ("Using placerholders");
using (var g = new TFGraph ())
{
var s = new TFSession (g);

Notice the variable type must match the cast in the TFTensor:

var var_a = g.Placeholder (TFDataType.Int16);
var var_b = g.Placeholder (TFDataType.Int16);

We are going to do addition and multiplication:

var add = g.Add (var_a, var_b);
var mul = g.Mul (var_a, var_b);
varrunner = s.GetRunner ();

Let's add two Tensors together (this is the variable type cast mentioned previously):

runner.AddInput (var_a, new TFTensor ((short)3));
runner.AddInput (var_b, new TFTensor ((short)2));
Console.WriteLine ("a+b={0}", runner.Run (add).GetValue ());

Now let's multiply two Tensors together:

runner = s.GetRunner ();
runner.AddInput (var_a, new TFTensor ((short)3));
runner.AddInput (var_b, new TFTensor ((short)2));
Console.WriteLine ("a*b={0}", runner.Run (mul).GetValue ());
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset