更多内容参考:TFLearn官方文档
TFLearn是一个深度学习库,它提供了基于tensorflow的高层API接口.它是在tensorflow上建立的一个模块化的易于使用的库,有助于加快你构建deep learning网络的过程,节省冗长的代码时间.
TFLearn 有如下特性:
- Easy-to-use and understand high-level API for implementing deep neural networks, with tutorial and examples.
(容易使用和易于理解的高层次 API 用于实现深度神经网络,附带教程和例子) - Fast prototyping through highly modular built-in neural network layers, regularizers, optimizers, metrics…
(通过高度模块化的内置神经网络层、正则化器、优化器等进行快速原型设计) - Full transparency over Tensorflow. All functions are built over tensors and can be used independently of TFLearn.
(对 TensorFlow 完全透明,所有函数都是基于 tensor,可以独立于 TFLearn 使用) - Powerful helper functions to train any TensorFlow graph, with support of multiple inputs, outputs and optimizers.
(强大的辅助函数,可训练任意 TensorFlow 图,支持多输入、多输出和优化器)
Easy and beautiful graph visualization, with details about weights, gradients, activations and more…
(简单而美观的图可视化,可以查看关于权值、梯度、特征图等细节) - Effortless device placement for using multiple CPU/GPU.
(无需人工干预,可使用多 CPU、多 GPU)
示例:TFLearn_Digit_Recognition
# Import Numpy, TensorFlow, TFLearn, and MNIST data import numpy as np import tensorflow as tf import tflearn import tflearn.datasets.mnist as mnist
# Retrieve the training and test data trainX, trainY, testX, testY = mnist.load_data(one_hot=True)
# Visualizing the data import matplotlib.pyplot as plt %matplotlib inline # Function for displaying a training image by it's index in the MNIST set def show_digit(index): label = trainY[index].argmax(axis=0) # Reshape 784 array into 28x28 image image = trainX[index].reshape([28,28]) plt.title('Training data, index: %d, Label: %d' % (index, label)) plt.imshow(image, cmap='gray_r') plt.show() # Display the first (index 0) training image show_digit(0)
定义网络:
# Define the neural network def build_model(): # This resets all parameters and variables, leave this here tf.reset_default_graph() # Inputs # trainX.shape为(55000, 784),是一个二维矩阵trainX.shape[1]返回第二个维度的值即784,trainX.shape[0]则返回55000. net = tflearn.input_data([None, trainX.shape[1]]) # Hidden layer(s) net = tflearn.fully_connected(net, 128, activation='ReLU') net = tflearn.fully_connected(net, 32, activation='ReLU') # Output layer and training model net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='sgd', learning_rate=0.01, loss='categorical_crossentropy') model = tflearn.DNN(net) #### Your code #### # Include the input layer, hidden layer(s), and set how you want to train the model # This model assumes that your network is named "net" model = tflearn.DNN(net) return model
# Build the model model = build_model()
# Training model.fit(trainX, trainY, validation_set=0.01, show_metric=True, batch_size=100, n_epoch=50)
Training Step: 38149 | time: 2.878s | SGD | epoch: 070 | loss: 0.00000 - acc: 0.9777 -- iter: 54400/54450 Training Step: 38150 | time: 3.886s | SGD | epoch: 070 | loss: 0.00000 - acc: 0.9769 | val_loss: 0.11651 - val_acc: 0.9709 -- iter: 54450/54450
--
测试:
# Compare the labels that our model predicts with the actual labels # Find the indices of the most confident prediction for each item. That tells us the predicted digit for that sample. predictions = np.array(model.predict(testX)).argmax(axis=1) # Calculate the accuracy, which is the percentage of times the predicated labels matched the actual labels actual = testY.argmax(axis=1) test_accuracy = np.mean(predictions == actual, axis=0) # Print out the result print("Test accuracy: ", test_accuracy)
Test accuracy: 0.967