赞
踩
1.DNN
1.1.DNN结构和数据说明
1.2 DNN代码实现
# -*- coding: utf-8 -*- """ Created on Tue May 28 19:22:44 2019 @author: lcl """ from __future__ import print_function # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) import tensorflow as tf # Parameters learning_rate = 0.1 num_steps = 500 batch_size = 128 display_step = 100 # Network Parameters n_hidden_1 = 256 # 1st layer number of neurons n_hidden_2 = 256 # 2nd layer number of neurons num_input = 784 # MNIST data input (img shape: 28*28) num_classes = 10 # MNIST total classes (0-9 digits) # tf Graph input X = tf.placeholder("float", [None, num_input]) Y = tf.placeholder("float", [None, num_classes]) # Store layers weight & bias weights = { 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) } biases = { 'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'out': tf.Variable(tf.random_normal([num_classes])) } # Create model def neural_net(x): # Hidden fully connected layer with 256 neurons layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) # Hidden fully connected layer with 256 neurons layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) # Output fully connected layer with a neuron for each class out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] return out_layer logits = neural_net(X) loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits,labels = Y)) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) correct_pred = tf.equal(tf.argmax(logits,1),tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(1,num_steps + 1): batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) if step % display_step == 0 or step == 1: # Calculate batch loss and accuracy loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x, Y: batch_y}) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc)) print("Optimization Finished!") # Calculate accuracy for MNIST test images print("Testing Accuracy:", \ sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))
1.3运行结果
1.4 相关函数说明
2.CNN
2.1 CNN结构
2.2 CNN代码实现
# -*- coding: utf-8 -*- """ Created on Wed May 29 14:31:38 2019 @author: lcl """ from __future__ import division, print_function, absolute_import import tensorflow as tf # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) #training parameters learning_rate = 0.001 num_steps = 100 batch_size = 128 display_step = 10 #Network parameters num_input = 784 num_classes = 10 dropout = 0.75 #tf graph input X = tf.placeholder(tf.float32,[None,num_input]) Y = tf.placeholder(tf.float32,[None,num_classes]) keep_prob = tf.placeholder(tf.float32) def conv2d(x,W,b,strides = 1): x = tf.nn.conv2d(x,W,strides = [1,strides,strides,1],padding = 'SAME') x = tf.nn.bias_add(x,b) return tf.nn.relu(x) def maxpool2d(x,k=2): return tf.nn.max_pool(x,ksize = [1,k,k,1],strides = [1,k,k,1],padding = 'SAME') def conv_net(x,weights,biases,dropout): x = tf.reshape(x,shape = [-1,28,28,1]) conv1 = conv2d(x,weights['wc1'],biases['bc1']) conv1 = maxpool2d(conv1,k = 2) conv2 = conv2d(conv1,weights['wc2'],biases['bc2']) conv2 = maxpool2d(conv2,k = 2) fc1 = tf.reshape(conv2,[-1,weights['wd1'].get_shape().as_list()[0]]) fc1 = tf.add(tf.matmul(fc1,weights['wd1']),biases['bd1']) fc1 = tf.nn.relu(fc1) fc1 = tf.nn.dropout(fc1,dropout) out = tf.add(tf.matmul(fc1,weights['out']),biases['out']) return out weights = { 'wc1':tf.Variable(tf.random_normal([5,5,1,32])), 'wc2':tf.Variable(tf.random_normal([5,5,32,64])), 'wd1':tf.Variable(tf.random_normal([7*7*64,1024])), 'out':tf.Variable(tf.random_normal([1024,num_classes])) } biases = { 'bc1':tf.Variable(tf.random_normal([32])), 'bc2':tf.Variable(tf.random_normal([64])), 'bd1':tf.Variable(tf.random_normal([1024])), 'out':tf.Variable(tf.random_normal([num_classes])) } logits = conv_net(X,weights,biases,keep_prob) prediction = tf.nn.softmax(logits) loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits,labels = Y)) optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate) train_op = optimizer.minimize(loss_op) correct_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(1,num_steps+1): batch_x,batch_y = mnist.train.next_batch(batch_size) sess.run(train_op,feed_dict = {X:batch_x,Y:batch_y,keep_prob:dropout}) if step % display_step == 0 or step == 1: loss,acc = sess.run([loss_op,accuracy],feed_dict = {X:batch_x,Y:batch_y,keep_prob:1.0}) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc)) print("Optimization Finished!") # Calculate accuracy for 256 MNIST test images print("Testing Accuracy:", \ sess.run(accuracy, feed_dict={X: mnist.test.images[:256], Y: mnist.test.labels[:256], keep_prob: 1.0}))
2.3 运行结果
2.4 参数说明
2828(原始图片大小) ---- 3030(padding)---- 2626(卷积)----- 2828(padding)----1414(pooling)----- 1616(padding) ----- 1212(卷积) ----- 1414(padding) ----- 7*7(pooling)
3 RNN
3.1 RNN结构
3.2 RNN实现代码
# -*- coding: utf-8 -*- """ Created on Wed May 29 16:05:54 2019 @author: admin """ from __future__ import print_function import tensorflow as tf from tensorflow.contrib import rnn # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) tf.reset_default_graph() #training parameters learning_rate = 0.001 training_steps = 500 batch_size = 128 display_step = 20 #network parameters num_input = 28 timesteps = 28 num_hidden = 128 num_classes = 10 #tf graph input X = tf.placeholder("float",[None,timesteps,num_input]) Y = tf.placeholder("float",[None,num_classes]) #define weights weights = { 'out':tf.Variable(tf.random_normal([num_hidden,num_classes])) } biases = { 'out':tf.Variable(tf.random_normal([num_classes])) } def RNN(x,weights,biases): x = tf.unstack(x,timesteps,1) lstm_cell = rnn.BasicLSTMCell(num_hidden,forget_bias = 1.0) #outputs,states = rnn.static_rnn(lstm_cell,x,dtype = tf.float32) outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32) return tf.matmul(outputs[-1],weights['out'] + biases['out']) logits = RNN(X,weights,biases) prediction = tf.nn.softmax(logits) loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits,labels = Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate) train_op = optimizer.minimize(loss_op) correct_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(1,training_steps + 1): batch_x,batch_y = mnist.train.next_batch(batch_size) batch_x = batch_x.reshape(batch_size,timesteps,num_input) sess.run(train_op,feed_dict = {X:batch_x,Y:batch_y}) if step % display_step == 0 or step == 1: loss,acc = sess.run([loss_op,accuracy],feed_dict = {X:batch_x,Y:batch_y}) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + \ "{:.3f}".format(acc)) print("Optimization Finished!") # Calculate accuracy for 128 mnist test images test_len = 128 test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input)) test_label = mnist.test.labels[:test_len] print("Testing Accuracy:", \ sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))
3.3 输出结果
3.4参数说明
xi为28*28矩阵的第i列,RNN中每次输入矩阵中的一列,共输入28次
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。