当前位置:   article > 正文

tf.keras学习总结(三):keras的函数式编程(Text-CNN实现)_keras输入将是32维向量的批次cnn例子

keras输入将是32维向量的批次cnn例子

首先回归一下之前写的一篇博客:keras函数式编程的官方文档示例


本篇文章以Text-CNN模型的实现为例,展示一下keras函数式编程的使用。建议先了解一下text-CNN模型


首先贴上keras函数式编程的第一句语句:

keras.layers.Input(shape=None,batch_shape=None,name=None,dtype=K.floatx(),sparse=False,tensor=None)

Input(shape=None,batch_shape=None,name=None,dtype=K.floatx(),sparse=False,tensor=None)

Input():用来实例化一个keras张量

#参数:

  • shape: 形状元组(整型),不包括batch size。for instance, shape=(32,) 表示了预期的输入将是一批32维的向量。
  • batch_shape: 形状元组(整型),包括了batch size。for instance, batch_shape=(10,32)表示了预期的输入将是10个32维向量的批次。
  • name: 对于该层是可选的名字字符串。在一个模型中是独一无二的(同一个名字不能复用2次)。如果name没有被特指将会自动生成。
  • dtype: 预期的输入数据类型
  • sparse: 特定的布尔值,占位符是否为sparse
  • tensor: 可选的存在的向量包装到Input层,如果设置了,该层将不会创建一个占位张量。

#返回

一个张量


下面贴上tensorflow官方文档上的示例:

  1. inputs = tf.keras.Input(shape=(32,)) # Returns a placeholder tensor
  2. # A layer instance is callable on a tensor, and returns a tensor.
  3. x = layers.Dense(64, activation='relu')(inputs)
  4. x = layers.Dense(64, activation='relu')(x)
  5. predictions = layers.Dense(10, activation='softmax')(x)
  6. # 在给定输入和输出的情况下实例化模型。
  7. model = tf.keras.Model(inputs=inputs, outputs=predictions)
  8. # The compile step specifies the training configuration.
  9. model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
  10. loss='categorical_crossentropy',
  11. metrics=['accuracy'])
  12. # Trains for 5 epochs
  13. model.fit(data, labels, batch_size=32, epochs=5)

下面为基于keras函数式编程的Text-CNN模型实现:

  1. """
  2. -------------------------------------------------
  3. File Name: text-cnn
  4. Description :
  5. Author : chaorenfei
  6. date: 2019/4/25
  7. -------------------------------------------------
  8. Change Activity:
  9. 2019/4/25:
  10. -------------------------------------------------
  11. """
  12. """
  13. CNN模型首次使用在文本分类,是Yoon Kim发表的“Convolutional Neural Networks for Sentence Classification”论文中。
  14. """
  15. import tensorflow as tf
  16. from tensorflow import keras
  17. import numpy as np
  18. # from tensorflow.keras.layers import Input, Dense
  19. imdb = keras.datasets.imdb
  20. (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
  21. ############################
  22. # explore data
  23. print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels)))
  24. # >> Training entries: 25000, labels: 25000
  25. print(train_data[0])
  26. # >>
  27. print(len(train_data[0]), len(train_data[1]))
  28. ############################
  29. # 将整数转换回字词:了解如何将整数转换回文本可能很有用。在以下代码中,我们将创建一个辅助函数来查询包含整数到字符串映射的字典对象:
  30. # A dictionary mapping words to an integer index
  31. word_index = imdb.get_word_index()
  32. # The first indices are reserved
  33. word_index = {k: (v + 3) for k, v in word_index.items()}
  34. word_index["<PAD>"] = 0
  35. word_index["<START>"] = 1
  36. word_index["<UNK>"] = 2 # unknown
  37. word_index["<UNUSED>"] = 3
  38. reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
  39. def decode_review(text):
  40. return ' '.join([reverse_word_index.get(i, '?') for i in text])
  41. # 现在,我们可以使用 decode_review 函数显示第一条影评的文本:
  42. print(decode_review(train_data[0]))
  43. ####################################
  44. # prepare data
  45. # 我们可以填充数组,使它们都具有相同的长度,然后创建一个形状为 max_length * num_reviews 的整数张量。我们可以使用一个能够处理这种形状的嵌入层作为网络中的第一层。
  46. # 使用 pad_sequences 函数将长度标准化
  47. train_data = keras.preprocessing.sequence.pad_sequences(train_data,
  48. value=word_index["<PAD>"],
  49. padding='post',
  50. maxlen=256)
  51. test_data = keras.preprocessing.sequence.pad_sequences(test_data,
  52. value=word_index["<PAD>"],
  53. padding='post',
  54. maxlen=256)
  55. # now, the len of data is 256
  56. print(train_data[0])
  57. ##########################################
  58. #### stucture the model
  59. convs = []
  60. inputs = keras.layers.Input(shape=(256,))
  61. embed1 = keras.layers.Embedding(10000, 32)(inputs)
  62. # embed = keras.layers.Reshape(-1,256, 32, 1)(embed1)
  63. print(embed1[0])
  64. def reshapes(embed1):
  65. embed = tf.reshape(embed1, [-1, 256, 32, 1]);
  66. return embed
  67. # embed = tf.reshape(embed1, [-1, 256, 32, 1])
  68. embed = keras.layers.Lambda(reshapes)(embed1);
  69. print(embed[0])
  70. l_conv1 = keras.layers.Conv2D(filters=3, kernel_size=(2, 32), activation='relu')(embed) #现长度 = 1+(原长度-卷积核大小+2*填充层大小) /步长 卷积核的形状(fsz,embedding_size
  71. l_pool1 = keras.layers.MaxPooling2D(pool_size=(255, 1))(l_conv1) # 这里面最大的不同 池化层核的大小与卷积完的数据长度一样
  72. l_pool11 = keras.layers.Flatten()(l_pool1) #一般为卷积网络最近全连接的前一层,用于将数据压缩成一维
  73. convs.append(l_pool11)
  74. l_conv2 = keras.layers.Conv2D(filters=3, kernel_size=(3, 32), activation='relu')(embed)
  75. l_pool2 = keras.layers.MaxPooling2D(pool_size=(254, 1))(l_conv2)
  76. l_pool22 = keras.layers.Flatten()(l_pool2)
  77. convs.append(l_pool22)
  78. l_conv3 = keras.layers.Conv2D(filters=3, kernel_size=(4, 32), activation='relu')(embed)
  79. l_pool3 = keras.layers.MaxPooling2D(pool_size=(253, 1))(l_conv3)
  80. l_pool33 = keras.layers.Flatten()(l_pool2)
  81. convs.append(l_pool33)
  82. merge = keras.layers.concatenate(convs, axis=1)
  83. out = keras.layers.Dropout(0.5)(merge)
  84. output = keras.layers.Dense(32, activation='relu')(out)
  85. pred = keras.layers.Dense(units=1, activation='sigmoid')(output)
  86. model = keras.models.Model(inputs=inputs, outputs=pred)
  87. # adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
  88. model.summary()
  89. model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
  90. ###############################################################
  91. # validation data
  92. x_val = train_data[:10000]
  93. partial_x_train = train_data[10000:]
  94. y_val = train_labels[:10000]
  95. partial_y_train = train_labels[10000:]
  96. # fit
  97. history = model.fit(partial_x_train, partial_y_train, batch_size=512,
  98. epochs=40,
  99. validation_data=(x_val, y_val),
  100. verbose=1)
  101. # evalute model
  102. results = model.evaluate(test_data, test_labels)
  103. print(results)
  104. # predict data
  105. predictions = model.predict(test_data)
  106. ##################################################################
  107. # 创建准确率和损失随时间变化的图
  108. # model.fit() 返回一个 History 对象,该对象包含一个字典,其中包括训练期间发生的所有情况:
  109. history_dict = history.history
  110. print(history_dict.keys())
  111. # >>dict_keys(['loss', 'val_loss', 'val_acc', 'acc'])
  112. # 可以使用这些指标绘制训练损失与验证损失图表以进行对比,并绘制训练准确率与验证准确率图表:
  113. import matplotlib.pyplot as plt
  114. acc = history.history['acc']
  115. val_acc = history.history['val_acc']
  116. loss = history.history['loss']
  117. val_loss = history.history['val_loss']
  118. epochs = range(1, len(acc) + 1)
  119. ##########-------------画图方式1-----------------
  120. # # "bo" is for "blue dot"
  121. # plt.plot(epochs, loss, 'bo', label='Training loss')
  122. # # b is for "solid blue line"
  123. # plt.plot(epochs, val_loss, 'b', label='Validation loss')
  124. # plt.title('Training and validation loss')
  125. # plt.xlabel('Epochs')
  126. # plt.ylabel('Loss')
  127. # plt.legend()
  128. #
  129. # plt.show()
  130. # # -----------------------------------------
  131. # plt.clf() # clear figure
  132. # acc_values = history_dict['acc']
  133. # val_acc_values = history_dict['val_acc']
  134. #
  135. # plt.plot(epochs, acc, 'bo', label='Training acc')
  136. # plt.plot(epochs, val_acc, 'b', label='Validation acc')
  137. # plt.title('Training and validation accuracy')
  138. # plt.xlabel('Epochs')
  139. # plt.ylabel('Accuracy')
  140. # plt.legend()
  141. # plt.show()
  142. #######--------画图方式2-------------------
  143. # fig = plt.figure()
  144. # ax = plt.subplot(1,2,1)
  145. # plt.plot(epochs, loss, 'bo', label='Training loss')
  146. # plt.plot(epochs, val_loss, 'b', label='Validation loss')
  147. # plt.title('Training and validation loss')
  148. # plt.xlabel('Epochs')
  149. # plt.ylabel('Loss')
  150. # plt.legend()
  151. #
  152. # ax2 = plt.subplot(1,2,2)
  153. # acc_values = history_dict['acc']
  154. # val_acc_values = history_dict['val_acc']
  155. #
  156. # plt.plot(epochs, acc, 'bo', label='Training acc')
  157. # plt.plot(epochs, val_acc, 'b', label='Validation acc')
  158. # plt.title('Training and validation accuracy')
  159. # plt.xlabel('Epochs')
  160. # plt.ylabel('Accuracy')
  161. # plt.legend()
  162. #
  163. # plt.show()

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/353335
推荐阅读
相关标签
  

闽ICP备14008679号