Python / Tensorflow

Python in Summary

Softly typed like R. No braces for scoping, instead whitespace has meaning.

In [1]:
def foo(x, y, z):
    print(x)
    print(y)
    print(z)
    
myX = "x"
myY = "y"
myZ = "z"

foo(myX, myY, myZ)
x
y
z

Structures

Brief enumeration of Lists, Dicts, Sets, and Tuples. Check out the PyDocs for more extensive documentation.

In [2]:
# List literal
print( [ 0, 1, 2, 3 ] )

# Special list syntax.
# Range is a generator function that you can iterate over
print( [ i for i in range(0,4) ] )

# Dict literal
# Note: Key ordering is not guaranteed
print( { "a": 1, "b": 2, "c": 3} )

# Sets
print( set([0, 1, 2, 3]) )

# Note: if we use the special list syntax in curly braces, we get a set - not a dict
print( {i for i in range(0,4)} )

# Tuples
# Much like a list
print( (0, 1) )
print( (0, 1, 2, 3))
# To make a 1-tuple, the comma is required after the first item.
# (Otherwise, it parses as a parenthesized expression).
print( (0,) )
[0, 1, 2, 3]
[0, 1, 2, 3]
{'c': 3, 'b': 2, 'a': 1}
{0, 1, 2, 3}
{0, 1, 2, 3}
(0, 1)
(0, 1, 2, 3)
(0,)

Python Hello World

Simple enough, right?

In [ ]:
msg = 'Hello, Python!'
print(msg)

Tensorflow

Import Tensorflow

"tf" is an arbitrary name chosen to abbreviate code. You can use anything, so long as you are consistant.

In [3]:
import tensorflow as tf

Tensorflow abstracts your flow

In the python hello world, we just made a variable and printed it. However, in tensorflow we don't actually have access to anything beyond inputs and outputs. To demonstrate this, we define a constant "msg" containing the string value we want to be printed. We then evaluate "msg" in a tensorflow session (just resolving its value) and print the result.

Is this redundant? Absolutely. In more complicated examples, we will need to do this so we can parallelize and scale well. Check out https://github.com/aymericdamien/TensorFlow-Examples for more examples.

In [4]:
msg = tf.constant('Hello, TensorFlow!')

sess = tf.Session()

print(sess.run(msg))
b'Hello, TensorFlow!'
In [ ]: