[ Prev ]
2017-10-26

-- Oct 25 In-Class Exercise Thread
import tensorflow as tf
def perceptron(weights, inputs, biases):
    nodes = tf.matmul(weights, inputs) + biases
    return step(nodes)
def step(nodes):
    nodes = tf.greater_equal(nodes, tf.constant(0, dtype=tf.float32))
    return tf.cast(nodes, tf.float32)
tf.reset_default_graph() x = tf.placeholder(tf.float32, shape=(3,1)) W1 = tf.Variable([[1, 1, 1], [-1, -1, -1], [1, 1, 1]], dtype=tf.float32) b1 = tf.Variable([[-0.5], [1.5], [-2.5]], dtype=tf.float32) W2 = tf.Variable(1, 1, 2 , dtype=tf.float32) b2 = tf.Variable(-2, dtype=tf.float32) my_layer1 = perceptron(W1, x, b1) my_layer2 = perceptron(W2, my_layer1, b2) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1 = session.run(my_layer1, {x: [[0], [1], [0]]}) l2 = session.run(my_layer2, {x: [[0], [1], [0]]}) print l2
import tensorflow as tf def perceptron(weights, inputs, biases): nodes = tf.matmul(weights, inputs) + biases return step(nodes) def step(nodes): nodes = tf.greater_equal(nodes, tf.constant(0, dtype=tf.float32)) return tf.cast(nodes, tf.float32) tf.reset_default_graph() x = tf.placeholder(tf.float32, shape=(3,1)) W1 = tf.Variable([[1, 1, 1], [-1, -1, -1], [1, 1, 1]], dtype=tf.float32) b1 = tf.Variable([[-0.5], [1.5], [-2.5]], dtype=tf.float32) W2 = tf.Variable([[1, 1, 2]], dtype=tf.float32) b2 = tf.Variable(-2, dtype=tf.float32) my_layer1 = perceptron(W1, x, b1) my_layer2 = perceptron(W2, my_layer1, b2) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1 = session.run(my_layer1, {x: [[0], [1], [0]]}) l2 = session.run(my_layer2, {x: [[0], [1], [0]]}) print l2

-- Oct 25 In-Class Exercise Thread
import os import tensorflow as tf os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' def perceptron(weights, inputs, biases, activation): nodes = tf.reduce_sum(weights * inputs, 1) + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=3) W = tf.constant([ [1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0] ]) B = tf.constant([ -0.5, 1.5, -2.5 ]) layer_1 = perceptron(W, x, B, step) W4 = tf.constant([ [1.0, 1.0, 2.0] ]) B4 = tf.constant([ -1.99 ]) layer_2 = perceptron(W4, layer_1, B4, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(layer_2, {x: [0, 0, 1]}) print session.run(layer_2, {x: [0, 1, 1]}) print session.run(layer_2, {x: [1, 1, 1]})
<nowiki>import os import tensorflow as tf os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' def perceptron(weights, inputs, biases, activation): nodes = tf.reduce_sum(weights * inputs, 1) + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=3) W = tf.constant([ [1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0] ]) B = tf.constant([ -0.5, 1.5, -2.5 ]) layer_1 = perceptron(W, x, B, step) W4 = tf.constant([ [1.0, 1.0, 2.0] ]) B4 = tf.constant([ -1.99 ]) layer_2 = perceptron(W4, layer_1, B4, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(layer_2, {x: [0, 0, 1]}) print session.run(layer_2, {x: [0, 1, 1]}) print session.run(layer_2, {x: [1, 1, 1]}) </nowiki>

-- Oct 25 In-Class Exercise Thread
import tensorflow as tf;
import os;
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 
 
def perceptron(weights, inputs, activation):
    nodes = tf.matmul(weights, inputs)
    print nodes
    # tf.Print()
    # print nodes.eval()
    return activation(nodes) 
 
def step(nodes):
    return tf.ceil(tf.clip_by_value(nodes, 0, 1)) 
 
x = tf.placeholder(tf.float32, shape=(3,1))
W1 = tf.Variable([[1/2, 1/2, 1/2], [-3/2, -3/2, -3/2], [5/2, 5/2, 5/2]], dtype=tf.float32)
W2 = tf.placeholder(dtype=tf.float32, shape=(1,3)) 
 
my_layer1 = perceptron(W1, x, step)
my_layer2 = perceptron( W2,my_layer1, step)
session = tf.Session()
init = tf.global_variables_initializer()
session.run(init)
l1= session.run(my_layer1, {x: [[0.0], [1.0], [0.0]]})
print(l1)
print
print(session.run(my_layer2, {W2:1/2, 1/2, 1	, my_layer1 :l1}))
(Edited: 2017-10-26)
<pre> import tensorflow as tf; import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' def perceptron(weights, inputs, activation): nodes = tf.matmul(weights, inputs) print nodes # tf.Print() # print nodes.eval() return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=(3,1)) W1 = tf.Variable([[1/2, 1/2, 1/2], [-3/2, -3/2, -3/2], [5/2, 5/2, 5/2]], dtype=tf.float32) W2 = tf.placeholder(dtype=tf.float32, shape=(1,3)) my_layer1 = perceptron(W1, x, step) my_layer2 = perceptron( W2,my_layer1, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1= session.run(my_layer1, {x: [[0.0], [1.0], [0.0]]}) print(l1) print print(session.run(my_layer2, {W2:[[1/2, 1/2, 1]], my_layer1 :l1})) </pre>
2017-10-27

-- Oct 25 In-Class Exercise Thread
Name = Krishna Teja Vojjila
import tensorflow as tf
def perceptron(weights, inputs, bias, activation):
    nodes = tf.reduce_sum(weights * inputs, 1) + bias
    return activation(nodes)
def step(nodes):
    return tf.ceil(tf.clip_by_value(nodes, 0, 1))
x = tf.placeholder(tf.float32, shape=3) W = tf.constant([
    [1.0, 1.0, 1.0],
    [-1.0, -1.0, -1.0],
    [1.0, 1.0, 1.0]])
  • weights B = tf.constant([
        -0.5,
        1.5,
        -2.5
    
    ])
  • p_layer_1 = perceptron(W, x, B, step) W4 = tf.constant([
        [1.0, 1.0, 2.0]
    
    ]) B4 = tf.constant([
        -1.99])
    
    p_layer_2 = perceptron(W4, layer_1, B4, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(p_layer_2, {x: [0, 0, 1]}) print session.run(p_layer_2, {x: [0, 1, 1]}) print session.run(p_layer_2, {x: [1, 1, 1]})
    Name = Krishna Teja Vojjila import tensorflow as tf def perceptron(weights, inputs, bias, activation): nodes = tf.reduce_sum(weights * inputs, 1) + bias return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=3) W = tf.constant([ [1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0]]) #weights B = tf.constant([ -0.5, 1.5, -2.5 ]) p_layer_1 = perceptron(W, x, B, step) W4 = tf.constant([ [1.0, 1.0, 2.0] ]) B4 = tf.constant([ -1.99]) p_layer_2 = perceptron(W4, layer_1, B4, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(p_layer_2, {x: [0, 0, 1]}) print session.run(p_layer_2, {x: [0, 1, 1]}) print session.run(p_layer_2, {x: [1, 1, 1]})
    2017-10-29

    -- Oct 25 In-Class Exercise Thread
    Resource Description for tf.JPG
    (Edited: 2017-10-29)
    ((resource:tf.JPG|Resource Description for tf.JPG))

    -- Oct 25 In-Class Exercise Thread
    import numpy as np import tensorflow as tf
    def perceptron(weights, inputs, biases, activation, silent=True):
        if not silent:
            print(f'Shape of weights: {tf.shape(weights)}')
            print(f'Shape of inputs: {tf.shape(inputs)}')
        
        nodes = tf.matmul(weights, inputs) + biases
    
        return activation(nodes)
    
    def step(nodes):
        """
        Step activation fnc
        """
        return tf.ceil(tf.clip_by_value(nodes, 0, 1))
    
    def class_exercise():
        """
        For Oct 25 exercise.
        """
    
        # Data
        x_train = [
            np.array([1, 0, 0]),
            np.array([0, 1, 0]),
            np.array([0, 0, 1]),
            np.array([1, 1, 1]),
            np.array([0, 0, 0]),
            np.array([1, 1, 0]),
            np.array([0, 1, 1]),
            np.array([1, 0, 1])
        ]
        y_train = np.array([1, 1, 1, 1, 0, 0, 0, 0])
    
        # Model Input
        x = tf.placeholder(tf.float32, shape=(3, 1))
        y = tf.placeholder(tf.float32)
    
        # Perceptron init
        uniform_init = tf.random_uniform_initializer(0, 1) 
        W1 = tf.get_variable("W1", shape=(3,3), initializer = uniform_init)
        b1 = tf.get_variable("b1", shape=(3,1), initializer = uniform_init)
        
        W4 = tf.get_variable("W4", shape=(1, 3), initializer = uniform_init)
        b4 = tf.get_variable("b4", shape=(1,1), initializer = uniform_init)
    
        # Init TF Session
        sess = tf.Session()
        init = tf.global_variables_initializer()
        sess.run(init)
    
        # Train
        l1 = perceptron(W1, x, b1, step, silent=False)
        l2 = perceptron(W4, l1, b4, step, silent=False)
    
        for i in range(8):
            print(sess.run(l2, {x: x_train[i]}))
    
    class_exercise()
    (Edited: 2017-10-29)
    import numpy as np import tensorflow as tf def perceptron(weights, inputs, biases, activation, silent=True): if not silent: print(f'Shape of weights: {tf.shape(weights)}') print(f'Shape of inputs: {tf.shape(inputs)}') nodes = tf.matmul(weights, inputs) + biases return activation(nodes) def step(nodes): """ Step activation fnc """ return tf.ceil(tf.clip_by_value(nodes, 0, 1)) def class_exercise(): """ For Oct 25 exercise. """ # Data x_train = [ np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1]), np.array([1, 1, 1]), np.array([0, 0, 0]), np.array([1, 1, 0]), np.array([0, 1, 1]), np.array([1, 0, 1]) ] y_train = np.array([1, 1, 1, 1, 0, 0, 0, 0]) # Model Input x = tf.placeholder(tf.float32, shape=(3, 1)) y = tf.placeholder(tf.float32) # Perceptron init uniform_init = tf.random_uniform_initializer(0, 1) W1 = tf.get_variable("W1", shape=(3,3), initializer = uniform_init) b1 = tf.get_variable("b1", shape=(3,1), initializer = uniform_init) W4 = tf.get_variable("W4", shape=(1, 3), initializer = uniform_init) b4 = tf.get_variable("b4", shape=(1,1), initializer = uniform_init) # Init TF Session sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) # Train l1 = perceptron(W1, x, b1, step, silent=False) l2 = perceptron(W4, l1, b4, step, silent=False) for i in range(8): print(sess.run(l2, {x: x_train[i]})) class_exercise()

    -- Oct 25 In-Class Exercise Thread
    import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    def perceptron(weights, inputs, biases):
        nodes = tf.matmul(weights, inputs) + biases
        return step(nodes)
    
    def step(nodes):
        nodes = tf.clip_by_value(nodes, 0, 1)
        return nodes
    
    def last_layer(weights, inputs, biases):
        nodes = tf.reduce_sum(tf.multiply(weights,inputs)) +biases
        return step(nodes)
    
    tf.reset_default_graph() x = tf.placeholder(tf.float32, shape=(3,1))
    W1 = tf.Variable([[2 , 2 , 2] , [-2/3 , -2/3 , -2/3 ] , [2/5 , 2/5 , 2/5]], dtype=tf.float32)
    b1 = tf.Variable([[-1], [5/3], [-1/5]], dtype=tf.float32)
    W2 = tf.Variable([ 1/2 , 1/2 , 1], dtype=tf.float32)
    b2 = tf.Variable(-1/3, dtype=tf.float32)
    my_layer1 = perceptron(W1, x, b1)
    my_layer2 = last_layer(W2, my_layer1, b2)
    session = tf.Session()
    init = tf.global_variables_initializer()
    session.run(init)
    l2 = session.run(my_layer2, {x: [[0], [1], [0]]})
    print (l2)
    (Edited: 2017-10-29)
    import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' def perceptron(weights, inputs, biases): nodes = tf.matmul(weights, inputs) + biases return step(nodes) def step(nodes): nodes = tf.clip_by_value(nodes, 0, 1) return nodes def last_layer(weights, inputs, biases): nodes = tf.reduce_sum(tf.multiply(weights,inputs)) +biases return step(nodes) tf.reset_default_graph() x = tf.placeholder(tf.float32, shape=(3,1)) W1 = tf.Variable([[2 , 2 , 2] , [-2/3 , -2/3 , -2/3 ] , [2/5 , 2/5 , 2/5]], dtype=tf.float32) b1 = tf.Variable([[-1], [5/3], [-1/5]], dtype=tf.float32) W2 = tf.Variable([ 1/2 , 1/2 , 1], dtype=tf.float32) b2 = tf.Variable(-1/3, dtype=tf.float32) my_layer1 = perceptron(W1, x, b1) my_layer2 = last_layer(W2, my_layer1, b2) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l2 = session.run(my_layer2, {x: [[0], [1], [0]]}) print (l2)
    2017-10-30

    -- Oct 25 In-Class Exercise Thread
    import tensorflow as tf def perceptron(weights, inputs, biases, activation): nodes = tf.reduce_sum(weights * inputs, 1) + biases return activation(nodes) def stepfunction(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=3) W = tf.constant([ [1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0] ]) B = tf.constant([ -0.5, 1.5, -2.5 ]) layer_1 = perceptron(W, x, B, stepfunction) W_2 = tf.constant([ [1.0, 1.0, 2.0] ]) B_2 = tf.constant([ -1.99 ]) layer_2 = perceptron(W_2, layer_1, B_2, stepfunction) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(layer_2, {x: [0, 1, 1]})
    <nowiki>import tensorflow as tf def perceptron(weights, inputs, biases, activation): nodes = tf.reduce_sum(weights * inputs, 1) + biases return activation(nodes) def stepfunction(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=3) W = tf.constant([ [1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0] ]) B = tf.constant([ -0.5, 1.5, -2.5 ]) layer_1 = perceptron(W, x, B, stepfunction) W_2 = tf.constant([ [1.0, 1.0, 2.0] ]) B_2 = tf.constant([ -1.99 ]) layer_2 = perceptron(W_2, layer_1, B_2, stepfunction) session = tf.Session() init = tf.global_variables_initializer() session.run(init) print session.run(layer_2, {x: [0, 1, 1]}) </nowiki>

    -- Oct 25 In-Class Exercise Thread
    Abhinaya Koduri import tensorflow as tf import os def perceptron(weights, inputs, biases, activation): nodes = weights * inputs + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=(3)) w1 = tf.Variable([[2, 2, 2], [-2 / 3, -2 / 3, -2 / 3], [2 / 5, 2 / 5, 2 / 5]], dtype=tf.float32) b1 = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) w2 = tf.Variable([1 / 2, 1 / 2, 1], dtype=tf.float32) b2 = tf.Variable(0.0, dtype=tf.float32) layer1 = perceptron(w1, x, b1, step) layer2 = perceptron(w2, my_layer1, b2, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1= session.run(my_layer1, {x: [0.0, 1.0, 0.0]}) print(l1) print(session.run(layer2, {x:l1[0]}))
    (Edited: 2017-10-30)
    <nowiki>Abhinaya Koduri import tensorflow as tf import os def perceptron(weights, inputs, biases, activation): nodes = weights * inputs + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) x = tf.placeholder(tf.float32, shape=(3)) w1 = tf.Variable([[2, 2, 2], [-2 / 3, -2 / 3, -2 / 3], [2 / 5, 2 / 5, 2 / 5]], dtype=tf.float32) b1 = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) w2 = tf.Variable([1 / 2, 1 / 2, 1], dtype=tf.float32) b2 = tf.Variable(0.0, dtype=tf.float32) layer1 = perceptron(w1, x, b1, step) layer2 = perceptron(w2, my_layer1, b2, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1= session.run(my_layer1, {x: [0.0, 1.0, 0.0]}) print(l1) print(session.run(layer2, {x:l1[0]}))</nowiki>

    -- Oct 25 In-Class Exercise Thread
    Abhinav Tipirisetty import tensorflow as tf def perceptron(weights, inputs, biases, activation): nodes = weights * inputs + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) input = tf.placeholder(tf.float32, shape=(3,)) W1 = tf.Variable([[2, 2, 2], [-2 / 3, -2 / 3, -2 / 3], [2 / 5, 2 / 5, 2 / 5]], dtype=tf.float32) b1 = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) W2 = tf.Variable([1 / 2, 1 / 2, 1], dtype=tf.float32) b2 = tf.Variable(0.0, dtype=tf.float32) layer1 = perceptron(W1, input, b1, step) layer2 = perceptron(W2, layer1, b2, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1= session.run(layer1, {input: [1.0, 1.0, 1.0]}) print(l1) print(session.run(layer2, {input:l1[0]}))
    Abhinav Tipirisetty <nowiki>import tensorflow as tf def perceptron(weights, inputs, biases, activation): nodes = weights * inputs + biases return activation(nodes) def step(nodes): return tf.ceil(tf.clip_by_value(nodes, 0, 1)) input = tf.placeholder(tf.float32, shape=(3,)) W1 = tf.Variable([[2, 2, 2], [-2 / 3, -2 / 3, -2 / 3], [2 / 5, 2 / 5, 2 / 5]], dtype=tf.float32) b1 = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) W2 = tf.Variable([1 / 2, 1 / 2, 1], dtype=tf.float32) b2 = tf.Variable(0.0, dtype=tf.float32) layer1 = perceptron(W1, input, b1, step) layer2 = perceptron(W2, layer1, b2, step) session = tf.Session() init = tf.global_variables_initializer() session.run(init) l1= session.run(layer1, {input: [1.0, 1.0, 1.0]}) print(l1) print(session.run(layer2, {input:l1[0]}))</nowiki>
    [ Next ]
    X