Tensorflow: python tf.gradients equivalent in C++ - tensorflow

What is the equivalent of Python function tf.gradients(loss, [var]) in C++? Thanks!

The equivalent function in C++ is tensorflow::AddSymbolicGradients(). You will need to obtain a tensorflow::Graph object representing your graph to use this function. However, adding gradients in C++ is still experimental, so beware that this function signature is subject to change.

Related

I have the code below which I want to translate into pytorch. I'm looking for a way to translate np.vectorize to any pytorch way in this case

I need to translate this code to pytorch. The code given below use np.vectorize. I am looking for a pytorch equivalent for this.
class SimplexPotentialProjection(object):
def __init__(self, potential, inversePotential, strong_convexity_const, precision = 1e-10):
self.inversePotential = inversePotential
self.gradPsi = np.vectorize(potential)
self.gradPsiInverse = np.vectorize(inversePotential)
self.precision = precision
self.strong_convexity_const = strong_convexity_const
The doc for numpy.vectorize clearly states that:
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.
Therefore, in order to convert your numpy code to pytorch you'll simply need apply potential and inversePotential in a loop over their tensor arguments.
However, that might be very inefficient. You would better re-implement your functions to act "natively" in a vectorized manner on tensors.

Calculation operations with the parameters of a TFLite quantized model

I am trying to implement image classification in hardware using the quantized Mobilenetv2 model taken from here. To do that, I first need to reproduce the inference process from the beginning to the end to make sure I understand the calculations/operations that are performed on the data.
The first target is the Conv fuction. I can see how it is being calculated, but there are several arguments that are passed to this function which I would like to know how they are produced: output_offset, output_multiplier,output_shift, output_activation_min, output_activation_max. I cannot find the previous function that calls the Conv() function with these parameters. This would hopefully give me an insight of how these arguments are generated. Could someone point me to the right line of the source code?
Another gap in the sourcecode is at the interpreter.invoke() function. I wish to track and see what happens next, but can not find the soursecode that implements the invoke() function. The help would be greatly appreciated!
If you want to know how the conv reference code is used you can read the code for the conv operator.
The python interpreter uses swig to call the C++ intepreter.
Hope this helps.

What exactly qualifies as a 'Tensor' in TensorFlow?

I am new to TensorFlow and just went through the eager execution tutorial and came across the tf.decode_csv function. Not knowing about it, I read the documentation. https://www.tensorflow.org/api_docs/python/tf/decode_csv
I don't really understand it.
The documentation says 'records: A Tensor of type string.'
So, my question is: What qualifies as a 'Tensor'?
I tried the following code:
dec_res = tf.decode_csv('0.1,0.2,0.3', [[0.0], [0.0], [0.0]])
print(dec_res, type(dec_res))
l = [[1,2,3],[4,5,6],[7,8,9]]
r = tf.reshape(l, [9,-1])
print(l, type(l))
print(r, type(r))
So the list dec_res contains tf.tensor objects. That seems reasonable to me. But is an ordinary string also a 'Tensor' according to the documentation?
Then I tried something else with the tf.reshape function. In the documentation https://www.tensorflow.org/api_docs/python/tf/reshape it says that 'tensor: A Tensor.' So, l is supposed to be a tensor. But it is not of type tf.tensor but simply a python list. This is confusing.
Then the documentation says
Returns:
A Tensor. Has the same type as tensor.
But the type of l is list where the type of r is tensorflow.python.framework.ops.Tensor. So the types are not the same.
Then I thought that TensorFlow is very generous with things being a tensor. So I tried:
class car(object):
def __init__(self, color):
self.color = color
red_car = car('red')
#test_reshape = tf.reshape(red_car, [1, -1])
print(red_car.color) # to check, that red_car exists.
Now, the line in comments results in an error.
So, can anyone help me to find out, what qualifies as a 'Tensor'?
P.S.: I tried to read the source code of tf.reshape as given in the documentation
Defined in tensorflow/python/ops/gen_array_ops.py.
But this file does not exist in the Github repo. Does anyone know how to read it?
https://www.tensorflow.org/programmers_guide/tensors
TensorFlow, as the name indicates, is a framework to define and run
computations involving tensors. A tensor is a generalization of
vectors and matrices to potentially higher dimensions. Internally,
TensorFlow represents tensors as n-dimensional arrays of base
datatypes.
What you are observing commes from the fact that tensorflow operations (like reshape) can be built from various python types using the function tf.convert_to_tensor:
https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor
All standard Python op constructors apply this function to each of
their Tensor-valued inputs, which allows those ops to accept numpy
arrays, Python lists, and scalars in addition to Tensor objects

Equivalent of theano.tensor.switch in Tensorflow

Is there any function in Tensorflow which does the exact same thing as theano.tensor.switch(cond, ift, iff)?
In TensorFlow you can use tf.cond(https://www.tensorflow.org/api_docs/python/tf/cond). There are some examples in the documentation.
Edit: As you mentioned, this operation is not element wise, then it's equivalent is tf.where (https://www.tensorflow.org/api_docs/python/tf/where)

Program in GMP Library

In Sage, there is a function 'append'. One example is as follows.
A=[]
for i in range(100):
if(i%2==0):
A.append(i)
In libgmp is there any such kind of function where I can store mpz_t values?
No. GMP just provides the primitives for mpz_t creation, deletion, and mathmatical operations. Support for data structures is provided by the language that is using libgmp. Sage uses Python as its language for gluing together other libraries and your example is just an example of Python code.