টেনসরফ্লো Estimator দিয়ে মডেল কম্পাইল/ট্রেন করা যায় 'train' মেথড ব্যবহার করে।
আরও পড়ুন: টেনসরফ্লো কী এবং কেরাস কীভাবে টেনসরফ্লোর সাথে কাজ করে নিউরাল নেটওয়ার্ক তৈরির জন্য?
আমরা কেরাস সিকুয়েনশিয়াল API ব্যবহার করব, যা একটি সিকুয়েনশিয়াল মডেল বানাতে সাহায্য করে যেখানে প্রতিটি লেয়ারের ঠিক একটি ইনপুট টেনসর এবং একটি আউটপুট টেনসর থাকে।
কমপক্ষে একটি লেয়ার থাকা নিউরাল নেটওয়ার্ককে কনভোল্যুশনাল লেয়ার বলা হয়। আমরা কনভোল্যুশনাল নিউরাল নেটওয়ার্ক ব্যবহার করে লার্নিং মডেল বানাতে পারি।
টেনসরফ্লো টেক্সট (TensorFlow Text) টেক্সট-রিলেটেড ক্লাস ও অপ্সের সংকলন যা টেনসরফ্লো ২.০-এর সাথে ব্যবহার করা যায়। টেনসরফ্লো টেক্সট সিকুয়েন্স মডেলিং প্রিপ্রসেস করতে ব্যবহৃত হয়।
নিচের কোড চালানোর জন্য আমরা গুগল কোলাবরেটরি ব্যবহার করছি। গুগল কোল্যাব বা কোলাবরেটরি ব্রাউজারের ওপর পাইথন কোড চালাতে সাহায্য করে এবং জিরো কনফিগারেশন ও ফ্রি জিপিইউ (GPU) অ্যাক্সেস দেয়। কোলাবরেটরি জুপিটার নোটবুকের ওপর বিল্ড করা হয়েছে।
এন Estimator হলো টেনসরফ্লোর হাই-লেভেল রিপ্রেজেন্টেশন একটি সম্পূর্ণ মডেলের। এটি সহজ স্কেলিং ও অ্যাসিঙ্ক্রোনাস ট্রেনিং-এর জন্য ডিজাইন করা।
মডেলটি আইরিস ডেটাসেট দিয়ে ট্রেন করা হয়েছে। এখানে ৪টি ফিচার এবং একটি লেবেল রয়েছে।
- সেপাল দৈর্ঘ্য (sepal length)
- সেপাল প্রস্থ (sepal width)
- পেটাল দৈর্ঘ্য (petal length)
- পেটাল প্রস্থ (petal width)
উদাহরণ
print("The model is being trained")
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:global_step/sec: 375.535
INFO:tensorflow:loss = 0.80712265, step = 200 (0.266 sec)
...
INFO:tensorflow:Loss for final step: 0.36297452.
ব্যাখ্যা
- একবার Estimator অবজেক্ট তৈরি হলে নিচের মেথডগুলো কল করা যায়:
- মডেল ট্রেন করা হয়।
- ট্রেনড মডেল ইভ্যালুয়েট করা হয়।
- এই মডেল প্রেডিকশন করতে ব্যবহৃত হয়।
- মডেল আবার ট্রেন করা হয়।
- এটি Estimator-এর train মেথড কল করে করা হয়।