Double curly-brace syntax in Torch - syntax

I am trying to better understand this syntax in Lua when using Torch:
local ten = torch.Tensor{{1,2,3},{4,5,6},{7,8,9}}
for i=1,(#ten)[2] do
print(ten[i][{{2}}])
end
Specifically the line
print(ten[i][{{2}}])
When I run the code, it prints
2
[torch.DoubleTensor of size 1]
5
[torch.DoubleTensor of size 1]
8
[torch.DoubleTensor of size 1]
I am familiar with table literals and their basic syntax, but what is the purpose of wrapping the '2' in double curly-braces, and how does it work in the engine?

The following answer was posted on GitHub when I asked the same question:
https://github.com/torch/torch7/issues/501#issuecomment-171290546
Have a look at this part of the documentation.
When you have single curly-braces, you are creating a selection of the tensor. So ten[{1}] is equivalent to ten[1], which is in turn equivalent to ten:select(1,1). If you have several indices like ten[{1,2}], this is also equivalent to the slower ten[1][2] (because the latter returns 2 times a tensor, whereas the former only returns it once). If you select on a 1D tensor, your output is a number.
When you have double curly-braces, it returns a narrow of the tensor, and a narrowed tensor is always a tensor (even if it only has one element). With double curly-braces, you can specify a range in which the tensor will be narrowed, which is not possible with single curly-braces. For example, you can do ten[{{1,2},1}], which will be a 1D tensor of dimension 2, and if your do ten[{{1,2},{2}}] it will return a 2D tensor of size 2x1.
For more information, have a look at select and narrow.
One last note, the doc for select is not precisely correct, as it's possible to do a select on a 1D tensor, and the output is a number.

Related

For loop in Julia. Syntax confusion

I am a complete noob in Julia and its syntax. I am trying to follow this article on semi-definite-programming on Julia.
I would apprecieate if someone can help me figure out what the for loop in In[4] actually does:
for i in 1:m
A[:, (i-1)*n+1:i*n] .= random_mat_create(n)
b[i] = tr(A[:, (i-1)*n+1:i*n]*X_test)
end
To my understanding it should create a vector of matrices A (m of those) as well as an m-dimensional vector b. I am totally confused though on the indexing of A and the indexing of b.
I would like an explanation of the :, (i-1)*n+1:i*n part of this code. The reason I ask here is because I also dont know what to Google or what to search for in Julia documentation.
(i-1)*n+1:i*n creates a range from (i-1)*n + 1 to i*n. For example, if i=2 and n=10, this range becomes 11:20, so A[:, (i-1)*n+1:i*n] will grab all the rows of A (that's what : does), and columns 11-20.
There are two operations there that are not clear to you:
: operator. Consider a Matrix a = zeros(3,3). You could use array slicing operator (similarly to numpy or Matlab) to select the entire second columns as: a[1:end,2]. However, when selecting everything from start to the end those two values can be omitted and hence you can write a[:,2] (this always looked the easiest way for me to remember that)
. (dot) operator. Julia is very careful about what gets vectorized and what not. In numpy or R, vectorizing operations happens kind of always automatically. In Julia you have the control - but with the control comes the responsibility. Hence trying to assign values to the second column by writing a[:, 2] = 5.0 will throw an error because there is vector on the right and a scalar on the left. If you want to vectorize you need to tell that to Julia. Hence the dot operator .= means "perform element-wise assignment". Note that any Julia function or operator, even your own functions can be decorated by such dot .. Since this is a very important language feature have a look at https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting

selecting matrices based on a variable

Is there a way I can use a command that selects a matrix to use based on a variable?
Need in this /
:If (way to select a matrix based on what variable L equals) (E,F)=1:Output E,F,"O
I don't want to make a specific go-to for every single matrix I need.
This is for creating maps with the matrix in case anyone has a better way.
If i understand correctly you want to get the value from a certain matrix, chosen dynamically depending on the value of a variable. You can kinda do this by putting the names of the matrices into a string, then grab a substring of the string, using sub(, at a dynamic offset, based on L, and then feeding that string into expr( to get a reference to the matrix, ie
:"[A][B][C]"->Str1, sub(Str1,2,1) yields "[B]", expr("[B]") yields Matrix B...so 2 maps to [B]. TI considers the symbol [A] (and all the other matrix vars) to be a single character, so "[A][B][C]" is a 3 character string.
Note that all of the matrix vars need to be input from the MATRIX menu (including inside the string). Typing in the individual [ A ] chracters will not work.
Also note you can't grab indexes off of a matrix returned with expr (ie expr("[A]")(1,2) so you need an extra matrix (I used [J]) to store the result.
For example
:"MAKE SOME MATRICES"
:[[1,2][3,4]]->[A]
:[[5,6][7,8]]->[B]
:[[9,10],[11,12]]->[C]
:"SAMPLE L VALUE"
:2->L
:"STORE REFERENCES TO THE"
:"MATRICES IN A STRING"
:"[A][B][C]"->Str1
:expr(sub(Str1, L, 1))->[J]
:"SHOWS 6"
:[J](1,2)
so then proceed normally with [J]
:If [J](E,F)
: "DO WHATEVER
Tested on an 84 SE, I assume it would work the same for anything in that family, except IIRC some older models only have matrices A-F

Pattern Recognition Algorithm/Technique

Background
I apologize for the music-based question, but the details don't really mean all that much. I'm sequentially going through a midi file and I'm looking for an efficient way to find a pattern in the data to find something called a tuplet. See image below:
The tuplets have the numbers (3 or 6) over top of them. I need to know at which position they begin in the data file. The numbers below the notes are the values you would see sequentially in the data file. Just in case you can't decipher the data below, here it is:
1, 2, 2.3333, 2.6666, 3, 3.5, 3.6666, 3.83333, 4, 4.1666, 4.3333, 4.5, 4.6666, 4.8333,
5, 6.3333, 6.6666, 7.1666, 7.3333, 7.5, 7.6666, 7.8333, 8, 8.1666, 8.333, 8.5, 8.6666.
The first tuplet begins at position 2 and the difference between the position of notes is 0.3333 (repeating)
The second tuplet begins at position 3.5 and the difference between the position of notes is 0.1666 (repeating)
The main issue is that in the note, unlike the image below, position 7 will not be noted in the data file because the data only file only lists note locations. The icon that you see in that location is called a rest, which is not notated in the data file.
Question
How can I find an efficient method to find the start of each tuplet? Is there some sort of recursive method?
I don't think you need any recursion for this.
The normal note values can only be represented by fractions of the beat of the type a / 2^b. The tuplets can be arbitrary fractions, but mostly I've seen something like triplets, quintuplets or (in your case sextuplets).
So the simplest way would be to compute the length of every note (maybe the time difference between two MIDI events? Or the length is stored explicitly in MIDI? I'm not that familiar with the format) and compute the rational representation of this length.
Every group of notes with a denominator that is not a power of two belongs to such a tuplet. To group the notes together, I would recommend the following approach (assuming that all notes of a tuplet have the same value):
Factorize the denominator into a power of two a and the rest b (e.g. a * b = 4 * 5)
Initialize an empty tuplet of size b
For every note compute the distance to the beginning of the tuplet and store the note at the corresponding position, inserting rests if necessary. The length of the tuplet can be computed by taking the minimum length l of all notes in the tuplet, so greedily adding them until the end of these notes exceeds a distance of l * b from the beginning of the tuplet
This way, you base the tuplet on the minimum note length and add all notes that fit into it.

Comparing vector of double

I am trying to compare two vectors.
v1 = {0.520974 , 0.438171 , 0.559061}
v2 = [0.520974 , 0.438171 , 0.559061}
I write v1 to a file, read and that's v2. For some reason when I compare the two vectors, I am getting false!
When I do: v1[0]-v2[0] I get 4.3123e-8
Thanks,
Double values, unlike integers, are fragile against write and read. That means, the information that represents them in a string is not necessarily complete.
The leading reason of that is rounding: it's like if you had 1/7 and wanted to write it on a paper in the same format as in your question, you'd get:
0.142857
That's exact to 6 decimal places, but no more than that, and the difference shows up. The only difference in the computer is that it counts in binary (and rounds in binary, too), and is further complicated by the fact that at output (or input) you coerce that into decimal (or back, respectively) and round again at each step. All of those are sources of little errors.
If you want to be able to save and reload your doubles exactly (on the same machine), do it in their native binary representation using a write and read. If you want them to be human-readable, you need to sacrifice the exact reconstruction. You'd then need to compare them up to a little allowed deviation.

GNU Simulated Annealing

I'm working from the template program given here:
https://www.gnu.org/software/gsl/manual/html_node/Trivial-example.html
The program as they give it compiles and runs perfectly, which is nice. What I would like to do is generalise this method to find the minimum of a function with an arbitrary number of parameters.
Some cursory reading suggests that the metric function (M1) is only used in certain diagnostic and printing situations and so can more or less be ignored. All that remains is then to define E1 and S1 appropriately. Unfortunately my knowledge of using pointers and void is incomplete, so I'm stuck trying to upgrade the configuration 'xp' to be an array of parameters, rather than a single double.
In my naivete tried moving from
double x = *((double *) xp);
to
double x = (*((double *) xp))[0];
where appropriate, but obviously that didn't work. I'm sure I'm missing something stupid, so any hints would be nice! I will obviously be defining my own E1 output function which will take these N parameters and return a number.
The underlying algorithm, gsl_siman_solve() from the link provided, is generalized to work with any data type. This is why the ubiquitous xp parameter is always being cast to a double pointer before use. It should be straightforward to use any struct or array or array of arrays instead of simply doubles provided all the callbacks are coded properly.
The problem is that gsl_siman_solve() only seems to support a scalar double step size, initial guess, and 'uniform' value (from gsl_rng_uniform()), so you would need to map scalar double values into what are naturally multidimensional quantities. This can be done, but it is messy and not very flexible. In your case, the mapping would be done in S1().
This is akin to mapping the digits of a decimal number into a multidimensional space: the ones digit represents the X axis, the tens digit represents the Y axis, and the hundreds digit represents the Z axis, for example. By incrementing an integer, one can walk the entire 3D space from (0, 0, 0) to (9, 9, 9). You don't have to use integers and powers of 10, and the components don't even have to have the same range, but there is an inherent limit in the range of each component of the packed value. You would actually do this in reverse: taking a scalar double and unpacking it into multiple quantities.
Lastly, your code double x = (*((double *) xp))[0]; won't work because you are attempting to dereference a double as an array, not a pointer to a double, which would be OK. In other words, it's that first * that is the problem.

Resources