2015-12-28 20:51:47 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Basic Operations in TensorFlow \n " ,
" \n " ,
" Credits: Forked from [TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples) by Aymeric Damien \n " ,
" \n " ,
" ## Setup \n " ,
" \n " ,
" Refer to the [setup instructions](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/Setup_TensorFlow.md) "
]
} ,
{
" cell_type " : " code " ,
2016-01-03 23:31:25 +08:00
" execution_count " : 1 ,
2015-12-28 20:51:47 +08:00
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" import tensorflow as tf "
]
} ,
{
" cell_type " : " code " ,
2016-01-03 23:31:25 +08:00
" execution_count " : 2 ,
2015-12-28 20:51:47 +08:00
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" # Basic constant operations \n " ,
" # The value returned by the constructor represents the output \n " ,
" # of the Constant op. \n " ,
" a = tf.constant(2) \n " ,
" b = tf.constant(3) "
]
} ,
{
" cell_type " : " code " ,
2016-01-03 23:31:25 +08:00
" execution_count " : 3 ,
2015-12-28 20:51:47 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" a=2, b=3 \n " ,
" Addition with constants: 5 \n " ,
" Multiplication with constants: 6 \n "
]
}
] ,
" source " : [
" # Launch the default graph. \n " ,
" with tf.Session() as sess: \n " ,
" print \" a=2, b=3 \" \n " ,
" print \" Addition with constants: %i \" % s ess.run(a+b) \n " ,
" print \" Multiplication with constants: %i \" % s ess.run(a*b) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 5 ,
" metadata " : {
2016-01-03 23:31:25 +08:00
" collapsed " : false
2015-12-28 20:51:47 +08:00
} ,
" outputs " : [ ] ,
" source " : [
" # Basic Operations with variable as graph input \n " ,
" # The value returned by the constructor represents the output \n " ,
" # of the Variable op. (define as input when running session) \n " ,
" # tf Graph input \n " ,
2016-01-03 23:31:25 +08:00
" a = tf.placeholder(tf.int16) \n " ,
" b = tf.placeholder(tf.int16) "
2015-12-28 20:51:47 +08:00
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 6 ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" # Define some operations \n " ,
" add = tf.add(a, b) \n " ,
" mul = tf.mul(a, b) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 7 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Addition with variables: 5 \n " ,
" Multiplication with variables: 6 \n "
]
}
] ,
" source " : [
" # Launch the default graph. \n " ,
" with tf.Session() as sess: \n " ,
" # Run every operation with variable input \n " ,
" print \" Addition with variables: %i \" % s ess.run(add, feed_dict= { a: 2, b: 3}) \n " ,
" print \" Multiplication with variables: %i \" % s ess.run(mul, feed_dict= { a: 2, b: 3}) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 8 ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" # ---------------- \n " ,
" # More in details: \n " ,
" # Matrix Multiplication from TensorFlow official tutorial \n " ,
" \n " ,
" # Create a Constant op that produces a 1x2 matrix. The op is \n " ,
" # added as a node to the default graph. \n " ,
" # \n " ,
" # The value returned by the constructor represents the output \n " ,
" # of the Constant op. \n " ,
" matrix1 = tf.constant([[3., 3.]]) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 9 ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" # Create another Constant that produces a 2x1 matrix. \n " ,
" matrix2 = tf.constant([[2.],[2.]]) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 10 ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" # Create a Matmul op that takes ' matrix1 ' and ' matrix2 ' as inputs. \n " ,
" # The returned value, ' product ' , represents the result of the matrix \n " ,
" # multiplication. \n " ,
" product = tf.matmul(matrix1, matrix2) "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 11 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" [[ 12.]] \n "
]
}
] ,
" source " : [
" # To run the matmul op we call the session ' run() ' method, passing ' product ' \n " ,
" # which represents the output of the matmul op. This indicates to the call \n " ,
" # that we want to get the output of the matmul op back. \n " ,
" # \n " ,
" # All inputs needed by the op are run automatically by the session. They \n " ,
" # typically are run in parallel. \n " ,
" # \n " ,
" # The call ' run(product) ' thus causes the execution of threes ops in the \n " ,
" # graph: the two constants and matmul. \n " ,
" # \n " ,
" # The output of the op is returned in ' result ' as a numpy `ndarray` object. \n " ,
" with tf.Session() as sess: \n " ,
" result = sess.run(product) \n " ,
" print result "
]
2016-01-03 23:31:25 +08:00
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [ ]
2015-12-28 20:51:47 +08:00
}
] ,
" metadata " : {
" kernelspec " : {
2016-01-03 23:31:25 +08:00
" display_name " : " Python 2 " ,
2015-12-28 20:51:47 +08:00
" language " : " python " ,
2016-01-03 23:31:25 +08:00
" name " : " python2 "
2015-12-28 20:51:47 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-01-03 23:31:25 +08:00
" version " : 2
2015-12-28 20:51:47 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-01-03 23:31:25 +08:00
" pygments_lexer " : " ipython2 " ,
" version " : " 2.7.5+ "
2015-12-28 20:51:47 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}