当前位置:网站首页>Using keras to build the basic model yingtailing flower

Using keras to build the basic model yingtailing flower

2022-06-11 04:51:00 Panbohhhhh

I recently taught myself , Feeling keras It seems very useful , Here is a record of the learning process , And give the source code .
I have written notes for detailed ideas .
A work of practice , Many deficiencies , Welcome to discuss .

First step : Build a model

Keras There are two types of models , Sequential model and functional model .
Here I use the sequence model , The method adopted is .add() Methods , Build layer by layer .

#define a network
def baseline_model():
    model = Sequential() # It's sequential 
    model.add(Dense(7, input_dim=4 , activation='tanh'))  # The input layer has 7 Nodes ,
    model.add(Dense(3, activation='softmax'))          # Output layer 
    model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy']) # Mean square error   Optimizer   Distinguish 
    return model

What needs to be noted here is , The model needs to know the input of data shape, Only the first level needs to be specified shape Parameters , The following layers can be automatically derived .
Before training , The function to configure the learning process is compile(), The three parameters are : Optimizer optimizer, Loss function loss, Discriminant function metrics.

The second step : Training

Note here , I use the kfold

estimator = KerasClassifier(build_fn=baseline_model, epochs = 20, batch_size= 1, verbose=1)
  • batch_size: Integers , Specifies the gradient descent for each batch Number of samples included .
  • epochs: Integers , At the end of training epochs Count
  • verbose: The log shows ,0 Is not output ,1 Record for the output progress bar ,2 For each epoch Output a record

The third step : Save performance parameters related to the model and output

The specific training described in this step ends –> Save the model ----> Call model ----> Output key value , A complete process .
Simply speaking , As a foundation keras The algorithm framework comes out .
In the future, I will also expand on it .

Fourth parts

Output results :
 Insert picture description here Take the first act :[0.8176036 0.1570888 0.02530759], We divide the data into 3 class ,setosa,versicolor,virginica.
The three data in this row , It means the possibility of these three categories ,0.817 The possibility of is the first kind ,.
The final result .
 Insert picture description here

See all the codes for details

All the code :

# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from keras.models import Sequential # Sequence   Serial 
from keras.layers import  Dense #
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score # Introduce cross validation 
from sklearn.model_selection import KFold # Test sets and validation sets 
from sklearn.preprocessing import LabelEncoder  # Convert text to numeric values 
from keras.models import model_from_json # Save the model as json, Read it out when you need it , Direct prediction .
from keras.optimizers import SGD



# reproducibility  repeat 
seed = 13
np.random.seed(seed)  # Out of order , Lower fit 



#load data  Import data 
df = pd.read_csv('iris.csv')  # Where are the documents 
X = df.values[:,0:4].astype(float)
Y = df.values[:,4]

encoder = LabelEncoder()
Y_encoded = encoder.fit_transform(Y)  # take 3 Letters are converted into  0 0 1  And so on. .
Y_onehot = np_utils.to_categorical(Y_encoded)  # These two should be used together , The effect is to convert characters into numbers , Then convert the numbers into matrices ,

''' ##  Specific effect , About LabEncoder  and to_categorical from keras.utils.np_utils import * # Class vector definition  b = [0,1,2,3,4,5,6,7,8] # call to_categorical take b according to 9 Class to transform  b = to_categorical(b, 9) print(b)  The results are as follows : [[1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1.]] '''

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#define a network
def baseline_model():
    model = Sequential() # It's sequential 
    model.add(Dense(7, input_dim=4 , activation='tanh'))  # The input layer has 7 Nodes ,
    model.add(Dense(3, activation='softmax'))          # Output layer 
    model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy']) # Mean square error   Optimizer   Distinguish 
    return model


estimator = KerasClassifier(build_fn=baseline_model, epochs = 20, batch_size= 1, verbose=1)

#evalute
kfold = KFold(n_splits=10,shuffle=True,random_state=seed) # The seed of the random generator 

result = cross_val_score(estimator, X,Y_onehot,cv=kfold)


print('Accuray of cross validation, mean %.2f, std %.2f'% (result.mean(),result.std()))



#save model
estimator.fit(X, Y_onehot)
model_json = estimator.model.to_json()
with open('model.json','w') as json_file:
    json_file.write(model_json)


estimator.model.save_weights('model.h5')
print('saved model to disk')



#load model and use it for prediction
json_file = open('model.json','r')
loaded_model_json = json_file.read()
json_file.close()


loaded_model = model_from_json(loaded_model_json)  # Know the network structure ,
loaded_model.load_weights('model.h5')   # Know the parameters and specific structure of each layer 
print('loaded model from disk')

predicted = loaded_model.predict(X)
print('predicted probability:'+ str(predicted))


predicted_label = loaded_model.predict_classes()
print('predicted label:'+ str(predicted_label))
原网站

版权声明
本文为[Panbohhhhh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020545290819.html