Пример #1
0
def TwoHidenLayerSigmodAndSoftmaxAdaGradMtd(DataLength, NumOfIteration):

    DATA_DIR = './data/fashion'

    mnist = LoadData.LoadDta(DATA_DIR, one_hot=True, reshape=False)

    X = tf.placeholder(tf.float32, [None, 28, 28, 1])
    Y_ = tf.placeholder(tf.float32, [None, 10])

    sess = tf.Session()
    RandNum = random.uniform(0, 0.7)
    #RandNum = 0.6124315340304713
    #RandNum = 0
    print('RandNum........')
    print(RandNum)
    step = 0.00000005
    #step = 0.00001
    #weights
    L1 = 300
    L2 = 100
    L3 = 10

    W1 = tf.Variable(tf.truncated_normal([784, L1], stddev=RandNum))
    W2 = tf.Variable(tf.truncated_normal([L1, L2], stddev=RandNum))
    W3 = tf.Variable(tf.truncated_normal([L2, L3], stddev=RandNum))

    #bias

    B1 = tf.Variable(tf.zeros([1, L1], tf.float32))
    B2 = tf.Variable(tf.zeros([1, L2], tf.float32))
    B3 = tf.Variable(tf.zeros([1, L3], tf.float32))
    XX = tf.reshape(X, [-1, 784])
    #Model

    Y1 = tf.nn.sigmoid(tf.matmul(XX, W1) + B1)
    Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2) + B2)
    #Y1 = tf.nn.softmax(tf.matmul(XX, W1) + B1)
    #Y2 = tf.nn.softmax(tf.matmul(Y1, W2) + B2)

    Ylogits = tf.matmul(Y2, W3) + B3
    Y = tf.nn.softmax(Ylogits)

    #Cross Entropy
    #cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits,
                                                            labels=Y_)
    cross_entropy = tf.reduce_mean(cross_entropy)

    # # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # # training, learning rate = 0.005
    lr = 0.0001 + tf.train.exponential_decay(0.03, step, 2000, 1 / math.e)
    #train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy)

    train_step = tf.train.AdagradOptimizer(lr).minimize(cross_entropy)

    #cost = tf.reduce_mean(tf.math.square(Y_ - Y))
    #cost = -tf.reduce_mean(Y_ * (Y))
    #train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    TrngCrossEntropy = []
    TestCrossEntropy = []
    Testacc_Array = []
    acc_Array = []
    print(
        'Two HiddenLayer Sigmoid and softmax Adagrad  Traing Started>...............................'
    )
    print('Two Iteretion Number **************')
    for i in range(1, NumOfIteration):

        # training on batches of 100 images with 100 labels
        batch_X, batch_Y = mnist.train.next_batch(DataLength)

        #print(i)

        sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y})
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W1, B1, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        acc_Array.append(a)
        TrngCrossEntropy.append(c)
        #TestAttrGen,TestClasGen = GenerateData(DataLength)
        ct, y, y_, w, b, at = sess.run(
            [cross_entropy, Y, Y_, W1, B1, accuracy],
            feed_dict={
                X: mnist.test.images[1:1000, :, :],
                Y_: mnist.test.labels[1:1000, :]
            })
        #c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W1, B1,accuracy], feed_dict={X: batch_X, Y_: batch_Y})
        Testacc_Array.append(at)
        TestCrossEntropy.append(ct)
        if ((i % 1000) == 0):
            print('Iteration ')
            print(i)
            print('Accuracy')
            print(at)
            print('Cross-Entropy')
            print(ct)

    print('Singe Layer Traing completed')

    return acc_Array, Testacc_Array, TrngCrossEntropy, TestCrossEntropy
def NoHidenLayerMtd(DataLength, NumOfIteration):

    step = 0.00005

    DATA_DIR = './data/fashion'

    mnist = LoadData.LoadDta(DATA_DIR, one_hot=True, reshape=False)

    X = tf.placeholder(tf.float32, [None, 28, 28, 1])
    Y_ = tf.placeholder(tf.float32, [None, 10])

    sess = tf.Session()

    #Random Number
    Randnum = random.uniform(0, 0.5)
    #Randnum = 0.04526982

    #weights

    #W = tf.Variable(tf.zeros([784,10],tf.float32))
    W = tf.Variable(tf.truncated_normal([784, 10], stddev=Randnum))

    #bias

    B = tf.Variable(tf.zeros([1, 10], tf.float32))
    XX = tf.reshape(X, [-1, 784])
    #Model
    Y = tf.nn.softmax(tf.matmul(XX, W) + B)
    #Cross Entropy
    cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))

    # # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # # training, learning rate = 0.005
    lr = 0.0005 + tf.train.exponential_decay(0.03, step, 2000, 1 / math.e)
    train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy)
    #train_step = tf.train.GradientDescentOptimizer(0.0005).minimize(cross_entropy)

    #cost = tf.reduce_mean(tf.math.square(Y_ - Y))
    #cost = -tf.reduce_mean(Y_ * (Y))
    #train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    TrngCrossEntropy = []
    TestCrossEntropy = []
    Testacc_Array = []
    acc_Array = []
    print('Singe Layer Traing Started>...............................')
    print('First Iteretion Number **************')
    for i in range(1, NumOfIteration):

        # training on batches of 100 images with 100 labels
        batch_X, batch_Y = mnist.train.next_batch(DataLength)

        #print(i)

        sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y})
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W, B, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        acc_Array.append(a)
        TrngCrossEntropy.append(c)
        #TestAttrGen,TestClasGen = GenerateData(DataLength)
        #c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B,accuracy], feed_dict={X: mnist.test.images, Y_: mnist.test.labels})
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W, B, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        Testacc_Array.append(a)
        TestCrossEntropy.append(c)

    print('Singe Layer Traing completed')

    #acc_Array.append(a)#
    print('c')

    print(c)
    print('y_Round')
    print(np.round(y, 2))
    print('y_Ceil')
    #print(np.round(y) +' '+ y_)

    print(np.round(y))

    print('y_')
    print(y_)
    print('w')
    print(w)
    print('b')
    print(b)
    print('a')
    print(a)
    return acc_Array, Testacc_Array, TrngCrossEntropy, TestCrossEntropy
Пример #3
0
def FiveHidenLayerReluMtd(DataLength, NumOfIteration):

    DATA_DIR = './data/fashion'

    mnist = LoadData.LoadDta(DATA_DIR, one_hot=True, reshape=False)

    X = tf.placeholder(tf.float32, [None, 28, 28, 1])
    Y_ = tf.placeholder(tf.float32, [None, 10])

    sess = tf.Session()
    #RandNum = random.uniform(0,0.5)
    RandNum = 0.23197358164873916
    print('RandNum........')
    print(RandNum)
    step = 0.00000005
    #weights
    L1 = 300
    L2 = 100
    L3 = 10
    #L4 = 15
    #L5 = 10

    W1 = tf.Variable(tf.truncated_normal([784, L1], stddev=RandNum))
    W2 = tf.Variable(tf.truncated_normal([L1, L2], stddev=RandNum))
    W3 = tf.Variable(tf.truncated_normal([L2, L3], stddev=RandNum))
    #W4 = tf.Variable(tf.truncated_normal([L3,L4],stddev=RandNum))
    #W5= tf.Variable(tf.truncated_normal([L4,L5],stddev=RandNum))

    #bias

    B1 = tf.Variable(tf.zeros([1, L1], tf.float32))
    B2 = tf.Variable(tf.zeros([1, L2], tf.float32))
    B3 = tf.Variable(tf.zeros([1, L3], tf.float32))
    #B4 = tf.Variable(tf.zeros([1,L4],tf.float32))
    #B5 = tf.Variable(tf.zeros([1,L5],tf.float32))
    XX = tf.reshape(X, [-1, 784])
    #Model

    Y1 = tf.nn.relu(tf.matmul(XX, W1) + B1)
    Y2 = tf.nn.relu(tf.matmul(Y1, W2) + B2)
    #Y3 = tf.nn.relu(tf.matmul(Y2, W3) + B3)
    #Y4 = tf.nn.relu(tf.matmul(Y3, W4) + B4)
    Ylogits = tf.matmul(Y2, W3) + B3
    Y = tf.nn.softmax(Ylogits)
    #Cross Entropy
    #cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits,
                                                            labels=Y_)
    cross_entropy = tf.reduce_mean(cross_entropy)

    # # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # # training, learning rate = 0.005
    lr = 0.000001 + tf.train.exponential_decay(0.03, step, 2000, 1 / math.e)
    #train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy)

    train_step = tf.train.GradientDescentOptimizer(lr).minimize(cross_entropy)

    #cost = tf.reduce_mean(tf.math.square(Y_ - Y))
    #cost = -tf.reduce_mean(Y_ * (Y))
    #train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    TrngCrossEntropy = []
    TestCrossEntropy = []
    Testacc_Array = []
    acc_Array = []
    print('Singe Layer Traing Started>...............................')
    print('Third Iteretion Number **************')
    for i in range(1, NumOfIteration):

        # training on batches of 100 images with 100 labels
        batch_X, batch_Y = mnist.train.next_batch(DataLength)

        #print(i)

        sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y})
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W1, B1, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        acc_Array.append(a)
        TrngCrossEntropy.append(c)
        #TestAttrGen,TestClasGen = GenerateData(DataLength)
        #c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B1,accuracy], feed_dict={X: mnist.test.images, Y_: mnist.test.labels})
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W1, B1, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        Testacc_Array.append(a)
        TestCrossEntropy.append(c)

    print(' Layer Traing completed')

    #acc_Array.append(a)#
    print('c')

    print(c)
    print('y_Round')
    print(np.round(y, 2))
    print('y_Ceil')
    #print(np.round(y) +' '+ y_)

    print(np.round(y))

    print('y_')
    print(y_)
    print('w')
    print(w)
    print('b')
    print(b)
    print('a')
    print(a)
    return acc_Array, Testacc_Array, TrngCrossEntropy, TestCrossEntropy
def TwoHidenLayerMtd(DataLength,NumOfIteration):
    
    
     
    DATA_DIR = './data/fashion'
    
    mnist = LoadData.LoadDta(DATA_DIR,one_hot=True, reshape=False)
    
    X = tf.placeholder(tf.float32,[None,28,28,1])
    Y_ = tf.placeholder(tf.float32,[None,10])

    sess = tf.Session()
         

    #weights
    L1 = 300
    L2 = 100
    L3 =10

    W1 = tf.Variable(tf.zeros([784,L1],tf.float32))
    W2 = tf.Variable(tf.zeros([L1,L2],tf.float32))
    W3 = tf.Variable(tf.zeros([L2,L3],tf.float32))

    #bias

    B1 = tf.Variable(tf.zeros([1,L1],tf.float32))
    B2 = tf.Variable(tf.zeros([1,L2],tf.float32))     
    B3 = tf.Variable(tf.zeros([1,L3],tf.float32))
    XX = tf.reshape(X, [-1, 784])
    #Model
    
    Y1 = tf.nn.sigmoid(tf.matmul(XX, W1) + B1)
    Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2) + B2)
    Y = tf.nn.softmax(tf.matmul(Y2, W3) + B3)
    #Cross Entropy 
    cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))


    
    
    # # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # # training, learning rate = 0.005
    train_step = tf.train.GradientDescentOptimizer(0.0005).minimize(cross_entropy)

    #cost = tf.reduce_mean(tf.math.square(Y_ - Y))
    #cost = -tf.reduce_mean(Y_ * (Y))
    #train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init) 
     
    TrngCrossEntropy = []
    TestCrossEntropy =[]
    Testacc_Array = []
    acc_Array = []
    print('Singe Layer Traing Started>...............................')
    for i in range(1,NumOfIteration):
    
        
            
        # training on batches of 100 images with 100 labels
        batch_X, batch_Y = mnist.train.next_batch(DataLength)

        print('Iteretion Number **************')
        print(i)
        
        sess.run(train_step,feed_dict = {X: batch_X,Y_: batch_Y})
        c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B,accuracy], feed_dict={X: batch_X, Y_: batch_Y})
        acc_Array.append(a)
        TrngCrossEntropy.append(c)
        #TestAttrGen,TestClasGen = GenerateData(DataLength)
        #c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B,accuracy], feed_dict={X: mnist.test.images, Y_: mnist.test.labels})    
        c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B,accuracy], feed_dict={X: batch_X, Y_: batch_Y})    
        Testacc_Array.append(a)
        TestCrossEntropy.append(c)


    print('Singe Layer Traing completed')       

    #acc_Array.append(a)#
    print('c')


    print(c)
    print('y_Round')
    print(np.round(y,2))
    print('y_Ceil')
    #print(np.round(y) +' '+ y_)

    print(np.round(y))

    print('y_')
    print(y_)
    print('w')
    print(w)
    print('b')
    print(b)
    print('a')
    print(a)    
    return acc_Array,Testacc_Array,TrngCrossEntropy,TestCrossEntropy
def NoHidenLayerMtd(DataLength, NumOfIteration):

    DATA_DIR = './data/fashion'

    mnist = LoadData.LoadDta(DATA_DIR, one_hot=True, reshape=False)

    X = tf.placeholder(tf.float32, [None, 28, 28, 1])
    Y_ = tf.placeholder(tf.float32, [None, 10])

    sess = tf.Session()

    #weights

    W = tf.Variable(tf.zeros([784, 10], tf.float32))

    #bias

    B = tf.Variable(tf.zeros([1, 10], tf.float32))
    XX = tf.reshape(X, [-1, 784])
    #Model
    Y = tf.nn.softmax(tf.matmul(XX, W) + B)
    #Cross Entropy
    cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y))

    # # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # # training, learning rate = 0.005
    train_step = tf.train.GradientDescentOptimizer(0.005).minimize(
        cross_entropy)

    #cost = tf.reduce_mean(tf.math.square(Y_ - Y))
    #cost = -tf.reduce_mean(Y_ * (Y))
    #train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)
    # Create a summary to monitor cost tensor
    tf.summary.scalar("loss", cross_entropy)

    # Create a summary to monitor accuracy tensor
    tf.summary.scalar("accuracy", accuracy)
    tf.summary.histogram("Weights", W)
    tf.summary.histogram("Bios", B)

    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter(
        "E:\\Study_materials\\Semester-4\\Project\\SourceCode\\Mid_Sem_Code\\FashionMaster25022019_backup\\FashionMaster02_backup\\tmp\\mnist_demo\\3"
    )
    writer.add_graph(sess.graph)

    # init
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)

    TrngCrossEntropy = []
    TestCrossEntropy = []
    Testacc_Array = []
    acc_Array = []

    print('No Hidden Layer Traing Started>...............................')
    print('First Iteretion Number **************')
    for i in range(1, NumOfIteration):

        # training on batches of 100 images with 100 labels
        batch_X, batch_Y = mnist.train.next_batch(DataLength)
        batchShapeX = batch_X.shape
        batchShapeY = batch_Y.shape

        _, s = sess.run([train_step, merged_summary],
                        feed_dict={
                            X: batch_X,
                            Y_: batch_Y
                        })
        c, y, y_, w, b, a = sess.run([cross_entropy, Y, Y_, W, B, accuracy],
                                     feed_dict={
                                         X: batch_X,
                                         Y_: batch_Y
                                     })
        acc_Array.append(a)
        TrngCrossEntropy.append(c)
        #TestAttrGen,TestClasGen = GenerateData(DataLength)
        ct, y, y_, w, b, at = sess.run([cross_entropy, Y, Y_, W, B, accuracy],
                                       feed_dict={
                                           X: mnist.test.images[1:1000, :, :],
                                           Y_: mnist.test.labels[1:1000, :]
                                       })
        #c, y,y_,w, b,a = sess.run([cross_entropy,Y,Y_, W, B,accuracy], feed_dict={X: batch_X, Y_: batch_Y})
        Testacc_Array.append(at)
        TestCrossEntropy.append(ct)

        #        s = sess.run(merged_summary,feed_dict = {X: batch_X,Y_: batch_Y})
        writer.add_summary(s, i)

        # if ((i % 1000) == 0):
        # print('Iteration ')
        # print(i)
        # print('Accuracy')
        # print(at)
        # print('Cross-Entropy')
        # print(ct)

    print('No Hidden Layer Traing completed')
    np.savetxt(
        'E:\\Study_materials\\Semester-4\\Project\\FinalSemProgress\\PlotData\\W1.txt',
        w)
    np.savetxt(
        'E:\\Study_materials\\Semester-4\\Project\\FinalSemProgress\\PlotData\\B1.txt',
        b)

    return acc_Array, Testacc_Array, TrngCrossEntropy, TestCrossEntropy