您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用TensorFlow創建CNN”,在日常操作中,相信很多人在如何使用TensorFlow創建CNN問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用TensorFlow創建CNN”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
# -*- coding:utf-8 -*- import tensorflow as tf import numpy as np # 下載mnist數據集 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('./mnist_data/', one_hot=True) # from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets # # mnist = read_data_sets('./mnist_data/', one_hot=True) n_output_layer = 10 # 定義待訓練的神經網絡 def convolutional_neural_network(data): weights = {'w_conv1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 'w_conv2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 'w_fc': tf.Variable(tf.random_normal([7 * 7 * 64, 1024])), 'out': tf.Variable(tf.random_normal([1024, n_output_layer]))} biases = {'b_conv1': tf.Variable(tf.random_normal([32])), 'b_conv2': tf.Variable(tf.random_normal([64])), 'b_fc': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_output_layer]))} data = tf.reshape(data, [-1, 28, 28, 1]) conv1 = tf.nn.relu( tf.add(tf.nn.conv2d(data, weights['w_conv1'], strides=[1, 1, 1, 1], padding='SAME'), biases['b_conv1'])) conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') conv2 = tf.nn.relu( tf.add(tf.nn.conv2d(conv1, weights['w_conv2'], strides=[1, 1, 1, 1], padding='SAME'), biases['b_conv2'])) conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') fc = tf.reshape(conv2, [-1, 7 * 7 * 64]) fc = tf.nn.relu(tf.add(tf.matmul(fc, weights['w_fc']), biases['b_fc'])) # dropout剔除一些"神經元" # fc = tf.nn.dropout(fc, 0.8) output = tf.add(tf.matmul(fc, weights['out']), biases['out']) return output # 每次使用100條數據進行訓練 batch_size = 100 X = tf.placeholder('float', [None, 28 * 28]) Y = tf.placeholder('float') # 使用數據訓練神經網絡 def train_neural_network(X, Y): predict = convolutional_neural_network(X) # cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict,labels=Y)) cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predict, labels=Y)) optimizer = tf.train.AdamOptimizer().minimize(cost_func) # learning rate 默認 0.001 epochs = 1 with tf.Session() as session: # session.run(tf.initialize_all_variables()) session.run(tf.global_variables_initializer()) epoch_loss = 0 for epoch in range(epochs): for i in range(int(mnist.train.num_examples / batch_size)): x, y = mnist.train.next_batch(batch_size) _, c = session.run([optimizer, cost_func], feed_dict={X: x, Y: y}) epoch_loss += c print(epoch, ' : ', epoch_loss) correct = tf.equal(tf.argmax(predict, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct, 'float')) print('準確率: ', accuracy.eval({X: mnist.test.images, Y: mnist.test.labels})) train_neural_network(X, Y)
執行結果:
準確率: 0.9789
下面使用tflearn重寫上面代碼,tflearn是TensorFlow的高級封裝,類似Keras。
tflearn提供了更簡單、直觀的接口。和scikit-learn差不多,代碼如下:
# -*- coding:utf-8 -*- import tflearn from tflearn.layers.conv import conv_2d, max_pool_2d from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.estimator import regression train_x, train_y, test_x, test_y = tflearn.datasets.mnist.load_data( data_dir="./mnist_data/",one_hot=True) train_x = train_x.reshape(-1, 28, 28, 1) test_x = test_x.reshape(-1, 28, 28, 1) # 定義神經網絡模型 conv_net = input_data(shape=[None, 28, 28, 1], name='input') conv_net = conv_2d(conv_net, 32, 2, activation='relu') conv_net = max_pool_2d(conv_net, 2) conv_net = conv_2d(conv_net, 64, 2, activation='relu') conv_net = max_pool_2d(conv_net, 2) conv_net = fully_connected(conv_net, 1024, activation='relu') conv_net = dropout(conv_net, 0.8) conv_net = fully_connected(conv_net, 10, activation='softmax') conv_net = regression(conv_net, optimizer='adam', loss='categorical_crossentropy', name='output') model = tflearn.DNN(conv_net) # 訓練 model.fit({'input': train_x}, {'output': train_y}, n_epoch=13, validation_set=({'input': test_x}, {'output': test_y}), snapshot_step=300, show_metric=True, run_id='mnist') model.save('./mnist.model') # 保存模型 """ model.load('mnist.model') # 加載模型 model.predict([test_x[1]]) # 預測 """
到此,關于“如何使用TensorFlow創建CNN”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。