Keras Do multilayer neural networks

Source: Internet
Author: User
Tags theano keras

I. Background and purpose

Background: Configure the Theano, get the GPU, to learn the Dnn method.

Objective: This study Keras basic usage, learn how to write MLP with Keras, learn keras the basic points of text.

Second, prepare

Toolkit: Theano, NumPy, Keras and other toolkits

Data set: If you can't get down, you can use the Thunder, to ~/.keras/datasets/below can

Code Location: examples/reuters_mlp.py

Third, the Code appreciation

"Trains and evaluate a simple mlpon the Reuters Newswire topic classification task." From __future__ import Print_functionimport numpy as Npnp.random.seed (1337) # for Reproducibilityfrom keras.datasets Impo RT reutersfrom Keras.models Import sequentialfrom keras.layers import dense, dropout, Activationfrom keras.utils import NP  _utilsfrom keras.preprocessing.text Import tokenizermax_words = #vocab大小batch_size = #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 ')
#分类数目--The original Reuters I remember is 10来, should be the corpus is the big one nb_classes = Np.max (y_train) +1print (nb_classes, ' classes ') print (' Vectorizing Sequence data ... ')
#tokenizetokenizer = Tokenizer (nb_words=max_words)
#序列化, take the top 1000 of DF
#这里有个非常好玩的事, the initial Wordindex,wordindex in X_train is the size of the word (it should be, because it's straight off)
#所以这个效率上还是很高的
#转化的还是binary, the default is not with Tfidfx_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)
#这个就好理解多了, it's coded. 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 (input_shape= (max_words))) #输入维度, 512== output Dimension Model.add (Activation (' Relu ')) # Activation function Model.add (dropout (0.5)) #dropout

#第二层model. Add (Dense (nb_classes)) Model.add (Activation (' Softmax '))
#损失函数设置, optimization function, metric model.compile (loss= ' categorical_crossentropy ', optimizer= ' Adam ', metrics=[' Accu Racy '])
#训练, cross-validation 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, ve rbose=1) print (' Test score: ', score[0]) print (' Test accuracy: ', score[1])

Iv. Comparison of training speed

This table is adjusted to a relatively good 20,000 word list, otherwise I think the discussion effect is meaningless

Training Time-CPU Training Time-gpu Val-cpu Val-gpu
First round 021 3s 79 79
Second round 021 3s 81 81
Third round 23s 3s 80 80
Fourth round 535 3s 78 79
Fifth round 40s 3s 80 80

See, even the MLP, the effect of ascension is very very big.

Keras Do multilayer neural networks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.