美文网首页
CS20 Assignment 1

CS20 Assignment 1

作者: ItchyHiker | 来源:发表于2018-07-23 23:18 被阅读0次

    Tags: Programming MachineLearning

    [TOC]

    03_linreg_starter.py

    报错:
    (Can not convert a float32 into a Tensor or Operation.)

    在求loss的时候接收参数和run里面的参数一样,将接收参数修改避免冲突。
    将:
    _, loss = sess.run([optimizer,loss], feed_dict={X:x, Y:y})
    改为:
    _, l = sess.run([optimizer,loss], feed_dict={X:x, Y:y})

    """ Starter code for simple linear regression example using placeholders
    Created by Chip Huyen (huyenn@cs.stanford.edu)
    CS20: "TensorFlow for Deep Learning Research"
    cs20.stanford.edu
    Lecture 03
    """
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    import time
    
    import numpy as np
    import matplotlib.pyplot as plt
    import tensorflow as tf
    
    import utils
    
    DATA_FILE = 'data/birth_life_2010.txt'
    
    # Step 1: read in data from the .txt file
    data, n_samples = utils.read_birth_life_data(DATA_FILE)
    
    # Step 2: create placeholders for X (birth rate) and Y (life expectancy)
    # Remember both X and Y are scalars with type float
    X, Y = None, None
    #############################
    ########## TO DO ############
    #############################
    X = tf.placeholder(dtype = tf.float32, name = "X")
    Y = tf.placeholder(dtype = tf.float32,  name = "Y") # [] means for scalar
    
    
    # Step 3: create weight and bias, initialized to 0.0
    # Make sure to use tf.get_variable
    w, b = None, None
    #############################
    ########## TO DO ############
    #############################
    w = tf.get_variable(name = "weights",  initializer = tf.constant(0.0))
    b = tf.get_variable(name = "bias",  initializer = tf.constant(0.0))
    
    # Step 4: build model to predict Y
    # e.g. how would you derive at Y_predicted given X, w, and b
    Y_predicted = None
    #############################
    ########## TO DO ############
    #############################
    Y_predict = tf.add(tf.multiply(X,w),b)
    # Step 5: use the square error as the loss function
    loss = None
    #############################
    ########## TO DO ############
    #############################
    loss = tf.square(tf.subtract(Y_predict, Y), name='loss')
    # Step 6: using gradient descent with learning rate of 0.001 to minimize loss
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)
    
    start = time.time()
    
    # Create a filewriter to write the model's graph to TensorBoard
    #############################
    ########## TO DO ############
    #############################
    writer = tf.summary.FileWriter('./graphs/linear_reg', tf.get_default_graph())       
    with tf.Session() as sess:
        # Step 7: initialize the necessary variables, in this case, w and b
        #############################
        ########## TO DO ############
        #############################
        sess.run(tf.global_variables_initializer())
        # Step 8: train the model for 100 epochs
        for i in range(100):
            total_loss = 0
            for x, y in data:
                # Execute train_op and get the value of loss.
                # Don't forget to feed in data for placeholders
                _, l = sess.run([optimizer,loss], feed_dict={X:x, Y:y})
                total_loss += l
    
            print('Epoch {0}: {1}'.format(i, total_loss/n_samples))
    
        # close the writer when you're done using it
        #############################
        ########## TO DO ############
        #############################
        writer.close()
        
        # Step 9: output the values of w and b
        w_out, b_out = None, None
        #############################
        ########## TO DO ############
        #############################
        w_out, b_out = sess.run([w, b])
    print('Took: %f seconds' %(time.time() - start))
    
    # uncomment the following lines to see the plot 
    plt.plot(data[:,0], data[:,1], 'bo', label='Real data')
    plt.plot(data[:,0], data[:,0] * w_out + b_out, 'r', label='Predicted data')
    plt.legend()
    plt.show()
    

    03_logreg_starter.py

    改进1:改用exponential_decay, 92.75% 验证准确度。

    """ Starter code for simple logistic regression model for MNIST
    with tf.data module
    MNIST dataset: yann.lecun.com/exdb/mnist/
    Created by Chip Huyen (chiphuyen@cs.stanford.edu)
    CS20: "TensorFlow for Deep Learning Research"
    cs20.stanford.edu
    Lecture 03
    """
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    
    import numpy as np
    import tensorflow as tf
    import time
    
    import utils
    
    # Define paramaters for the model
    learning_rate = 0.01
    batch_size = 128
    n_epochs = 30
    n_train = 60000
    n_test = 10000
    
    # Step 1: Read in data
    mnist_folder = 'data/mnist'
    utils.download_mnist(mnist_folder)
    train, val, test = utils.read_mnist(mnist_folder, flatten=True)
    print(type(train))
    # Step 2: Create datasets and iterator
    # create training Dataset and batch it
    train_data = tf.data.Dataset.from_tensor_slices(train)
    train_data = train_data.shuffle(10000) # if you want to shuffle your data
    train_data = train_data.batch(batch_size)
    
    # create testing Dataset and batch it
    test_data = tf.data.Dataset.from_tensor_slices(test)
    test_data = test_data.shuffle(10000)
    test_data = test_data.batch(10000)
    
    
    # create one iterator and initialize it with different datasets
    iterator = tf.data.Iterator.from_structure(train_data.output_types, 
                                               train_data.output_shapes)
    img, label = iterator.get_next()
    print(img.shape)
    print(label.shape)
    train_init = iterator.make_initializer(train_data)  # initializer for train_data
    test_init = iterator.make_initializer(test_data)    # initializer for train_data
    
    # Step 3: create weights and bias
    # w is initialized to random variables with mean of 0, stddev of 0.01
    # b is initialized to 0
    # shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
    # shape of b depends on Y
    w, b = None, None
    #############################
    ########## TO DO ############
    #############################
    w = tf.get_variable(initializer = tf.random_normal_initializer(mean=0, stddev=0.01), shape = [784,10], name = "weights")
    b = tf.get_variable(initializer = tf.random_normal_initializer(mean=0, stddev=0.01), shape = [1,10], name = "bias")
    
    # Step 4: build model
    # the model that returns the logits.
    # this logits will be later passed through softmax layer
    logits = None
    #############################
    ########## TO DO ############
    #############################
    logits = tf.matmul(img, w) + b
    
    # Step 5: define loss function
    # use cross entropy of softmax of logits as the loss function
    loss = None
    #############################
    ########## TO DO ############
    #############################
    entropy = tf.nn.softmax_cross_entropy_with_logits(labels = label, logits=logits, name="entropy")
    loss = tf.reduce_mean(entropy, name = "loss")
    
    # Step 6: define optimizer
    # using Adamn Optimizer with pre-defined learning rate to minimize loss
    optimizer = None
    #############################
    ########## TO DO ############
    #############################
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
    
    # Step 7: calculate accuracy with test set
    preds = tf.nn.softmax(logits)
    correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(label, 1))
    accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
    
    writer = tf.summary.FileWriter('./graphs/logreg', tf.get_default_graph())
    with tf.Session() as sess:
       
        start_time = time.time()
        sess.run(tf.global_variables_initializer())
    
        # train the model n_epochs times
        for i in range(n_epochs):   
            sess.run(train_init)    # drawing samples from train_data
            total_loss = 0
            n_batches = 0
            try:
                while True:
                    _, l = sess.run([optimizer, loss])
                    total_loss += l
                    n_batches += 1
            except tf.errors.OutOfRangeError:
                pass
            print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))
        print('Total time: {0} seconds'.format(time.time() - start_time))
    
        # test the model
        sess.run(test_init)         # drawing samples from test_data
        total_correct_preds = 0
        try:
            while True:
                accuracy_batch = sess.run(accuracy)
                total_correct_preds += accuracy_batch
        except tf.errors.OutOfRangeError:
            pass
    
        print('Accuracy {0}'.format(total_correct_preds/n_test))
    writer.close()
    

    q2b.py

    修改学习率和迭代次数:0.8876

    """ Starter code for simple logistic regression model for MNIST
    with tf.data module
    MNIST dataset: yann.lecun.com/exdb/mnist/
    Created by Chip Huyen (chiphuyen@cs.stanford.edu)
    CS20: "TensorFlow for Deep Learning Research"
    cs20.stanford.edu
    Lecture 03
    """
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
    
    import numpy as np
    import tensorflow as tf
    import time
    
    import utils
    
    # Define paramaters for the model
    global_step = tf.Variable(0, trainable=False)
    learning_rate = 0.001
    # learning_rate = tf.train.exponential_decay(0.01, global_step, decay_steps = 10, decay_rate = 0.2)
    batch_size = 128
    n_epochs = 100
    n_train = 60000
    n_test = 10000
    
    # Step 1: Read in data
    mnist_folder = 'data/notMNIST_large'
    # utils.download_mnist(mnist_folder)
    train, val, test = utils.read_mnist(mnist_folder, flatten=True)
    # print(type(train))
    # Step 2: Create datasets and iterator
    # create training Dataset and batch it
    train_data = tf.data.Dataset.from_tensor_slices(train)
    train_data = train_data.shuffle(10000) # if you want to shuffle your data
    train_data = train_data.batch(batch_size)
    
    # create testing Dataset and batch it
    test_data = tf.data.Dataset.from_tensor_slices(test)
    test_data = test_data.shuffle(10000)
    test_data = test_data.batch(10000)
    
    
    # create one iterator and initialize it with different datasets
    iterator = tf.data.Iterator.from_structure(train_data.output_types, 
                                               train_data.output_shapes)
    img, label = iterator.get_next()
    # print(img.shape)
    # print(label.shape)
    train_init = iterator.make_initializer(train_data)  # initializer for train_data
    test_init = iterator.make_initializer(test_data)    # initializer for train_data
    
    # Step 3: create weights and bias
    # w is initialized to random variables with mean of 0, stddev of 0.01
    # b is initialized to 0
    # shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
    # shape of b depends on Y
    w, b = None, None
    #############################
    ########## TO DO ############
    #############################
    w = tf.get_variable(initializer = tf.random_normal_initializer(mean=0, stddev=0.01), shape = [784,10], name = "weights")
    b = tf.get_variable(initializer = tf.random_normal_initializer(mean=0, stddev=0.01), shape = [1,10], name = "bias")
    
    
    # Step 4: build model
    # the model that returns the logits.
    # this logits will be later passed through softmax layer
    logits = None
    #############################
    ########## TO DO ############
    #############################
    logits = tf.matmul(img, w) + b
    
    # Step 5: define loss function
    # use cross entropy of softmax of logits as the loss function
    loss = None
    #############################
    ########## TO DO ############
    #############################
    entropy = tf.nn.softmax_cross_entropy_with_logits(labels = label, logits=logits, name="entropy")
    loss = tf.reduce_mean(entropy, name = "loss")
    
    # Step 6: define optimizer
    # using Adamn Optimizer with pre-defined learning rate to minimize loss
    optimizer = None
    #############################
    ########## TO DO ############
    #############################
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step)
    
    # Step 7: calculate accuracy with test set
    preds = tf.nn.softmax(logits)
    correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(label, 1))
    accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
    
    writer = tf.summary.FileWriter('./graphs/logreg_notMNIST', tf.get_default_graph())
    with tf.Session() as sess:
       
        start_time = time.time()
        sess.run(tf.global_variables_initializer())
    
        # train the model n_epochs times
        for i in range(n_epochs):   
            sess.run(train_init)    # drawing samples from train_data
            total_loss = 0
            n_batches = 0
            #if i % 10 == 0:
            #    learning_rate *= 0.2
            #    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
    
            try:
                while True:
                    _, l = sess.run([optimizer, loss])
                    total_loss += l
                    n_batches += 1
            except tf.errors.OutOfRangeError:
                pass
            print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))
        print('Total time: {0} seconds'.format(time.time() - start_time))
    
        # test the model
        sess.run(test_init)         # drawing samples from test_data
        total_correct_preds = 0
        try:
            while True:
                accuracy_batch = sess.run(accuracy)
                total_correct_preds += accuracy_batch
        except tf.errors.OutOfRangeError:
            pass
    
        print('Accuracy {0}'.format(total_correct_preds/n_test))
    writer.close()
    

    相关文章

      网友评论

          本文标题:CS20 Assignment 1

          本文链接:https://www.haomeiwen.com/subject/owpsmftx.html