1

I am working with the DCGAN code. I need to modify the reward that is given to one of the neural nets by adding a function that would take the output of this neural net, analyse it, and issue a penalty on it. So my loss function would look like:

self.g_loss = self.g_loss + self.penalty

Problem is

  • this penalty function only takes the numpy arrays as an input (I have no way of modifying this),

  • neural network output is a tf.tensor,

  • and as the values haven't been assigned to the neural net yet (technically it hasn't been built yet) I can't run neither .eval() nor sess.run().

So how would I convert a tensorflow tensor into numpy array in this case?

1
  • If you expect to train your neural network you'll need to also provide a gradient for your penalty. If you do have it, you probably need to add a custom op to tensorflow to take care of this... Commented Oct 25, 2017 at 16:04

1 Answer 1

2

Tensorflow has tf.py_func for wrapping Python functions and passing tensors to them. However, you can't then use this loss function to train the network, because Tensorflow doesn't automatically differentiate numpy code.

Luckily for you, autograd does automatically differentiate numpy code. If you use that, in another tf.pyfunc call, you can get gradients, which you can then put back into the tensorflow graph on the backward pass.

Here's an example of how you can do it all in this gist.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply. Currently I am using g_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1) \ .minimize(self.g_loss, var_list=self.g_vars) as my optimiser. How would I keep that for all (or at least tf part of) my loss?
Also running the tf-autograd.py throws the following error: InvalidArgumentError (see above for traceback): 0-th value returned by pyfunc_1 is double, but expects float [[Node: PyFunc_1 = PyFunc[Tin=[DT_DOUBLE], Tout=[DT_FLOAT], token="pyfunc_1", _device="/job:localhost/replica:0/task:0/cpu:0"](Variable/read/_3)]] .
Are you running this on gpu? In that case Tensorflow defaults to float32, while numpy is default float64.
Yes, I run on GPU, how do I fix this discrepancy?
I've updated the gist so it'll work on a system with gpus, but the only way I could get it to work was by enforcing float32 and cpu device usage almost everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.