Python - tensorflow.device() Last Updated : 06 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. device() is used to explicitly specify the device in which operation should be performed. Syntax: tensorflow.device( device_name ) Parameters: device_name: It specifies the device name to be used in this context. Returns: It returns a context manager that specifies the default device to use for newly created ops. Example 1: Python3 # Importing the library import tensorflow as tf # Initializing Device Specification device_spec = tf.DeviceSpec(job ="localhost", replica = 0, device_type = "CPU") # Printing the DeviceSpec print('Device Spec: ', device_spec.to_string()) # Enabling device logging tf.debugging.set_log_device_placement(True) # Specifying the device with tf.device(device_spec): a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) c = tf.matmul(a, b) Output: Device Spec: /job:localhost/replica:0/device:CPU:* Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0 Example 2: In this example device specification specifies GPU to be used but system couldn't find GPU so it will run the operation on CPU. Python3 # Importing the library import tensorflow as tf # Initializing Device Specification device_spec = tf.DeviceSpec(job ="localhost", replica = 0, device_type = "GPU") # Printing the DeviceSpec print('Device Spec: ', device_spec.to_string()) # Enabling device logging tf.debugging.set_log_device_placement(True) # Specifying the device with tf.device(device_spec): a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) c = tf.matmul(a, b) Output: Device Spec: /job:localhost/replica:0/device:GPU:* Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0 Comment More infoAdvertise with us Next Article Python - tensorflow.identity() A aman neekhara Follow Improve Article Tags : Machine Learning Tensorflow Python Tensorflow-math-functions Practice Tags : Machine Learning Similar Reads Python - tensorflow.DeviceSpec TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. DeviceSpec represents the specification of TensorFlow device. This specification might be partial. If a DeviceSpec is partially specified, it will be merged with other 1 min read Python - tensorflow.eye() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. tensorflow.eye() is used to generate identity matrix. Syntax: tensorflow.eye( num_rows, num_columns, batch_shape, dtype, name) Parameters: num_rows: It is int32 scalar 2 min read Python - tensorflow.DeviceSpec.__eq__() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. __eq__() is used to check the equality of two DeviceSpec objects. Syntax: tensorflow.DeviceSpec.__eq__( other ) Parameters: other: It is a DeviceSpec object. Returns: It 2 min read Python - tensorflow.identity() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. identity() returns a Tensor with the same shape and contents as input. Syntax: tensorflow.identity(input, name) Parameters: input: It is a Tensor.name(optional): It def 2 min read Python - tensorflow.DeviceSpec.replace() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. replace() is used to override the specification of DeviceSpec object and get the new object. Syntax: tensorflow.DeviceSpec.replace(**kwargs) Parameters: **kwargs: This m 1 min read Python - tensorflow.gather() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. gather() is used to slice the input tensor based on the indices provided. Syntax: tensorflow.gather( params, indices, validate_indices, axis, batch_dims, name) Paramete 2 min read Like