9951 explained code solutions for 126 technologies


python-tensorflowHow do I use a TensorFlow activation function in Python?


To use a TensorFlow activation function in Python, first import the tf.nn module:

import tensorflow as tf

Then, create a placeholder for the input data:

x = tf.placeholder(tf.float32, shape=[None, input_dim])

Next, define the activation function, for example, a ReLU activation:

activation = tf.nn.relu(x)

Finally, run a session to execute the activation function:

with tf.Session() as sess:
    result = sess.run(activation, feed_dict={x: input_data})

The output of the activation function is stored in the result variable.

Code explanation

  1. import tensorflow as tf: imports the TensorFlow library.
  2. x = tf.placeholder(tf.float32, shape=[None, input_dim]): creates a placeholder for the input data.
  3. activation = tf.nn.relu(x): defines the activation function (in this example, a ReLU activation).
  4. with tf.Session() as sess:: creates a session to execute the activation function.
  5. result = sess.run(activation, feed_dict={x: input_data}): runs the activation function and stores the output in the result variable.

Helpful links

Edit this code on GitHub