TensorFlow.org এ দেখুন | Google Colab-এ চালান | GitHub এ দেখুন | নোটবুক ডাউনলোড করুন |
ওভারভিউ
এই গাইডটি TensorFlow 2 (TF2) ব্যবহার করে কোড লেখার জন্য সর্বোত্তম অনুশীলনের একটি তালিকা প্রদান করে, এটি এমন ব্যবহারকারীদের জন্য লেখা হয়েছে যারা সম্প্রতি TensorFlow 1 (TF1) থেকে স্যুইচ ওভার করেছেন। আপনার TF1 কোড TF2 এ স্থানান্তরিত করার বিষয়ে আরও তথ্যের জন্য গাইডের মাইগ্রেট বিভাগটি পড়ুন।
সেটআপ
এই নির্দেশিকায় উদাহরণগুলির জন্য TensorFlow এবং অন্যান্য নির্ভরতা আমদানি করুন।
import tensorflow as tf
import tensorflow_datasets as tfds
ইডিওম্যাটিক টেনসরফ্লো 2 এর জন্য সুপারিশ
আপনার কোডকে ছোট মডিউলগুলিতে রিফ্যাক্টর করুন
একটি ভাল অভ্যাস হল আপনার কোডকে প্রয়োজন অনুসারে ছোট ফাংশনে রিফ্যাক্টর করা। সেরা পারফরম্যান্সের জন্য, আপনার tf.function এ আপনি করতে পারেন এমন গণনার সবচেয়ে বড় ব্লকগুলিকে সাজানোর চেষ্টা করা উচিত (মনে রাখবেন যে tf.function দ্বারা ডাকা নেস্টেড পাইথন ফাংশনগুলির জন্য তাদের নিজস্ব আলাদা সজ্জার প্রয়োজন হয় না, যদি না আপনি ভিন্ন jit_compile ব্যবহার করতে চান। tf.function এর জন্য সেটিংস)। আপনার ব্যবহারের ক্ষেত্রে নির্ভর করে, এটি একাধিক প্রশিক্ষণের ধাপ বা এমনকি আপনার পুরো প্রশিক্ষণ লুপ হতে পারে। অনুমান ব্যবহারের ক্ষেত্রে, এটি একটি একক মডেল ফরওয়ার্ড পাস হতে পারে।
কিছু tf.keras.optimizer এর জন্য ডিফল্ট শেখার হার সামঞ্জস্য করুন
কিছু কেরাস অপ্টিমাইজারের TF2-এ বিভিন্ন শিক্ষার হার রয়েছে। আপনি যদি আপনার মডেলগুলির জন্য অভিসারী আচরণে পরিবর্তন দেখতে পান তবে ডিফল্ট শেখার হারগুলি পরীক্ষা করুন৷
optimizers.SGD , optimizers.Adam , or optimizers.RMSprop এর জন্য কোন পরিবর্তন নেই৷
নিম্নলিখিত ডিফল্ট শেখার হার পরিবর্তিত হয়েছে:
-
optimizers.Adagrad0.01থেকে0.001পর্যন্ত -
optimizers.Adadelta। অ্যাডাডেল্টা1.0থেকে0.001পর্যন্ত -
optimizers.Adamax0.002থেকে0.001পর্যন্ত -
optimizers.Nadam0.002থেকে0.001পর্যন্ত
ভেরিয়েবল পরিচালনা করতে tf.Module s এবং Keras লেয়ার ব্যবহার করুন
tf.Module s এবং tf.keras.layers.Layer s সুবিধাজনক variables এবং trainable_variables বৈশিষ্ট্যগুলি অফার করে, যা পুনরাবৃত্তভাবে সমস্ত নির্ভরশীল ভেরিয়েবল সংগ্রহ করে। এটি ভেরিয়েবলগুলি যেখানে ব্যবহার করা হচ্ছে সেখানে স্থানীয়ভাবে পরিচালনা করা সহজ করে তোলে।
Keras স্তর/মডেলগুলি tf.train.Checkpointable থেকে উত্তরাধিকার সূত্রে প্রাপ্ত এবং @tf.function এর সাথে একীভূত করা হয়েছে, যা কেরাস অবজেক্টগুলি থেকে সরাসরি চেকপয়েন্ট বা সংরক্ষিত মডেলগুলি রপ্তানি করা সম্ভব করে৷ এই ইন্টিগ্রেশনের সুবিধা নিতে আপনাকে Keras' Model.fit API ব্যবহার করতে হবে না।
কেরাস ব্যবহার করে কীভাবে প্রাসঙ্গিক ভেরিয়েবলের একটি উপসেট সংগ্রহ করতে হয় তা শিখতে কেরাস গাইডে স্থানান্তর শেখার এবং ফাইন-টিউনিং সম্পর্কিত বিভাগটি পড়ুন।
tf.data.Dataset s এবং tf.function একত্রিত করুন
TensorFlow ডেটাসেট প্যাকেজ ( tfds ) এ tf.data.Dataset অবজেক্ট হিসাবে পূর্বনির্ধারিত ডেটাসেট লোড করার জন্য ইউটিলিটি রয়েছে। এই উদাহরণের জন্য, আপনি tfds ব্যবহার করে tfds ডেটাসেট লোড করতে পারেন:
datasets, info = tfds.load(name='mnist', with_info=True, as_supervised=True)
mnist_train, mnist_test = datasets['train'], datasets['test']
তারপর প্রশিক্ষণের জন্য ডেটা প্রস্তুত করুন:
- প্রতিটি চিত্র পুনরায় স্কেল করুন।
- উদাহরণের ক্রম এলোমেলো করুন।
- ছবি এবং লেবেল ব্যাচ সংগ্রহ করুন.
BUFFER_SIZE = 10 # Use a much larger value for real code
BATCH_SIZE = 64
NUM_EPOCHS = 5
def scale(image, label):
image = tf.cast(image, tf.float32)
image /= 255
return image, label
উদাহরণ সংক্ষিপ্ত রাখতে, শুধুমাত্র 5 ব্যাচ ফেরাতে ডেটাসেটটি ট্রিম করুন:
train_data = mnist_train.map(scale).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
test_data = mnist_test.map(scale).batch(BATCH_SIZE)
STEPS_PER_EPOCH = 5
train_data = train_data.take(STEPS_PER_EPOCH)
test_data = test_data.take(STEPS_PER_EPOCH)
image_batch, label_batch = next(iter(train_data))
2021-12-08 17:15:01.637157: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
মেমরিতে মানানসই প্রশিক্ষণ ডেটার উপর পুনরাবৃত্তি করতে নিয়মিত পাইথন পুনরাবৃত্তি ব্যবহার করুন। অন্যথায়, tf.data.Dataset হল ডিস্ক থেকে প্রশিক্ষণ ডেটা স্ট্রিম করার সর্বোত্তম উপায়। ডেটাসেটগুলি পুনরাবৃত্তিযোগ্য (পুনরাবৃত্ত নয়) এবং অন্যান্য পাইথন পুনরাবৃত্তের মতোই কাজ করে। আপনি tf.function এ আপনার কোড র্যাপ করে ডেটাসেট অ্যাসিঙ্ক প্রিফেচিং/স্ট্রিমিং বৈশিষ্ট্যগুলি সম্পূর্ণরূপে ব্যবহার করতে পারেন, যা অটোগ্রাফ ব্যবহার করে সমতুল্য গ্রাফ অপারেশনগুলির সাথে পাইথন পুনরাবৃত্তিকে প্রতিস্থাপন করে।
@tf.function
def train(model, dataset, optimizer):
for x, y in dataset:
with tf.GradientTape() as tape:
# training=True is only needed if there are layers with different
# behavior during training versus inference (e.g. Dropout).
prediction = model(x, training=True)
loss = loss_fn(prediction, y)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
আপনি Model.fit API ব্যবহার করলে, আপনাকে ডেটাসেট পুনরাবৃত্তি নিয়ে চিন্তা করতে হবে না।
model.compile(optimizer=optimizer, loss=loss_fn)
model.fit(dataset)
কেরাস প্রশিক্ষণ লুপ ব্যবহার করুন
আপনার প্রশিক্ষণ প্রক্রিয়ার নিম্ন-স্তরের নিয়ন্ত্রণের প্রয়োজন না হলে, কেরাসের অন্তর্নির্মিত fit , evaluate এবং predict পদ্ধতি ব্যবহার করার পরামর্শ দেওয়া হয়। এই পদ্ধতিগুলি বাস্তবায়ন নির্বিশেষে মডেলটিকে প্রশিক্ষণের জন্য একটি অভিন্ন ইন্টারফেস প্রদান করে (ক্রমিক, কার্যকরী, বা উপ-শ্রেণীবদ্ধ)।
এই পদ্ধতির সুবিধার মধ্যে রয়েছে:
- তারা Numpy অ্যারে, পাইথন জেনারেটর এবং
tf.data.Datasetsগ্রহণ করে। - তারা স্বয়ংক্রিয়ভাবে নিয়মিতকরণ, এবং সক্রিয়করণ ক্ষতি প্রয়োগ করে।
- তারা
tf.distributeসমর্থন করে যেখানে হার্ডওয়্যার কনফিগারেশন নির্বিশেষে প্রশিক্ষণ কোড একই থাকে। - তারা ক্ষতি এবং মেট্রিক্স হিসাবে নির্বিচারে কলযোগ্য সমর্থন করে।
- তারা
tf.keras.callbacks.TensorBoardএবং কাস্টম কলব্যাকের মতো কলব্যাক সমর্থন করে। - তারা পারফরম্যান্ট, স্বয়ংক্রিয়ভাবে TensorFlow গ্রাফ ব্যবহার করে।
এখানে একটি Dataset ব্যবহার করে একটি মডেল প্রশিক্ষণের একটি উদাহরণ রয়েছে। এটি কীভাবে কাজ করে তার বিস্তারিত জানার জন্য, টিউটোরিয়ালগুলি দেখুন ।
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(0.02),
input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10)
])
# Model is the full model w/o custom layers
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_data, epochs=NUM_EPOCHS)
loss, acc = model.evaluate(test_data)
print("Loss {}, Accuracy {}".format(loss, acc))
Epoch 1/5 5/5 [==============================] - 9s 7ms/step - loss: 1.5762 - accuracy: 0.4938 Epoch 2/5 2021-12-08 17:15:11.145429: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. 5/5 [==============================] - 0s 6ms/step - loss: 0.5087 - accuracy: 0.8969 Epoch 3/5 2021-12-08 17:15:11.559374: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. 5/5 [==============================] - 2s 5ms/step - loss: 0.3348 - accuracy: 0.9469 Epoch 4/5 2021-12-08 17:15:13.860407: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. 5/5 [==============================] - 0s 5ms/step - loss: 0.2445 - accuracy: 0.9688 Epoch 5/5 2021-12-08 17:15:14.269850: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. 5/5 [==============================] - 0s 6ms/step - loss: 0.2006 - accuracy: 0.9719 2021-12-08 17:15:14.717552: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. 5/5 [==============================] - 1s 4ms/step - loss: 1.4553 - accuracy: 0.5781 Loss 1.4552843570709229, Accuracy 0.578125 2021-12-08 17:15:15.862684: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
প্রশিক্ষণ কাস্টমাইজ করুন এবং আপনার নিজস্ব লুপ লিখুন
যদি কেরাস মডেলগুলি আপনার জন্য কাজ করে, তবে আপনার প্রশিক্ষণের ধাপ বা বাইরের প্রশিক্ষণ লুপগুলির আরও নমনীয়তা এবং নিয়ন্ত্রণের প্রয়োজন, আপনি নিজের প্রশিক্ষণের পদক্ষেপগুলি বা এমনকি সম্পূর্ণ প্রশিক্ষণ লুপগুলি বাস্তবায়ন করতে পারেন। আরও জানতে fit কাস্টমাইজ করার বিষয়ে কেরাস গাইড দেখুন।
এছাড়াও আপনি tf.keras.callbacks.Callback হিসাবে অনেক কিছু বাস্তবায়ন করতে পারেন।
এই পদ্ধতির অনেক সুবিধা রয়েছে যা আগে উল্লেখ করা হয়েছে, কিন্তু এটি আপনাকে ট্রেনের ধাপ এবং এমনকি বাইরের লুপের নিয়ন্ত্রণ দেয়।
একটি আদর্শ প্রশিক্ষণ লুপের তিনটি ধাপ রয়েছে:
- উদাহরণের ব্যাচ পেতে একটি পাইথন জেনারেটর বা
tf.data.Datasetএর উপর পুনরাবৃত্তি করুন। - গ্রেডিয়েন্ট সংগ্রহ করতে
tf.GradientTapeব্যবহার করুন। - মডেলের ভেরিয়েবলে ওজন আপডেট প্রয়োগ করতে
tf.keras.optimizersএর একটি ব্যবহার করুন।
মনে রাখবেন:
- সর্বদা সাবক্লাসড লেয়ার এবং মডেলের
callপদ্ধতিতে একটিtrainingযুক্তি অন্তর্ভুক্ত করুন। -
trainingযুক্তি সঠিকভাবে সেট সহ মডেল কল নিশ্চিত করুন. - ব্যবহারের উপর নির্ভর করে, মডেল ভেরিয়েবল নাও থাকতে পারে যতক্ষণ না মডেলটি ডেটার একটি ব্যাচে চালানো হয়।
- আপনাকে মডেলের জন্য নিয়মিতকরণের ক্ষতির মতো জিনিসগুলি ম্যানুয়ালি পরিচালনা করতে হবে।
ভেরিয়েবল ইনিশিয়ালাইজার চালানোর বা ম্যানুয়াল কন্ট্রোল নির্ভরতা যোগ করার দরকার নেই। tf.function আপনার জন্য সৃষ্টির উপর স্বয়ংক্রিয় নিয়ন্ত্রণ নির্ভরতা এবং পরিবর্তনশীল প্রাথমিককরণ পরিচালনা করে।
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(0.02),
input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(10)
])
optimizer = tf.keras.optimizers.Adam(0.001)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
@tf.function
def train_step(inputs, labels):
with tf.GradientTape() as tape:
predictions = model(inputs, training=True)
regularization_loss=tf.math.add_n(model.losses)
pred_loss=loss_fn(labels, predictions)
total_loss=pred_loss + regularization_loss
gradients = tape.gradient(total_loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
for epoch in range(NUM_EPOCHS):
for inputs, labels in train_data:
train_step(inputs, labels)
print("Finished epoch", epoch)
2021-12-08 17:15:16.714849: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Finished epoch 0 2021-12-08 17:15:17.097043: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Finished epoch 1 2021-12-08 17:15:17.502480: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Finished epoch 2 2021-12-08 17:15:17.873701: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Finished epoch 3 Finished epoch 4 2021-12-08 17:15:18.344196: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
পাইথন কন্ট্রোল ফ্লো সহ tf.function এর সুবিধা নিন
tf.function ডেটা-নির্ভর নিয়ন্ত্রণ প্রবাহকে tf.cond এবং tf.while_loop এর মতো গ্রাফ-মোড সমতুল্যগুলিতে রূপান্তর করার একটি উপায় সরবরাহ করে।
একটি সাধারণ জায়গা যেখানে ডেটা-নির্ভর নিয়ন্ত্রণ প্রবাহ প্রদর্শিত হয় ক্রম মডেলগুলিতে। tf.keras.layers.RNN একটি RNN সেল মোড়ানো হয়, যা আপনাকে স্থিতিশীল বা গতিশীলভাবে পুনরাবৃত্তি আনরোল করতে দেয়। উদাহরণ হিসাবে, আপনি নিম্নরূপ গতিশীল আনরোল পুনরায় প্রয়োগ করতে পারেন।
class DynamicRNN(tf.keras.Model):
def __init__(self, rnn_cell):
super(DynamicRNN, self).__init__(self)
self.cell = rnn_cell
@tf.function(input_signature=[tf.TensorSpec(dtype=tf.float32, shape=[None, None, 3])])
def call(self, input_data):
# [batch, time, features] -> [time, batch, features]
input_data = tf.transpose(input_data, [1, 0, 2])
timesteps = tf.shape(input_data)[0]
batch_size = tf.shape(input_data)[1]
outputs = tf.TensorArray(tf.float32, timesteps)
state = self.cell.get_initial_state(batch_size = batch_size, dtype=tf.float32)
for i in tf.range(timesteps):
output, state = self.cell(input_data[i], state)
outputs = outputs.write(i, output)
return tf.transpose(outputs.stack(), [1, 0, 2]), state
lstm_cell = tf.keras.layers.LSTMCell(units = 13)
my_rnn = DynamicRNN(lstm_cell)
outputs, state = my_rnn(tf.random.normal(shape=[10,20,3]))
print(outputs.shape)
(10, 20, 13)
আরও তথ্যের জন্য tf.function গাইড পড়ুন।
নতুন শৈলী মেট্রিক্স এবং ক্ষতি
মেট্রিক্স এবং লস উভয়ই বস্তু যা সাগ্রহে এবং tf.function s-এ কাজ করে।
একটি ক্ষতির বস্তু কলযোগ্য, এবং আর্গুমেন্ট হিসাবে ( y_true , y_pred ) আশা করে:
cce = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
cce([[1, 0]], [[-1.0,3.0]]).numpy()
4.01815
তথ্য সংগ্রহ এবং প্রদর্শন করতে মেট্রিক্স ব্যবহার করুন
আপনি তথ্য সংগ্রহ করতে tf.summary এবং সারাংশ লগ করতে tf.metrics ব্যবহার করতে পারেন এবং একটি প্রসঙ্গ ম্যানেজার ব্যবহার করে এটিকে একজন লেখকের কাছে পুনঃনির্দেশ করতে পারেন। সারাংশগুলি সরাসরি লেখকের কাছে নির্গত হয় যার অর্থ হল আপনাকে অবশ্যই কলসাইটে step মান প্রদান করতে হবে।
summary_writer = tf.summary.create_file_writer('/tmp/summaries')
with summary_writer.as_default():
tf.summary.scalar('loss', 0.1, step=42)
সংক্ষিপ্তসার হিসাবে লগ করার আগে ডেটা একত্রিত করতে tf.metrics ব্যবহার করুন। মেট্রিক্স রাষ্ট্রীয়; যখন আপনি result পদ্ধতিতে কল করেন তখন তারা মান সংগ্রহ করে এবং একটি ক্রমবর্ধমান ফলাফল প্রদান করে (যেমন Mean.result )। Model.reset_states দিয়ে জমা হওয়া মানগুলি সাফ করুন।
def train(model, optimizer, dataset, log_freq=10):
avg_loss = tf.keras.metrics.Mean(name='loss', dtype=tf.float32)
for images, labels in dataset:
loss = train_step(model, optimizer, images, labels)
avg_loss.update_state(loss)
if tf.equal(optimizer.iterations % log_freq, 0):
tf.summary.scalar('loss', avg_loss.result(), step=optimizer.iterations)
avg_loss.reset_states()
def test(model, test_x, test_y, step_num):
# training=False is only needed if there are layers with different
# behavior during training versus inference (e.g. Dropout).
loss = loss_fn(model(test_x, training=False), test_y)
tf.summary.scalar('loss', loss, step=step_num)
train_summary_writer = tf.summary.create_file_writer('/tmp/summaries/train')
test_summary_writer = tf.summary.create_file_writer('/tmp/summaries/test')
with train_summary_writer.as_default():
train(model, optimizer, dataset)
with test_summary_writer.as_default():
test(model, test_x, test_y, optimizer.iterations)
সারাংশ লগ ডিরেক্টরিতে TensorBoard নির্দেশ করে উত্পন্ন সারাংশগুলি কল্পনা করুন:
tensorboard --logdir /tmp/summaries
টেনসরবোর্ডে ভিজ্যুয়ালাইজেশনের জন্য সারাংশ ডেটা লিখতে tf.summary API ব্যবহার করুন। আরও তথ্যের জন্য, tf.summary গাইড পড়ুন।
# Create the metrics
loss_metric = tf.keras.metrics.Mean(name='train_loss')
accuracy_metric = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
@tf.function
def train_step(inputs, labels):
with tf.GradientTape() as tape:
predictions = model(inputs, training=True)
regularization_loss=tf.math.add_n(model.losses)
pred_loss=loss_fn(labels, predictions)
total_loss=pred_loss + regularization_loss
gradients = tape.gradient(total_loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
# Update the metrics
loss_metric.update_state(total_loss)
accuracy_metric.update_state(labels, predictions)
for epoch in range(NUM_EPOCHS):
# Reset the metrics
loss_metric.reset_states()
accuracy_metric.reset_states()
for inputs, labels in train_data:
train_step(inputs, labels)
# Get the metric results
mean_loss=loss_metric.result()
mean_accuracy = accuracy_metric.result()
print('Epoch: ', epoch)
print(' loss: {:.3f}'.format(mean_loss))
print(' accuracy: {:.3f}'.format(mean_accuracy))
2021-12-08 17:15:19.339736: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Epoch: 0 loss: 0.142 accuracy: 0.991 2021-12-08 17:15:19.781743: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Epoch: 1 loss: 0.125 accuracy: 0.997 2021-12-08 17:15:20.219033: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Epoch: 2 loss: 0.110 accuracy: 0.997 2021-12-08 17:15:20.598085: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead. Epoch: 3 loss: 0.099 accuracy: 0.997 Epoch: 4 loss: 0.085 accuracy: 1.000 2021-12-08 17:15:20.981787: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
কেরাস মেট্রিক নাম
কেরাস মডেলগুলি মেট্রিক নামগুলি পরিচালনা করার বিষয়ে সামঞ্জস্যপূর্ণ। আপনি যখন মেট্রিক্সের তালিকায় একটি স্ট্রিং পাস করেন, তখন সেই সঠিক স্ট্রিংটি মেট্রিকের name হিসাবে ব্যবহৃত হয়। keras.callbacks .fit দ্বারা প্রত্যাবর্তিত ইতিহাস বস্তুতে এবং model.fit এ পাস করা লগগুলিতে এই নামগুলি দৃশ্যমান। মেট্রিক তালিকায় আপনি যে স্ট্রিং পাস করেছেন তাতে সেট করা হয়েছে।
model.compile(
optimizer = tf.keras.optimizers.Adam(0.001),
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics = ['acc', 'accuracy', tf.keras.metrics.SparseCategoricalAccuracy(name="my_accuracy")])
history = model.fit(train_data)
5/5 [==============================] - 1s 5ms/step - loss: 0.0963 - acc: 0.9969 - accuracy: 0.9969 - my_accuracy: 0.9969 2021-12-08 17:15:21.942940: W tensorflow/core/kernels/data/cache_dataset_ops.cc:768] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
history.history.keys()
dict_keys(['loss', 'acc', 'accuracy', 'my_accuracy'])
ডিবাগিং
আকার, ডেটা প্রকার এবং মান পরিদর্শন করতে আপনার কোড ধাপে ধাপে চালানোর জন্য উত্সাহী সম্পাদন ব্যবহার করুন। কিছু কিছু API, যেমন tf.function , tf.keras , ইত্যাদি গ্রাফ এক্সিকিউশন ব্যবহার করার জন্য ডিজাইন করা হয়েছে, কর্মক্ষমতা এবং বহনযোগ্যতার জন্য। ডিবাগ করার সময়, tf.config.run_functions_eagerly(True) ব্যবহার করুন এই কোডের ভিতরে তীক্ষ্ণ এক্সিকিউশন ব্যবহার করতে।
উদাহরণ স্বরূপ:
@tf.function
def f(x):
if x > 0:
import pdb
pdb.set_trace()
x = x + 1
return x
tf.config.run_functions_eagerly(True)
f(tf.constant(1))
>>> f()
-> x = x + 1
(Pdb) l
6 @tf.function
7 def f(x):
8 if x > 0:
9 import pdb
10 pdb.set_trace()
11 -> x = x + 1
12 return x
13
14 tf.config.run_functions_eagerly(True)
15 f(tf.constant(1))
[EOF]
এটি কেরাস মডেল এবং অন্যান্য এপিআইগুলির মধ্যেও কাজ করে যা আগ্রহী সম্পাদনকে সমর্থন করে:
class CustomModel(tf.keras.models.Model):
@tf.function
def call(self, input_data):
if tf.reduce_mean(input_data) > 0:
return input_data
else:
import pdb
pdb.set_trace()
return input_data // 2
tf.config.run_functions_eagerly(True)
model = CustomModel()
model(tf.constant([-2, -4]))
>>> call()
-> return input_data // 2
(Pdb) l
10 if tf.reduce_mean(input_data) > 0:
11 return input_data
12 else:
13 import pdb
14 pdb.set_trace()
15 -> return input_data // 2
16
17
18 tf.config.run_functions_eagerly(True)
19 model = CustomModel()
20 model(tf.constant([-2, -4]))
মন্তব্য:
tf.keras.Modelপদ্ধতি যেমন ফণার নীচেtf.functionসহ গ্রাফ হিসাবেfit,evaluateএবংpredictকার্যকর করা।tf.keras.Model.compileব্যবহার করার সময়, একটিtf.functionএ মোড়ানো থেকেModelলজিক নিষ্ক্রিয় করতেrun_eagerly = Trueসেট করুন।tf.data.experimental.enable_debug_modeএর জন্য ডিবাগ মোড সক্ষম করতেtf.dataব্যবহার করুন। আরো বিস্তারিত জানার জন্য API ডক্স পড়ুন.
আপনার বস্তুতে tf.Tensors না
এই টেনসর বস্তুগুলি হয় একটি tf.function বা আগ্রহী প্রেক্ষাপটে তৈরি হতে পারে এবং এই টেনসরগুলি ভিন্নভাবে আচরণ করে। সর্বদা tf.Tensor s ব্যবহার করুন শুধুমাত্র মধ্যবর্তী মানের জন্য।
অবস্থা ট্র্যাক করতে, tf.Variable s ব্যবহার করুন কারণ তারা উভয় প্রসঙ্গেই সর্বদা ব্যবহারযোগ্য। আরও জানতে tf.Variable গাইড পড়ুন।
সম্পদ এবং আরও পড়া
TF2 কীভাবে ব্যবহার করবেন সে সম্পর্কে আরও জানতে TF2 গাইড এবং টিউটোরিয়াল পড়ুন।
আপনি যদি আগে TF1.x ব্যবহার করেন, তাহলে আপনার কোড TF2-এ স্থানান্তরিত করার পরামর্শ দেওয়া হয়। আরও জানতে মাইগ্রেশন গাইড পড়ুন।
TensorFlow.org এ দেখুন
Google Colab-এ চালান
GitHub এ দেখুন
নোটবুক ডাউনলোড করুন