赞
踩
首先回归一下之前写的一篇博客: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张量
#参数:
#返回
一个张量
下面贴上tensorflow官方文档上的示例:
- inputs = tf.keras.Input(shape=(32,)) # Returns a placeholder tensor
-
- # A layer instance is callable on a tensor, and returns a tensor.
- x = layers.Dense(64, activation='relu')(inputs)
- x = layers.Dense(64, activation='relu')(x)
- predictions = layers.Dense(10, activation='softmax')(x)
- # 在给定输入和输出的情况下实例化模型。
- model = tf.keras.Model(inputs=inputs, outputs=predictions)
-
- # The compile step specifies the training configuration.
- model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
- loss='categorical_crossentropy',
- metrics=['accuracy'])
-
- # Trains for 5 epochs
- model.fit(data, labels, batch_size=32, epochs=5)
下面为基于keras函数式编程的Text-CNN模型实现:
- """
- -------------------------------------------------
- File Name: text-cnn
- Description :
- Author : chaorenfei
- date: 2019/4/25
- -------------------------------------------------
- Change Activity:
- 2019/4/25:
- -------------------------------------------------
- """
- """
- CNN模型首次使用在文本分类,是Yoon Kim发表的“Convolutional Neural Networks for Sentence Classification”论文中。
- """
- import tensorflow as tf
- from tensorflow import keras
- import numpy as np
- # from tensorflow.keras.layers import Input, Dense
-
-
- imdb = keras.datasets.imdb
-
- (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
- ############################
- # explore data
- print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels)))
- # >> Training entries: 25000, labels: 25000
- print(train_data[0])
- # >>
- print(len(train_data[0]), len(train_data[1]))
- ############################
- # 将整数转换回字词:了解如何将整数转换回文本可能很有用。在以下代码中,我们将创建一个辅助函数来查询包含整数到字符串映射的字典对象:
- # A dictionary mapping words to an integer index
- word_index = imdb.get_word_index()
-
- # The first indices are reserved
- word_index = {k: (v + 3) for k, v in word_index.items()}
- word_index["<PAD>"] = 0
- word_index["<START>"] = 1
- word_index["<UNK>"] = 2 # unknown
- word_index["<UNUSED>"] = 3
-
- reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
-
-
- def decode_review(text):
- return ' '.join([reverse_word_index.get(i, '?') for i in text])
-
-
- # 现在,我们可以使用 decode_review 函数显示第一条影评的文本:
- print(decode_review(train_data[0]))
- ####################################
- # prepare data
- # 我们可以填充数组,使它们都具有相同的长度,然后创建一个形状为 max_length * num_reviews 的整数张量。我们可以使用一个能够处理这种形状的嵌入层作为网络中的第一层。
- # 使用 pad_sequences 函数将长度标准化
- train_data = keras.preprocessing.sequence.pad_sequences(train_data,
- value=word_index["<PAD>"],
- padding='post',
- maxlen=256)
-
- test_data = keras.preprocessing.sequence.pad_sequences(test_data,
- value=word_index["<PAD>"],
- padding='post',
- maxlen=256)
- # now, the len of data is 256
- print(train_data[0])
- ##########################################
- #### stucture the model
-
- convs = []
- inputs = keras.layers.Input(shape=(256,))
- embed1 = keras.layers.Embedding(10000, 32)(inputs)
- # embed = keras.layers.Reshape(-1,256, 32, 1)(embed1)
- print(embed1[0])
- def reshapes(embed1):
- embed = tf.reshape(embed1, [-1, 256, 32, 1]);
- return embed
- # embed = tf.reshape(embed1, [-1, 256, 32, 1])
- embed = keras.layers.Lambda(reshapes)(embed1);
- print(embed[0])
- l_conv1 = keras.layers.Conv2D(filters=3, kernel_size=(2, 32), activation='relu')(embed) #现长度 = 1+(原长度-卷积核大小+2*填充层大小) /步长 卷积核的形状(fsz,embedding_size)
- l_pool1 = keras.layers.MaxPooling2D(pool_size=(255, 1))(l_conv1) # 这里面最大的不同 池化层核的大小与卷积完的数据长度一样
- l_pool11 = keras.layers.Flatten()(l_pool1) #一般为卷积网络最近全连接的前一层,用于将数据压缩成一维
- convs.append(l_pool11)
- l_conv2 = keras.layers.Conv2D(filters=3, kernel_size=(3, 32), activation='relu')(embed)
- l_pool2 = keras.layers.MaxPooling2D(pool_size=(254, 1))(l_conv2)
- l_pool22 = keras.layers.Flatten()(l_pool2)
- convs.append(l_pool22)
- l_conv3 = keras.layers.Conv2D(filters=3, kernel_size=(4, 32), activation='relu')(embed)
- l_pool3 = keras.layers.MaxPooling2D(pool_size=(253, 1))(l_conv3)
- l_pool33 = keras.layers.Flatten()(l_pool2)
- convs.append(l_pool33)
- merge = keras.layers.concatenate(convs, axis=1)
-
- out = keras.layers.Dropout(0.5)(merge)
-
- output = keras.layers.Dense(32, activation='relu')(out)
-
- pred = keras.layers.Dense(units=1, activation='sigmoid')(output)
-
- model = keras.models.Model(inputs=inputs, outputs=pred)
- # adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
- model.summary()
- model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
-
- ###############################################################
-
- # validation data
- x_val = train_data[:10000]
- partial_x_train = train_data[10000:]
-
- y_val = train_labels[:10000]
- partial_y_train = train_labels[10000:]
- # fit
- history = model.fit(partial_x_train, partial_y_train, batch_size=512,
- epochs=40,
- validation_data=(x_val, y_val),
- verbose=1)
- # evalute model
- results = model.evaluate(test_data, test_labels)
-
- print(results)
- # predict data
- predictions = model.predict(test_data)
- ##################################################################
- # 创建准确率和损失随时间变化的图
- # model.fit() 返回一个 History 对象,该对象包含一个字典,其中包括训练期间发生的所有情况:
- history_dict = history.history
- print(history_dict.keys())
- # >>dict_keys(['loss', 'val_loss', 'val_acc', 'acc'])
- # 可以使用这些指标绘制训练损失与验证损失图表以进行对比,并绘制训练准确率与验证准确率图表:
- import matplotlib.pyplot as plt
-
- acc = history.history['acc']
- val_acc = history.history['val_acc']
- loss = history.history['loss']
- val_loss = history.history['val_loss']
-
- epochs = range(1, len(acc) + 1)
- ##########-------------画图方式1-----------------
-
- # # "bo" is for "blue dot"
- # plt.plot(epochs, loss, 'bo', label='Training loss')
- # # b is for "solid blue line"
- # plt.plot(epochs, val_loss, 'b', label='Validation loss')
- # plt.title('Training and validation loss')
- # plt.xlabel('Epochs')
- # plt.ylabel('Loss')
- # plt.legend()
- #
- # plt.show()
- # # -----------------------------------------
- # plt.clf() # clear figure
- # acc_values = history_dict['acc']
- # val_acc_values = history_dict['val_acc']
- #
- # plt.plot(epochs, acc, 'bo', label='Training acc')
- # plt.plot(epochs, val_acc, 'b', label='Validation acc')
- # plt.title('Training and validation accuracy')
- # plt.xlabel('Epochs')
- # plt.ylabel('Accuracy')
- # plt.legend()
-
- # plt.show()
-
- #######--------画图方式2-------------------
- # fig = plt.figure()
- # ax = plt.subplot(1,2,1)
- # plt.plot(epochs, loss, 'bo', label='Training loss')
- # plt.plot(epochs, val_loss, 'b', label='Validation loss')
- # plt.title('Training and validation loss')
- # plt.xlabel('Epochs')
- # plt.ylabel('Loss')
- # plt.legend()
- #
- # ax2 = plt.subplot(1,2,2)
- # acc_values = history_dict['acc']
- # val_acc_values = history_dict['val_acc']
- #
- # plt.plot(epochs, acc, 'bo', label='Training acc')
- # plt.plot(epochs, val_acc, 'b', label='Validation acc')
- # plt.title('Training and validation accuracy')
- # plt.xlabel('Epochs')
- # plt.ylabel('Accuracy')
- # plt.legend()
- #
- # plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。