In [1]:
import tensorflow as tf
sess = tf.Session()
In [3]:
import numpy as np
x_vals = np.array([1., 3., 5., 7., 9.])
x_data = tf.placeholder(tf.float32)
m_const = tf.constant(3.)
In [8]:
my_product = tf.multiply(x_data, m_const)
my_product
Out[8]:
In [9]:
for x_val in x_vals:
print(sess.run(my_product, feed_dict={x_data: x_val}))
In [10]:
sess.run(my_product, feed_dict={x_data: x_vals})
Out[10]:
In [13]:
my_array = np.array([[1., 3., 5., 7., 9.],
[-2., 0., 2., 4., 6.],
[-6., -3., 0., 3., 6.]])
x_vals = np.array([my_array, my_array + 1])
In [17]:
x_data = tf.placeholder(tf.float32, shape=(3, 5))
In [20]:
m1 = tf.constant([[1.], [0], [-1.], [2.], [4.]])
m2 = tf.constant([[2.]])
a1 = tf.constant([[10.]])
In [21]:
m1
Out[21]:
In [22]:
m2
Out[22]:
In [23]:
a1
Out[23]:
In [24]:
x_data
Out[24]:
In [29]:
prod1 = tf.matmul(x_data, m1)
prod2 = tf.matmul(prod1, m2)
add1 = tf.add(prod2, a1)
In [26]:
prod1
Out[26]:
In [28]:
prod2
Out[28]:
In [30]:
add1
Out[30]:
In [31]:
for x_val in x_vals:
print(sess.run(add1, feed_dict={x_data: x_val}))
In [32]:
x_data = tf.placeholder(tf.float32, shape=(3, None))
In [33]:
x_data
Out[33]:
In [34]:
x_shape = [1, 4, 4, 1]
x_val = np.random.uniform(size=x_shape)
In [35]:
x_val
Out[35]:
In [38]:
x_data = tf.placeholder(tf.float32, shape=x_shape)
x_data
Out[38]:
In [39]:
my_filter = tf.constant(0.25, shape=[2, 2, 1, 1])
my_strides = [1, 2, 2, 1]
mov_avg_layer = tf.nn.conv2d(x_data, my_filter, my_strides, padding='SAME', name='Moving_Avg_Window')
In [40]:
mov_avg_layer
Out[40]:
In [41]:
def custom_layer(input_matrix):
input_matrix_sqeezed = tf.squeeze(input_matrix)
A = tf.constant([[1., 2.], [-1., 3.]])
b = tf.constant(1., shape=[2, 2])
temp1 = tf.matmul(A, input_matrix_sqeezed)
temp = tf.add(temp1, b)
return tf.sigmoid(temp)
In [42]:
with tf.name_scope('Custom_Layer') as scope:
custom_layer1 = custom_layer(mov_avg_layer)
In [44]:
sess.run(custom_layer1, feed_dict={x_data: x_val})
Out[44]:
In [45]:
x_vals = tf.linspace(-1., 1., 500)
In [46]:
x_vals
Out[46]:
In [47]:
target = tf.constant(0.)
In [48]:
target
Out[48]:
In [50]:
l2_y_vals = tf.square(target - x_vals)
In [52]:
x_array = sess.run(x_vals)
l2_y_out = sess.run(l2_y_vals)
In [54]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(x_array, l2_y_out)
Out[54]:
In [56]:
l1_y_vals = tf.abs(target - x_vals)
l1_y_out = sess.run(l1_y_vals)
In [57]:
plt.plot(x_array, l1_y_out)
Out[57]:
In [63]:
delta1 = tf.constant(0.25)
phuber1_y_vals = tf.multiply(tf.square(delta1), tf.sqrt(1. + tf.square((target - x_vals) / delta1)) - 1.)
phuber1_y_out = sess.run(phuber1_y_vals)
plt.plot(x_array, phuber1_y_out)
Out[63]:
In [64]:
delta2 = tf.constant(5.)
phuber2_y_vals = tf.multiply(tf.square(delta2), tf.sqrt(1. + tf.square((target - x_vals) / delta2)) - 1.)
phuber2_y_out = sess.run(phuber2_y_vals)
plt.plot(x_array, phuber2_y_out)
Out[64]:
In [76]:
x_vals = tf.linspace(-2., 2., 500)
x_array = sess.run(x_vals)
target = tf.constant(1.)
targets = tf.fill([500, ], 1.)
In [77]:
hinge_y_vals = tf.maximum(0., 1. - tf.multiply(target, x_vals))
hinge_y_out = sess.run(hinge_y_vals)
plt.plot(x_array, hinge_y_out)
Out[77]:
In [80]:
xentropy_y_vals = - tf.multiply(target, tf.log(x_vals)) - tf.multiply((1. - target), tf.log(1. - x_vals))
xentropy_y_out = sess.run(xentropy_y_vals)
plt.plot(x_array, xentropy_y_out)
Out[80]:
In [85]:
xentropy_sigmoid_y_vals = tf.nn.sigmoid_cross_entropy_with_logits(logits=x_vals, labels=targets)
xentropy_sigmoid_y_out = sess.run(xentropy_sigmoid_y_vals)
plt.plot(x_array, xentropy_sigmoid_y_out)
Out[85]:
In [ ]: