Exemple #1
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True,
          is_plot=True):
    ops.reset_default_graph()  # 能够重新运行模型而不覆盖tf变量
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape  # 获取输入节点数量和样本数
    n_y = Y_train.shape[0]  # 获取输出节点数量
    costs = []

    X, Y = create_placeholders(n_x, n_y)
    parameters = initialize_parameters([12288, 25, 12, 6])
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3, Y)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        for i in range(num_epochs):
            epoch_cost = 0  # 每代的成本
            num_minibatches = int(m / minibatch_size)  # minibatch的总数量
            seed += 1
            minibatches = tf_utils.random_mini_batches(
                X_train, Y_train, mini_batch_size=minibatch_size, seed=seed)
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })
                epoch_cost += minibatch_cost / num_minibatches
            if i % 5 == 0:
                costs.append(epoch_cost)
                if print_cost and i % 100 == 0:
                    print("epoch ={},epoch_cost ={}".format(i, epoch_cost))
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()
        parameters = sess.run(parameters)
        print("参数已经保存到session。")
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #2
0
def model(X_train, Y_train, X_test, Y_test, learning_rate=0.0001,
          num_epochs=1500, minibatch_size=32, print_cost=True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    X, Y = create_placeholders(n_x, n_y)

    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    #cost is compute graph
    cost = compute_cost(Z3, Y)
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0
            num_minibatches = int(m/minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y})
                epoch_cost += minibatch_cost/num_minibatches
            if print_cost == True and epoch % 100 == 0:
                print('Cost after epoch %i: %f' %(epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title('Learning rate =' + str(learning_rate))
        plt.show()
        print('Parameters have been trained')

        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
        print('Train Accuracy:', accuracy.eval({X:X_train, Y:Y_train}))
        print('Test Accuracy:', accuracy.eval({X:X_test, Y:Y_test}))
        return parameters
Exemple #3
0
def model(X_train,Y_train,X_test,Y_test,learning_rate = 0.0001,
          num_epochs = 1500, minibatch_size = 32, print_cost = True):
    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (n_x,m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    X,Y = create_placeholders(n_x,n_y)
    
    parameters = initialize_parameters()
    
    Z3 = forward_propagation(X,parameters)
    
    cost = compute_cost(Z3,Y)
    
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
    
    init = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init)
        
        for epoch in range(num_epochs):
            epoch_cost = 0.
            num_minibatches = int(m/minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train,Y_train,minibatch_size,seed)
            
            for minibatch in minibatches:
                (minibatch_X,minibatch_Y) = minibatch
                _ , minibatch_cost = sess.run([optimizer,cost],feed_dict = {X:minibatch_X,Y:minibatch_Y})
                
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches
                
            if print_cost == True and epoch%100 == 0:
                print("Cost after epoch %i: %f"%(epoch,epoch_cost))
            if print_cost == True and epoch%5 == 0:
                costs.append(epoch_cost)
        
        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()
        
        #save the parameters in a variable
        parameters = sess.run(parameters)
        print("parameters have been trained")

        correct_prediction = tf.equal(tf.argmax(Z3),tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
        
        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #4
0
def model(X_original, Y_original, layers_dims, learning_rate=0.0007, mini_batch_size=64, num_epochs=10000, print_cost=True):
    ops.reset_default_graph()  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)
    seed = 10
    costs = list()

    n_x = X_original.shape[0]
    n_y = Y_original.shape[0]
    m = X_original.shape[1]

    X, Y = create_placeholder(n_x=n_x, n_y=n_y)
    parameters = initialize_parameters(layers_dims=layers_dims)
    ZL = forward_prop(X=X, parameters=parameters)
    cost = compute_loss(ZL=ZL, Y=Y)
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    init = tf.global_variables_initializer()
    with tf.Session() as sesh:
        sesh.run(init)

        # Do the training loop
        for epoch in range(num_epochs):
            epoch_cost = 0.
            num_minibatches = int(m / mini_batch_size)  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            mini_batches = random_mini_batches(X_original, Y_original, mini_batch_size, seed)

            for mini_batch in mini_batches:
                mini_batch_X, mini_batch_Y = mini_batch

                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                _, mini_batch_cost = sesh.run([optimizer, cost],  # don't need value from optimizer
                                              feed_dict={X: mini_batch_X,
                                                         Y: mini_batch_Y})
                epoch_cost += mini_batch_cost / num_minibatches

            # Print the cost every 1000 epoch
            if print_cost and epoch % 1000 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost and epoch % 100 == 0:
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sesh.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))
        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print("Train Accuracy:", accuracy.eval({X: X_original, Y: Y_original}))
        return parameters
Exemple #5
0
def model(X_train, Y_train, X_test, Y_test,
          learning_rate=0.0001, num_epochs=1500, minibatch_size=32,
          print_cost=True, is_plot=True):
    ops.reset_default_graph() # 能够重新运行模型而不覆盖tf变量
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    X, Y = create_placeholders(n_x, n_y)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3, Y)

    # 反向传播,使用Adam优化
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
    # 初始化所有的变量
    init = tf.global_variables_initializer()
    # 开始会话并计算
    with tf.Session() as session:
        session.run(init)

        for epoch in range(num_epochs):
            epoch_cost = 0
            num_minibatches = int(m/minibatch_size)
            seed = seed + 1
            minibatches = tf_utils.random_mini_batches(X_train,Y_train,minibatch_size,seed)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = session.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})
                epoch_cost = epoch_cost + minibatch_cost/num_minibatches

            if epoch % 5 == 0:
                costs.append(epoch_cost)
                if print_cost and epoch % 100 == 0:
                    print("epoch = " + str(epoch) + "  epoch_cost = " + str(epoch_cost))

        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.xlabel('iterations')
            plt.ylabel('cost')
            plt.title('learning_rate = ' + str(learning_rate))
            plt.show()

        parameters = session.run(parameters)
        print("参数已经保存到session中")

        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

    return parameters
Exemple #6
0
def model(X_train,Y_train,X_test,Y_test,layers,learning_rate=0.0001,
            num_epochs=200,minibatch_size=32,print_cost=True):
    (d_x,num_pics)=X_train.shape
    d_y=Y_train.shape[0]
    #ops.reset_default_graph()
    tf.set_random_seed(1)
    seed=3
    costs=[]

    X,Y=creat_placeholders(d_x,d_y)

    parameters=initial_parameters(layers)

    Z=forward_propagation(X,parameters)

    cost=compute_cost(Z,Y)

    optmizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    init=tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(num_epochs):
            num_minibatches=int(num_pics/minibatch_size)
            seed += 1
            minibatches=random_mini_batches(X_train, Y_train, minibatch_size, seed)
            epoch_cost=0
            for mbatch_x,mbatch_y in minibatches:
                _,minibatch_cost=sess.run([optmizer,cost],feed_dict={X:mbatch_x,Y:mbatch_y})
                epoch_cost += minibatch_cost/num_minibatches

            if print_cost == True and epoch % 10 == 0:
                costs.append(epoch_cost)
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
                #print(mbatch_x[0,:])

        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        parameters=sess.run(parameters)

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #7
0
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.00001,
          num_epochs = 25, minibatch_size = 32, print_cost = True):
    """
    调用函数进行训练
    :param X_train:
    :param Y_train:
    :param X_test:
    :param Y_test:
    :param learning_rate:
    :param num_epochs:
    :param minibatch_size:
    :param print_cost:
    :return:
    """
    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []
    X, Y = create_placeholders(n_x, n_y)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3, Y)
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict=
                                                        {X: minibatch_X, Y: minibatch_Y})
                print(minibatch_cost)
                epoch_cost += minibatch_cost / num_minibatches
            if print_cost == True and epoch % 15 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
        plt.plot(np.squeeze(costs))
        plt.ylabel('损失')
        plt.xlabel('迭代 ')
        plt.title("学习率" + str(learning_rate))
        plt.show()
        parameters = sess.run(parameters)
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print ("训练集学习率:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("测试集学习率:", accuracy.eval({X: X_test, Y: Y_test}))
        return parameters
Exemple #8
0
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
          num_epochs = 1500, minibatch_size = 32, print_cost = True):
		  
    ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)                             # to keep consistent results
    seed = 3                                          # to keep consistent results
    (n_x, m) = X_train.shape                          # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]                            # n_y : output size
    costs = []                                        # To keep track of the cost
    
    X, Y = create_placeholders(n_x,n_y)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X,parameters)
    cost = compute_cost(Z3,Y)
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0.                           # Defines a cost related to an epoch
            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _ , minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})
                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
                
        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        parameters = sess.run(parameters)
        print ("Parameters have been trained!")
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
        
        return parameters
Exemple #9
0
def model(X_train,Y_train,X_test,Y_test,learning_rate=0.0001,num_epochs=1500,minibatch_size=32,print_cost):
	ops.reset_default_graph()
	tf.set_random_seed(1)
	seed=3
	(n_x,m) = X_train.shape
	n_y = Y_train.shape[0]
	costs = []

	X,Y = create_placeholders(n_x,n_y)

	parameters = initialize_parameters()

	Z3 = forward_propagation(X,parameters)

	cost = compute_cost(Z3,Y)

	optimizer = tf.train.AdamOptimizer(learning_rate =learning_rate).minimize(cost)
	init = tf.global_variables_initializer()

	with tf.Session() as sess:
		sess.run(init)

		for epoch in range(num_epochs):
			epoch_cost=0
			num_minibatches = int(m/minibatch_size)
			seed=seed+1;
			minibatches = random_mini_batches(X_train,Y_train,minibatch_size,seed)

			for minibatch in minibatches:
				(minibatch_X,minibatch_Y) = minibatch

				_,minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})

				epoch_cost += minibatch_cost/num_minibatches


            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)	

         # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        parameters = sess.run(parameters)

 	return parameters
def model(X_train,
          Y_train,
          X_test,
          layer_dims,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):

    #Variables
    ops.reset_default_graph()
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    lambd = 25
    beta = (lambd / m)

    #Creating placeholders
    X, Y = create_placeholders(n_x, n_y)

    #Initialize parameters
    parameters = initialize_parameters(layer_dims)

    #Forward Propagation
    ZL = forward_propagation(X, parameters)

    #Cost
    cost = compute_cost(ZL, Y, parameters, beta)

    #Backpropagation and Optimizing
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    #Tensorflow init
    init = tf.global_variables_initializer()

    #Starting the Session and the Model
    with tf.Session() as sess:

        #Running the init
        sess.run(init)

        #Training Loop
        for epoch in range(num_epochs):

            #Making the Minibatches
            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            #Iterating over the minibatches
            for minibatch in minibatches:

                (minibatch_X, minibatch_Y) = minibatch
                #Running the session for the whole model
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / minibatch_size

            #Printing the cost after 10 epochs
            if print_cost and epoch % 10 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost and epoch % 5 == 0:
                costs.append(epoch_cost)
        #Training Ends
        print("Cost after epoch %i: %f" % (num_epochs, costs[-1]))
        # Plotting the Cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per fives)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))

        return parameters
def model(X_train, Y_train, X_test, Y_test, learning_rate=0.0001, num_epoches=1500, minibatch_size=32, print_cost=True):
    '''
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    '''

    # to be able to rerun the model without overwriting tf variables
    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    # Create placeholder
    X, Y = create_placeholder(n_x, n_y)

    # Initialize parameters
    parameters = Initializing_parameters()

    # Forward propagation
    Z3 = forward_propagation(X, parameters)

    # Cost function
    cost = compute_cost(Z3, Y)

    # Backward propagation
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:
        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epoches):
            epoch_cost = 0
            num_minibatches = int(m / minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch

                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y})

                epoch_cost += minibatch_cost / num_minibatches
            
            if print_cost and epoch % 100 == 0:
                print('Cost after epoch {}: {}'.format(epoch, epoch_cost))
            if print_cost and epoch % 5 == 0:
                costs.append(epoch_cost)
        
        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title('Learning rate={}'.format(learning_rate))
        plt.show()

        # save parameters in a variable
        parameters = sess.run(parameters)
        print('Parameters have been trained!')

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))

        print('Train Accuracy:', accuracy.eval({X:X_train, Y:Y_train}))
        print('Test Accuracy:', accuracy.eval({X:X_test, Y:Y_test}))

        return parameters
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          epochs=1500,
          minibatch_size=32,
          keep_prob=[1, 1],
          print_cost=True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    LINEAR (12288) --> RELU (25) --> RELU (12) --> SOFTMAX (6)
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    # time
    tic = time.process_time()

    tf.reset_default_graph(
    )  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)  # to keep consistent results
    seed = 3  # to keep consistent results
    (
        n_x, m
    ) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost

    # Create Placeholders of shape (n_x, n_y)
    ### START CODE HERE ### (1 line)
    X, Y = create_placeholders(n_x, n_y)
    ### END CODE HERE ###

    # Initialize parameters
    ### START CODE HERE ### (1 line)
    parameters = initialize_parameters()
    ### END CODE HERE ###

    # Forward propagation: Build the forward propagation in the tensorflow graph
    ### START CODE HERE ### (1 line)
    Z3 = forward_propagation(X, parameters, keep_prob)
    ### END CODE HERE ###

    # Cost function: Add cost function to tensorflow graph
    ### START CODE HERE ### (1 line)
    cost = compute_cost(Z3, Y)
    ### END CODE HERE ###

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    ### START CODE HERE ### (1 line)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    ### END CODE HERE ###

    # Initialize all the variables
    init = tf.global_variables_initializer()

    # file for early stop (not recommended to use!)
    early_stop = open("early_stop.txt", 'r')

    print("\nStart training...")

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })
                ### END CODE HERE ###

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
                stdout.flush()
                early_stop.seek(0)
                if early_stop.read() == 'True':
                    print("Early Stopped (not recommended)!")
                    costs.append(epoch_cost)
                    break
            if epoch % 5 == 0:
                costs.append(epoch_cost)

        # time
        toc = time.process_time()

        # close early stop file
        early_stop.close()

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.draw()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("\nParameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        print("\nnumber of trainable parameters: " + str(
            np.sum([
                np.prod(v.get_shape().as_list())
                for v in tf.trainable_variables()
            ])))
        print("\ntime: %.3f" % (toc - tic) + " sec, for parameters: ")
        args, _, _, values = inspect.getargvalues(inspect.currentframe())
        [print("{}: {}".format(i, values[i])) for i in args[4:]]

        stdout.flush()

        # Showing plots for non blocking
        plt.show()

        return parameters
Exemple #13
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=10,
          minibatch_size=10000,
          print_cost=True):
    """

    :param X_train: training set, input size = 2, real and imag parts, number of training examples = unknown
    :param Y_train: test set, of shape( output size = 2, number of training examples = same as X_train)
    :param X_test: training set, of shape( input size = 2, number of test examples = 120)
    :param Y_test: test set, of shape( output size = 2, number of test examples = 120)
    :param learning_rate: learning rate
    :param num_epochs: epoch numbers
    :param minibatch_size: size of minibatch
    :param print_cost: True to print the cost every 100 expochs
    :return:
    :parameters -- parameters learnt by the model. They can then be used to predict
    """

    ops.reset_default_graph()
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []

    X, Y = create_placeholders(n_x=n_x, n_y=n_y)
    parameters = initial_Parameter()
    Z3 = forward_propogation(X, parameters)
    cost = compute_cost(Z3, Y)
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0
            num_minibatches = int(m / minibatch_size)
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)
            for minibatch in minibatches:

                (minibatch_X, minibatch_Y) = minibatch

                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / num_minibatches

            if print_cost == True and epoch % 10 == 0:

                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("learning rate = " + str(learning_rate))
        plt.show()

        parameters = sess.run(parameters)
        print("Parameters have been trained")

        # correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        #
        # accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        accuracy = Y + Z3

        print("Train Accuracy: ", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy: ", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #14
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):

    ops.reset_default_graph()
    # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)
    # to keep consistent results
    seed = 3
    # to keep consistent results
    (n_x, m) = X_train.shape
    # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]
    # n_y : output size
    costs = []

    #X=tf.placeholder(X_train,shape=(n_x,m))
    #Y=tf.placeholder(Y_train,shape=(n_y,1))
    X, Y = create_placeholders(n_x, n_y)

    parameters = initialize_parameters()

    Z3 = forward_propagation(X, parameters)

    cost = compute_cost(Z3, Y)

    optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=learning_rate).minimize(cost)
    #_,c = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})

    init = tf.global_variables_initializer()

    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })
                ### END CODE HERE ###

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
def GestureRecognition():
    learning_rate = 0.0001
    num_epochs = 1500
    minibatch_size = 32
    print_cost = True
    is_plot = True
    costs = []

    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = tf_utils.load_dataset(
    )
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0],
                                           -1).T  #每一列就是一个样本
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T

    m = X_train_flatten.shape[1]

    #归一化数据
    X_train = X_train_flatten / 255
    X_test = X_test_flatten / 255

    #转换为独热矩阵
    Y_train = tf_utils.convert_to_one_hot(Y_train_orig, 6)
    Y_test = tf_utils.convert_to_one_hot(Y_test_orig, 6)

    tf.set_random_seed(1)
    seed = 3
    tf.compat.v1.reset_default_graph()  # 用于清除默认图形堆栈并重置全局默认图形。

    X = tf.compat.v1.placeholder(tf.float32, [X_train.shape[0], None],
                                 name="X")
    Y = tf.compat.v1.placeholder(tf.float32, [Y_train.shape[0], None],
                                 name="Y")

    parameters = InitParameter([12288, 25, 12, 6])

    z = ForwardPropagation(X, parameters)
    cost = ComputeCost(z, Y)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    init = tf.global_variables_initializer()

    with tf.compat.v1.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0  # 每代的成本
            num_minibatches = int(m / minibatch_size)  # minibatch的总数量
            seed = seed + 1
            minibatches = tf_utils.random_mini_batches(X_train, Y_train,
                                                       minibatch_size, seed)

            for minibatch in minibatches:
                # 选择一个minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # 数据已经准备好了,开始运行session
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                # 计算这个minibatch在这一代中所占的误差
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches

            # 记录并打印成本
            ## 记录成本
            if epoch % 5 == 0:
                costs.append(epoch_cost)
                # 是否打印:
                if print_cost and epoch % 100 == 0:
                    print("epoch = " + str(epoch) + "    epoch_cost = " +
                          str(epoch_cost))

        # 是否绘制图谱
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()

        # 保存学习后的参数
        parameters = sess.run(parameters)
        print("参数已经保存到session。")

        # 计算当前的预测结果
        correct_prediction = tf.equal(tf.argmax(z), tf.argmax(Y))

        # 计算准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #16
0
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
          num_epochs = 1500, minibatch_size = 64, print_cost = True):

    ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)                             # to keep consistent results
    seed = 1
#    seed = np.random.randint(0,10000)                                          # to keep consistent results
    (n_x, m) = X_train.shape                          # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]                            # n_y : output size
    costs = []                                        # To keep track of the cost
    layers_dims = [n_x, 15, 10, 10, n_y]

    # Create Placeholders of shape (n_x, n_y)
    X, Y =  create_placeholders(n_x, n_y)

    # Initialize parameters
    parameters = initialize_parameters(layers_dims)

    # Forward propagation: Build the forward propagation in the tensorflow graph
    ZL = forward_propagation(X, parameters)

    # Cost function: Add cost function to tensorflow graph
    cost = compute_cost(ZL, Y)

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)

    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.                       # Defines a cost related to an epoch
            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch
                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict = {X: minibatch_X, Y: minibatch_Y})

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 1000 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 10 == 0:
                costs.append(epoch_cost)

        # plot the cost
        #plt.plot(np.squeeze(costs))
        #plt.ylabel('cost')
        #plt.xlabel('iterations (per tens)')
        #plt.title("Learning rate =" + str(learning_rate))
        #plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print (accuracy)
        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        y_p = tf.argmax(ZL, axis = 0)

        precision, update = tf.metrics.precision(tf.argmax(Y), tf.argmax(ZL))
        sess.run(tf.local_variables_initializer())

        val_precision = update.eval(feed_dict = {X: X_test, Y: Y_test})
#        sess.run(precision, feed_dict = {X: X_train, Y: Y_train})

#        recall, _ = tf.metrics.recall(labels = tf.argmax(Y), predictions = tf.argmax(ZL))
#        auc, _ = tf.metrics.auc(labels = tf.argmax(Y), predictions = tf.argmax(ZL))
#        cf = tf.confusion_matrix(labels = tf.argmax(Y), predictions = tf.argmax(ZL))
#        val_precision, val_recall, val_auc, val_cf = sess.run([precision, reca], feed_dict= {X:np.random.randn(12288,1080), Y:np.random.randn(6,1080)})
#        print ("Precision", sess.run(precision))
#        print ("Precision", val_precision)
#        print ("recall",val_recall)
#        print ("auc", val_auc)
#        print ("confusion_matrix")
        print (val_precision)


        y_true = np.argmax(Y_train, axis = 0)
        print ("validation accuracy:")
        val_accuracy, y_pred = sess.run([accuracy, y_p], feed_dict={X:X_test, Y: Y_test})
        print (y_true, y_pred)

        print ("Precision",sklearn.metrics.precision_score(y_true, y_pred, average = 'micro'))
        print ("Recall", sklearn.metrics.recall_score(y_true, y_pred, average = 'micro'))
        print ("f1_score", sklearn.metrics.f1_score(y_true, y_pred, average = 'micro'))
        print ("confusion_matrix")
        print (sklearn.metrics.confusion_matrix(y_true, y_pred))
#        fpr, tpr, tresholds = sklearn.metrics.roc_curve(y_true, y_pred)

#        predicted = y_pred
#        actual = y_true
#        TP = tf.count_nonzero(predicted * actual)
#        TN = tf.count_nonzero((predicted - 1) * (actual - 1))
#        FP = tf.count_nonzero(predicted * (actual - 1))
#        FN = tf.count_nonzero((predicted - 1) * actual)
#        precision = TP / (TP + FP)
#        recall = TP / (TP + FN)
#        f1 = 2 * precision * recall / (precision + recall)

        return parameters, costs
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
          num_epochs = 1500, minibatch_size = 32, print_cost = True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    
    ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)                             # to keep consistent results
    seed = 3                                          # to keep consistent results
    (n_x, m) = X_train.shape                          # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]                            # n_y : output size
    costs = []                                        # To keep track of the cost
    
    # Create Placeholders of shape (n_x, n_y)
    ### START CODE HERE ### (1 line)
    X, Y = create_placeholders(n_x, n_y)
    ### END CODE HERE ###

    # Initialize parameters
    ### START CODE HERE ### (1 line)
    parameters = initialize_parameters()
    ### END CODE HERE ###
    
    # Forward propagation: Build the forward propagation in the tensorflow graph
    ### START CODE HERE ### (1 line)
    Z3 = forward_propagation(X, parameters)
    ### END CODE HERE ###
    
    # Cost function: Add cost function to tensorflow graph
    ### START CODE HERE ### (1 line)
    cost = compute_cost(Z3,Y)
    ### END CODE HERE ###
    
    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    ### START CODE HERE ### (1 line)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost)
    ### END CODE HERE ###
    
    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:
        
        # Run the initialization
        sess.run(init)
        
        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.                       # Defines a cost related to an epoch
            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch
                
                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})
                ### END CODE HERE ###
                
                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
                
        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
        
        return parameters
Exemple #18
0
def train(x_train, y_train):

    input_x_flatten_size = x_train.shape[1]
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32,
                           shape=(None, input_x_flatten_size),
                           name='x-input')
        y_ = tf.placeholder(tf.float32,
                            shape=(None, OUTPUT_NODE),
                            name='y-input')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
    y = mnist_inference.inference(x, regularizer, None)

    # Step of training number
    global_step = tf.Variable(0, trainable=False)
    with tf.name_scope('moving_average'):
        variabl_averages = tf.train.ExponentialMovingAverage(
            MOVING_AVERAGE_DECAY, global_step)
        variabl_averages_op = variabl_averages.apply(tf.trainable_variables())

    with tf.name_scope('loss_function'):
        # loss function
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=y, labels=tf.argmax(y_, 1))
        cross_entropy_mean = tf.reduce_mean(cross_entropy)
        loss = cross_entropy_mean + tf.add_n(tf.get_collection('lossess'))

    input_x_number_examples = x_train.shape[0]
    with tf.name_scope('train_step'):
        # learning rate decay
        learning_rate = tf.train.exponential_decay(
            LEARNING_RATE_BASE, global_step,
            input_x_number_examples / BATCH_SIZE, LEARNING_RATE_DECAY)
        # Note as from https://www.tensorflow.org/api_docs/python/tf/train/GradientDescentOptimizer
        # global_step: Optional Variable to increment by one after the variables have been updated.
        train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
            loss, global_step=global_step)

        with tf.control_dependencies([train_step, variabl_averages_op]):
            train_op = tf.no_op(name='train')

    saver = tf.train.Saver(tf.global_variables())
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        seed = 3
        mini_batches = random_mini_batches(tf.transpose(x_train),
                                           tf.transpose(y_train), BATCH_SIZE,
                                           seed)
        for i in range(TRAINING_STEPS):
            k = i % len(mini_batches)
            if k == 0:
                seed = seed + 1
                mini_batches = random_mini_batches(tf.transpose(x_train),
                                                   tf.transpose(y_train),
                                                   BATCH_SIZE, seed)
            mini_x_batches, mini_y_batches = mini_batches[k]
            _, loss_value, step = sess.run([train_op, loss, global_step],
                                           feed_dict={
                                               x: mini_x_batches,
                                               y_: mini_y_batches
                                           })
            if (i % 1000) == 0:
                print(
                    "After %d training step(s), loss on training batch is %g" %
                    (step, loss_value))
                print("----- global_step: ", sess.run(global_step))
                if (step == 1):
                    saver.save(sess,
                               os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
                               global_step=global_step)
                else:
                    saver.save(sess,
                               os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
                               global_step=global_step,
                               write_meta_graph=False)
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_sizes=32,
          print_cost=True):

    ops.reset_default_graph()
    #some values
    (n_x, m) = X_train.shape  #64*64*3
    n_y = Y_train.shape[0]
    n_h1 = 25
    n_h2 = 12
    costs = []
    tf.set_random_seed(1)
    seed = 3

    #input placeholser
    X = tf.placeholder(tf.float32, shape=(n_x, None), name="X")
    Y = tf.placeholder(tf.float32, shape=(n_y, None), name="Y")

    #regularizer
    regularizer = tf.contrib.layers.l2_regularizer(0.1)
    #initialize parameters
    W1 = tf.get_variable(
        "W1", [n_h1, n_x],
        initializer=tf.contrib.layers.xavier_initializer(seed=1),
        regularizer=regularizer)
    b1 = tf.get_variable("b1", [n_h1, 1], initializer=tf.zeros_initializer())
    W2 = tf.get_variable(
        "W2", [n_h2, n_h1],
        initializer=tf.contrib.layers.xavier_initializer(seed=1),
        regularizer=regularizer)
    b2 = tf.get_variable("b2", [n_h2, 1], initializer=tf.zeros_initializer())
    W3 = tf.get_variable(
        "W3", [n_y, n_h2],
        initializer=tf.contrib.layers.xavier_initializer(seed=1),
        regularizer=regularizer)
    b3 = tf.get_variable("b3", [n_y, 1], initializer=tf.zeros_initializer())

    parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2, "W3": W3, "b3": b3}
    print("W1 = " + str(parameters["W1"]))
    print("b1 = " + str(parameters["b1"]))
    print("W2 = " + str(parameters["W2"]))
    print("b2 = " + str(parameters["b2"]))

    #forward propagation
    Z1 = tf.matmul(W1, X) + b1
    A1 = tf.nn.relu(Z1)
    Z2 = tf.matmul(W2, A1) + b2
    A2 = tf.nn.relu(Z2)
    Z3 = tf.matmul(W3, A2) + b3

    #cost
    logits = tf.transpose(Z3)
    labels = tf.transpose(Y)
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
    reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    reg_term = tf.contrib.layers.apply_regularization(regularizer,
                                                      reg_variables)
    cost += reg_term

    #backforward prop adn upadte
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    #initialize all the variables
    init = tf.global_variables_initializer()

    #start session
    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(num_epochs):
            epoch_cost = 0
            num_minibatches = int(m / minibatch_sizes)
            seed = seed + 1

            minibatches = random_mini_batches(X_train, Y_train,
                                              minibatch_sizes, seed)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch

                #execute the optimizer
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / num_minibatches

            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))

                #每100次计算一下正确率
                #caculate the correct predictions
                correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
                #caculate accuracy on the set
                accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

                print("Train Accuracy:", accuracy.eval({
                    X: X_train,
                    Y: Y_train
                }))
                print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        #save the parameters
        #        parameters_tf = {"W1": W1,
        #                  "b1": b1,
        #                  "W2": W2,
        #                  "b2": b2,
        #                  "W3": W3,
        #                  "b3": b3}
        parameters = sess.run(parameters)

        #caculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        #caculate accuracy on the set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

    return parameters
    # Initialize all the variables
    init = tf.global_variables_initializer()
​
    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):
​
            epoch_cost = 0.                       # Defines a cost related to an epoch
            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
​
            for minibatch in minibatches:
​
                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict = {X: minibatch_X, Y: minibatch_Y})
                ### END CODE HERE ###

                epoch_cost += minibatch_cost / minibatch_size
​
            # Print the cost every epoch
Exemple #21
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):

    # Linear -> ReLU -> Linear -> ReLU -> Linear -> Softmax

    ops.reset_default_graph()

    #initialization of model
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    costs = []
    X, Y = create_placeholders(n_x, n_y)
    parameters = initialize_parameters()

    #frorward prop
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3, Y)

    #backprop
    optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=learning_rate).minimize(cost)

    # Tensorflow variables initializer
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(num_epochs):
            epoch_cost = 0
            num_minibatches = int(m / minibatch_size)
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size)
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })
                epoch_cost += minibatch_cost / num_minibatches
            if (print_cost and epoch % 100 == 0):
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if (print_cost and epoch % 5 == 0):
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #22
0
def train_mynetwork(x1_train_set, x2_train_set, x1_train_set_full, x2_train_set_full, x1_test_set, x2_test_set, y_train_set, y_test_set, MODEL,
           learning_rate_base = 0.001, beta_reg = 0.001, num_epochs = 150, minibatch_size = 64, print_cost = True):
    
    ops.reset_default_graph()                       
    tf.set_random_seed(1)                          
    seed = 1                                    
    (m, n_x1) = x1_train_set.shape                        
    (m, n_x2) = x2_train_set.shape
    (m, n_y) = y_train_set.shape                            

    costs = []                                   
    costs_dev = []
    train_acc = []
    val_acc = []
    correct_prediction = 0
    
    # Create Placeholders of shape (n_x, n_y)
    x1, x2, x1_full, x2_full, y, isTraining = create_placeholders(n_x1, n_x2, n_y)

    # Initialize parameters
    parameters = initialize_parameters()
    
    with tf.name_scope("network"):

         joint_layer, l2_loss, x1_de, x2_de = mynetwork(x1, x2, parameters, isTraining)
         
    global_step = tf.Variable(0, trainable = False)
    learning_rate = tf.train.exponential_decay(learning_rate_base, global_step, 30 * m/minibatch_size, 0.5, staircase = True)
    
    with tf.name_scope("optimization"):
         # network optimization
         cost, optimizer = mynetwork_optimaization(joint_layer, y, x1_de, x2_de, x1_full, x2_full, l2_loss, beta_reg, learning_rate, global_step)

    with tf.name_scope("metrics"):
         # Calculate the correct predictions
         joint_layerT = tf.transpose(joint_layer)
         yT = tf.transpose(y)
         correct_prediction = tf.equal(tf.argmax(joint_layerT), tf.argmax(yT))
         accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    # Initialize all the variables
    init = tf.global_variables_initializer()
    saver = tf.train.Saver() 
    
    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:
        
        # Run the initialization
        sess.run(init)
        
        # Do the training loop
        for epoch in range(num_epochs + 1):

            epoch_cost = 0.                       # Defines a cost related to an epoch
            epoch_acc = 0.
            num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(x1_train_set, x2_train_set, x1_train_set_full, x2_train_set_full, y_train_set, minibatch_size, seed)
            for minibatch in minibatches:

                # Select a minibatch
                (batch_x1, batch_x2, batch_x1_full, batch_x2_full, batch_y) = minibatch
                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                _, minibatch_cost, minibatch_acc = sess.run([optimizer, cost, accuracy], feed_dict={x1: batch_x1, x2: batch_x2, x1_full: batch_x1_full, x2_full: batch_x2_full, y: batch_y, isTraining: True})
           
                epoch_cost += minibatch_cost / (num_minibatches+ 1)
                epoch_acc += minibatch_acc / (num_minibatches + 1)
 
            if MODEL.strip() == 'MML':  
               # Multimodal Learning (MML): close BN by "isTraining: False"            
               feature, epoch_cost_dev, epoch_acc_dev = sess.run([joint_layerT, cost, accuracy], feed_dict={x1: x1_test_set, x2: x2_test_set, x1_full: x1_test_set, x2_full: x2_test_set, y: y_test_set, isTraining: False})
           
            if MODEL.strip() == 'CML':  
               # Crossmodal Learning (CML): open BN by "isTraining: True"   
               feature, epoch_cost_dev, epoch_acc_dev = sess.run([joint_layerT, cost, accuracy], feed_dict={x1: x1_test_set, x2: x2_test_set, x1_full: x1_test_set, x2_full: x2_test_set, y: y_test_set, isTraining: True})
            
            # Print the cost every epoch
            if print_cost == True and epoch % 50 == 0:
                print ("epoch %i: Train_loss: %f, Val_loss: %f, Train_acc: %f, Val_acc: %f" % (epoch, epoch_cost, epoch_cost_dev, epoch_acc, epoch_acc_dev))
                
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
                train_acc.append(epoch_acc)
                costs_dev.append(epoch_cost_dev)
                val_acc.append(epoch_acc_dev)
        
        # plot the cost      
        plt.plot(np.squeeze(costs))
        plt.plot(np.squeeze(costs_dev))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()
        # plot the accuracy 
        plt.plot(np.squeeze(train_acc))
        plt.plot(np.squeeze(val_acc))
        plt.ylabel('accuracy')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()
         
        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")
  
        print("save model")
        save_path = saver.save(sess,"D:\Python_Project\MDL-RS/save_EnDe_FC/model.ckpt")
        print("save model:{0} Finished".format(save_path))
  
        return parameters, val_acc, feature
def model_train_MLP(X_train, Y_train, X_test, Y_test, 
                    learning_rate=0.0001, epochs=1500, minibatch_size=32, 
                    seed=1, verbose=0):
    """
    training a three-layer tensorflow neural network: 
        LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX
    
    @param X_train: training set of shape [12288, m], m is the number of training sample size
    @param Y_train: training label of shape [6, m]
    @param X_test: testing set while training with shape [12288, m_tst], m_tst is the number of test sample size 
    @param Y_test: testing label of shape [6, m_tst]
    
    @param learning_rate: learning rate of the optimization
    @param epochs: number of epochs of training loop
    @param minibatch_size: size of a minibatch
    @param seed: random seed
    @param verbose: 0 to keep silence
                    1 to print the cost of training for 10 epoch
                    2 to print the cost of training & testing for 10 epoch
    
    @return: parameters - after model training 
    @return: cost of train and test if need
    """
    
    tf.set_random_seed(seed)
    ops.reset_default_graph()  # re-run of model without overwriting variables

    n_x, m = X_train.shape
    n_y, _ = Y_train.shape
    train_costs = []  # keep track of the train cost
    test_costs = []  # keep track of the test cost
    
    #---- create the computation graph ----# 
    # create placeholder of shape [n_x, n_y] in computation graph
    X = tf.placeholder(dtype=tf.float32, shape=[n_x,None], name='X')
    Y = tf.placeholder(dtype=tf.float32, shape=[n_y,None], name='Y')
    # parameters initialization
    parameters = initialize_parameters_MLP(seed)
    # forward propagation
    Z3 = forward_propagation_MLP(X, parameters)
    # calculation of cost
    cost = compute_cost_MLP(Z3, Y)
    # construction of optimizer
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
                
    init = tf.global_variables_initializer()  # initial all the variables of tf

    #---- Start the session to compute the tensorflow graph ----#
    with tf.Session() as sess:
        sess.run(init)
        
        # training loop
        for epoch in range(epochs):
            epoch_cost = 0.0  # this turn cost
            num_minibatches = int(m/minibatch_size)
            seed += 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)  # subsample
            
            for minibatch in minibatches:
                minibatch_X, minibatch_Y = minibatch
                        
                # run the graph on a minibatch
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X, Y:minibatch_Y})
                epoch_cost += minibatch_cost/num_minibatches
                
            # printing the cost every epoch
            if verbose == 1 and epoch % 10 == 0:
                print("epoch %i: train cost %f" % (epoch, epoch_cost))
                train_costs.append(epoch_cost)   
                   
            if verbose == 2 and epoch % 10 == 0:
                # calculating test cost
                Z3_2 = forward_propagation_MLP(X, sess.run(parameters))  # computation graph
                cost_2 = compute_cost_MLP(Z3_2, Y)
                with tf.Session() as sess_2:  # session
                    test_cost = sess_2.run(cost_2, feed_dict={X:X_test, Y:Y_test})  
                               
                print("epoch %i: train cost%f, test cost%f" % (epoch, epoch_cost, test_cost))
                train_costs.append(epoch_cost)  
                test_costs.append(test_cost)
                
        # save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")        

        # get accuracy result
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))    

        return parameters, train_costs, test_costs
def model(X_train, Y_train, X_valid, Y_valid, learning_rate = 0.0001,
          num_epochs = 100, minibatch_size = 8, print_cost = True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = N, number of training examples = ?)
    Y_train -- test set, of shape (output size = 2, number of training examples = ?)
    X_test -- training set, of shape (input size = N, number of training examples = ?)
    Y_test -- test set, of shape (output size = 2, number of test examples = ?)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    
    ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
    #tf.set_random_seed(1)                             # to keep consistent results
    #seed = 3                                          # to keep consistent results
    (n_x, m) = X_train.shape                          # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]                            # n_y : output size
    costs = []                                        # To keep track of the cost
    
    # Create Placeholders of shape (n_x, n_y)
    X, Y = create_placeholders(n_x, n_y)

    # Initialize parameters
    parameters = initialize_parameters()
    
    #with open("Multiscale_modeling_0-Copy1.txt", "rb") as myFile:
    #    old_parameters = pickle.load(myFile)
    #    print ('loading old parameters successfully')
    
    #parameters = copy_parameters(old_parameters)
    
    # Forward propagation: Build the forward propagation in the tensorflow graph
    A3 = forward_propagation(X, parameters)
    
    # Cost function: Add cost function to tensorflow graph
    cost = compute_cost(A3, Y)
    
    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    
    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:
        
        # Run the initialization
        sess.run(init)
        
        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.                       # Defines a cost related to an epoch
            num_minibatches = (int)(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
            #seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch
                
                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _ , minibatch_cost = sess.run([optimizer, cost], 
                                             feed_dict={X: minibatch_X, 
                                                        Y: minibatch_Y})
                ### END CODE HERE ###
                
                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 10 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 1 == 0:
                costs.append(epoch_cost)
                
        # plot the cost
        plt.figure()
        plt.plot(np.log(np.squeeze(costs)))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.savefig('DNN_Multiscale_Modeling')

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")
        
        
        return parameters
def model(X_train,
          y_train,
          X_test,
          y_test,
          num_epochs=1500,
          minibatch_size=64,
          learning_rate=0.0001,
          print_cost=True):
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape
    n_y = y_train.shape[0]

    #costs=[]

    X, y = create_placeholders(n_x, n_y)

    parameters = initialize_params(n_x, n_y, l1=20, l2=15)

    z3 = forward_prop(X, parameters)
    cost = get_cost(z3, y)

    optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=learning_rate).minimize(cost)

    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(0, num_epochs):
            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)
            num_minibatches = int(m / minibatch_size)
            seed += 1
            minibatches = random_mini_batches(X_train, y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 y: minibatch_Y
                                             })
                epoch_cost += minibatch_cost / num_minibatches

            if print_cost == True and epoch % 100 == 0:
                print(str(epoch_cost) + ' at epoch number ' + str(epoch))

    # sess=tf.Session()
        parameters = sess.run(parameters)
        print('Training done')

        correct_prediction = tf.equal(tf.argmax(z3), tf.argmax(y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print("Train Accuracy:", str((accuracy.eval({
            X: X_train,
            y: y_train
        }))))
        print("Test Accuracy:", str((accuracy.eval({X: X_test, y: y_test}))))
    # sess.close()
    return (parameters)
Exemple #26
0
def train_model(X_train, Y_train, X_test, Y_test):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    ops.reset_default_graph(
    )  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)  # to keep consistent results
    seed = 3  # to keep consistent results
    (
        n_x, m
    ) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost

    # Create Placeholders of shape (n_x, n_y)
    X, Y = create_placeholders(n_x, n_y)

    # Initialize parameters
    parameters = initialize_parameters()

    # Forward propagation: Build the forward propagation in the tensorflow graph
    Z3 = forward_propagation(X, parameters)

    # use softmax to select out the largest index from the one-hot matrix
    predicts = tf.nn.softmax(Z3, name="predicts", axis=0)

    # Cost function: Add cost function to tensorflow graph
    cost = compute_cost(Z3, Y)

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    optimizer = tf.train.AdamOptimizer(
        learning_rate=LEARNING_RATE).minimize(cost)

    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # before epochs saving step 1
        tf.train.write_graph(sess.graph_def, 'out', MODEL_NAME + '.pbtxt',
                             True)

        # Do the training loop
        for epoch in range(NUM_EPOCHS):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / MINIBATCH_SIZE
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, MINIBATCH_SIZE,
                                              seed)

            for minibatch in minibatches:
                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if PRINT_COST == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if PRINT_COST == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # after epoches saving step 2
        saver = tf.train.Saver()
        saver.save(sess, 'out/' + MODEL_NAME + '.chkp')

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.00001,
          num_epochs=1500,
          minibatch_size=16,
          print_cost=True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    tf.set_random_seed(1)  # to keep consistent results
    seed = 3  # to keep consistent results
    (
        n_x, m
    ) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost
    X, Y = create_placeholders(n_x, n_y)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3, Y)
    global_step = tf.Variable(0, trainable=False)
    #learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,
    #                                      10000, 0.96, staircase=True)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y,
                                                 keep_prob: 0.90
                                             })
                epoch_cost += minibatch_cost / num_minibatches
            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()
        parameters = sess.run(parameters)
        print("Parameters have been trained!")
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print("Train Accuracy:",
              accuracy.eval({
                  X: X_train,
                  Y: Y_train,
                  keep_prob: 1
              }))
        print("Test Accuracy:",
              accuracy.eval({
                  X: X_test,
                  Y: Y_test,
                  keep_prob: 1
              }))
        return parameters
Exemple #28
0
def model(X_train, Y_train, X_test, Y_test, costs, learning_rate = 0.001,#learning_rate = 0.0001,
          num_epochs = 200, minibatch_size = 2048, print_cost = True):
          #num_epochs = 100, minibatch_size = 32, print_cost = True):
    """
    A three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set
    Y_train -- test set
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    
    ops.reset_default_graph()
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]
    
    X, Y = create_placeholders(n_x, n_y)

    parameters = initialize_parameters(n_x, n_y)
    
    Zf = forward_propagation(X, parameters)
    
    cost = compute_cost(Zf, Y, parameters)

    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
    
    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        
        sess.run(init)
        
        for epoch in range(num_epochs):

            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size)

            for minibatch in minibatches:

                (minibatch_X, minibatch_Y) = minibatch
                
                _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})
                
                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)
                
        # plot the cost
        #plt.plot(np.squeeze(costs[1:]))
        plt.plot(np.squeeze(costs))
        
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print ("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Zf), tf.argmax(Y))
        #correct_prediction = tf.equal(tf.argmax(Z3[1:,]), tf.argmax(Y[1:,]))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
        
        
        confusion = tf.confusion_matrix(labels=tf.argmax(Y), predictions=tf.argmax(Zf), num_classes=2)
        print("Train Confusion Matrix:")
        print(pd.DataFrame(confusion.eval({X: X_train, Y: Y_train}), index = ['Actual Survival', 'Actual Default'], columns = ['Predicted Survival', 'Predicted Default']))
        print("Test Confusion Matrix:")
        print(pd.DataFrame(confusion.eval({X: X_test, Y: Y_test}), index = ['Actual Survival', 'Actual Default'], columns = ['Predicted Survival', 'Predicted Default']))
        
#        auc = tf.metrics.auc(labels=tf.argmax(Y), predictions=tf.argmax(Z3))
#        print ("Train AUC:", auc.eval({X: X_train, Y: Y_train}))
#        print ("Test AUC:", auc.eval({X: X_test, Y: Y_test}))
        
        
#########This part has been added to compute the AUC of the Model
        #Vector of probabilities of surviving
        Y_train_pred = tf.nn.softmax(Zf, axis = 0).eval({X: X_train})[0]
        Y_test_pred = tf.nn.softmax(Zf, axis = 0).eval({X: X_test})[0]
        
        #Vector of status with 0 for Surviving loan and 1 for defaulting loan
        y_train = Y_train[1,]
        y_test = Y_test[1,]
        
        fpr_train, tpr_train, thresholds_train = metrics.roc_curve(y_train, Y_train_pred, pos_label=0)
        print("Train AUC: ", metrics.auc(fpr_train, tpr_train) )

    
        fpr_test, tpr_test, thresholds_test = metrics.roc_curve(y_test, Y_test_pred , pos_label=0)
        print("Test AUC: ", metrics.auc(fpr_test, tpr_test) )
        
        plt.figure()
        plt.plot(fpr_train, tpr_train, color='darkslategray', lw=2, label='ROC curve Train (area = %0.2f)' % metrics.auc(fpr_train, tpr_train))
        plt.plot(fpr_test, tpr_test, color='darkred', lw=2, label='ROC curve Test (area = %0.2f)' % metrics.auc(fpr_test, tpr_test))
        plt.plot([0, 1], [0, 1], color='goldenrod', lw=2, linestyle='--')


        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title('ROC')
        plt.legend(loc="lower right")
        plt.show()
        
        df = pd.DataFrame(confusion.eval({X: X_train, Y: Y_train}))
        sn.set(font_scale=1.4)#for label size
        sn.heatmap(df, annot=True,annot_kws={"size": 16})
        
        return parameters
Exemple #29
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):
    """
    Implements a three-layer (2 hidden) tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    (
        n_x, m
    ) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost

    # Create Placeholders of shape (n_x, n_y)
    X, Y = utils.create_placeholders(n_x, n_y)

    # Initialize parameters
    parameters = initialize_parameters()

    # Forward propagation: Build the forward propagation in the tensorflow graph
    Z3 = forward_propagation(X, parameters)

    # Cost function: Add cost function to tensorflow graph
    cost = compute_cost(Z3, Y)

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            minibatches = utils.random_mini_batches(X_train, Y_train,
                                                    minibatch_size)

            for minibatch in minibatches:
                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the optimizer and the cost,
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost and epoch % 100 == 0:
                print("epoch %i %f" % (epoch, epoch_cost))
            if print_cost and epoch % 5 == 0:
                costs.append(epoch_cost)
        # print (parameters)
        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        # print (parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1000,
          minibatch_size=32,
          print_cost=True,
          is_plot=True):
    """
    实现一个三层的TensorFlow神经网络:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX

    参数:
        X_train - 训练集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 1080)
        Y_train - 训练集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 1080)
        X_test - 测试集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 120)
        Y_test - 测试集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 120)
        learning_rate - 学习速率
        num_epochs - 整个训练集的遍历次数
        mini_batch_size - 每个小批量数据集的大小
        print_cost - 是否打印成本,每100代打印一次
        is_plot - 是否绘制曲线图

    返回:
        parameters - 学习后的参数
    """
    ops.reset_default_graph()  # 能够重新运行模型而不覆盖tf变量
    tf.set_random_seed(1)
    seed = 3
    n_x = X_train.shape[0]
    m = X_train.shape[1]  # 获取输入节点数量12288和样本数1080

    n_y = Y_train.shape[0]  # 获取输出节点数量6
    costs = []  # 成本集

    # 给X和Y创建placeholder
    X, Y = create_placeholders(
        n_x, n_y)  # 输入输入节点12288,输出节点6,返回输出矩阵和输出矩阵的规模[12288 ?] [6 ?]

    # 初始化参数
    parameters = initialize_parameters()

    # 前向传播
    Z3 = forward_propagation(X, parameters)  # X需要压缩为12288 1080

    # 计算成本
    cost = compute_cost(Z3, Y)

    # 反向传播,使用Adam优化
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    # 初始化所有的变量
    init = tf.global_variables_initializer()

    # 开始会话并计算
    with tf.Session() as sess:
        # 初始化
        sess.run(init)

        # 正常训练的循环
        for epoch in range(num_epochs):

            epoch_cost = 0  # 每代的成本
            num_minibatches = int(
                m / minibatch_size)  # m = 1080(样本总数),minibatch的总数量
            seed = seed + 1
            minibatches = tf_utils.random_mini_batches(X_train, Y_train,
                                                       minibatch_size, seed)

            for minibatch in minibatches:

                # 选择一个minibatch
                (minibatch_X, minibatch_Y) = minibatch  # 一组训练样本的x和y

                # 数据已经准备好了,开始运行session
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                # 计算这个minibatch在这一代中所占的误差
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches  # 每一圈epoch平均误差

            #记录并打印成本
            ## 记录成本
            if epoch % 10 == 0:
                costs.append(epoch_cost)
                #是否打印:
                if print_cost and epoch % 100 == 0:
                    print("epoch = " + str(epoch) + "    epoch_cost = " +
                          str(epoch_cost))

        # 是否绘制图谱
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()

        # 保存学习后的参数
        parameters = sess.run(parameters)
        print("参数已经保存到session。")

        # 计算当前的预测结果
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # 计算准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #31
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):
    ops.reset_default_graph()  # 将计算图返回到默认空状态
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape  # (n_x: 特征数量, m : 训练集中的样本数)
    n_y = Y_train.shape[0]
    costs = []

    # 创建占位符
    X, Y = create_placeholders(n_x, n_y)

    # 初始化参数
    parameters = initialize_parameters()

    # 构建前向传播操作
    Z3 = forward_propagation(X, parameters)

    # 构建成本计算操作
    cost = computer_cost(Z3, Y)

    # 构建反向传播,为反向传播指定优化算法和学习率以及成本函数,这里我们使用adam算法,
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)

    # 定义初始化操作
    init = tf.global_variables_initializer()

    # 开始一个tensorflow的session
    with tf.Session() as sess:

        # 执行初始化操作
        sess.run(init)

        # 执行epochs指定的训练次数,一个epoch就是完整的向整个数据集学习一次
        for epoch in range(num_epochs):

            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)  # 计算有多少个子训练集
            seed = seed + 1
            # 将数据集分成若干子训练集
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            # 循环遍历每一个子训练集
            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch

                # 这行代码会使整个计算图被执行,从前向传播操作到反向传播操作,最后到参数更新操作。
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                epoch_cost += minibatch_cost / num_minibatches

            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # 画出cost成本的走势图
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # 从计算图中获取训练好了的参数,后面我们就可以用这些参数来识别手语了!
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # 分别计算一下在训练集和测试集上面的预测精准度
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
Exemple #32
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.0001,
          num_epochs=1500,
          minibatch_size=32,
          print_cost=True):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    ops.reset_default_graph(
    )  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)  # to keep consistent results
    seed = 3  # to keep consistent results
    (
        n_x, m
    ) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost

    # Create Placeholders of shape (n_x, n_y)
    ### START CODE HERE ### (1 line)
    X, Y = create_placeholders(n_x, n_y)
    ### END CODE HERE ###

    # Initialize parameters
    ### START CODE HERE ### (1 line)
    parameters = initialize_parameters()
    ### END CODE HERE ###

    # Forward propagation: Build the forward propagation in the tensorflow graph
    ### START CODE HERE ### (1 line)
    Z3 = forward_propagation(X, parameters)
    ### END CODE HERE ###

    # Cost function: Add cost function to tensorflow graph
    ### START CODE HERE ### (1 line)
    cost = compute_cost(Z3, Y)
    ### END CODE HERE ###

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    ### START CODE HERE ### (1 line)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    ### END CODE HERE ###

    # Initialize all the variables
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })
                ### END CODE HERE ###

                epoch_cost += minibatch_cost / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable
        parameters = sess.run(parameters)
        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
def model(X_train,Y_train,X_test,Y_test,
          learning_rate = 0.0001,num_epochs = 1500,minibatch_size = 32,
          print_cost = True,is_plot = True):
    '''
    实现一个三层的TensorFlow神经网络:linear->relu->linear->relu->linear->softmax
    :param X_train: 训练集,维度为(输入大小(输入节点数量) = 12288,样本数量=1080)
    :param Y_train: 训练集分类数量,维度为(输出大小(输出节点数量)=6,样本数量=1080)
    :param X_test: 测试集,维度为(输入大小(输入节点数量)=12288,,样本数量= 120)
    :param Y_test: 测试集分类数量,维度为(输出大小(输出节点数量) = 6,样本数量= 120)
    :param learning_rate: 学习速率
    :param num_epochs: 整个训练集的遍历次数
    :param minibatch_size: 每个小批量数据集的大小
    :param print_cost: 是否打印成本,每100代打印一次
    :param is_plot: 是否绘制曲线
    :return: 
    parameters - 学习后的参数
    '''
    ops.reset_default_graph() #能够重新运行模型而不覆盖tf变量
    tf.set_random_seed(1)
    seed = 3
    (n_x,m) = X_train.shape#获取输入节点数量和样本数
    n_y = Y_train.shape[0]#获取输出节点数量
    costs = []#成本集

    #给X和Y创建placeholder
    X,Y=creat_placeholders(n_x,n_y)
    #初始化参数
    parameters = initialize_parameters()

    #前向传播
    Z3 = forward_propagation(X,parameters)

    #计算成本
    cost = compute_cost(Z3,Y)

    #反向传播,使用Adam优化
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    #初始化所有变量
    init = tf.global_variables_initializer()

    #开始会话并计算
    with tf.Session() as sess:
        #初始化
        sess.run(init)

        #正常训练的循环
        for epoch in range(num_epochs):
            epoch_cost = 0#每代的成本
            num_minibatches = int(m/minibatch_size)#minibatch的总数量
            seed =seed +1
            minibatches = tf_utils.random_mini_batches(X_train,Y_train,minibatch_size,seed)

            for minibatch in minibatches:

                #选择一个minibatch
                (minibatch_X,minibatch_Y) = minibatch

                #数据已经准备好,开始运行session
                _,minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})

                #计算这个minibatch在这一代中所占的误差
                epoch__cost = epoch_cost +minibatch_cost/num_minibatches

            #记录并打印成本
            ##记录成本
            if epoch%5==0:
                costs.append(epoch__cost)
                #是否打印:
                if print_cost and epoch%100 == 0:
                    print('epoch = '+str(epoch)+'epoch_cost'+str(epoch__cost))

        #是否绘制图谱
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations(per ten)')
            plt.title('learning rate ='+str(learning_rate))
            plt.show()

        #保存学习后的参数
        parameters = sess.run(parameters)
        print('参数已保存到session')

        #计算当前的预测结果
        correct_prediction = tf.equal(tf.argmax(Z3),tf.argmax(Y))

        #计算准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))

        print('训练集的准确率:',accuracy.eval({X:X_train,Y:Y_train}))
        print('测试集的准确率:',accuracy.eval({X:X_test,Y:Y_test}))

        return parameters