keep copying...
Keywords: one hidden layer, dropout, ReLu activation function, adaptive learning rate Adagrad, Sofmax function
Attached code:
# Load the MNIST dataset from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) # Create tensorflow's default Interactive Session, so that you don't need to specify a session to perform various operations later sex = tf.InteractiveSession () # input node 28*28 int_units = 784 # number of hidden layer nodes h1_units = 300 # Bias are all initialized to 0, and weights are initialized to a truncated normal distribution with a standard deviation of 0.1 w1 = tf.Variable(tf.truncated_normal([int_units,h1_units],stddev = 0.1)) b1 = tf.Variable(tf.zeros([h1_units])) w2 = tf.Variable(tf.truncated_normal([h1_units,10])) b2 = tf.Variable(tf.zeros([10])) # define the placeholder for x x = tf.placeholder(tf.float32,[None,int_units]) # The ratio of dropout is the probability of keeping the node < 1 keep_prob = tf.placeholder(tf.float32) # define the model structure # Hidden layer output, use relu as activation function hidden1 = tf.nn.relu(tf.matmul(x,w1)+b1) # dropout of the hidden layer, keep_prob means ratio<1 hidden1_drop = tf.nn.dropout(hidden1,keep_prob) # The softmax function takes the high probability as the output y = tf.nn.softmax(tf.matmul(hidden1_drop,w2)+b2) # Input placeholder for true value (label) y_ = tf.placeholder(tf.float32,[None,10]) # loss function using cross entropy cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1])) # Use Adagrad optimizer with learning rate set to 0.3 (along with optimizers like Adam, Adadelta, etc.) train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy) # training steps # Initialize computational graph variables tf.global_variables_initializer().run() # Iterate 3000 times, a batch size of 100, a total of 300,000 samples # 300,000 samples, each epoch has 5,500 data, which is equivalent to 5 times (300,000/5500) more epochs for i in range(3000): batch_xs,batch_ys = mnist.train.next_batch(100) # keep_prob = 0.75 means keep 75% of nodes train_step.run({x:batch_xs,y_:batch_ys,keep_prob:0.75}) # Evaluate the accuracy of the model # tf.argmax(input, dims), returns the subscript of the maximum value, usually used with tf.equal to calculate the model accuracy # dims = 1 operates by row, dims=0 operates by column # tf.equal correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) # tf.cast() performs format conversion, converts the bool type of correct_prediction to float32 and then averages accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # Because it is the prediction part, we can directly set keep_prob to 1 print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})) unfinished...