How do I find the max value in tf.Tensor? - python

How can I find the max value in each element so that I get 2, 4, 6, 8?
import tensorflow as tf
a = tf.constant([
[[1, 2]], [[3, 4]],
[[5, 6]], [[7, 8]]])
I tried the following code:
tf.reduce_max(a, keepdims=True)
but that just gives me 8 as output whilst ignoring the rest.

You have to change your axis parameter to -1 like this:
import tensorflow as tf
a = tf.constant([
[[1, 2]], [[3, 4]],
[[5, 6]], [[7, 8]]])
print(tf.reduce_max(a, axis=-1, keepdims=False))
'''
tf.Tensor(
[[2]
[4]
[6]
[8]], shape=(4, 1), dtype=int32)
'''
since you have a 3D-tensor and want to access the last dimension.

Related

Sqrt only one column in numpy

I have the following 2D array, and want to take a square root of only column A.
import numpy as np
a = np.matrix([[1, 2], [3, 4], [5, 6], [7, 8]])
a
matrix([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
This is giving me sqrt of two columns. How can I only take a square root of column A?
b = np.sqrt(a[:, [0, 1]])
b
matrix([[1. , 1.41421356],
[1.73205081, 2. ],
[2.23606798, 2.44948974],
[2.64575131, 2.82842712]])
Use out to do in place operation
import numpy as np
a = np.matrix([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=np.float64)
np.sqrt(a, where=[True, False],out=a)
Output:
[[1. 2. ]
[1.73205081 4. ]
[2.23606798 6. ]
[2.64575131 8. ]]
Try it online
I can't comment so I'm just going to have to put my answer here:
You can do it in-place with the following:
a = np.matrix([[1, 2], [3, 4], [5, 6], [7, 8]], dtype=np.float)
a[:, 0] = np.sqrt(a[:, 0])
Do note that because your initial types were int, that you must specify the dtype=np.float if not it will all get cast to ints.
if you want only first column, you must this
b = np.sqrt(a[:, [0]])
if you want all columns but first columns sqrt, you try this
df=pd.DataFrame(a)
df.loc[:,0]=df.loc[:,0].apply(np.sqrt)

extracting subtensor from a tensor according to an index tensor

I have this tensor:
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
and I have this index tensor:
tensor([0, 1])
and what I want to get is the subtensors according to dim 1 and the corresponding indices in the index tensor, that is:
tensor([[1, 2],
[7, 8]])
tried to use torch.gather() function and advanced indexing with no success, can anyone help?
You are implicitly using the index of each value of your index tensor. They just happen to be the same as the values. If you want to walk through the first level, elements of the tensor, you can use torch.arange to construct the first level indices.
import torch
from torch import tensor
t = tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
ix = tensor([0, 1])
ix0 = torch.arange(0, ix.shape.numel())
t[ix0, ix]
# returns:
tensor([[1, 2],
[7, 8]])

Slicing a matrix by an index matrix in TensorFlow

I have an n-by-m matrix X and an n-by-r index matrix I. I am wondering what are the relevant TensorFlow operators that allow me to get an n-by-r matrix R such that R[i,j] = X[i,I[i,j]]. As an example, let's say
X = tf.constant([[1,2,3],
[4,5,6],
[7,8,9]])
I = tf.constant([[1,2],
[1,0],
[0,2]])
The desired result would be a tensor
R = [[2, 3],
[5, 4],
[7, 9]]
I tried to use each column of the matrix I as the index and do tf.diag_part(tf.gather(X', index)), which seems to give me one column of R if I has the same number of row as X. For example,
idx = tf.transpose(I)[0] #[1,1,0]
res = tf.diag_part(tf.gather(tf.transpose(X), idx))
# res will be [2,5,7], i,e, first colum of R
Another attempt:
res = tf.transpose(tf.gather(tf.transpose(X), I),[0,2,1])
print(res.eval())
array([[[2, 3],
[5, 6],
[8, 9]],
[[2, 1],
[5, 4],
[8, 7]],
[[3, 1],
[6, 4],
[7, 9]]], dtype=int32)
From here i just need to be able to select the "diagonal entries" res[0,0], res[1,1] and res[2,2] to get R. I get stuck here though...
Use tf.gather with batch_dims argument:
res = tf.gather(X, I, batch_dims=1)

Reorganizing a 3d numpy array

I've tried and searched for a few days, I've come closer but need your help.
I have a 3d array in python,
shape(files)
>> (31,2049,2)
which corresponds to 31 input files with 2 columns of data with 2048 rows and a header.
I'd like to sort this array based on the header, which is a number, in each file.
I tried to follow NumPy: sorting 3D array but keeping 2nd dimension assigned to first , but i'm incredibly confused.
First I try to setup get my headers for the argsort, I thought I could do
sortval=files[:][0][0]
but this does not work..
Then I simply did a for loop to iterate and get my headers
for i in xrange(shape(files)[0]:
sortval.append([i][0][0])
Then
sortedIdx = np.argsort(sortval)
This works, however I dont understand whats happening in the last line..
files = files[np.arange(len(deck))[:,np.newaxis],sortedIdx]
Help would be appreciated.
Another way to do this is with np.take
header = a[:,0,0]
sorted = np.take(a, np.argsort(header), axis=0)
Here we can use a simple example to demonstrate what your code is doing:
First we create a random 3D numpy matrix:
a = (np.random.rand(3,3,2)*10).astype(int)
array([[[3, 1],
[3, 7],
[0, 3]],
[[2, 9],
[1, 0],
[9, 2]],
[[9, 2],
[8, 8],
[8, 0]]])
Then a[:] will gives a itself, and a[:][0][0] is just the first row in first 2D array in a, which is:
a[:][0]
# array([[3, 1],
# [3, 7],
# [0, 3]])
a[:][0][0]
# array([3, 1])
What you want is the header which are 3,2,9 in this example, so we can use a[:, 0, 0] to extract them:
a[:,0,0]
# array([3, 2, 9])
Now we sort the above list and get an index array:
np.argsort(a[:,0,0])
# array([1, 0, 2])
In order to rearrange the entire 3D array, we need to slice the array with correct order. And np.arange(len(a))[:,np.newaxis] is equal to np.arange(len(a)).reshape(-1,1) which creates a sequential 2D index array:
np.arange(len(a))[:,np.newaxis]
# array([[0],
# [1],
# [2]])
Without the 2D array, we will slice the array to 2 dimension
a[np.arange(3), np.argsort(a[:,0,0])]
# array([[3, 7],
# [2, 9],
# [8, 0]])
With the 2D array, we can perform 3D slicing and keeps the shape:
a[np.arange(3).reshape(-1,1), np.argsort(a[:,0,0])]
array([[[3, 7],
[3, 1],
[0, 3]],
[[1, 0],
[2, 9],
[9, 2]],
[[8, 8],
[9, 2],
[8, 0]]])
And above is the final result you want.
Edit:
To arange the 2D arrays:, one could use:
a[np.argsort(a[:,0,0])]
array([[[2, 9],
[1, 0],
[9, 2]],
[[3, 1],
[3, 7],
[0, 3]],
[[9, 2],
[8, 8],
[8, 0]]])

how to explain the output of tf.rank in tensorflow

I am new in tensorflow and have a question about tf.rank method.
In the doc https://www.tensorflow.org/api_docs/python/tf/rank there is a simple example about the tf.rank:
# shape of tensor 't' is [2, 2, 3]
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
tf.rank(t) # 3
But when I run the code below:
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
print(tf.rank(t)) # 3
I get output like:
Tensor("Rank:0", shape=(), dtype=int32)
Why can I get the output of "3"?
As I said in the comments of this question, tf.rank(t) creates a tensor in charge of evaluating the rank of tensor t. If you use the python print() function, it just prints information about the tensor itself.
Let's assign the tf.rank(t) tensor to a variable rank (as suggested by #Picnix_) and evaluate its value under a tf.Session():
import tensorflow as tf
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
rank = tf.rank(t)
with tf.Session() as sess:
rank_value = sess.run(rank)
print(rank_value) # Outputs --> 3
So, rank_value is the variable containing the value of tensor rank, and as documentation suggest its value is 3. Hope this puts some light on how tensorflow works.

Categories