DZone
AI Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > AI Zone > Learn TensorFlow: Vectors

Learn TensorFlow: Vectors

Learn about TensorFlow and explore vectors.

Ngoc Minh Tran user avatar by
Ngoc Minh Tran
CORE ·
Jan. 22, 19 · AI Zone · Tutorial
Like (1)
Save
Tweet
6.49K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Data structure TensorFlow

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Highlights from the New NIST SSDF
  • Bad or Good? Behavior Driven Development within Scrum.
  • How to Migrate to the Open-Source Cadence Workflow
  • A Guide to Maven 3 Beta

Comments

AI Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo