टेंसरफ्लो का उपयोग एस्टिमेटर के साथ 'train' मेथड की मदद से मॉडल को कंपाइल करने के लिए किया जा सकता है।
और पढ़ें: टेंसरफ्लो क्या है और केरस टेंसरफ्लो के साथ न्यूरल नेटवर्क बनाने में कैसे काम करता है?
हम केरस सीक्वेंशियल API का उपयोग करेंगे, जो एक सीक्वेंशियल मॉडल बनाने में सहायक है जिसका उपयोग लेयर के सादे स्टैक के साथ काम करने के लिए किया जाता है, जहां प्रत्येक लेयर में ठीक एक इनपुट टेंसर और एक आउटपुट टेंसर होता है।
एक न्यूरल नेटवर्क जिसमें कम से कम एक लेयर होती है, कन्वोल्यूशनल लेयर के रूप में जानी जाती है। हम कन्वोल्यूशनल न्यूरल नेटवर्क का उपयोग लर्निंग मॉडल बनाने के लिए कर सकते हैं।
टेंसरफ्लो टेक्स्ट में टेक्स्ट-संबंधित क्लास और ऑप्स का संग्रह होता है जिसे टेंसरफ्लो 2.0 के साथ उपयोग किया जा सकता है। टेंसरफ्लो टेक्स्ट का उपयोग सीक्वेंस मॉडलिंग को प्रीप्रोसेस करने के लिए किया जा सकता है।
हम नीचे दिए गए कोड को चलाने के लिए Google Colaboratory का उपयोग कर रहे हैं। Google Colab या कोलेबोरेटरी ब्राउज़र पर पायथन कोड चलाने में मदद करता है और इसके लिए शून्य कॉन्फ़िगरेशन और GPU (ग्राफिकल प्रोसेसिंग यूनिट्स) तक मुफ्त पहुंच की आवश्यकता होती है। कोलेबोरेटरी को जुपिटर नोटबुक के शीर्ष पर बनाया गया है।
एक एस्टिमेटर टेंसरफ्लो का एक पूर्ण मॉडल का उच्च-स्तरीय प्रतिनिधित्व है। इसे आसान स्केलिंग और एसिंक्रोनस ट्रेनिंग के लिए डिज़ाइन किया गया है।
मॉडल को आइरिस डेटा सेट का उपयोग करके प्रशिक्षित किया जाता है। 4 फीचर हैं, और एक लेबल।
- सेपल लंबाई
- सेपल चौड़ाई
- पेटल लंबाई
- पेटल चौड़ाई
उदाहरण
print("मॉडल प्रशिक्षित किया जा रहा है")
classifier.train(input_fn=lambda: input_fn(train, train_y, training=True), steps=5000)
कोड क्रेडिट - https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
आउटपुट
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version. Instructions for updating: Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx. If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/keras/optimizer_v2/adagrad.py:83: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 0...
INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpbhg2uvbr/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 0...
INFO:tensorflow:loss = 1.1140382, step = 0
INFO:tensorflow:global_step/sec: 312.415
INFO:tensorflow:loss = 0.8781501, step = 100 (0.321 sec)
...
INFO:tensorflow:loss = 0.36297452, step = 5000 (0.273 sec)
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 5000...
INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpbhg2uvbr/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 5000...
INFO:tensorflow:Loss for final step: 0.36297452.
<tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7fc9983ed470>
व्याख्या
- एक बार एस्टिमेटर ऑब्जेक्ट बन जाने के बाद, नीचे दिए गए मेथड को कॉल किया जा सकता है:
- मॉडल प्रशिक्षित किया जाता है।
- प्रशिक्षित मॉडल का मूल्यांकन किया जाता है।
- यह मॉडल भविष्यवाणी करने के लिए उपयोग किया जाता है।
- मॉडल को फिर से प्रशिक्षित किया जाता है।
- यह एस्टिमेटर के train मेथड को कॉल करके किया जाता है।