In [55]:
import tensorflow as tf

a = tf.constant([1,2,3,4]*4, dtype='float32', shape=[4,4])

x = tf.truncated_normal([4,1], stddev=0.1)

b = tf.constant(0.1, shape=[4,1])

y = tf.matmul(a, x) + b

init = tf.initialize_all_variables()

grads = tf.gradients(y, x)

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(a))
    print(sess.run(x))
    print(sess.run(y))
    print(sess.run(grads))


[[ 1.  2.  3.  4.]
 [ 1.  2.  3.  4.]
 [ 1.  2.  3.  4.]
 [ 1.  2.  3.  4.]]
[[-0.15170328]
 [-0.05440265]
 [ 0.07244036]
 [ 0.09646168]]
[[ 0.3792029]
 [ 0.3792029]
 [ 0.3792029]
 [ 0.3792029]]
[array([[  4.],
       [  8.],
       [ 12.],
       [ 16.]], dtype=float32)]

In [117]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

a = np.arange(-np.pi, np.pi, 0.02)

x = tf.placeholder("float", shape=[a.shape[0]])

y1 = tf.sin(x) 
y2 = tf.sigmoid(x)


init = tf.initialize_all_variables()

grad_y1 = tf.gradients(y1, x)
grad_y2 = tf.gradients(y2, x)


with tf.Session() as sess:
    sess.run(init)
    
    v1, g1  = sess.run([y1,grad_y1], feed_dict={x:a})
    v2, g2  = sess.run([y2,grad_y2], feed_dict={x:a})

    fig, ax = plt.subplots(1, 2, figsize=(13,5))
    ax[0].plot(a, v1, lw=3)
    ax[0].plot(a, g1[0], lw=3)
    ax[0].set_xlim(-0.4-np.pi, np.pi + 0.4)
    xlabels = [r"$-\pi$", r"$-\frac{\pi}{2}$", r"$0$", r"$\frac{\pi}{2}$",   r"$\pi$"]
    ax[0].set_xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
    ax[0].set_xticklabels(xlabels, fontsize=22)
    ax[0].set_yticklabels(np.linspace(-1,1, 5, endpoint=True), fontsize=16)
    ax[0].legend(["sin(x)", "gradients"], fontsize=20, loc=2)
    
    ax[1].plot(a, v2, lw=3)
    ax[1].plot(a, g2[0], lw=3)
    ax[1].set_xlim(-0.4+np.pi, np.pi + 0.4)
    ax[1].set_xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
    ax[1].set_xticklabels(xlabels, fontsize=22)
    ax[1].set_yticklabels(np.linspace(0,1, 6, endpoint=True), fontsize=16)
    ax[1].legend(["sigmoid(x)", "gradients"], fontsize=20, loc=2)    
    
    
    plt.show



In [ ]: