程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

python-tensorflow框架学习 -3

发布于2020-02-24 22:45     阅读(760)     评论(0)     点赞(20)     收藏(2)


本文接着  python-tensorflow框架学习 -2,本部分后面会持续更新,目前只写了两个子部份: CNN + RNN

part three: CNN - mnist classification;  RNN - mnist classification ; RNN - mnist regression

* CNN - mnist classification : 利用CNN网络构建mnist数据集的分类任务

* RNN - mnist classification : 利用RNN网络构建mnist数据集的分类任务

* RNN - cos function regression : 利用RNN网络构建cos函数预测回归任务

3.1 CNN -classification.py

  1. # 一个简单的mnist手写数字识别分类任务(CNN网络) 600step: 86%
  2. import tensorflow as tf
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from tensorflow.examples.tutorials.mnist import input_data
  6. tf.set_random_seed(1)
  7. np.random.seed(1)
  8. BATCH_SIZE = 50
  9. LR = 0.001
  10. # 1 构建测试集、并为训练集设置占位符 【mnist数据集,如果本地相应数据集下没有则会通过滤镜进行下载,否则直接使用】
  11. mnist = input_data.read_data_sets('./mnist', one_hot=True) # they has been normalized to range (0,1)
  12. test_x = mnist.test.images[:2000]
  13. test_y = mnist.test.labels[:2000] # 先从mnist的test数据集中获取相应部分的测试集
  14. tf_x = tf.placeholder(tf.float32, [None, 28*28])/225.
  15. image = tf.reshape(tf_x, [-1, 28, 28, 1]) # [batch, height, width, channel] :[batch, 28, 28, 1]
  16. tf_y = tf.placeholder(tf.int32, [None, 10])
  17. # 2 CNN网络构建
  18. conv1 = tf.layers.conv2d(inputs=image, filters=16, kernel_size=5
  19. ,strides=1, padding='same', activation=tf.nn.relu
  20. ) # [batch, 28, 28, 1] to [batch, 28, 28, 16]
  21. pool1 = tf.layers.max_pooling2d(conv1, pool_size=2, strides=2) # to [batch, 14, 14, 16]
  22. conv2 = tf.layers.conv2d(inputs=pool1, filters=32, kernel_size=5, strides=1, padding='same', activation=tf.nn.relu) # to [batch, 14, 14, 32]
  23. pool2 = tf.layers.max_pooling2d(conv2, pool_size=2, strides=2) # to [batch, 7, 7, 32]
  24. flat_res = tf.reshape(pool2, [-1, 7*7*32]) # to [batch, 7*7*32]
  25. output = tf.layers.dense(flat_res, 10) # output layer: to [batch, 10]
  26. # 3 构建相应的loss和train_op
  27. loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # cross entropy func
  28. train_op = tf.train.AdamOptimizer(LR).minimize(loss)
  29. accuracy = tf.metrics.accuracy(labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output,axis=1),)[1] # lables和preds均是一个10维度的向量
  30. sess = tf.Session()
  31. init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
  32. sess.run(init_op) # initialize var in graph
  33. # 4 following function (plot_with_labels) is for visualization, can be ignored if not interested 【TSNE 降维方法】
  34. from matplotlib import cm
  35. try:
  36. from sklearn.manifold import TSNE; HAS_SK = True
  37. except:
  38. HAS_SK = False; print('\nplease install sklearn for layer visualization')
  39. def plot_with_labels(lowDWeights, labels):
  40. plt.cla()
  41. X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
  42. for x, y, s in zip(X, Y, labels):
  43. c = cm.rainbow(int(225*s/9))
  44. plt.text(x, y, s, backgroundcolor=c, fontsize=9)
  45. plt.xlim(X.min(),X.max())
  46. plt.ylim(Y.min(), Y.max())
  47. plt.title('Visualize last layer')
  48. plt.show()
  49. plt.pause(0.01)
  50. plt.ion()
  51. # 5 训练
  52. for step in range(600):
  53. b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
  54. _, loss_ = sess.run([train_op, loss], feed_dict={tf_x:b_x, tf_y:b_y})
  55. if step % 50 == 0:
  56. accuracy_, flat_representation = sess.run([accuracy, flat_res], {tf_x: test_x, tf_y: test_y})
  57. print('step:', step,"| train loss :%.4f"%loss_, "|test accuracy:%.4f"%accuracy_)
  58. if HAS_SK:
  59. # visualization of trained flatten layer (T-SNE)
  60. tsne = TSNE(perplexity=30, n_components=2, init='pca',n_iter=5000)
  61. plot_only = 500
  62. low_dim_embs = tsne.fit_transform(flat_representation[:plot_only, :])
  63. labels = np.argmax(test_y,axis=1)[:plot_only]
  64. plot_with_labels(low_dim_embs, labels)
  65. plt.ioff()
  66. # 6 打印10个预测和真实的例子
  67. test_output = sess.run(output, feed_dict={tf_x: test_x[:10]})
  68. pred_y = np.argmax(test_output, 1)
  69. print('pred_numbers:', pred_y)
  70. print('real_numbers:', np.argmax(test_y[:10], 1))

结果分析:每过50step,会打印相应的loss 和 accuracy value,并show出当前tsne降维后的各类分类结果。[tsne是一个动态变化的过程,便于分析, 本博客仅仅展示600step的最终结果]

  1. step: 0 | train loss :2.2956 |test accuracy:0.1365
  2. step: 50 | train loss :0.3620 |test accuracy:0.4765
  3. step: 100 | train loss :0.2210 |test accuracy:0.6137
  4. step: 150 | train loss :0.2453 |test accuracy:0.6873
  5. step: 200 | train loss :0.1987 |test accuracy:0.7373
  6. step: 250 | train loss :0.2205 |test accuracy:0.7717
  7. step: 300 | train loss :0.0810 |test accuracy:0.7940
  8. step: 350 | train loss :0.0702 |test accuracy:0.8144
  9. step: 400 | train loss :0.1085 |test accuracy:0.8296
  10. step: 450 | train loss :0.0775 |test accuracy:0.8425
  11. step: 500 | train loss :0.1935 |test accuracy:0.8535
  12. step: 550 | train loss :0.1200 |test accuracy:0.8618
  13. pred_numbers: [7 2 1 0 4 1 4 9 5 9]
  14. real_numbers: [7 2 1 0 4 1 4 9 5 9]

分类结果tsne可视化最终状态:

3.2 RNN -classification.py 【同样的分类数据集,利用RNN网络来建立分类任务】

  1. # mnist的一个简单的rnn分类问题示例代码 89% 的准确率
  2. import tensorflow as tf
  3. from tensorflow.examples.tutorials.mnist import input_data
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. tf.set_random_seed(1)
  7. np.random.seed(1)
  8. # Hyper Parameters
  9. BATCH_SIZE = 64
  10. TIME_STEP = 28 # rnn time step / image height
  11. INPUT_SIZE = 28 # rnn input size / image width
  12. LR = 0.01 # learning rate
  13. # data
  14. mnist = input_data.read_data_sets('./mnist', one_hot=True) # they has been normalized to range (0,1)
  15. test_x = mnist.test.images[:2000]
  16. test_y = mnist.test.labels[:2000]
  17. # plot one example
  18. print(mnist.train.images.shape) # (55000, 28 * 28)
  19. print(mnist.train.labels.shape) # (55000, 10)
  20. plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
  21. plt.title('%i' % np.argmax(mnist.train.labels[0]))
  22. plt.show()
  23. # tensorflow placeholders
  24. tf_x = tf.placeholder(tf.float32, [None, TIME_STEP * INPUT_SIZE]) # shape(batch, 784)
  25. image = tf.reshape(tf_x, [-1, TIME_STEP, INPUT_SIZE]) # (batch, height, width, channel)
  26. tf_y = tf.placeholder(tf.int32, [None, 10]) # input y
  27. # RNN
  28. rnn_cell = tf.nn.rnn_cell.LSTMCell(num_units=64)
  29. outputs, (h_c, h_n) = tf.nn.dynamic_rnn(
  30. rnn_cell, # cell you have chosen
  31. image, # input
  32. initial_state=None, # the initial hidden state
  33. dtype=tf.float32, # must given if set initial_state = None
  34. time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
  35. )
  36. output = tf.layers.dense(outputs[:, -1, :], 10) # output based on the last output step
  37. loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # compute cost
  38. train_op = tf.train.AdamOptimizer(LR).minimize(loss)
  39. accuracy = tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables
  40. labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
  41. sess = tf.Session()
  42. init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
  43. sess.run(init_op) # initialize var in graph
  44. for step in range(1200): # training
  45. b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
  46. _, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y})
  47. if step % 50 == 0: # testing
  48. accuracy_ = sess.run(accuracy, {tf_x: test_x, tf_y: test_y})
  49. print('train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)
  50. # print 10 predictions from test data
  51. test_output = sess.run(output, {tf_x: test_x[:10]})
  52. pred_y = np.argmax(test_output, 1)
  53. print(pred_y, 'prediction number')
  54. print(np.argmax(test_y[:10], 1), 'real number')

结果:

示例手写数字图例:

  1. train loss: 2.3271 | test accuracy: 0.14
  2. train loss: 0.8738 | test accuracy: 0.42
  3. train loss: 0.5579 | test accuracy: 0.53
  4. train loss: 0.5082 | test accuracy: 0.62
  5. train loss: 0.3069 | test accuracy: 0.68
  6. train loss: 0.1639 | test accuracy: 0.72
  7. train loss: 0.2474 | test accuracy: 0.75
  8. train loss: 0.1535 | test accuracy: 0.77
  9. train loss: 0.0769 | test accuracy: 0.79
  10. train loss: 0.2508 | test accuracy: 0.80
  11. train loss: 0.1221 | test accuracy: 0.82
  12. train loss: 0.3389 | test accuracy: 0.83
  13. train loss: 0.1557 | test accuracy: 0.84
  14. train loss: 0.0876 | test accuracy: 0.84
  15. train loss: 0.1594 | test accuracy: 0.85
  16. train loss: 0.2142 | test accuracy: 0.86
  17. train loss: 0.0384 | test accuracy: 0.86
  18. train loss: 0.2602 | test accuracy: 0.87
  19. train loss: 0.2522 | test accuracy: 0.87
  20. train loss: 0.0524 | test accuracy: 0.88
  21. train loss: 0.1400 | test accuracy: 0.88
  22. train loss: 0.0989 | test accuracy: 0.88
  23. train loss: 0.1122 | test accuracy: 0.89
  24. train loss: 0.0807 | test accuracy: 0.89
  25. [7 2 1 0 4 1 4 9 5 9] prediction number
  26. [7 2 1 0 4 1 4 9 5 9] real number

3.3 RNN -regression.py 【基于RNN网络,构建一个预测cos函数的回归模型】

  1. import tensorflow as tf
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. # Hyper Parameters
  5. TIME_STEP = 10 # rnn time step
  6. INPUT_SIZE = 1 # rnn input size
  7. CELL_SIZE = 32 # rnn cell size
  8. LR = 0.02 # learning rate
  9. # show data
  10. steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)
  11. x_np = np.sin(steps); y_np = np.cos(steps) # float32 for converting torch FloatTensor
  12. plt.plot(steps, y_np, 'r-', label='target (cos)'); plt.plot(steps, x_np, 'b-', label='input (sin)')
  13. plt.legend(loc='best'); plt.show()
  14. # tensorflow placeholders
  15. tf_x = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE]) # shape(batch, 5, 1)
  16. tf_y = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE]) # input y
  17. # RNN
  18. rnn_cell = tf.nn.rnn_cell.LSTMCell(num_units=CELL_SIZE)
  19. init_s = rnn_cell.zero_state(batch_size=1, dtype=tf.float32) # very first hidden state
  20. outputs, final_s = tf.nn.dynamic_rnn(
  21. rnn_cell, # cell you have chosen
  22. tf_x, # input
  23. initial_state=init_s, # the initial hidden state
  24. time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
  25. )
  26. outs2D = tf.reshape(outputs, [-1, CELL_SIZE]) # reshape 3D output to 2D for fully connected layer
  27. net_outs2D = tf.layers.dense(outs2D, INPUT_SIZE)
  28. outs = tf.reshape(net_outs2D, [-1, TIME_STEP, INPUT_SIZE]) # reshape back to 3D
  29. loss = tf.losses.mean_squared_error(labels=tf_y, predictions=outs) # compute cost
  30. train_op = tf.train.AdamOptimizer(LR).minimize(loss)
  31. sess = tf.Session()
  32. sess.run(tf.global_variables_initializer()) # initialize var in graph
  33. plt.figure(1, figsize=(12, 5)); plt.ion() # continuously plot
  34. for step in range(60):
  35. start, end = step * np.pi, (step+1)*np.pi # time range
  36. # use sin predicts cos
  37. steps = np.linspace(start, end, TIME_STEP)
  38. x = np.sin(steps)[np.newaxis, :, np.newaxis] # shape (batch, time_step, input_size)
  39. y = np.cos(steps)[np.newaxis, :, np.newaxis]
  40. if 'final_s_' not in globals(): # first state, no any hidden state
  41. feed_dict = {tf_x: x, tf_y: y}
  42. else: # has hidden state, so pass it to rnn
  43. feed_dict = {tf_x: x, tf_y: y, init_s: final_s_}
  44. _, pred_, final_s_ = sess.run([train_op, outs, final_s], feed_dict) # train
  45. # plotting
  46. plt.plot(steps, y.flatten(), 'r-'); plt.plot(steps, pred_.flatten(), 'b-')
  47. plt.ylim((-1.2, 1.2)); plt.draw(); plt.pause(0.05)
  48. plt.ioff(); plt.show()

真实曲线:红色曲线:cos函数(y)   +  蓝色曲线:sin函数(x)  ,goal: 基于sin函数预测cos函数。

预测;

发布了178 篇原创文章 · 获赞 80 · 访问量 11万+


所属网站分类: 技术文章 > 博客

作者:从新来过

链接:https://www.pythonheidong.com/blog/article/232613/5c96953fad548a19724d/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)