TENSORFLOW'S HELLO WORLD

TENSORFLOW'S HELLO WORLD


How does TensorFlow work?

Computations are made with operations, also referred to as "ops," and are defined by TensorFlow as Graphs. Therefore, using TensorFlow is equivalent to specifying a series of operations in a Graph.An example of a TensorFlow graph is shown in the image below.Tensors over the edges of this graph include W, x, and b.

After calling Add and adding the outcome of the prior operator with b, MatMul is an operation over the tensors W and b. Each operation's resultant tensor crosses over with the one after it until the point at which the desired outcome is possible.

No alt text provided for this image

Importing TensorFlow


import tensorflow as tf
print(tf.__version__)
        

tf.function and AutoGraph

On calling the TensorFlow functions that construct new?tf.Operation?and?tf.Tensor?objects. As mentioned, each?tf.Operation?is a?node?and each?tf.Tensor?is an edge in the graph.

Lets add 2 constants to our graph. For example, calling tf.constant([2], name = 'constant_a') adds a single?tf.Operation?to the default graph. This operation produces the value 2, and returns a?tf.Tensor?that represents the value of the constant.

Notice:?tf.constant([2], name="constant_a") creates a new tf.Operation named "constant_a" and returns a tf.Tensor named "constant_a:0".


a = tf.constant([2],name = 'constant_a')
b = tf.constant([3],name = 'constant_b')        

TensorFlow Autograph is used to generate a TensorFlow static execution graph for the python functions that are annotated with tf.function. TensorFlow Autograph is instructed by the tf.function annotation to convert the function add into TensorFlow control flow, which defines the TensorFlow static execution graph.

@tf.function
def add(a,b):
    c = tf.add(a,b)
    print(c)
    return c

result = add(a,b)
tf.print(result[0])        

Even this silly example of adding 2 constants to reach a simple result defines the basis of TensorFlow. Define your operations (In this case our constants and?tf.add), define a Python function named?add?and decorate it with using the?tf.function?annotator.

What is the meaning of Tensor?

In TensorFlow all data is passed between operations in a computation graph, and these are passed in the form of Tensors, hence the name of TensorFlow.

The word?tensor?from new latin means "that which stretches". It is a mathematical object that is named "tensor" because an early application of tensors was the study of materials stretching under tension. The contemporary meaning of tensors can be taken as multidimensional arrays.

Defining multidimensional arrays using TensorFlow

Scalar = tf.constant(2
Vector = tf.constant([5,6,2])
Matrix = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
Tensor = tf.constant( [ [[1,2,3],[2,3,4],[3,4,5]] , [[4,5,6],[5,6,7],[6,7,8]] , [[7,8,9],[8,9,10],[9,10,11]] ] )


print ("Scalar (1 entry):\n %s " % Scalar)


print ("Vector (3 entries) :\n %s" % Vector)


print ("Matrix (3x3 entries):\n %s" % Matrix)


print ("Tensor (3x3x3 entries) :\n %s" % Tensor))        

tf.shape would return the shape of the data structure

Scalar.shape

Tensor.shape        

Demo with Tensorflow functions


Matrix_one = tf.constant([[1,2,3],[2,3,4],[3,4,5]]
Matrix_two = tf.constant([[2,2,2],[2,2,2],[2,2,2]])


@tf.function
def add():
? ? add_1_operation = tf.add(Matrix_one, Matrix_two)
? ? return add_1_operation






print ("Defined using tensorflow function :")
add_1_operation = add()
print(add_1_operation)
print ("Defined using normal expressions :")
add_2_operation = Matrix_one + Matrix_two
print(add_2_operation))        

With the regular symbol definition and also the TensorFlow function we were able to get an element-wise multiplication, also known as Hadamard product.

But what if we want the regular matrix product?

We then need to use another TensorFlow function called?tf.matmul():


Matrix_one = tf.constant([[1,1],[3,3]]
Matrix_two = tf.constant([[2,3],[3,4]])

@tf.function
def mathmul():
return tf.matmul(Matrix_one,Matrix_two)

multiplication = mathmul()

print(multiplication)
        


Why Tensors?

We benefit from the Tensor structure because it gives us the flexibility to shape the dataset however we see fit. Due to the way that information in images is encoded, it is especially useful when working with images. When considering images, it is simple to see that they have a height and width; therefore, it makes sense to represent the information they contain with a two-dimensional structure (a matrix). However, once you consider that images also have colours, you realise that we need another dimension to add information about the colours. This is where tensors are especially useful.

Images are encoded using colour channels, with the most prevalent colour channel being RGB, which means that the image data is represented into each colour intensity in a colour channel at a given point.


Variables

TensorFlow variables are used to share and persist some stats that are manipulated by program. That is, when defining a variable, TensorFlow adds a?tf.Operation?to the graph. Then, this operation will store a writable tensor value. So, it can be updated the value of a variable through each run.

v = tf.Variable(0)        

now create a python method?increment_by_one. This method will internally call?td.add?that takes in two arguments, the?reference_variable?to update, and assign it to the?value_to_update?it by.

@tf.function
def increment_by_one(v):
    v = tf.add(v,1)
    returnv        

To update the value of the variable?v, we simply call the?increment_by_one?method and pass the variable to it. Then invoke this method thrice. This method will increment the variable by one and print the updated value each time.

for i in range(3):
    v = incment_by_one(v)
    print(v)        

Operations

perations are nodes that represent the mathematical operations over the tensors on a graph. These operations can be any kind of functions, like add and subtract tensor or maybe an activation function.

tf.constant,?tf.matmul,?tf.add,?tf.nn.sigmoid?are some of the operations in TensorFlow. These are like functions in python but operate directly over tensors and each one does a specific thing.


a = tf.constant([5]
b = tf.constant([2])
c = tf.add(a,b)
d = tf.subtract(a,b)

        

Conclusion:

Thus conclude with some basic concepts of Tensorflow.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了