In [39]:
import tensorflow as tf
import matplotlib.pyplot as plt

a = tf.constant([2, 3], name="a-vector")
b = tf.constant([4, 5], name="b-vector")
x = tf.add(a, b, name="add-vector")
with tf.Session() as sess:
    writer = tf.summary.FileWriter('./graphs', sess.graph)
    print sess.run(x)

writer.close()

# try tensorboard with command:
# $ tensorboard --logdir="graphs"


[6 8]

In [66]:
# basic tensorflow constants, types
zeros = tf.zeros([2, 3], tf.int32) 
two_dim_tensor = tf.constant([[2, 1], [3, 5]], name = "2d-tensor")
zeros_like = tf.zeros_like(two_dim_tensor)
ones = tf.ones([3, 3], tf.float32)
ones_like = tf.ones_like(two_dim_tensor)



# sequence data generation
linspace = tf.linspace(10.0, 14.0, 3);
limit = tf.range(2, 10, 3);

with tf.Session() as sess:
    print sess.run(zeros)
    
# generate random data according to some distribution
%matplotlib inline  

n = 500000
A = tf.truncated_normal((n,))
B = tf.random_normal((n,))
with tf.Session() as sess:
    a, b = sess.run([A, B])

# plot normal distriution and truncated normal distribution in the same graph.
plt.subplot(3,2,1);
plt.hist(a, 100, (-4.2, 4.2));
plt.subplot(3,2,2);
plt.hist(b, 100, (-4.2, 4.2));

# random uniform distribution
rand_t = tf.random_uniform([5], 0, 10, dtype=tf.int32, seed=0)
with tf.Session() as sess:
    t = sess.run(rand_t);
    
plt.subplot(3, 2, 3);
plt.plot(t);

# random shuffle plot
f_sequence = tf.range(1, 10);
f_shuffle = tf.random_shuffle(f_sequence)
with tf.Session() as sess:
    f_s = sess.run(f_shuffle);
    
plt.subplot(3, 2, 4);
plt.plot(f_s);

# random crop 
c_crop_before = tf.constant(([[3, 4, 5], [7, 8, 9], [0, 1, 2]]), name="before-crop");
c_crop_after = tf.random_crop(c_crop_before, [2, 2], name="after-crop");
with tf.Session() as sess:
    c_crop_plot = sess.run(c_crop_after)
    print c_crop_plot
    
    
# multinomial

multi_samples = tf.constant([[10., 10.]]);
multi_before = tf.multinomial(tf.log(multi_samples), 5)
with tf.Session() as sess:
    print sess.run(multi_samples)


[[0 0 0]
 [0 0 0]]
[[8 9]
 [1 2]]
[[ 10.  10.]]

In [73]:
# multinomial
multi_samples = tf.log([[10., 10., 10.]]);
multi_before = tf.multinomial(multi_samples, 5)
with tf.Session() as sess:
    print sess.run(multi_samples)
    print sess.run(multi_before)


[[ 2.30258512  2.30258512  4.60517025]]
[[2 2 2 1 2]]

In [2]:
import tensorflow as tf
# use InteractiveSession
sess = tf.InteractiveSession()

ones = tf.ones([2, 4], dtype=tf.int32);
print ones.eval();

fills = tf.fill([2, 4], 8)
print fills.eval();

linspace = tf.linspace(0.0, 10.0, 5);
print linspace.eval();

rang = tf.range(0.0, 10, 5);
print rang.eval();


[[1 1 1 1]
 [1 1 1 1]]
[[8 8 8 8]
 [8 8 8 8]]
[  0.    2.5   5.    7.5  10. ]
[ 0.  5.]

In [3]:
import tensorflow as tf
vector = tf.constant([2.0, 8], name="vector")
print tf.get_default_graph().as_graph_def();


node {
  name: "ones"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 2
          }
          dim {
            size: 4
          }
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "Fill/dims"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 2
          }
        }
        tensor_content: "\002\000\000\000\004\000\000\000"
      }
    }
  }
}
node {
  name: "Fill/value"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 8
      }
    }
  }
}
node {
  name: "Fill"
  op: "Fill"
  input: "Fill/dims"
  input: "Fill/value"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "LinSpace/start"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 0.0
      }
    }
  }
}
node {
  name: "LinSpace/stop"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 10.0
      }
    }
  }
}
node {
  name: "LinSpace/num"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 5
      }
    }
  }
}
node {
  name: "LinSpace"
  op: "LinSpace"
  input: "LinSpace/start"
  input: "LinSpace/stop"
  input: "LinSpace/num"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range/start"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 0.0
      }
    }
  }
}
node {
  name: "range/limit"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 10
      }
    }
  }
}
node {
  name: "range/delta"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 5
      }
    }
  }
}
node {
  name: "range/Cast_1"
  op: "Cast"
  input: "range/limit"
  attr {
    key: "DstT"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "SrcT"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range/Cast_2"
  op: "Cast"
  input: "range/delta"
  attr {
    key: "DstT"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "SrcT"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range"
  op: "Range"
  input: "range/start"
  input: "range/Cast_1"
  input: "range/Cast_2"
  attr {
    key: "Tidx"
    value {
      type: DT_FLOAT
    }
  }
}
node {
  name: "range_1/start"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 0
      }
    }
  }
}
node {
  name: "range_1/limit"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 10
      }
    }
  }
}
node {
  name: "range_1/delta"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 5
      }
    }
  }
}
node {
  name: "range_1"
  op: "Range"
  input: "range_1/start"
  input: "range_1/limit"
  input: "range_1/delta"
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "ones_1"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 2
          }
          dim {
            size: 4
          }
        }
        int_val: 1
      }
    }
  }
}
node {
  name: "Fill_1/dims"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 2
          }
        }
        tensor_content: "\002\000\000\000\004\000\000\000"
      }
    }
  }
}
node {
  name: "Fill_1/value"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 8
      }
    }
  }
}
node {
  name: "Fill_1"
  op: "Fill"
  input: "Fill_1/dims"
  input: "Fill_1/value"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "LinSpace_1/start"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 0.0
      }
    }
  }
}
node {
  name: "LinSpace_1/stop"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 10.0
      }
    }
  }
}
node {
  name: "LinSpace_1/num"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 5
      }
    }
  }
}
node {
  name: "LinSpace_1"
  op: "LinSpace"
  input: "LinSpace_1/start"
  input: "LinSpace_1/stop"
  input: "LinSpace_1/num"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "Tidx"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range_2/start"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
        }
        float_val: 0.0
      }
    }
  }
}
node {
  name: "range_2/limit"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 10
      }
    }
  }
}
node {
  name: "range_2/delta"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 5
      }
    }
  }
}
node {
  name: "range_2/Cast_1"
  op: "Cast"
  input: "range_2/limit"
  attr {
    key: "DstT"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "SrcT"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range_2/Cast_2"
  op: "Cast"
  input: "range_2/delta"
  attr {
    key: "DstT"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "SrcT"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "range_2"
  op: "Range"
  input: "range_2/start"
  input: "range_2/Cast_1"
  input: "range_2/Cast_2"
  attr {
    key: "Tidx"
    value {
      type: DT_FLOAT
    }
  }
}
node {
  name: "vector"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_FLOAT
        tensor_shape {
          dim {
            size: 2
          }
        }
        tensor_content: "\000\000\000@\000\000\000A"
      }
    }
  }
}
versions {
  producer: 24
}


In [6]:
import tensorflow as tf
cons = tf.constant(8, name="consant")
var = tf.Variable(8, name="Variable")
init = tf.global_variables_initializer()

with tf.Session() as sess:
    print sess.run(cons)
    sess.run(init)
    print sess.run(var)


8
8

In [20]:
import tensorflow as tf
var1 = tf.Variable(8, name="var1")

var2 = var1.assign(1*2)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print var2.eval()
    print var2.eval()
    print var2.eval()


2
2
2

In [21]:
import tensorflow as tf
var1 = tf.Variable(8, name="var1")

var2 = var1.assign(var1*2)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(var1.initializer)
    print var2.eval() # result is 16
    print var2.eval() # result is 32
    print var2.eval() # result is 64


16
32
64

In [ ]: