Tensorflow JS Probabilties - tensorflow.js

i have multiple feature columns and a result column where i want to predict if something happens or not.
so I'm training my model and finally i do
const predictions = model.predict(xTest).argMax(-1);
this returns a tensor and when getting the data with:
predictions.dataSync ()
i get values like [0, 1, 1, 1, 0, ...]
is there any way to get probabilities like in python? [0.121, 0.421, 0.8621, ...]
I only found one result:
https://groups.google.com/a/tensorflow.org/g/tfjs/c/TvcB69MUj_I?pli=1
is this still the case? are there no probabilities in javascript?

tf.argMax returns the indices of the maximum value along the axis. If you rather want to have the maximum value itself you could use tf.max instead
const x = tf.tensor2d([[1, 2, 3],[ 4, 8, 4]]);
x.max(-1).print() // [3, 8]
x.argMax(-1).print() // [2, 1]

Related

extract blocks of columns (as seperated subarrays) indicated by 1D binary array

Based on a 1D binary mask, for example, np.array([0,0,0,1,1,1,0,0,1,1,0]), I would like to extract the columns of another array, indicated by the 1's in the binary mask, as as sub-arrays/separate blocks, like [9, 3.5, 7]) and [2.8, 9.1] (I am just making up the numbers to illustrate the point).
So far what I have (again just as a demo to illustrate what my goal is, not the data where this operation will be performed):
arr = torch.from_numpy(np.array([0,0,0,1,1,1,0,0,1,1,0]))
split_idx = torch.where(torch.diff(arr) == 1)[0]+1
torch.tensor_split(arr, split_idx.tolist())
The output is:
(tensor([0, 0, 0]),
tensor([1, 1, 1]),
tensor([0, 0]),
tensor([1, 1]),
tensor([0]))
What I would like to have in the end is:
(tensor([1, 1, 1]),
tensor([1, 1]))
Do you know how to implement it, preferably in pytorch, but numpy functions are also fine. A million thanks in advance!!
You can construct your tensor of slice indices with your approach. Only thing is you were missing the indices for the position of the end of each slice. You can do something like:
>>> slices = arr.diff().abs().nonzero().flatten()+1
tensor([ 3, 6, 8, 10])
Then apply tensor_split and slice to only keep every other element:
>>> torch.tensor_split(arr, slices)[1::2]
(tensor([1, 1, 1]), tensor([1, 1]))

Two if statements in a for loop?

class Solution:
def transformArray(self, arr: List[int]) -> List[int]:
x=arr
while True:
f=True
for i in range(1,len(arr)-1):
if arr[i-1]<arr[i] and arr[i]>arr[i+1]:
f=False
x[i]=x[i]-1
print(x[i])
if arr[i-1]>arr[i] and arr[i]<arr[i+1]:
f=False
x[i]=x[i]+1
print(x[i])
#print(x)
x=arr
if f==True:
break
return x
In the above code both the if statements don't execute , only the second one does. I have tried using elif but it still doesn't work. What am i missing here?
For your code, I considered two types of examples as input for the array list
For example 1, when the arr = [1, 2, 1, 4], the 2nd element is bigger than 1st and 3rd
The first if statement (if arr[i-1]<arr[i] and arr[i]>arr[i+1]:) is working, because both the conditions are met and it gives the output x = [1, 1, 1, 4]
In example 2, when the arr = [3, 2, 3, 4], the 2nd element is smaller than the 1st and 3rd
The second if statement (if arr[i-1]>arr[i] and arr[i]<arr[i+1]:) is working, because both the conditions are met and it gives the output x = [3, 3, 3, 4]
So, the working of if statements largely depends on the elements in the array. Both the if statements' purpose is totally opposite. If one satisfies the condition the other will not.
Hope my answer provides some clarification.

How to make a route from an array

I would like to create a route In Python from the following data
([[ 0, 1],[ 0, 2],[ 0, 8],[ 1, 7],[ 2, 9],[ 3, 6],[ 4, 3],[ 5, 0],[ 6, 0], [ 7, 0][ 8, 4],[ 9, 5],[10, 10]])
The outcome that I would like to have would be a route like [0,1][1,7],[7,0] (0-1-7-0) and [0,2],[2,9][9,5][5,0] (0-2-9-5-0) and [0,8][8,4][4,3][3,6][6,0] (0-8-4-3-6-0). I have tried making the first array into a tuple and I think that would work, but can't seem to find how to sort the array.
Is there some kind of loop which could help me sort it and thereafter make tuples of it?
The "data" that you have describes a graph. I suggest adding this keyword to your question.
The vertices of the graph are the integers 0 to 10. The edges of the graph are your 2-element lists [0, 1], [0, 2], [0, 8], etc.
Now you are looking for a path through your graph. What requirements do you have on the path you're looking for? Should it be the longest path in the graph? Should it be the shortest path between two particular nodes?
If you're only looking for a maximal path, i.e. a path that cannot be further extended, then a greedy algorithm suffices:
initialize the path with one arbitrary edge
loop:
look for an edge in data that can extend the path
pop the edge from data and add it to the path
break the loop when no appropriate edge can be found in data

Populate a vector in Matlab using a smaller vector

I need to populate a vector with elements of another, smaller vector. So say the vector I need to populate is of length ten and is currently all zeros, i.e.
vector = [0,0,0,0,0,0,0,0,0,0]
Now suppose I have already define a vector
p = [1, 2, 3, 4, 5]
How could I populate "vector" with the array "p" so that the result is [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]? Bear in mind, I want the other positions in "vector" to remain unchanged. I have already tried using repmat(p, length(p)) but that ends up giving me something of the form [1,2,3,4,5,1,2,3,4,5]. Thanks!
Try a combination of vector slicing and concatenation:
vector = cat(1, p, vector(5:))
This is faster:
vector(1:5) = p
More generally,
vector(1:numel(p)) = p

Usage of Pydatalog Aggregate Functions

I have been playing around with the various aggregate functions to get a feel for them, and after being confused for the past few days I am in need of clarification. I either get completely unintuitive behavior or unhelpful errors. For instance, I test:
(p[X]==min_(Y, order_by=Z)) <= Y.in_((4,6,2)) & Z.in_((6,))
looking at sample output:
p[0]==X,Y,Z
([(6,)], [4, 6, 2], [6, 6, 6])
p[1]==X,Y,Z
([(6,)], [6, 4, 2], [6, 6, 6])
p[2]==X,Y,Z
([(6,)], [4, 2, 6], [6, 6, 6])
Why is the minimum 6? 2. Why has the value bound to Z been repeated 3 times? 3. What exactly is the purpose of 'order_by' in relation to the list from which a minimum value is found? 4. Why does the output change based upon if there are multiple values in the 'order_by' list; why does a specific value--6, in this case--in the 'order_by' list effect the output as it has? Another example:
(p[X]==min_(Y, order_by=Z)) <= Y.in_((4,6,2)) & Z.in_((0,))
Output:
p[0]==X,Y,Z
([(6,)], [4, 6, 2], [0, 0, 0])
p[1]==X,Y,Z
([(6,)], [2, 6, 4], [0, 0, 0])
p[2]==X,Y,Z
([(2,)], [2, 6, 4], [0, 0, 0])
Why did the output of X change--from 6 to 2--based upon the indexed provided? Even though the output was wrong in the previous example, at least it was consistent for the indexes used; with there only being one min/max, this makes since.
I at least get to see the output using the min_, max_, sum_ functions; but, I am lost when it comes to rank_ and running_sum_. I follow a similar process when defining my function:
(p[X]==running_sum_(Z, group_by=Z, order_by=Z)) <= Z.in_((43,34,65))
I try to view the output:
p[0]==X
I get the error:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/UserList.py", line 16, in repr
def repr(self): return repr(self.data)
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 109, in data
self.todo.ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 566, in ask
self._data = Body(self.pre_calculations, self).ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 686, in ask
self._data = literal.lua.ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 909, in _
invoke(subgoal)
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 664, in invoke
todo.do() # get the thunk and execute it
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 640, in do
self.thunk()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 846, in
aggregate.complete(base_subgoal, subgoal))
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 820, in complete
result = [ tuple(l.terms) for l in list(base_subgoal.facts.values())]
AttributeError: 'bool' object has no attribute 'values'
What does this mean? What was done incorrectly? What are the relations shared by the running_sum_ (and rank_) parameters--'group_by' and 'order_by'?
As there seems to be no examples on the web, 2 or 3 short examples of rank_ and running_sum_ usage would be greatly appreciated.
Aggregate clauses are solved in 2 steps :
first resolve the unknowns in the clause, while ignoring the aggregate function
then apply the aggregate function on the result
Here is how you could write the first clause :
(p[None]==min_(Y, order_by=Y)) <= Y.in_((4,6,2))
The variable(s) in the bracket after p is used as the "group by" in SQL, and must also appear in the body of the clause. In this case, it does not vary, so I use None. The order_by variable is needed when you want to retrieve another value than the one you order by.
Let's say you want to retrieve the names of the youngest pupil in each class of a school. The base predicate would be pupil(ClassName, Name, Age).
+ pupil('1A', 'John', 8)
+ pupil('1B', 'Joe', 9)
The aggregate clause would be :
(younger[ClassName] == min_(Name, order_by= Age)) <= pupil(ClassName, Name, Age)
The query would then be :
(younger[ClassName]==X)

Resources