zoukankan      html  css  js  c++  java
  • keras做多层神经网络

    一、 背景与目的

    背景:配置好了theano,弄了gpu, 要学dnn方法。

    目的:本篇学习keras基本用法, 学习怎么用keras写mlp,学keras搞文本的基本要点。

    二、 准备

    工具包: theano、numpy、keras等工具包

    数据集: 如果下不来, 可以用迅雷下,弄到~/.keras/datasets/下面即可

    代码位置:examples/reuters_mlp.py

    三、 代码赏析

    '''Trains and evaluate a simple MLP
    on the Reuters newswire topic classification task.
    '''
    
    from __future__ import print_function
    import numpy as np
    np.random.seed(1337)  # for reproducibility
    
    from keras.datasets import reuters
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Activation
    from keras.utils import np_utils
    from keras.preprocessing.text import Tokenizer
    
    max_words = 1000 #vocab大小
    batch_size = 32 #mini_batch_size 
    nb_epoch = 5 #大循环次数
    
    print('Loading data...')
    (X_train, y_train), (X_test, y_test) = reuters.load_data(nb_words=max_words, test_split=0.2) #载入路透社语料
    #打印 print(len(X_train), 'train sequences') print(len(X_test), 'test sequences')
    #分类数目--原版路透社我记着是10来着,应该是语料用的是大的那个 nb_classes = np.max(y_train)+1 print(nb_classes, 'classes') print('Vectorizing sequence data...')
    #tokenize tokenizer = Tokenizer(nb_words=max_words)
    #序列化,取df前1000大
    #这里有个非常好玩的事, X_train 里面初始存的是wordindex,wordindex是按照词大小来的(应该是,因为直接就给撇了)
    #所以这个效率上还是很高的
    #转化的还是binary,默认不是用tfidf X_train = tokenizer.sequences_to_matrix(X_train, mode='binary') X_test = tokenizer.sequences_to_matrix(X_test, mode='binary') print('X_train shape:', X_train.shape) print('X_test shape:', X_test.shape) print('Convert class vector to binary class matrix (for use with categorical_crossentropy)')
    #这个就好理解多了, 编码而已 Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) print('Y_train shape:', Y_train.shape) print('Y_test shape:', Y_test.shape) print('Building model...') model = Sequential()
    #第一层
    #Dense就是全连接层 model.add(Dense(512, input_shape=(max_words,))) #输入维度, 512==输出维度 model.add(Activation('relu')) #激活函数 model.add(Dropout(0.5)) #dropout

    #第二层 model.add(Dense(nb_classes)) model.add(Activation('softmax'))
    #损失函数设置、优化函数,衡量标准 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    #训练,交叉验证 history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1, validation_split=0.1) score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1) print('Test score:', score[0]) print('Test accuracy:', score[1])

    四、 训练速度比较

    此表调整到了相对好一点的两万词表,要不然我觉得讨论效果没什么意义

      训练时间-cpu 训练时间-gpu val-cpu val-gpu
    第一轮 22s     3s 79 79
    第二轮 22s 3s 81 81
    第三轮 23s 3s 80 80
    第四轮 33s 3s 78 79
    第五轮 40s 3s 80 80

    看的出来,即使是mlp,效果的提升也是非常非常大的。

  • 相关阅读:
    【算法学习笔记】27.动态规划 解题报告 SJTU OJ 1254 传手绢
    【算法学习笔记】26.扫描维护法 解题报告 SJTU OJ 1133 数星星
    【算法学习笔记】25.贪心法 均分纸牌问题的分析
    【算法学习笔记】24.记忆化搜索 解题报告 SJTU OJ 1002 二哥种花生
    【算法学习笔记】23.动态规划 解题报告 SJTU OJ 1280 整装待发
    【算法学习笔记】22.算法设计初步 二分查找 上下界判断
    【算法学习笔记】21.算法设计初步 求第k个数 划分法 快排法
    【算法学习笔记】20.算法设计初步 归并排序 求逆序数
    【算法学习笔记】19.算法设计初步 最大子列和问题的几种方法
    【算法学习笔记】18.暴力求解法06 隐式图搜索2 八数码问题 未启发
  • 原文地址:https://www.cnblogs.com/lavi/p/5877767.html
Copyright © 2011-2022 走看看