In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people don't know what to do about how to construct neural network quickly by applying Tensorflow2.0 's Eager mode. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
TensorFlow is the mainstream framework for the development of deep learning algorithms. recently, with the rise of frameworks such as keras and pytorch, it has been challenged. In order to cope with the competition, it is also evolving. The recent version 2.0 makes the application of the framework easier and easier to use. In this section, we discuss how to use the eager pattern proposed in version 2.0. We will use it in later chapters to develop more complex generative adversarial networks. One of the major features of the latest popular deep learning framework, keras, is the ease of use and understandability of the interface. It is deeply encapsulated on the basis of Tensorflow, it hides a lot of technical details, and adjusts the design pattern at the same time, which makes the development based on keras much easier than Tensorflow. But the corresponding problem of keras is that too good encapsulation is good for ease of use, but it is not conducive to developers, especially beginners'in-depth understanding of model design. Because our topic is to learn the design principles of neural networks, because keras overencapsulates the details of model design patterns, it is not good for learners. In order to strike a balance between ease of use and mastery of design details, I chose the Eager mode that TF2.0 brings so that I can have both fish and bear's paw. Let's first look at the difference between the Eager model and the traditional model. One of the major features of the traditional mode is that the code first creates a session object. The deep learning network model is actually an operation graph composed of a variety of computing nodes. When the model runs, it depends on the driving and management of the operation graph by the session object. Let's first look at the basic development process of the traditional pattern:
Import tensorflow as tf a = tf.constant (3.0b) = tf.placeholder (dtype = tf.float32) c = tf.add (Azov b) sess = tf.Session () # create session object init = tf.global_variables_initializer () sess.run (init) # initialize session object feed = {b: 2.0} # assign value to variable b c_res = sess.run (c, feed) # get the result print (c_res) through the session-driven calculation chart
From the above code you will feel an awkward, placeholder is used to open up a piece of memory, and then through feed to assign values to the opened up memory, and then use run to drive the operation of the entire computing process, the difference between this design pattern and the traditional programming pattern is to spare a bend, for many TF beginners, it takes a lot of effort to adapt to this pattern at the beginning. Let's take a look at the design process of the above code in eager mode. The first thing to note is that to turn on eager mode, you need to execute the following code at the beginning:
Import tensorflow as tf import tensorflow.contrib.eager as tfe tf.enable_eager_execution ()
After the code is executed, TF enters eager mode. Let's take a look at how to implement the previous operation step: def add (num1, num2): a = tf.convert_to_tensor (num1) # converts the value into a TF tensor, which helps to speed up the operation b = tf.convert_to_tensor (num2) c = a + b return c.numpy () # convert the tensor to the numerical add_res = add (3.0,4.0) print (add_res)
After the code runs, the output result is 7.0. you can see that the characteristic of eager mode is that it can perform all the operation steps from top to bottom like the traditional programming mode, without creating a session object, and then driving the execution of all operation steps through the session object. This design pattern is easier to understand. Let's take a look at how to develop a simple neural network using the eager pattern. Similar to "Hello World!", an entry-level training project commonly used in neural network programming is called iris recognition. Its petal characteristics are obvious, and the width and length of petals of different varieties are different, so the corresponding varieties can be identified after reading petal information through neural network. First, let's load the corresponding training data:
From sklearn import datasets, preprocessing, model_selection data = datasets.load_iris () # load data into memory x = preprocessing.MinMaxScaler (feature_range = (- 1 data')). Fit_transform (data ['data']) # preprocesses the data values to (- 1) to facilitate network recognition # different classified varieties are represented by vectors, for example, there are three different varieties Then y = preprocessing.OneHotEncoder (sparse = False). Fit_transform (data ['target'] .reshape (- 1,1)) x_train, x_test, y_train, y_test = model_selection.train_test_split (x, y, test_size = 0.25, stratify = y) # the data are divided into training set test set print (len (x_train))
After the code runs, you can see that there are 112 pieces of data with training. Next, let's create a simple three-layer network: class IrisClassifyModel (object): def _ _ init__ (self, hidden_unit, output_unit): # here only two layers of network are built. The first layer is the input data self.hidden_layer = tf.keras.layers.Dense (units = hidden_unit, activation = tf.nn.tanh, use_bias = True). Name= "hidden_layer") self.output_layer = tf.keras.layers.Dense (units = output_unit, activation = None, use_bias = True, name= "output_layer") def _ _ call__ (self Inputs): return self.output_layer (self.hidden_layer (inputs)) Let's check the correctness of the network construction with the following code: # construct input data to verify that the network is working properly model = IrisClassifyModel (10,3) train_dataset = tf.data.Dataset.from_tensor_slices ((x_train, y_train)) for x, y in tfe.Iterator (train_dataset.batch (32)): output = model (x) print (output.numpy () break)
If the code runs correctly and outputs the corresponding results, it shows that there is nothing wrong with the network design. Then we use the following code to design the loss function and count the accuracy of the network prediction: def make_loss (model, inputs, labels): return tf.reduce_sum (tf.nn.softmax_cross_entropy_with_logits_v2 (logits = model (inputs), labels = labels)) opt = tf.train.AdamOptimizer (learning_rate = 0.01) def train (model, x, y): opt.minimize (lambda:make_loss (model, x) Y) accuracy = tfe.metrics.Accuracy () def check_accuracy (model, x_batch, y_batch): # Statistics the accuracy of network judgment results accuracy (tf.argmax (model (tf.constant (x_batch)), axis = 1), tf.argmax (tf.constant (y_batch), axis = 1) return accuracy
Finally, we start the network training process, and then draw the results of the network training:
Import numpy as np model = IrisClassifyModel (10,3) epochs = 50 acc_history = np.zeros (epochs) for epoch in range (epochs): for (x_batch, y_batch) in tfe.Iterator (train_dataset.shuffle (1000) .batch (32): train (model, x_batch, y_batch) acc = check_accuracy (model, x_batch) Y_batch) acc_ [epoch] = acc.result () .numpy () import matplotlib.pyplot as plt plt.figure () plt.plot (acc_history) plt.xlabel ('Epoch') plt.ylabel (' Accuracy') plt.show () the result of the above code running is as follows:
It can be seen that the accuracy of the network is more than 95% after training. The purpose of this section is to introduce the eager pattern of TF2.0 and to make technical preparations for the later development of more complex networks.
After reading the above, have you mastered how to quickly build a neural network using Tensorflow2.0 's Eager model? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.