tflearn中使用cnn做分类并做预测

数据情况在上一篇博客中已经提到,直接看代码,定义网络结构比较简单:

 
    
  1. from __future__ import division, print_function, absolute_import
  2. import tensorflow as tf
  3. import tflearn
  4. from tflearn.layers.core import input_data, dropout, fully_connected
  5. from tflearn.layers.conv import conv_1d, global_max_pool
  6. from tflearn.layers.merge_ops import merge
  7. from tflearn.layers.estimator import regression
  8. from tflearn.data_utils import to_categorical, pad_sequences
  9. from tflearn.datasets import imdb
  10. import pickle
  11. import numpy as np
  12. """
  13. 还是加载imdb.pkl数据
  14. """
  15. train, test, _ = imdb.load_data(path='imdb.pkl', n_words=10000,
  16. valid_portion=0.1)
  17. trainX, trainY = train
  18. testX, testY = test
  19. """
  20. 转化为固定长度的向量,这里固定长度为100
  21. """
  22. trainX = pad_sequences(trainX, maxlen=100, value=0.)
  23. testX = pad_sequences(testX, maxlen=100, value=0.)
  24. """
  25. 二值化向量
  26. """
  27. trainY = to_categorical(trainY, nb_classes=2)
  28. testY = to_categorical(testY, nb_classes=2)
  29. """
  30. 构建卷积神经网络,这里卷积神经网网络为1d卷积
  31. """
  32. network = input_data(shape=[None, 100], name='input')
  33. network = tflearn.embedding(network, input_dim=10000, output_dim=128)
  34. branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
  35. branch2 = conv_1d(network, 128, 4, padding='valid', activation='relu', regularizer="L2")
  36. branch3 = conv_1d(network, 128, 5, padding='valid', activation='relu', regularizer="L2")
  37. network = merge([branch1, branch2, branch3], mode='concat', axis=1)
  38. network = tf.expand_dims(network, 2)
  39. network = global_max_pool(network)
  40. network = dropout(network, 0.5)
  41. network = fully_connected(network, 2, activation='softmax')
  42. network = regression(network, optimizer='adam', learning_rate=0.001,
  43. loss='categorical_crossentropy', name='target')
  44. """
  45. 训练开始
  46. """
  47. model = tflearn.DNN(network, tensorboard_verbose=0)
  48. model.fit(trainX, trainY, n_epoch = 1, shuffle=True, validation_set=(testX, testY), show_metric=True, batch_size=32)
  49. """
  50. 模型保存
  51. """
  52. model.save("cnn.model")
  53. """
  54. 做测试使用
  55. """
  56. test=np.linspace(1,101,100).reshape(1,100)
  57. print("测试结果:",model.predict(test))

模型训练结果以及模型保存情况:

 
    
  1. Training Step: 697 | total loss: 0.40838 | time: 79.960s
  2. | Adam | epoch: 001 | loss: 0.40838 - acc: 0.8247 -- iter: 22304/22500
  3. Training Step: 698 | total loss: 0.39128 | time: 80.112s
  4. | Adam | epoch: 001 | loss: 0.39128 - acc: 0.8329 -- iter: 22336/22500
  5. Training Step: 699 | total loss: 0.38896 | time: 80.298s
  6. | Adam | epoch: 001 | loss: 0.38896 - acc: 0.8402 -- iter: 22368/22500
  7. Training Step: 700 | total loss: 0.39468 | time: 80.456s
  8. | Adam | epoch: 001 | loss: 0.39468 - acc: 0.8343 -- iter: 22400/22500
  9. Training Step: 701 | total loss: 0.39380 | time: 80.640s
  10. | Adam | epoch: 001 | loss: 0.39380 - acc: 0.8353 -- iter: 22432/22500
  11. Training Step: 702 | total loss: 0.38980 | time: 80.787s
  12. | Adam | epoch: 001 | loss: 0.38980 - acc: 0.8392 -- iter: 22464/22500
  13. Training Step: 703 | total loss: 0.39020 | time: 80.970s
  14. | Adam | epoch: 001 | loss: 0.39020 - acc: 0.8397 -- iter: 22496/22500
  15. Training Step: 704 | total loss: 0.38543 | time: 82.891s
  16. | Adam | epoch: 001 | loss: 0.38543 - acc: 0.8370 | val_loss: 0.44625 - val_acc: 0.7880 -- iter: 22500/22500
  17. --
  18. 测试结果: [[ 0.77064246 0.2293576 ]]



加载模型并做预测:

 
    
  1. import tensorflow as tf
  2. import numpy as np
  3. import tflearn
  4. from tflearn.layers.core import input_data, dropout, fully_connected
  5. from tflearn.layers.conv import conv_1d, global_max_pool
  6. from tflearn.layers.merge_ops import merge
  7. from tflearn.layers.estimator import regression
  8. """
  9. 跟训练模型的网络结构一样
  10. """
  11. network = input_data(shape=[None, 100], name='input')
  12. network = tflearn.embedding(network, input_dim=10000, output_dim=128)
  13. branch1 = conv_1d(network, 128, 3, padding='valid', activation='relu', regularizer="L2")
  14. branch2 = conv_1d(network, 128, 4, padding='valid', activation='relu', regularizer="L2")
  15. branch3 = conv_1d(network, 128, 5, padding='valid', activation='relu', regularizer="L2")
  16. network = merge([branch1, branch2, branch3], mode='concat', axis=1)
  17. network = tf.expand_dims(network, 2)
  18. network = global_max_pool(network)
  19. network = dropout(network, 0.5)
  20. network = fully_connected(network, 2, activation='softmax')
  21. network = regression(network, optimizer='adam', learning_rate=0.001,
  22. loss='categorical_crossentropy', name='target')
  23. """
  24. 加载模型做预测
  25. """
  26. model = tflearn.DNN(network)
  27. model.load("cnn.model")
  28. test=np.linspace(1,101,100).reshape(1,100)
  29. # Predict [[ 0.7725634 0.22743654]]
  30. prediction = model.predict(test)
  31. print("模型预测结果",prediction)


结果:

 
    
  1. 2017-10-15 19:35:14.940689: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
  2. 模型预测结果 [[ 0.77064246 0.2293576 ]]
  3. Process finished with exit code 0


基于tflearn高阶api怎么做文本分类基本上完成,模型能正确加载也并能做成正确预测,这部署在service上并不是什么难事了,在上一篇博客提到怎么部署成service。

你可能感兴趣的:(python编程,机器学习)