First TensorFlow Neurons

Load dependencies


In [1]:
import numpy as np
np.random.seed(42)
import tensorflow as tf
tf.set_random_seed(42)
import matplotlib.pyplot as plt
%matplotlib inline

Set number of neurons


In [2]:
n_input = 784
n_dense = 128

Define placeholder Tensor for simulated MNIST digits


In [3]:
x = tf.placeholder(tf.float32, [None, n_input])

Create Variable Tensors for neuron biases b and weight matrix W


In [4]:
b = tf.Variable(tf.zeros([n_dense]))
W = tf.Variable(tf.random_uniform([n_input, n_dense])) # 1.
# W = tf.Variable(tf.random_normal([n_input, n_dense])) # 2.
# W = tf.get_variable('W', [n_input, n_dense], 
#                     initializer=tf.contrib.layers.xavier_initializer())

Design the computational graph


In [5]:
z = tf.add(tf.matmul(x, W), b)
a = tf.sigmoid(z) # first with tf.sigmoid(), then stick with tf.tanh() or tf.nn.relu()

Create op for variable initialization


In [6]:
initializer_op = tf.global_variables_initializer()

Execute the graph in a session


In [7]:
with tf.Session() as session:
    session.run(initializer_op)
    
    layer_output = session.run(a, {x: np.random.random([1, n_input])})

In [8]:
layer_output


Out[8]:
array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]], dtype=float32)

In [9]:
_ = plt.hist(np.transpose(layer_output))



In [ ]: