• Keras Cheat Sheet: Neural Networks in Python
  • By Karlijn Willems
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Minghao23
  • Proofreader: Xuyuey, LSvih

Build your own neural network using the Keras Checklist for deep learning in Python for beginners, with code examples

Keras is an easy-to-use and powerful library based on Theano and TensorFlow that provides some high-level neural network interfaces for developing and evaluating deep learning models.

We recently launched the first online interactive Deep Learning course developed using Keras 2.0, called “Deep Learning in Python”.

Now, DataCamp has created the Keras Quick Check sheet for those who have already taken the course but still need a page of reference, or those who need an extra push to get started.

Soon, this checklist will familiarize you with how to load datasets from the library, how to preprocess data, how to construct a model structure, and how to compile, train, and evaluate it. Since you have considerable freedom in how you build your own model, you’ll see this quick checklist showing some simple examples of key Keras code before you can start building your own neural networks in Python.

In addition, you can see some examples of how to check your model and how to save and load the model. Finally, you’ll find some examples of how to make predictions about test data and how to fine-tune the model by tuning tuning parameters and early stops.

In short, you’ll see that this checklist doesn’t just show you six steps for building a neural network in Python using the Keras library.

Overall, this checklist will speed up your Python deep learning journey: with the help of these code examples, you’ll soon be preprocessing, creating, verifying, and tuning your deep learning model!

(Click on the image above to download a printable version, or read the online version below.)

Python Data science Quick check table: Keras

Keras is an easy-to-use and powerful library based on Theano and TensorFlow that provides some high-level neural network interfaces for developing and evaluating deep learning models.

A basic example

>>> import numpy as np >>> from keras.models import Sequential >>> from keras.layers import Dense >>> data = Random ((1000,100)) >>> Labels = Np.random. Randint (2,size=(1000,1)) >>> Model = Sequential() >>> model.add(Dense(32, activation='relu', input_dim=100))
>>> model.add(Dense(1, activation='sigmoid'))
>>> model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
>>> model.fit(data,labels,epochs=10,batch_size=32)
>>> predictions = model.predict(data)
Copy the code

data

Your data needs to be stored as Numpy Arrays or Numpy Arrays lists. Ideally, the data will be split into training sets and test sets, which you can do with the train_test_split module under sklearn.cross_validation.

Keras data set

>>> from keras.datasets import boston_housing, mnist, cifar10, imdb
>>> (x_train,y_train),(x_test,y_test) = mnist.load_data()
>>> (x_train2,y_train2),(x_test2,y_test2) = boston_housing.load_data()
>>> (x_train3,y_train3),(x_test3,y_test3) = cifar10.load_data()
>>> (x_train4,y_train4),(x_test4,y_test4) = imdb.load_data(num_words=20000)
>>> num_classes = 10
Copy the code

other

>>> from urllib.request import urlopen >>> data = np.loadtxt(urlopen(" http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data") ,delimiter=" ,") >>> X = data[:,0:8] >>> y = data [:,8]Copy the code

pretreatment

The sequence filling

>>> from keras.preprocessing import sequence
>>> x_train4 = sequence.pad_sequences(x_train4,maxlen=80)
>>> x_test4 = sequence.pad_sequences(x_test4,maxlen=80)
Copy the code

One – Hot coding

>>> from keras.utils import to_categorical
>>> Y_train = to_categorical(y_train, num_classes)
>>> Y_test = to_categorical(y_test, num_classes)
>>> Y_train3 = to_categorical(y_train3, num_classes)
>>> Y_test3 = to_categorical(y_test3, num_classes)
Copy the code

Training and test sets

>>> from sklearn.model_selection import train_test_split >>> X_train5, X_test5, y_train5, y_test5 = train_test_split(X, Y, test_size = 0.33, random_state = 42)Copy the code

Standardization/normalization

>>> from sklearn.preprocessing import StandardScaler
>>> scaler = StandardScaler().fit(x_train2)
>>> standardized_X = scaler.transform(x_train2)
>>> standardized_X_test = scaler.transform(x_test2)
Copy the code

Model structure

Sequential model

>>> from keras.models import Sequential
>>> model = Sequential()
>>> model2 = Sequential()
>>> model3 = Sequential()
Copy the code

Multilayer perceptron (MLP)

The second category

>>> from keras.layers import Dense
>>> model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
>>> model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
>>> model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
Copy the code

Many classification

>>> from keras.layers import Dropout
>>> model.add(Dense(512,activation='relu',input_shape=(784,))) >>> Model.add (Dropout(0.2)) >>> Model.add (Dense(512,activation='relu')) >>> Model.add (Dropout(0.2)) >>> Model.add (Dense(10,activation='softmax'))
Copy the code

Return to the

>>> model.add(Dense(64, activation='relu', input_dim=train_data.shape[1]))
>>> model.add(Dense(1))
Copy the code

Convolutional Neural Networks (CNN)

>>> from keras. Layers import Activation, Conv2D, MaxPooling2D, Flatten >>> model2.add(Conv2D(32, (3,3), padding='same', input_shape=x_train.shape[1:]))
>>> model2.add(Activation('relu')) > > > model2. Add (Conv2D (32, (3, 3))) > > > model2. Add (Activation ('relu')) > > > model2. Add (MaxPooling2D (pool_size = (2, 2))) > > > model2. Add (Dropout (0.25) > > > model2. Add (Conv2D (64, (3, 3), padding ='same'))
>>> model2.add(Activation('relu'))
>>> model2.add(Conv2D(64, (3, 3)))
>>> model2.add(Activation('relu')) > > > model2. Add (MaxPooling2D (pool_size = (2, 2))) > > > model2. Add (Dropout (0.25) > > > model2. Add (Flatten ()) > > > model2.add(Dense(512)) >>> model2.add(Activation('relu')) >>> Model2. add(Dropout(0.5)) >>> Model2. add(Dense(num_classes)) >>> Model2. add(Activation(Dense(num_classes))'softmax'))
Copy the code

Recurrent Neural Network (RNN)

>>> From keras.klayers import Embedding,LSTM >>> Model3. add(Embedding(20000,128)) >>> Model3. Add (LSTM (128, dropout = 0.2, recurrent_dropout = 0.2)) > > > model3. Add (Dense (1, the activation ='sigmoid'))
Copy the code

Check the model

Shape for the output of the model

>>> model.output_shape
Copy the code

Model describes

>>> model.summary()
Copy the code

The model configuration

>>> model.get_config()
Copy the code

List all the weight tensors in the model

>>> model.get_weights()
Copy the code

Compilation model

Multilayer perceptron (MLP)

Multilayer perceptron: dichotomies

>>> model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Copy the code

Multilayer perceptron: multiclassification

>>> model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
Copy the code

Multilayer perceptrons: regression

>>> model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
Copy the code

Recurrent Neural Network (RNN)

>>> model3.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Copy the code

Model training

>>> model3.fit(x_train4, y_train4, batch_size=32, epochs=15, verbose=1, validation_data=(x_test4, y_test4))
Copy the code

Evaluate your model’s performance

>>> score = model3.evaluate(x_test, y_test, batch_size=32)
Copy the code

To predict

>>> model3.predict(x_test4, batch_size=32)
>>> model3.predict_classes(x_test4,batch_size=32)
Copy the code

Save/load the model

>>> from keras.models import load_model
>>> model3.save('model_file.h5')
>>> my_model = load_model('my_model.h5')
Copy the code

Model fine-tuning

To optimize the parameters

>>> Optimizers import RMSprop >>> opt = RMSprop(LR =0.0001, decay= 1E-6) >>> Model2.compile (loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
Copy the code

Early stop

>>> from keras.callbacks import EarlyStopping
>>> early_stopping_monitor = EarlyStopping(patience=2)
>>> model3.fit(x_train4, y_train4, batch_size=32, epochs=15, validation_data=(x_test4, y_test4), callbacks=[early_stopping_monitor])
Copy the code

Further exploration

Starting with the Keras tutorial, you will learn in a simple, step-by-step way how to explore and preprocess a data set about wine quality, build multi-layer perceptrons for classification and regression tasks, compile, fit and evaluate models, and fine-tune the models you build.

In addition, don’t miss our SciKit-Learn, NumPy and Pandas tables!

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.