In this Python tutorial, we will focus on how to fix the* attributeerror: module ‘tensorflow’ has no attribute** ‘Function’* in our model, and also we will look at some examples of how we can use the

**function in**

*tf.function()***. And we will cover these topics.**

*TensorFlow*- Attributeerror: module ‘tensorflow’ has no attribute ‘Function’
- Attributeerror: module ‘tensorflow’ has no attribute ‘numpy function’
- Attributeerror: module ‘tensorflow’ has no attribute ‘py_function’

**Table of Contents**show

## Attributeerror: module ‘tensorflow’ has no attribute ‘Function’

- In this section, we will discuss the attributeerror module ‘tensorflow’ has no attribute ‘function’.
- You can create script graphs with the
**tf.function().**It is a transformation tool that takes your Python code and turns it into dataflow graphs independent of Python. You must do this in order to use SavedModel, which will aid in the creation of portable and effective models.

**Example**:

```
import tensorflow as tf
@tf.Function
def f():
return m ** 3 + n
m = tf.constant([-6, -7])
n = tf.Variable([7, -6])
f()
```

Here is the implementation of the following given code

Here is the Solution to this error.

In this example, we will use the **tf.function()** and this function is used to compile a function into a callable TensorFlow graph.

**Syntax**:

Here is the Syntax of **tf.function()** in Python TensorFlow

```
tf.function(
func=None,
input_signature=None,
autograph=True,
jit_compile=None,
reduce_retracing=False,
experimental_implements=None,
experimental_autograph_options=None,
experimental_relax_shapes=None,
experimental_compile=None,
experimental_follow_type_hints=None
)
```

- It consists of a few parameters
**func**: It is a to-be-compiled function. In the event that func is None,**tf.function()**provides a decorator that only requires one argument: func. In other words, tf.function(func, input signature) and tf.function(func, input signature) are equal.**input_signature**: The shapes and dtypes of the Tensors that will be sent to this function are defined by a possible nested sequence of tf.TensorSpec objects. For each inferred input signature, a separate function is created if None. Every input to func must be a Tensor if an input signature is given; func cannot accept****kwargs**.**autograph**: Whether to apply autograph to func before drawing a graph. Python control flow statements that depend on data need and by default**autograph=True**.**jit_compile**: If True, compiles the function using XLA. XLA performs compiler optimizations, such as fusion, and attempts to emit more efficient code.**reduce_retracing**: When true**tf.function()**attempts to reduce the value of retracting by using the generic shape.**experimental_implements**: if it is given, the name of a function that this implements as an illustration. This is kept as an attribute in the inference function and can be found when the serialized function is processed.**experimental_autograph_options**: It is an optional parameter and it’s experimental function values.**experimental_compile**: It is a jit compile and deleted by this function.

**Example**:

```
import tensorflow as tf
@tf.function
def f():
return m ** 3 + n
m = tf.constant([-6, -7])
n = tf.Variable([7, -6])
f()
```

In the following given code first, we have imported the tensorflow library and then used the **tf.function()** and define the function with the **f()** name. Next, we will declare two tensors by using the **tf.constant()**, and** tf.Variable()**.

Here is the Output of the following given code.

This is how we can solve the attributeerror module ‘tensorflow’ has no attribute ‘Function’.

Read: Module ‘tensorflow’ has no attribute ‘div’

## Attributeerror: module ‘tensorflow’ has no attribute ‘numpy_Function’

- Here we will discuss the attributeerror module tensorflow has no attribute ‘numpy_function’.
- A Python function that takes numpy.ndarray objects as input and outputs a list of those items (or a single numpy. ndarray). This function’s parameter types must match those of the associated tf, and it must accept as many arguments as there are tensors.

**Example**:

```
import tensorflow as tf
import numpy as np
def new_numpy_function(x):
return np.sinh(x)
@tf.function(input_signature=[tf.TensorSpec(None, tf.float32)])
def tf_function(input):
result = tf.numpy_Function(new_numpy_function, [input], tf.float32)
return result * result
tf_function(tf.constant(2.))
```

Here is the execution of the following given code

Here is the Solution to this error.

In this example, we are going to use the **tf.numpy_function()**, and this function is enfolded in a python function and used as a TensorFlow operation.

**Syntax**:

```
tf.numpy_function(
func, inp, Tout, stateful=True, name=None
)
```

- It consists of a few parameters
**func**: It is a to-be-compiled function. In the event that func is None,**tf.function()**provides a decorator that only requires one argument: func. In other words, tf.function(func, input signature) and tf.function(func, input signature) are equal.**inp**: It is a list of tensors objects.**Tout**: A single tensorflow data type, if there is just one, or a list or tuple of tensorflow data types, specifying that the function returns.**stateful**:- By default, it takes the true value and the runtime to regard the function as stateless by setting this argument to False, you can do some optimizations.
- When given the same input and output, a stateless function returns the same result and has no side effects; its purpose is to have a return value.
- A stateful function’s behavior when given the stateful parameter False is unspecified. Since this is a stateful procedure, care should be exercised in particular while modifying the input arguments.

```
import tensorflow as tf
import numpy as np
def new_numpy_function(x):
return np.sinh(x)
@tf.function(input_signature=[tf.TensorSpec(None, tf.float32)])
def tf_function(input):
result = tf.numpy_function(new_numpy_function, [input], tf.float32)
return result * result
tf_function(tf.constant(2.))
```

In the following given code first, we have imported the tensorflow library and then define a function (new_numpy_function) within this function we have assigned the **‘x’** variable to it.

You can refer to the below Screenshot.

As you can see in the Screenshot, we have solved the attributerror module TensorFlow has no attribute numpy_Function.

Read: Attributeerror: module ‘tensorflow’ has no attribute ‘mul’

## Attributeerror: module ‘tensorflow’ has no attribute ‘py_func’

- You can execute any Python code inside of a TensorFlow graph using the tf.py func() operator.
- It is especially useful for wrapping unique NumPy operations for which there is currently no comparable TensorFlow operator. The
**Sess.run()**calls inside the graph can be replaced by the addition of**tf.py func()**.

**Example:**

```
import tensorflow as tf
def numpy_new_func(n):
n[0] = 0.
return n
tf.py_func(numpy_new_func, inp=[tf.constant([2., 3.])],
Tout=tf.float32, stateful=False)
```

You can refer to the below Screenshot

Here is the Solution to this error.

In this example, we will use the **tf.compat.v1.py_func()** and it is a near but equivalent, TensorFlow tensor to the wrapped function rather than NumPy arrays, which can benefit from accelerators and give gradients.

**Syntax**:

Let’s have a look at the Syntax and understand the working of **the tf.compat.v1.py_func()** function in Python TensorFlow.

```
tf.compat.v1.py_func(
func, inp, Tout, stateful=True, name=None
)
```

Example:

```
import tensorflow as tf
def numpy_new_func(n):
n[0] = 0.
return n
tf.compat.v1.py_func(numpy_new_func, inp=[tf.constant([2., 3.])],
Tout=tf.float32, stateful=False)
```

You can refer to the below Screenshot.

This is how we can solve the attributeerror module tensorflow has no attribute py_func.

Also, take a look at some more Tensorflow tutorials.

- TensorFlow Learning Rate Scheduler
- Tensorflow convert string to int
- Batch Normalization TensorFlow
- TensorFlow feed_dict + 9 Examples
- TensorFlow next_batch + Examples

In this Python tutorial, we have focused on how to fix the* attributeerror: module tensorflow has no attribute** ‘Function’* in our model, and also we will look at some examples of how we can use the

**function in**

*tf.function()***. And we have covered these topics.**

*TensorFlow*- Attributeerror: module ‘tensorflow’ has no attribute ‘numpy function’
- Attributeerror: module ‘tensorflow’ has no attribute ‘py_function’

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.