In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use Keras for simple classification in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Important functions of classification in Keras 1, np_utils.to_categorical
Np_utils.to_categorical is used to convert tags into binary sequences such as (nb_samples, nb_classes).
Suppose num_classes = 10.
For example, [1pm 2pm 3, … " 4] into:
[[0,1,0,0,0,0,0,0]
[0,0,1,0,0,0,0,0]
[0,0,0,1,0,0,0,0]
……
[0,0,0,0,1,0,0,0]]
In this form.
If you convert Y_train to a binary sequence, you can use the following ways:
Y_train = np_utils.to_categorical (10), Activation
Activation is the activation function and is generally used in the output of each layer.
When we use the Sequential model to build functions, we just need to add Activation after each layer of Dense.
The Sequential function also supports the construction of all layers directly in the parameters, as follows.
Model = Sequential ([Dense (32 relu), Activation ("relu"), Dense (10), Activation ("softmax")])
Two of these Activation uses the relu function and the softmax function, respectively.
3. Metrics= ['accuracy']
Adding metrics= ['accuracy'] to model.compile means that the classification accuracy needs to be calculated. The specific usage is as follows:
Model.compile (loss = 'categorical_crossentropy', optimizer = rmsprop, metrics= [' accuracy']) all code
This is a simple neural network with only one hidden layer, which is used to complete handwriting recognition. In this example, the optimizer used is RMSprop, and the available optimizer can refer to the Keras Chinese documentation.
Import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense,Activation # # full connection layer from keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.optimizers import RMSprop# acquires the training set (X-ray training training) = mnist.load_data () # first standardize X_train = X_train.reshape (X_train.shape [0],-1) / 255X_test = X_test.reshape (X_test.shape [0]) -1) / 25 to calculate the categorical_crossentropy, you need to categorical# the classification result, that is, you need to transform the tag into a binary sequence such as (nb_samples, nb_classes) Y_test = np_utils.to_categorical (model = 10) # build the model model = Sequential ([Dense (32 parallelism), Activation ("relu"), Dense (10) Activation ("softmax")]) rmsprop = RMSprop (lr = 0.001grad Rho = 0.9 accuracy') # # compilemodel.compile (loss = 'categorical_crossentropy',optimizer = rmsprop,metrics= [' ntraining]) print ("\ ntraining") cost = model.fit) print ("\ nTest") cost,accuracy = model.evaluate B = model.layers [0] .get _ weights () print ("accuracy:", accuracy)
The experimental results are as follows:
Epoch 1ax 260000108us/stepaccuracy 60000 [=]-12s 202us/step-loss: 0.3512-acc: 0.9022Epoch 2Accord 260000acc 60000 [=]-11s 183us/step-acc: 0.2037-acc: 0.9419Test10000/10000 [=]-1s 108us/stepaccuracy: 0.9464 "how to use Keras for simple classification in python" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.