{{announcement.body}}
{{announcement.title}}

Learn TensorFlow: Vectors

DZone 's Guide to

Learn TensorFlow: Vectors

Learn about TensorFlow and explore vectors.

· AI Zone ·
Free Resource

TensorFlow

TensorFlow is an open-source library that was developed by the Google Brain team, and it was released in November 2015. Before working with TensorFlow, we need to understand the following basic concepts:

  • Graph: Layout of the learning process. It does not include data.
  • Data: Examples that are used to train. It has two kinds, which are inputs and targets.
  • Session: Where we feed the graph with data or Session = Graph + Data. We can do this by using placeholders — gates to introduce examples.

We can install Anaconda to use TensorFlow.

Vectors

In Machine Learning, vectors can be used as a good way to represent numeric data. When using vectors, we can meet the following basic operations:

  • Add two vectors
  • Subtract two vectors
  • Mutilply a vector with a scalar (i.e., a number)
  • Norm (i.e, magnitude or length of a vector)
  • Dot product of two vectors

The operations on vectors can be implemented by using the functions from the TensorFlow library, but before using this library, we must:

import tensorflow as tf

Next step is to create the graph:

#####GRAPH

vec_1 = tf.placeholder(tf.float32)

vec_2 = tf.placeholder(tf.float32)

scalar = tf.placeholder(tf.float32)

vector_add = tf.add(vec_1,vec_2)

vector_subtract = tf.subtract(vec_1,vec_2)

scalar_multiply = tf.multiply(scalar,vec_1)

norm = tf.norm(vec_1)

dot = tf.tensordot(vec_1, vec_2, 1)

We can feed the graph with data through the session. Data can be declared as lists of numbers:

#############DATA

v = [1,2]

w = [2,3]

c = 3

Session can be created:

##########SESSION

with tf.Session() as sess:

       result_add = sess.run(vector_add, feed_dict={vec_1:v,vec_2:w})

       result_sub = sess.run(vector_subtract, feed_dict={vec_1:v,vec_2:w})

       result_mul = sess.run(scalar_multiply, feed_dict={scalar:c,vec_1:v})

       result_norm = sess.run(norm , feed_dict={vec_1:v})

       result_dot = sess.run(dot, feed_dict={vec_1:v,vec_2:w})

Finally, we can create some outputs:

###########OUTPUT
print(result_add.tolist())
print(result_sub.tolist())
print(result_mul.tolist())
print(result_norm)
print(result_dot)

The result can look like this:

[3.0, 5.0]
[-1.0, -1.0]
[3.0, 6.0]
2.236068
8.0

Conclusion

Starting from the simplest things is one of the best ways to learn the TensorFlow. Through operations on vectors, I hope you (and both me) — TensorFlow beginners — will understand how to use the TensorFlow before using it for more complex tasks in the future.  



If you enjoyed this article and want to learn more about TensorFlow, check out this collection of tutorials and articles on all things TensorFlow.

Topics:
ai ,tensorflow ,python ,tutorial ,artificial intelligence ,vectors ,anaconda ,anaconda python environment

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}