In [1]:
import tensorflow as tf

In [2]:
tf.zeros([3, 3])


Out[2]:
<tf.Tensor 'zeros:0' shape=(3, 3) dtype=float32>

In [3]:
tf.ones([3, 3])


Out[3]:
<tf.Tensor 'ones:0' shape=(3, 3) dtype=float32>

In [4]:
tf.fill([3, 3], 42)


Out[4]:
<tf.Tensor 'Fill:0' shape=(3, 3) dtype=int32>

In [5]:
constant_tsr = tf.constant([1, 2, 3])
constant_tsr


Out[5]:
<tf.Tensor 'Const:0' shape=(3,) dtype=int32>

In [6]:
tf.zeros_like(constant_tsr)


Out[6]:
<tf.Tensor 'zeros_like:0' shape=(3,) dtype=int32>

In [7]:
tf.ones_like(constant_tsr)


Out[7]:
<tf.Tensor 'ones_like:0' shape=(3,) dtype=int32>

In [8]:
tf.linspace(0.0, 1.0, 3)


Out[8]:
<tf.Tensor 'LinSpace:0' shape=(3,) dtype=float32>

In [9]:
tf.range(6, 15, 3)


Out[9]:
<tf.Tensor 'range:0' shape=(3,) dtype=int32>

In [10]:
tf.random_uniform([3, 3], minval=0, maxval=1)


Out[10]:
<tf.Tensor 'random_uniform:0' shape=(3, 3) dtype=float32>

In [11]:
tf.random_normal([3, 3], mean=0.0, stddev=1.0)


Out[11]:
<tf.Tensor 'random_normal:0' shape=(3, 3) dtype=float32>

In [12]:
tf.truncated_normal([3, 3], mean=0.0, stddev=1.0)


Out[12]:
<tf.Tensor 'truncated_normal:0' shape=(3, 3) dtype=float32>

In [13]:
my_var = tf.Variable(tf.zeros([2, 3]))
sess = tf.Session()
initialize_op = tf.global_variables_initializer()
sess.run(initialize_op)

In [30]:
import numpy as np
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[2, 2])
y = tf.identity(x)
x_vals = np.random.rand(2, 2)
sess.run(y, feed_dict={x: x_vals})


Out[30]:
array([[ 0.84918463,  0.20622016],
       [ 0.73926389,  0.94779098]], dtype=float32)

In [31]:
import numpy as np
import tensorflow as tf
sess = tf.Session()

In [38]:
identity_matrix = tf.diag([1.0, 1.0, 1.0])
print(identity_matrix)
print(sess.run(identity_matrix))


Tensor("Diag_2:0", shape=(3, 3), dtype=float32)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]

In [36]:
A = tf.truncated_normal([2, 3])
B = tf.fill([2, 3], 5.0)
C = tf.random_uniform([3, 2])
D = tf.convert_to_tensor(np.array(
    [[1., 2., 3.],
     [-3., -7., -1.],
     [0., 5., -2.]]
))

In [46]:
print(sess.run(B))


[[ 5.  5.  5.]
 [ 5.  5.  5.]]

In [43]:
print(sess.run(A + B))


[[ 5.19373512  4.94957685  3.75595045]
 [ 4.53172874  4.03519726  3.87743998]]

In [44]:
print(sess.run(B - B))


[[ 0.  0.  0.]
 [ 0.  0.  0.]]

In [45]:
print(sess.run(tf.matmul(B, identity_matrix)))


[[ 5.  5.  5.]
 [ 5.  5.  5.]]

In [47]:
sess.run(C)


Out[47]:
array([[ 0.80826008,  0.66252875],
       [ 0.14581406,  0.29976416],
       [ 0.08765745,  0.3801676 ]], dtype=float32)

In [48]:
sess.run(tf.transpose(C))


Out[48]:
array([[ 0.57005787,  0.04247582,  0.94901741],
       [ 0.64883411,  0.5056982 ,  0.37611508]], dtype=float32)

In [49]:
sess.run(tf.matrix_determinant(D))


Out[49]:
-38.0

In [50]:
sess.run(tf.matrix_inverse(D))


Out[50]:
array([[-0.5       , -0.5       , -0.5       ],
       [ 0.15789474,  0.05263158,  0.21052632],
       [ 0.39473684,  0.13157895,  0.02631579]])

In [53]:
aaa = tf.matmul(D, tf.matrix_inverse(D))
aaa


Out[53]:
<tf.Tensor 'MatMul_2:0' shape=(3, 3) dtype=float64>

In [54]:
sess.run(aaa)


Out[54]:
array([[  1.00000000e+00,   0.00000000e+00,  -8.32667268e-17],
       [ -1.11022302e-16,   1.00000000e+00,   1.04083409e-16],
       [  1.11022302e-16,   0.00000000e+00,   1.00000000e+00]])

In [55]:
sess.run(tf.cholesky(identity_matrix))


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

In [56]:
sess.run(tf.self_adjoint_eig(D))


Out[56]:
(array([-10.65907521,  -0.22750691,   2.88658212]),
 array([[ 0.21749542,  0.63250104, -0.74339638],
        [ 0.84526515,  0.2587998 ,  0.46749277],
        [-0.4880805 ,  0.73004459,  0.47834331]]))

In [57]:
sess.run(tf.div(3, 4))


Out[57]:
0

In [58]:
sess.run(tf.truediv(3, 4))


Out[58]:
0.75

In [59]:
sess.run(tf.div(3.0, 4.0))


Out[59]:
0.75

In [60]:
sess.run(tf.floordiv(3.0, 4.0))


Out[60]:
0.0

In [61]:
sess.run(tf.mod(22.0, 5.0))


Out[61]:
2.0

In [62]:
def custom_polynomial(value):
    return(tf.subtract(3 * tf.square(value), value) + 10)

In [67]:
sess.run(custom_polynomial(11))


Out[67]:
362

In [75]:
sess.run(tf.nn.relu([-3., 3., 10.]))


Out[75]:
array([  0.,   3.,  10.], dtype=float32)

In [76]:
sess.run(tf.nn.relu6([-3., 3., 10.]))


Out[76]:
array([ 0.,  3.,  6.], dtype=float32)

In [77]:
sess.run(tf.nn.sigmoid([-1., 0., 1.]))


Out[77]:
array([ 0.26894143,  0.5       ,  0.7310586 ], dtype=float32)

In [78]:
sess.run(tf.nn.tanh([-1., 0., 1.]))


Out[78]:
array([-0.76159418,  0.        ,  0.76159418], dtype=float32)

In [79]:
sess.run(tf.nn.softsign([-1., 0., 1.]))


Out[79]:
array([-0.5,  0. ,  0.5], dtype=float32)

In [91]:
import matplotlib.pyplot as plt
%matplotlib inline
xlist = np.linspace(-5, 5, 1000)
ss = sess.run(tf.nn.softsign(xlist))
sp = sess.run(tf.nn.softplus(xlist))
el = sess.run(tf.nn.elu(xlist))
plt.plot(xlist, ss)
plt.plot(xlist, sp)
plt.plot(xlist, el)


Out[91]:
[<matplotlib.lines.Line2D at 0x113acee80>]

In [87]:
sess.run(tf.nn.softplus([-1., 0., 1.]))


Out[87]:
array([ 0.31326166,  0.69314718,  1.31326163], dtype=float32)

In [93]:
from sklearn import datasets
iris = datasets.load_iris()

In [95]:
len(iris.data)


Out[95]:
150

In [96]:
len(iris.target)


Out[96]:
150

In [98]:
iris.data[0]


Out[98]:
array([ 5.1,  3.5,  1.4,  0.2])

In [99]:
iris.target[0]


Out[99]:
0

In [100]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [101]:
len(mnist.train.images)


Out[101]:
55000

In [102]:
len(mnist.test.images)


Out[102]:
10000

In [103]:
len(mnist.validation.images)


Out[103]:
5000