In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use Keras for regression operation in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Keras for regression operation in python" can help you solve the problem.
What is Keras?
Keras is an open source artificial neural network library written by Python, which can be used as the high-level application program interface of Tensorflow, Microsoft-CNTK and Theano to design, debug, evaluate, apply and visualize the deep learning model.
Keras is the equivalent of a higher-level library than Tensorflow and Theano, and it can use Tensorflow or Theano as the underlying framework to add code in a more concise and efficient way.
After the release of Tensorflow 2.0.0 in 2018, Keras was officially established as the Tensorflow high-level API.
Important basic functions in Keras 1, Sequential
Sequential is also called sequential model.
The sequential model is the simplest linear, structural order from beginning to end, without bifurcation, and is a linear stack of multiple network layers.
Before using Keras to build the model, you only need to build the model with the following functions.
Model = Sequential ()
At this point, an ordered model has been established, and then when layers are added to the model, they are added in order.
2 、 Dense
Dense is used to add a fully connected layer to Sequential. The schematic diagram of the full connection layer is as follows. (the picture is from Baidu encyclopedia)
Specifically, in the simple BP neural network, the weight connection between the input layer and the hidden layer has the same meaning as the full connection layer.
In Keras, if you need to add a full connection layer to model, you can use the following function.
Model.add (Dense (output_dim = 1m = 1))
This means that the input dimension is 1 and the output dimension is also 1.
3 、 model.compile
The role of model.compile in Keras is mainly used to define loss functions and optimizers.
The calling method is as follows:
Model.compile (loss='mse', optimizer='sgd', metrics= ['accuracy'])
Where loss is used to define the loss function for calculating loss, and its options are as follows:
1. Mse: root mean square error, which is often used in regression prediction.
2. Categorical_crossentropy: also known as multi-class logarithmic loss, note that when using this objective function, the label needs to be transformed into a binary sequence in the shape of (nb_samples, nb_classes), which is often used for classification.
3. Sparse_categorical_crossentrop: as above, sparse tags can be accepted.
Optimizer is used to define the optimizer, either by default or exported from keras.optimizers.
The content that can be selected can be found in Keras Chinese documents. The random gradient descent method sgd is selected above.
Metrics= ['accuracy'] is often used in classification operations, but it is not applicable in this example. Accuracy represents the accuracy of calculating classification.
All codes
This example is an example of univariate linear regression.
Import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense # # fully connected layer import matplotlib.pyplot as plt # generates test data X = np.linspace (- 11,200) np.random.shuffle (X) Y = 0.5cm X + 2 + np.random.normal (0mem0.05, (200,)) # divides the training set and the test set into training sets and test sets Y [160,160:] # startmodel = Sequential () model.add (Dense (output_dim = 1pm inputtrains dim = 1)) # compilemodel.compile (loss = 'mse',optimizer =' sgd') # training print ("\ ntraining") for step in range (2001): cost = model.train_on_batch (Xerox trainers) if step0 = = 0: print ("tarin_cost:", cost) # Test print ("\ nTest") cost = model.evaluate (X_test) B = model.layers [0] .get _ weights () print ("Weights", W, "biaxes", b) # Forecast result Y = model.predict (X_test) plt.scatter (X-ray test) plt.plot (X-ray test) plt.show ()
The implementation results are as follows:
Tarin_cost: 4.506874tarin_cost: 0.21098542tarin_cost: 0.041809298tarin_cost: 0.013134768tarin_cost: 0.0055761375tarin_cost: 0.0035068158tarin_cost: 0.0029388934tarin_cost: 0.002783tarin_cost: 0.0027402083tarin_cost: 0.002728462tarin_cost: 0.0027252387tarin_cost: 0.0027243525tarin_cost: 0.0027241106tarin_cost: 0.0027240426tarin_cost: 0.002724025tarin_cost: 0.0027240203tarin_cost: 0.0027240184tarin_cost: 0.0027240182tarin_cost: 0.0027240175tarin_cost: 0. 0027240175tarin_cost: 0.0027240175Test40/40 [=]-0s 874us/stepWeights [[0.5041559]] biaxes [1.9961643]
This is the end of the introduction on "how to use Keras for regression operation in python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.