How to convert 1D tensor to regular javascript array? - tensorflow.js

How to convert 1D tensor to regular Javascript array in Tensorflow.js?
My 1D Tensor is like this:
Tensor [-0.0005436, -0.0021222, 0.0006213, 0.0014624, 0.0012601, 0.0007024, -0.0001113, -0.0011119, -0.0021328, -0.0031764]

You can use .dataSync() to get the values of a tensor in a TypedArray and if you want a standard JS array you can use Array.from(), which creates arrays out of array-like objects.
const tensor = tf.tensor1d([1, 2, 3]);
const values = tensor.dataSync();
const arr = Array.from(values);
console.log(values);
console.log(arr);
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#0.14.1/dist/tf.min.js"></script>
Keep in mind using .dataSync() blocks the UI thread until the values are ready, which can cause performance issues. If you want to load the values asynchronously you can use .data(), which returns a Promise resolving to the TypedArray.

To convert tf.tensor to plain js array there are array() and arraySync() methods.
e.g. tf.tensor([1, 2, 5]).arraySync()

Related

Can't add array to Matlab dictionary

I'm trying to add an array to a dictionary in Matlab.
In Python it's very simple, just make a dictionary and for a key assign an array, but in Matlab I get this error:
Error using () Dimensions of the key and value must be compatible, or the value must be scalar.
Basically all I want to do is:
d = dictionary
arr = [1 0.2 7 0.3]
d('key') = arr
But it doesn't work and I don't understand what I am supposed to do.
According to the documentation:
Individual elements of values must be scalars of the same data type. If values need to be heterogeneous or nonscalar, use a cell array.
So, to store an array under a key, use:
d("key") = {arr};
% create an array
array = [1, 2, 3];
% create a dictionary
dictionary = containers.Map;
% add the array to the dictionary
dictionary('array') = array;
I found this alternative by accident looking for a matlab reddit or discord server. And I basically found an amazing AI assistant website https://gistlib.com/matlab/add-array-to-dictionary-in-matlab.
So this solves this mystery.

Exchange different arrays elements in ruby

Is there any ruby method that I can use to replace two different arrays elements?
For instance, I have these two arrays:
#Before exchange
arr_one = [1,2,3,4,5]
arr_two = ["some", "thing", "new"]
After replacements the elements I am expecting something like this:
#After exchange
arr_one = ["some", "thing", "new"]
arr_two = [1,2,3,4,5]
How can I handle this with or without a ruby method?
You mean, you want to 'exchange' values of local variables? It's quite easy in Ruby:
arr_one, arr_two = arr_two, arr_one
If you need to have the arrays stay put (i.e. the values of the variables not change) and exchange the contents, then this should do:
arr_tmp = arr_one.dup
arr_one.replace(arr_two)
arr_two.replace(arr_tmp)

Defining a map to arrays containing slices in Golang

In Go (golang), is it possible to define a map from strings to arrays, and in each array element I want to store a slice. Like this:
var data = make(map[string][2]Slice[]float64)
Then I want to retrieve my data, something like this:
floatValue0 = data["string-key"][0][#]
floatValue1 = data["string-key"][1][#]
data := map[string][2][]float64{"golang": {[]float64{3.14, 3.15}, []float64{3.12, 3.16}}}
fmt.Println(data["golang"][0][0])
output:
3.14

Using fill with multi dim arrays

Is it possible to use fill to pass in a array into an array of tuples in ruby using fill?
For example I am trying to combine the following two arrays using zip, and then plan on transposing them. I am trying the following
column_name_tuples = [["foo"], ["bar"]]
column_label_tuples = [["Foo Bar"]]
column_label_tuples.fill(column_name_tuples.size..column_label_tuples.size - 1) { [nil] }
This results in column labels being filled as follows
[["Foo Bar"], nil]
When in fact I need it to be filled like this so I can do a transpose afterwards
[["Foo Bar"], [nil]]
You can do it like so:
column_label_tuples.fill([nil], column_label_tuples.size,
column_name_tuples.size-column_label_tuples.size)
#=> now [["Foo Bar"], [nil]]
which reduces to:
column_label_tuples.fill([nil], 1, 2-1)

How do I algorithmically instantiate and manipulate a multidimensional array in Scala

I am trying to wrote a program to manage a Database through a Scala Gui, and have been running into alot of trouble formatting my data in such a way as to input it into a Table and have the Column Headers populate. To do this, I have been told I would need to use an Array[Array[Any]] instead of an ArrayBuffer[ArrayBuffer[String]] as I have been using.
My problem is that the way I am trying to fill these arrays is modular: I am trying to use the same function to draw from different tables in a MySQL database, each of which has a different number of columns and entries.
I have been able to (I think) define a 2-D array with
val Data = new Array[Array[String]](numColumns)(numRows)
but I haven't found any ways of editing individual cells in this new array.
Data(i)(j)=Value //or
Data(i,j)=Value
do not work, and give me errors about "Update" functionality
I am sure this can't possibly be as complicated as I have been making it, so what is the easy way of managing these things in this language?
You don't need to read your data into an Array of Arrays - you just need to convert it to that format when you feed it to the Table constuctor - which is easy, as demonstrated my answer to your other question: How do I configure the Column names in a Scala Table?
If you're creating a 2D array, the idiom you want is
val data = Array.ofDim[String](numColumms, numRows)
(There is also new Array[String](numColumns, numRows), but that's deprecated.)
You access element (i, j) of an Array data with data(i)(j) (remember they start from 0).
But in general you should avoid mutable collections (like Array, ArrayBuffer) unless there's a good reason. Try Vector instead.
Without knowing the format in which you're retrieving data from the database it's not possible to say how to put it into a collection.
Update:
You can alternatively put the type information on the left hand side, so the following are equivalent (decide for yourself which you prefer):
val a: Array[Array[String]] = Array.ofDim(2,2)
val a = Array.ofDim[String](2,2)
To explain the syntax for accessing / updating elements: as in Java, a multi-dimensional array is just an array of arrays. So here, a(i) is element i of a, which an Array[String], and so a(i)(j) is element j of that array, which is a String.
Luigi's answer is great, but I'd like to shed some light on why your code isn't working.
val Data = new Array[Array[String]](numColumns)(numRows)
does not do what you expect it to do. The new Array[Array[String]](numColumns) part does create an array of array of strings with numColumns entries, with all entries (arrys of strings) being null, and returns it. The following (numRows) then just calls the apply function on that returned object, which returns the numRowsth entry in that list, which is null.
You can try that out in the scala REPL: When you input
new Array[Array[String]](10)(9)
you get this as output:
res0: Array[String] = null
Luigi's solution, instead
Array.ofDim[String](2,2)
does the right thing:
res1: Array[Array[String]] = Array(Array(null, null), Array(null, null))
It's rather ugly, but you can update a multidimensional array with update
> val data = Array.ofDim[String](2,2)
data: Array[Array[String]] = Array(Array(null, null), Array(null, null))
> data(0).update(0, "foo")
> data
data: Array[Array[String]] = Array(Array(foo, null), Array(null, null))
Not sure about the efficiency of this technique.
Luigi's answer is great, but I just wanted to point out another way of initialising an Array that is more idiomatic/functional – using tabulate. This takes a function that takes the array cell coordinates as input and produces the cell value:
scala> Array.tabulate[String](4, 4) _
res0: (Int, Int) => String => Array[Array[String]] = <function1>
scala> val data = Array.tabulate(4, 4) {case (x, y) => x * y }
data: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 1, 2, 3), Array(0, 2, 4, 6), Array(0, 3, 6, 9))

Resources