index rows from 2D tensor - tensorflow

Suppose I have a 2D tensor A of floats, and a 1D tensor B of ints. The numbers in B represent indices to the rows of A. How do I efficiently perform a lookup of this indices inside a tensorflow graph?

Have you already tried tf.gather() ?
See also this question: TensorFlow: using a tensor to index another tensor

Related

Is there a differentiable way to cast a tensor into int64?

I'm creating a layer which uses its input tensors to create a SparseTensor, i.e., the input tensors are the respective indices and values of the SparseTensor.
Since:
indices: A 2-D int64 tensor of shape
And because tf.cast(x, tf.int64) is not differentiable, I'm not sure if this is achievable.
Another option is to find a turnaround based on tf.round(), but SparseTensor won't accept a different type of tensor as indices.
Is there a way to cast a tensor to integer and not having None for gradient ?
How can I create a SparseTensor using previous layers outputs, which are float tensors ?

tensorflow reshaping convolutional filters for visualization

I have a 4D tensor of filter/kernel weights (of convolutional layer).
They're being passed to the subsequent operation with shape [5,5,3,32], 32 RGB 5x5 filters.
to collect their values for monitoring/analysis/storage using tf.summary.image I need to reshape this tensor into the shape [32,5,5,3], to then view/store each of the 32 filters as individual images of [5,5,3]
is this possible purely using tf.reshape()? or do I need to do multiple tensor transformations?
You need transpose instead of reshape, tf.transpose(t, (3,0,1,2)) should do what you need (suppose t is your tensor here), which shifts the last axis as the first axis.

regarding converting 1D to 2D in tensorflow

Given a one dimensional data, how to re-shape it to 2D matrix so that I can leverage the existing 2D convolution in tensorflow?
I have to assume that you are talking about an array. If that is correct then you should be able to convert it using reshape.
from the tensorflow site
https://www.tensorflow.org/api_docs/python/tf/reshape

How to select entries based on index returned by tf.argmin in Tensorflow

Given two matrices of the same shape Loss and Weights.
I need to return the result of tf.reduce_min(Loss, axis=0) and also the corresponding weights from the Weights matrix (selected from the same indices where tf.reduce_min selected its results).
I can use tf.argmin(Loss, 0) to find the indices with the minimal values. How do I use these indices to get the corresponding values from the Weights matrix? I think is possible to implement using tf.gather, but the results won't be differentiable. Any known solutions to this?
Gather is differentiable, on the values it returns. It wont be differentiable w.r.t. the indices, because that is mathematically impossible.

Use coo_matrix in TensorFlow

I'm doing a Matrix Factorization in TensorFlow, I want to use coo_matrix from Spicy.sparse cause it uses less memory and it makes it easy to put all my data into my matrix for training data.
Is it possible to use coo_matrix to initialize a variable in tensorflow?
Or do I have to create a session and feed the data I got into tensorflow using sess.run() with feed_dict.
I hope that you understand my question and my problem otherwise comment and i will try to fix it.
The closest thing TensorFlow has to scipy.sparse.coo_matrix is tf.SparseTensor, which is the sparse equivalent of tf.Tensor. It will probably be easiest to feed a coo_matrix into your program.
A tf.SparseTensor is a slight generalization of COO matrices, where the tensor is represented as three dense tf.Tensor objects:
indices: An N x D matrix of tf.int64 values in which each row represents the coordinates of a non-zero value. N is the number of non-zeroes, and D is the rank of the equivalent dense tensor (2 in the case of a matrix).
values: A length-N vector of values, where element i is the value of the element whose coordinates are given on row i of indices.
dense_shape: A length-D vector of tf.int64, representing the shape of the equivalent dense tensor.
For example, you could use the following code, which uses tf.sparse_placeholder() to define a tf.SparseTensor that you can feed, and a tf.SparseTensorValue that represents the actual value being fed :
sparse_input = tf.sparse_placeholder(dtype=tf.float32, shape=[100, 100])
# ...
train_op = ...
coo_matrix = scipy.sparse.coo_matrix(...)
# Wrap `coo_matrix` in the `tf.SparseTensorValue` form that TensorFlow expects.
# SciPy stores the row and column coordinates as separate vectors, so we must
# stack and transpose them to make an indices matrix of the appropriate shape.
tf_coo_matrix = tf.SparseTensorValue(
indices=np.array([coo_matrix.rows, coo_matrix.cols]).T,
values=coo_matrix.data,
dense_shape=coo_matrix.shape)
Once you have converted your coo_matrix to a tf.SparseTensorValue, you can feed sparse_input with the tf.SparseTensorValue directly:
sess.run(train_op, feed_dict={sparse_input: tf_coo_matrix})