Tensorflow: Find greater than pairs and stack along axis - python-3.6

The problem I have using tensorflow is as follows:
For one tensor X with dims n X m
X = [[x11,x12...,x1m],[x21,x22...,x2m],...[xn1,xn2...,xnm]]
I want to get an n X m X m tensor which are n m X m matrices
Each m X m matrix is the result of:
tf.math.greater(tf.reshape(x,(-1,1)), x) where x is a row of X
In words, for every row k in X, Im trying to get the pairs i,j where xki > xkj. This gives me a matrix, and then I want to stack those matrices along the first axis, to get a n m x m cube.
Example:
X = [[1,2],[4,3], [5,7]
Result = [[[False, False],[True, False]],[[False, True],[False, False]], [[False, False],[True, False]]]
Result has shape 3 X 2 X 2

Reshaping each row is the same as reshaping all rows. Try this:
def fun(X):
n, m = X.shape
X1 = tf.expand_dims(X, -1)
X2 = tf.reshape(X, (n, 1, m))
return tf.math.greater(X1, X2)
X = tf.Variable([[1,2],[4,3], [5,7]])
print(fun(X))
Output:
tf.Tensor(
[[[False False]
[ True False]]
[[False True]
[False False]]
[[False False]
[ True False]]], shape=(3, 2, 2), dtype=bool)

Related

R match variables from different lists

I want to write a loop, in which values from 3 different lists are put into another function
x = list(value1, value2, value3)
y = list(value1, value2, value3)
z = list(value1, value2, value3)
Example: function (x1, y1, z1)
I want to insert the values from the same column(x1, y1, z1 .... x3, y3, z3)of the different lists into the function. How could I do that?
This could be achieved using e.g. mapply:
x <- list(1, 2, 3)
y <- list(4, 5, 6)
z <- list(7, 8, 9)
mapply(function(vx, vy, vz) vx + vy + vz, x, y, z)
#> [1] 12 15 18
or using purrr::pmap:
purrr::pmap(list(x, y, z), function(vx, vy, vz) vx + vy + vz)
You get value1 from x list by using x[[1]]. Likewise, you get velue3 from y list by using y[[3]], etc.
You can then use these values in your function in various ways, depending on what your function has to do with these values.
#stefan has shown two efficient ways to do that.
If your function has to take three values from the same indices to result in a single value, such as mean, you can combine the list to become a matrix with three columns and three rows, so that you can apply this function to each column or each row with single index.
x = list(c(-3,0,2))
y = list(c(10,-1,20))
z = list(c(0.7, 0.5, 0.9))
myData = cbind(x,y,z)
myData
# x y z
# [1,] -3 10 0.7
# [2,] 0 -1 0.5
# [3,] 2 20 0.9
To get the mean of the value1s :
mean(myData[1,])
# [1] 2.566667
Suppose you want to compute the means and the standard deviations of each row, you can write a function to do that
myfun = function(x){
Mean = apply(x, 1, mean)
SD = apply(x, 1, sd)
result = rbind(Mean, SD)
return(result)
}
and then apply the function to your matrix:
myfun(myData)
# [,1] [,2] [,3]
# Mean 2.566667 -0.1666667 7.633333
# SD 6.698010 0.7637626 10.723961
This is just an example. Many other, more efficient, ways are possible.

R - Merge Matrices on a common column

I am trying to merge a list of matrices all by the first column like this:
a x x
a q q
b y y
c z z
d w w x x x x
e v v q q q q
e r r y y y y
----------> z z z z
a x x w w w w
a q q v v v v
b y y r r r r
c z z
d w w
e v v
e r r
I would like to use the first column to combine the matrices but it does not need to be in the resulting matrix. The thing that is challenging me is the fact that there are multiple instances of the same value in the first row (a and e)
I have been looking around but unable to find any solutions that account for the same values in the column that the matrices are being joined with. With my current code (shown bellow) I get something like:
x x x x
q q q q
x x x x
q q q q
x x x x
q q q q
y y y y
z z z z
w w w w
v v v v
r r r r
v v v v
r r r r
v v v v
r r r r
I cant seem to find out why the duplicate rows are appearing but it has something to do with the length of list so I am assuming it takes place in the merge function.
mergeM <- function(list){ # list is a list of matrices
len = length(list)
mat = merge(list[[1]],list[[2]],by.x = "V1", by.y = "V1", all = TRUE)
if(len >2){
for(i in 3:len){
mat = merge(mat,list[[i]],by.x = "V1", by.y = "V1", all = TRUE)
}
}
mat = mat[,-1]
return(mat)
}# end function

vector multiplication in pytorch

In pytorch. I want to multiply each vector of Matrix A by each vector of Matrix B:
A = M x N
B = L x N
result = (M x L) x N
Try:
result = A[:, None, :] * B[None, ...]
I tried this, and it is working:
torch.einsum('bj,aj->baj', input_unfolded, self.weights)
You can design any multiplication pattern using this approach.

sorting correlation matrix R

I have created a correlation matrix in R, using the cor function.
I would like to extract the 10 largest (closest to 1) and 10 smallest (closest to -1) from this matrix with the corresponding row and column indices.
Here is a sample code of how I am obtaining the correlation matrix:
xs = rnorm(10000)
ys = rnorm(10000)
zs = rnorm(10000)
cor1 <- cor(data.frame(xs,ys,zs))
I obtain:
xs ys zs
xs 1.00000000 -0.01077785 -0.01308803
ys -0.01077785 1.00000000 0.01176254
zs -0.01308803 0.01176254 1.00000000
Any suggestions?
Thanks!
If mat is your correlation matrix, you can get the locations of the top and bottom 10 like this...
min10 <- which(mat<=sort(mat)[10], arr.ind = TRUE)
max10 <- which(-mat<=sort(-mat)[10], arr.ind = TRUE)
Each of these is a n x 2 matrix, where the columns are the row and column numbers of mat for those elements meeting the criteria.

How to write and calculate Sum/ Product (of a function) in R

Assuming that I have a function, let's say f(x).
How can I write the product or sum of this function for given limits in x.
For instance product of f for x=1 until x=5
f(1)*f(2)*f(3)*f(4)*f(5)
Additionally I need to figure this out for sums/double sums.
Consider f(x,y) and the sum while x runs from 1 to 3 and y runs from 0 to x-1.
If written in mathematica, it would be this:
Sum[f[x, y], {x, 1, 3}, {y, 0, x - 1}]
and the output would be this
f[1, 0] + f[2, 0] + f[2, 1] + f[3, 0] + f[3, 1] + f[3, 2]
f is not defined for simplicity.
EDIT: example as requested:
f <- function (x,y) {
x + 2*y
}
Calculate sum where x runs from 1 to 3 and y runs from 0 to x-1.
(this is equal to 22 btw)
You can do this:
f <- function (x,y) {
x + 2*y
}
)
#calculate f for all combinations
tmp <- outer(1:3, 0:2, f)
#discard undesired combinations and sum
sum(tmp[lower.tri(tmp, diag = TRUE)])
#[1] 22
Alternatively you can use a loop to create the desired combinations only. This is much slower:
inds <- lapply(1:3, function(x) data.frame(x = x, y = 0:(x-1)))
inds <- do.call(rbind, inds)
sum(do.call(f, inds))
#[1] 22

Resources