How to interpret kotlin's #Metadata annotation content? - annotations

I know that kotlin generates #Metadata annotation for each compiled class. I also saw the comment on this type:
This annotation is present on any class file produced by the Kotlin compiler and is read by the compiler and reflection.
The question is how it is used by the compiler(assuming that it's a result of compilation)?
And I'll ask help with interpretation of content of the annotation. For example, this one:
#Metadata(
mv = {1, 1, 9},
bv = {1, 0, 2},
k = 1,
d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u0000 \u00032\u00020\u0001:\u0001\u0003B\u0005¢\u0006\u0002\u0010\u0002¨\u0006\u0004"},
d2 = {"LMain;", "", "()V", "Companion", "production sources for module kotlin-examples"}
)
Thanks in advance!

Related

Swift - Declaring a Set using named variable

I am trying to understand Sets in Swift and how to declare them correctly but I have found the following a little confusing.
I understand that this works:
let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7]
let setFromElements = Set(elements)
But I don't understand why the following doesn't:
let setFromElements : Set = elements
Or even:
let setFromElements : Set<Int> = elements
When the following is valid:
let setFromArray : Set = [ 1, 2, 4, 5]
Can someone please help me understand why this is the case?
let setFromArray: Set = [ 1, 2, 4, 5] works because Set conforms to ExpressibleByArrayLiteral and hence has an initializer that takes an ArrayLiteral. See Set.init(arrayLiteral:). This conformance gives syntactic sugar for not having to explicitly call the init.
On the other hand, once you save the array literal into a variable using let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7], elements becomes an Array, not an ArrayLiteral and hence another initializer of Set has to be called that takes an Array. This init does not provide syntactic sugar like ExpressibleByArrayLiteral does, so you explicitly have to call the init by doing Set(array).
Set has an initializer that takes an array, and that makes a set, by taking the unique items in the array. But a set is not an array, two different types, so you can't just use = to assign one to the other.

how to apply "Gather" operation like numpy in Caffe2?

I am new to Caffe2, and I want to compose an operation like this:
Numpy way
example code
pytoch way
example code
My question is, how to compose Caffe2 operators to make the same operators like above? I have tried some compositions but still I couldn't find the right one. If anyone knows the composition, please help, I will be really appreciate for it.
There is a Gather operator in Caffe2. The main problem with this operator is that you can't set the axis (it's always 0). So, if we run this code:
model = ModelHelper(name="test")
s = np.arange(20).reshape(4, 5)
y = np.asarray([0, 1, 2])
workspace.FeedBlob('s', s.astype(np.float32))
workspace.FeedBlob('y', y.astype(np.int32))
model.net.Gather(['s', 'y'], ['out'])
workspace.RunNetOnce(model.net)
out = workspace.FetchBlob('out')
print(out)
We will get:
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[ 10. 11. 12. 13. 14.]]
One solution could be to reshape s to a 1D array and transform y in the same way. First of all, we have to implement an operator to transform y. In this case, we will use a numpy function called ravel_multi_index:
class RavelMultiIndexOp(object):
def forward(self, inputs, outputs):
blob_out = outputs[0]
index = np.ravel_multi_index(inputs[0].data, inputs[1].shape)
blob_out.reshape(index.shape)
blob_out.data[...] = index
Now, we can reimplement our original code:
model = ModelHelper(name="test")
s = np.arange(20).reshape(4, 5)
y = np.asarray([[0, 1, 2],[0, 1, 2]])
workspace.FeedBlob('s', s.astype(np.float32))
workspace.FeedBlob('y', y.astype(np.int32))
model.net.Python(RavelMultiIndexOp().forward)(
['y', 's'], ['y'], name='RavelMultiIndex'
)
model.net.Reshape('s', ['s_reshaped', 's_old'], shape=(-1, 1))
model.net.Gather(['s_reshaped', 'y'], ['out'])
workspace.RunNetOnce(model.net)
out = workspace.FetchBlob('out')
print(out)
Output:
[[ 0.]
[ 6.]
[ 12.]]
You may want to reshape it to (1, -1).

Dynamic Json Keys in Scala

I'm new to scala (from python) and I'm trying to create a Json object that has dynamic keys. I would like to use some starting number as the top-level key and then combinations involving that number as second-level keys.
From reading the play-json docs/examples, I've seen how to build these nested structures. While that will work for the top-level keys (there are only 17 of them), this is a combinatorial problem and the power set contains ~130k combinations that would be the second-level keys so it isn't feasible to list that structure out. I also saw the use of a case class for structures, however the parameter name becomes the key in those instances which is not what I'm looking for.
Currently, I'm considering using HashMaps with the MultiMap trait so that I can map multiple combinations to the same original starting number and then second-level keys would be the combinations themselves.
I have python code that does this, but it takes 3-4 days to work through up-to-9-number combinations for all 17 starting numbers. The ideal final format would look something like below.
Perhaps it isn't possible to do in scala given the goal of using immutable structures. I suppose using regex on a string of the output might be an option as well. I'm open to any solutions regarding data structures to hold the info and how to approach the problem. Thanks!
{
"2": {
"(2, 3, 4, 5, 6)": {
"best_permutation": "(2, 4, 3, 5, 6)",
"amount": 26.0
},
"(2, 4, 5, 6)": {
"best_permutation": "(2, 5, 4, 6)",
"amount": 21.0
}
},
"3": {
"(3, 2, 4, 5, 6)": {
"best_permutation": "(3, 4, 2, 5, 6)",
"amount": 26.0
},
"(3, 4, 5, 6)": {
"best_permutation": "(3, 5, 4, 6)",
"amount": 21.0
}
}
}
EDIT:
There is no real data source other than the matrix I'm using as my lookup table. I've posted the links to the lookup table I'm using and the program if it might help, but essentially, I'm generating the content myself within the code.
For a given combination, I have a function that basically takes the first value of the combination (which is to be the starting point) and then uses the tail of that combination to generate a permutation.
After that I prepend the starting location to the front of each permutation and then use sliding(2) to work my way through the permutation looking up the amount which is in a breeze.linalg.DenseMatrix by using the two values to index the matrix I've provided below and summing the amounts gathered by indexing the matrix with the two sliding values (subtracting 1 from each value to account for the 0-based indexing).
At this point, it is just a matter of gathering the information (starting_location, combination, best_permutation and the amount) and constructing the nested HashMap. I'm using scala 2.11.8 if it makes any difference.
MATRIX: see here.
PROGRAM:see here.

How do I use fbcunn nn.HSM

I'd love a simple example of how to use the nn.HSM hierarchical softmax module in fbcunn.
The documentation is nonexistent (see here). I did find this user group post which describes what the cluster "mapping" should look like, and there are some tests lying around for the module and its corresponding criterion but none of them use the module in a "normal" way, e.g. as the final layer in a simple feed-forward neural network.
Here's a small example that should work:
require 'torch'
require 'fbcunn'
hidden_dim = 10
input_dim = 20
batch_size = 1
gpuid = 0
function to_cuda(x) return gpuid >= 0 and x:cuda() or x end
inputs = to_cuda(torch.rand(batch_size, input_dim))
targets = to_cuda(torch.ones(batch_size):long())
-- #mapping should equal the number of possible outputs, in our case 8
-- this mapping defines 4 clusters, two size 2, one size 3, one size 1
mapping = { {1, 1}, {1, 2}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {4, 1}, {4, 2} }
linear = to_cuda(nn.Sequential():add(nn.Linear(input_dim,hidden_dim)))
hsm = to_cuda(nn.HSM(mapping, hidden_dim))
h_out = linear:forward(inputs)
s_out, err = hsm:forward(h_out, targets)
df_ds = hsm:backward(h_out, targets)
linear:backward(inputs, df_ds)
Note that as of this posting, the above example will only work on a GPU (cannot run on CPU, aka gpuid=-1); See this issue.
Additionally, the GPU must have compute capability 3.5 or greater; the example at the link above provides an even smaller example that will run on a GPU with compute capability 3.0.

Mathematica importing issue

I have exported matrix from Mathematica
Export["all.txt", all]
i.e.
Matrix is s.t
{{1,2,3,4},{1,2,3,4}}
I tried to import it back, but it is not usable..
Import["text.txt"]
{1, 2, 3}
{1, 2, 3}
,
Import["text.txt", "Data"]
{{"{1,", "2,", "3}"}, {"{1,", "2,", "3}"}}
or
Import["text.txt", "String"]
{1, 2, 3}
{1, 2, 3}
I cannnot use it because it is not matrix. How can I import it back as matrix? so I can get do some command like %[[1]][[1]] to obtain rows and column
There's nothing wrong with the file extension .txt, but when Mathematica exports and imports a text file, it assumes it is a string. You can see:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.txt"]]
(* Output: String *)
Instead, you can change the file extension as Eugene suggests:
Export["C:\\test.dat", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.dat"]]
(* Output: List *)
Notice that it won't even use a file extension it doesn't understand:
Export["C:\\test.xyz", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.xyz"]]
(* Output: Export::type: "List cannot be exported to the "XYZ" format." *)
Really, what's happening is Import and Export are trying to infer the type you want to use from the file extension. You can always specify that type manually instead:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}},"List"]
Head[Import["C:\\test.txt"]]
(* Output: String *)
The output files for type "List" and type "String" are going to be the same, so you need to specify it when you import the file too:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}},"List"]
Head[Import["C:\\test.txt","List"]]
(* Output: List *)
Note that this would work even if you forgot to Export with the type "List" specified, since the output file would still be compatible for importing as a list.
Worst case scenario, you can always try this:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
ToExpression[Import["C:\\test.txt"]]
(* Output: {4,5,6} *)
That doesn't quite work and only gives you bottom row. You'd have to be a bit pedantic:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
ToExpression[StringSplit[Import["C:\\test.txt"], "\n"]]
But of all these possibilities, the best and simplest I've mentioned is to simply tell the Import command that what you have is a list:
Import["C:\\test.txt","List"]
That's why that optional argument exists for Import in the first place. The reason it's optional is so we can be lazy and let Mathematica decide that .txt files are strings and .dat files are lists, but if you don't want Mathematica to do that (i.e. Mathematica is doing it wrong), you just specify "List" or whatever type of file you are importing.
You almost got this right but you used the type "Data" which is actually meant to indicate (from the documentation):
"data in a generic Wolfram Language form (list, string, etc.)"
which means you won't get much help there -- Mathematica will still decide this is a string. The type you want is "List".
I've noticed long time ago (since 6th versions) that Mathematica has some issues with *.txt files. Use suffix .dat. It has been proven issue-proof for me with matrices or anything.