Similar to tf.py_func and tf.py_function but it doesn't require defining
the inputs or the dtypes of the outputs a priori.
In Eager mode it would convert the tf.Tensors to np.arrays before passing to
func and then convert back the outputs from np.arrays to tf.Tensors.
In Graph mode it would create different tf.py_function for each combination
of dtype of the inputs and cache them for reuse.
Insteadofdoing:defsum(x):returnnp.sum(x)inputs=tf.constant([3,4])outputs=tf.py_function(sum,inputs,Tout=[tf.int64])inputs=tf.constant([3.,4.])outputs=tf.py_function(sum,inputs,Tout=[tf.float32])#### Do:@eager_utils.np_functiondefsum(x):returnnp.sum(x)inputs=tf.constant([3,4])outputs=sum(inputs)# Infers that Tout is tf.int64inputs=tf.constant([3.,4.])outputs=sum(inputs)# Infers that Tout is tf.float32# Output dtype is always float32 for valid input dtypes.@eager_utils.np_function(output_dtypes=np.float32)defmean(x):returnnp.mean(x)# Output dtype depends on the input dtype.@eager_utils.np_function(output_dtypes=lambdax:(x,x))defrepeat(x):returnx,xwithcontext.graph_mode():outputs=sum(tf.constant([3,4]))outputs2=sum(tf.constant([3.,4.]))sess.run(outputs)# np.array(7)sess.run(outputs2)# np.array(7.)withcontext.eager_mode():inputs=tf.constant([3,4])outputs=sum(tf.constant([3,4]))# tf.Tensor([7])outputs=sum(tf.constant([3.,4.]))# tf.Tensor([7.])
Args:
func: A numpy function, that takes numpy arrays as inputs and return numpy
arrays as outputs.
output_dtypes: Optional list of dtypes or a function that maps input dtypes
to output dtypes. Examples: output_dtypes=[tf.float32],
output_dtypes=lambda x: x (outputs have the same dtype as inputs). If it
is not provided in Graph mode the func would be called to infer the
output dtypes.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf_agents.utils.eager_utils.np_function\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/utils/eager_utils.py#L489-L624) |\n\nDecorator that allow a numpy function to be used in Eager and Graph modes. \n\n tf_agents.utils.eager_utils.np_function(\n func=None, output_dtypes=None\n )\n\nSimilar to `tf.py_func` and [`tf.py_function`](https://www.tensorflow.org/api_docs/python/tf/py_function) but it doesn't require defining\nthe inputs or the dtypes of the outputs a priori.\n\nIn Eager mode it would convert the tf.Tensors to np.arrays before passing to\n`func` and then convert back the outputs from np.arrays to tf.Tensors.\n\nIn Graph mode it would create different tf.py_function for each combination\nof dtype of the inputs and cache them for reuse.\n**Note:** In Graph mode: if `output_dtypes` is not provided then `func` would be called with `np.ones()` to infer the output dtypes, and therefore `func` should be stateless. \n\n Instead of doing:\n\n def sum(x):\n return np.sum(x)\n inputs = tf.constant([3, 4])\n outputs = tf.py_function(sum, inputs, Tout=[tf.int64])\n\n inputs = tf.constant([3., 4.])\n outputs = tf.py_function(sum, inputs, Tout=[tf.float32])\n\n #### Do:\n\n\n @eager_utils.np_function\n def sum(x):\n return np.sum(x)\n\n inputs = tf.constant([3, 4])\n outputs = sum(inputs) # Infers that Tout is tf.int64\n\n inputs = tf.constant([3., 4.])\n outputs = sum(inputs) # Infers that Tout is tf.float32\n\n # Output dtype is always float32 for valid input dtypes.\n @eager_utils.np_function(output_dtypes=np.float32)\n def mean(x):\n return np.mean(x)\n\n # Output dtype depends on the input dtype.\n @eager_utils.np_function(output_dtypes=lambda x: (x, x))\n def repeat(x):\n return x, x\n\n with context.graph_mode():\n outputs = sum(tf.constant([3, 4]))\n outputs2 = sum(tf.constant([3., 4.]))\n sess.run(outputs) # np.array(7)\n sess.run(outputs2) # np.array(7.)\n\n with context.eager_mode():\n inputs = tf.constant([3, 4])\n outputs = sum(tf.constant([3, 4])) # tf.Tensor([7])\n outputs = sum(tf.constant([3., 4.])) # tf.Tensor([7.])\n\nArgs:\nfunc: A numpy function, that takes numpy arrays as inputs and return numpy\narrays as outputs.\noutput_dtypes: Optional list of dtypes or a function that maps input dtypes\nto output dtypes. Examples: output_dtypes=\\[tf.float32\\],\noutput_dtypes=lambda x: x (outputs have the same dtype as inputs). If it\nis not provided in Graph mode the `func` would be called to infer the\noutput dtypes.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A wrapped function that can be used with TF code. ||\n\n\u003cbr /\u003e"]]