For a picture to achieve RNN operation, mainly by first getting a whole, then slicing, get the last input result output *_w[' out '] + _b[' out ' = final output result
First Step: Data loading
ImportTensorFlow as TF fromTensorflow.contribImportRNN fromTensorflow.examples.tutorials.mnistImportInput_dataImportNumPy as NPImportMatplotlib.pyplot as PltPrint("Packages Imported") Mnist= Input_data.read_data_sets ("data/", one_hot=True) Trainimgs, Trainlabels, Testimgs, Testlabels=mnist.train.images, Mnist.train.labels, Mnist.test.images, Mnist.test.labelsntrain, Ntest, Dim, nclasses= Trainimgs.shape[0], testimgs.shape[0], trainimgs.shape[1], trainlabels.shape[1]
Step Two: Initialize parameters
Diminput = 28Dimhidden= 128#nclasses = TenDimoutput =nclassesnsteps= 28#w parameter Initializationweights = { 'Hidden': TF. Variable (Tf.random_normal ([Diminput, Dimhidden])),' out': TF. Variable (Tf.random_normal ([Dimhidden, Dimoutput])}#b Initialization of parametersbiases = { 'Hidden': TF. Variable (Tf.random_normal ([Dimhidden])),' out': TF. Variable (Tf.random_normal ([Dimoutput])}
Step three: Building the RNN function
def_rnn (_x, _w, _b, _nsteps, _name):#The first step: Convert input, enter _x is also a batchsize=5 5 28*28 picture, need to input from #[Batchsize,nsteps,diminput]==>[nsteps,batchsize,diminput]_x = Tf.transpose (_x, [1, 0, 2]) #Step Two: Reshape _x for [nsteps*batchsize,diminput]_x = Tf.reshape (_x, [-1, Diminput]) #Step Three: input layer, hidden layer_h = Tf.matmul (_x, _w['Hidden']) + _b['Hidden'] #Fourth Step: Cut the data into ' nsteps ' slices, the first slice is the I batch data #Tensoflow >0.12_hsplit =tf.split (_h, _nsteps, 0)#tensoflow <0.12 _hsplit = Tf.split (0,_nsteps,_h) #Fifth step: Calculate LSTM Final Output (_lstm_o) and state (_lstm_s) #both _lstm_o and _lstm_s have ' batchsize ' elements #_lstm_o for predictive outputWith Tf.variable_scope (_name) as scope:#represents a common share of variablesscope.reuse_variables ()#Forget_bias = 1.0 do not forget data ## #tensorflow <1.0 #Lstm_cell = Tf.nn.rnn_cell. Basiclstmcell (Dimhidden,forget_bias = 1.0) #_lstm_o,_sltm_s = Tf.nn.rnn (lstm_cell,_hsplit,dtype=tf.float32) ## #tensorflow 1.0Lstm_cell =RNN. Basiclstmcell (Dimhidden) _lstm_o, _lstm_s= Rnn.static_rnn (Lstm_cell, _hsplit, dtype=Tf.float32)#Sixth step: output, need the last RNN unit as the predictive output, so take _lstm_o[-1]_o = Tf.matmul (_lstm_o[-1], _w[' out']) + _b[' out'] return { 'X': _x,'H': _h,'_hsplit': _hsplit,'Lstm_o': _lstm_o,'lstm_s': _lstm_s,'O': _o}
Fourth step: Construct cost function and accuracy function
Learning_rate = 0.001x= Tf.placeholder ("float", [None, Nsteps, diminput]) y= Tf.placeholder ("float", [None, Dimoutput]) myrnn= _RNN (x, Weights, biases, nsteps,'Basic') pred= myrnn['O']cost= Tf.reduce_mean (Tf.nn.softmax_cross_entropy_with_logits (logits=pred, labels=y)) Optm= Tf.train.GradientDescentOptimizer (learning_rate). Minimize (Cost)#AdamACCR = Tf.reduce_mean (Tf.cast (Tf.equal (Tf.argmax, 1), pred (Y, 1)) ( tf.float32)) init=Tf.global_variables_initializer ()Print("Network ready!")
Fifth step: Train the model, lower the cost, optimize the parameters
#number of training sessionsTraining_epochs = 5#number of pictures per trainingBatch_size = 16#number of impressions in the loopDisplay_step = 1Sess=TF. Session () sess.run (init)Print("Start Optimization") forEpochinchRange (Training_epochs): Avg_cost=0.#total_batch = Int (mnist.train.num_examples/batch_size)Total_batch = 100#Loop over all batches forIinchRange (Total_batch): Batch_xs, Batch_ys=Mnist.train.next_batch (batch_size) Batch_xs=Batch_xs.reshape (batch_size, Nsteps, Diminput)#print (Batch_xs.shape) #print (Batch_ys.shape) #Batch_ys = Batch_ys.reshape ((batch_size, dimoutput)) #Fit training using batch dataFeeds ={x:batch_xs, Y:batch_ys} sess.run (Optm, Feed_dict=feeds)#Compute Average lossAvg_cost + = Sess.run (cost, feed_dict=feeds)/Total_batch#Display logs per epoch step ifEpoch% Display_step = =0:Print("Epoch:%03d/%03d cost:%.9f"%(Epoch, Training_epochs, avg_cost)) feeds={x:batch_xs, y:batch_ys} TRAIN_ACC= Sess.run (ACCR, feed_dict=feeds)Print("Training accuracy:%.3f"%(TRAIN_ACC)) Testimgs=Testimgs.reshape ((Ntest, Nsteps, diminput)) feeds={x:testimgs, y:testlabels} TEST_ACC= Sess.run (ACCR, feed_dict=feeds)Print("Test accuracy:%.3f"%(TEST_ACC))Print("optimization finished.")
Learn from me algorithm-TensorFlow implement RNN operation