Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

TF Basics

Overview
Tensors
The most important attributes of a tf.Tensor are its shape and dtype :

Tensor.shape : tells you the size of the tensor along each of its axes.
Tensor.dtype : tells you the type of all the elements in the tensor.

A number of specialized tensors are available:


see tf.Variable , tf.constant , tf.placeholder , tf.sparse.SparseTensor ,
and tf.RaggedTensor .

a = np.array([1, 2, 3])
b = tf.constant(a)a[0] = 4
print(b) # tf.Tensor([4 2 3], shape=(3,), dtype=int64)

Note: Typically, anywhere a TensorFlow function expects a Tensor as input, the function will
also accept anything that can be converted to a Tensor using tf.convert_to_tensor

For example, a list can be converted to a tensor as tf.convert_to_tensor([1,2,3])

Variables
Normal tf.Tensor objects are immutable. To store model weights (or other mutable state) in
TensorFlow use a tf.Variable .

Automatic differentiation
Gradient descent and related algorithms are a cornerstone of modern machine learning.

To enable this, TensorFlow implements automatic differentiation (autodiff), which uses calculus
to compute gradients. Typically you'll use this to calculate the gradient of a
model's error or loss with respect to its weights.

x = tf.Variable(1.0)
def f(x):
y = x**2 + 2*x - 5
return y

You might also like