Zero, import,

1. Import the Keras library and print the version information

import keras
print(keras.__version__)
Copy the code

A simple example

MNIST classification of handwritten digital images is realized using MLP model

1.1 Selection Model

2. Initialize a Sequential model

model = Sequential()
Copy the code

1.2 Network Construction

3. Add a hidden layer for model with 784 inputs and 784 outputs, and use RELu for activation function

model.add(Dense(units=784, activation='relu', input_dim=784))
Copy the code

4. Add 10 output layers for model on the previous basis, and use Softmax for activation function

model.add(Dense(units=10, activation='softmax'))
Copy the code

5. Run. Summary () to view model parameters

model.summary()
Copy the code

1.3 Compilation Model

6. Compile () is used to configure the learning process. The cost function loss is categorical_crossentropy, the optimization algorithm optimizer is SGD, and the performance indicator is accuracy

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

1.4 training

Read data (omitted)

7. One-hot code y value

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
Copy the code

8. Send data into model training

model.fit(x_train, y_train, epochs=5, batch_size=32)
Copy the code

9. Evaluate model performance

score = model.evaluate(x_test, y_test, batch_size=128)
print("loss:",score[0])
print("accu:",score[1])
Copy the code

1.5 predict

10. Use models to predict

model.predict_classes(x_test, batch_size=128)
Copy the code

Second, a slightly more complex sequential model

LeNet5 is used to classify CIFAR10 data sets

2.1 Model Selection

11. Create a new sequence model

model = Sequential()
Copy the code

2.2 Network Construction

12. Complete input-C1: Add a 2d convolution layer with the INPUT of 32x32x3, the size of convolution kernel is 5×5 and the kernel type is 6, and assume that we accidentally missed relu

model.add(Conv2D(6, (5.5), input_shape=(32.32.3)))
Copy the code

13. I accidentally left out relu just now, and now I can add it

model.add(Activation('relu'))
Copy the code

14. Complete c1-S2:2X2 lower sampling layer

model.add(MaxPooling2D(pool_size=(2.2)))
Copy the code

15. Complete S2-C3:2D convolution, 16 cores, 5×5 size, don’t forget RELu

model.add(Conv2D(16, (5.5), activation='relu'))
Copy the code

16. Complete c3-S4:2X2 lower sampling layer

model.add(MaxPooling2D(pool_size=(2.2)))
Copy the code

17. Complete S4-C5: Add flat layer first

model.add(Flatten())
Copy the code

18. Add the full connection layer, output 120 dimensions, and activate function RELu

model.add(Dense(120, activation='relu'))
Copy the code

19. Complete C5-F6: Add full connection layer, 84 outputs, activate function RELu

model.add(Dense(84, activation='relu'))
Copy the code

20. Complete F6-Output: Add full connection layer, 10 outputs, activate function Softmax

model.add(Dense(10, activation='softmax'))
Copy the code

2.3 build

21. Set parameters of stochastic gradient descent SGD optimization algorithm: Learning_rate =0.01, epoch=25, Decay = Learning_rate /epoch, Momentum =0.9, Nesterov =False

from keras.optimizers import SGD

lrate = 0.01
epoch = 10
decay = lrate/epoch
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
Copy the code

23. To compile the model, the cost function loss is defined as categorical_crossentropy. The optimization algorithm has been defined previously

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

2.4 training

Read data & Preprocessing (omitted)

24. Feed the data into the model and set 20% as validation sets

history=model.fit(x=train_X, y=train_Y,validation_split=0.2, epochs=10, batch_size=32, verbose=1)
Copy the code

25. Accuracy value of training set and verification set of visual historical training Loss value of training set and verification set of visual historical training

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train'.'val'], loc='upper left')
plt.show()
Copy the code

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train'.'val'], loc='upper left')
plt.show()
Copy the code

26. Model evaluation

scores = model.evaluate(test_X, test_Y, verbose=0)
print(model.metrics_names)
print(scores)
Copy the code

2.5 predict

27. Forecast results

prediction=model.predict_classes(test_X)
prediction[:10]
Copy the code

Visual prediction results

Display confusion matrix

import pandas as pd
print(classes)
pd.crosstab(y_gt.reshape(- 1),prediction,rownames=['label'],colnames=['predict'])
Copy the code

3, The Model

This section implements a multiple input multiple output model

3.1 Building a Network

Here we choose the functional model (Model), so there is no need to instantiate in advance, the network structure is implemented first

28. Definition ①, the main input layer, receives the news headline itself, which is a sequence of integers (each integer encodes a word). These integers are between 1 and 10,000 words (a 10,000-word vocabulary), and the sequence length is 100 words. Named main_input

main_input = Input(shape=(100,), dtype='int32', name='main_input')
Copy the code

29. Define ②, encode the input sequence as a sequence of dense vectors, and each dimension of output vector is 512.

x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)
Copy the code

30. Definition ③, LSTM layer converts the sequence of vectors into a single vector, which contains the context information of the entire sequence, and the output dimension is 32

lstm_out = LSTM(32)(x)
Copy the code

⑩, as an auxiliary loss, LSTM layer and Embedding layer can be trained smoothly even if the main loss of the model is very high. Output dimension 1, activation function sigmoID, named aux_output

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
Copy the code

Define ⑨, enter auxiliary data, a 5-dimensional vector, and name it aux_input

auxiliary_input = Input(shape=(5,), name='aux_input')
Copy the code

33. Definition ④, connect the auxiliary input data with the output of the LSTM layer and input it into the model

x = keras.layers.concatenate([lstm_out, auxiliary_input])
Copy the code

34. Define ⑤⑥⑦, stack multiple fully connected network layers, the output is 64 dimensions

x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
Copy the code

8, output layer, activation function sigmoid, named main_output

main_output = Dense(1, activation='sigmoid', name='main_output')(x)
Copy the code

3.2 Defining the Model

36. Define a model with two inputs and two outputs

model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])
Copy the code

3.3 build

37. Compile the model and assign a 0.2 weight to the auxiliary loss

model.compile(optimizer='rmsprop',
              loss={'main_output': 'binary_crossentropy'.'aux_output': 'binary_crossentropy'},
              loss_weights={'main_output': 1..'aux_output': 0.2})
Copy the code

3.4 training

Reading data (omitted)

38. Feed data into model training

model.fit({'main_input': headline_data, 'aux_input': additional_data},
          {'main_output': headline_labels, 'aux_output': additional_labels},
          epochs=50, batch_size=32,verbose=0)
Copy the code

3.5 predict

model.predict({'main_input': headline_data, 'aux_input': additional_data})
Copy the code

4. Save and read the model

39. Save the model and its weights

Json_file = open(model.to_json)"model.json"."w")
json_file.write(model_json)
json_file.close() # save weights model.save_weights()"model.h5")
Copy the code

40. Read the model and its weights

from keras.models importJson_file = open('model.json'.'r')
loaded_model_json = json_file.read()
json_file.closeLoaded_model.load_weights () loaded_model = model_from_json(loaded_model_json)"model.h5")
Copy the code
Compile model.compile(optimizer='rmsprop',
              loss={'main_output': 'binary_crossentropy'.'aux_output': 'binary_crossentropy'},
              loss_weights={'main_output': 1..'aux_output': 0.2})
Copy the code

“`php

Highlights of past For beginners entry route of artificial intelligence and data download AI based machine learning online manual deep learning online manual download update (PDF to 25 sets) this qq group, 1003271085 to join WeChat group please reply “add group” to get a sale standing knowledge star coupons, please reply “planet” knowledge like articles, point in watching

Copy the code