3

It is such a long title, but hopefully I will be able to explain myself properly in a few sentences:

I am trying to minimize a given score function using Tensorflow inspired by what was published in Minimize a function of one variable in Tensorflow. The value for such score function is obtained through making a call to a Matlab script which needs to be provided with only one parameter (which is related to the input variable, a tensor).

To do so I am using the beta version of Tensorflow 2.0, which includes a feature known as eager execution which allows to access to the contents of each tensor without needing to run any session whatsoever.

Here you may find a scratch of my code:

import tensorflow as tf
import numpy as np
eng = matlab.engine.start_matlab()

def costFunction():
    z = tf.add(x,y).numpy()
    H = np.asarray(eng.matlabfunction(matlab.double(z.tolist()),...)) # There are other parameters (Python lists) to be passed as arguments to my Matlab script alongside them, not included for the sake of simplicity
    h = tf.convert_to_tensor(...) # Here I retrieve those elements from matrix H which I actually aim to maximize
    return h

x = tf.Variable(initial_value=tf.zeros([6,N], tf.float64), trainable=True)
opt = tf.optimizers.Adam(learning_rate=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
iters = 1000
for i in range(iters):
    train = opt.minimize(costFunction, tunedPhases)
    if i % 100 == 0:
        print("Iteration {}, loss: {}".format(i+1, costFunction())) 

Sadly, this solution does still not work out as I receive the following error message as output:

ValueError: No gradients provided for any variable: ['Variable:0'].

After a exhaustive search, I think this problem is related to this old post (TensorFlow: 'ValueError: No gradients provided for any variable'), which was solved by doing the corresponding operations from cost function directly to the tensors. However, I have no other option but to invoke this matlabfunction and use its output as the output of my cost function.

Do you have any ideas about how to overcome this?

Many thanks in advance, and may you all have a nice week!

ailoher
  • 71
  • 6
  • tensorflow is not going to be able to calculate gradients for a function in matlab so you can't use this directly as the loss function as is done in the example you linked to. What does the matlab script do? Is there no way to replicate in tensorflow? – Stewart_R Jul 31 '19 at 07:08

0 Answers0