מדריך סגנון קוד TensorFlow

סגנון פייתון

עקוב אחר המדריך בסגנון PEP 8 Python , למעט TensorFlow משתמש ב-2 רווחים במקום 4. נא להתאים ל- Google Python Style Guide , ולהשתמש ב-Pylint כדי לבדוק את השינויים ב-Python.

pylint

כדי להתקין pylint :

$ pip install pylint

כדי לבדוק קובץ עם pylint מספריית השורש של קוד המקור TensorFlow:

$ pylint --rcfile=tensorflow/tools/ci_build/pylintrc tensorflow/python/keras/losses.py

גירסאות Python נתמכות

עבור גירסאות Python נתמכות, עיין במדריך ההתקנה של TensorFlow.

ראה את סטטוס הבנייה המתמשכת של TensorFlow עבור רכיבים רשמיים ותומכים בקהילה.

סגנון קידוד C++

שינויים בקוד TensorFlow C++ צריכים להתאים ל- Google C++ Style Guide ולפרטי הסגנון הספציפיים של TensorFlow . השתמש clang-format כדי לבדוק את השינויים שלך ב-C/C++.

כדי להתקין על אובונטו 16+, בצע:

$ apt-get install -y clang-format

אתה יכול לבדוק את הפורמט של קובץ C/C++ באמצעות הדברים הבאים:

$ clang-format <my_cc_file> --style=google > /tmp/my_cc_file.cc
$ diff <my_cc_file> /tmp/my_cc_file.cc

שפות אחרות

מוסכמות TensorFlow ושימושים מיוחדים

פעולות פייתון

פעולת TensorFlow היא פונקציה שבהינתן טנסור קלט מחזירה טנסור פלט (או מוסיפה אופ לגרף בעת בניית גרפים).

  • הארגומנט הראשון צריך להיות טנסורים, ואחריו פרמטרים בסיסיים של Python. הארגומנט האחרון הוא name עם ערך ברירת המחדל של None .
  • ארגומנטים של טנסור צריכים להיות טנסור בודד או מחזור של טנסורים. כלומר, "טנזור או רשימה של טנסורים" היא רחבה מדי. ראה assert_proper_iterable .
  • פעולות שלוקחות טנסורים כארגומנטים צריכות לקרוא convert_to_tensor כדי להמיר תשומות שאינן טנסור לטנזורים אם הן משתמשות בפעולות C++. שימו לב שהארגומנטים עדיין מתוארים כאובייקט Tensor מסוג dtype ספציפי בתיעוד.
  • לכל פעולת Python צריך להיות name_scope . כפי שניתן לראות להלן, העבר את שם האופ כמחרוזת.
  • פעולות צריכות להכיל הערת Python נרחבת עם הצהרות Args והחזרות המסבירות את הסוג והמשמעות של כל ערך. יש לציין בתיאור צורות, dtypes או דרגות אפשריות. ראה פרטי תיעוד.
  • להגברת השימושיות, כלול דוגמה לשימוש עם כניסות/יציאות של ה-op בסעיף הדוגמה.
  • הימנע משימוש מפורש ב- tf.Tensor.eval או tf.Session.run . לדוגמה, כדי לכתוב לוגיקה שתלויה בערך Tensor, השתמש בזרימת הבקרה TensorFlow. לחלופין, הגבל את הפעולה להפעלה רק כאשר הפעלה נלהבת מופעלת ( tf.executing_eagerly() ).

דוגמא:

def my_op(tensor_in, other_tensor_in, my_param, other_param=0.5,
          output_collections=(), name=None):
  """My operation that adds two tensors with given coefficients.

  Args:
    tensor_in: `Tensor`, input tensor.
    other_tensor_in: `Tensor`, same shape as `tensor_in`, other input tensor.
    my_param: `float`, coefficient for `tensor_in`.
    other_param: `float`, coefficient for `other_tensor_in`.
    output_collections: `tuple` of `string`s, name of the collection to
                        collect result of this op.
    name: `string`, name of the operation.

  Returns:
    `Tensor` of same shape as `tensor_in`, sum of input values with coefficients.

  Example:
    >>> my_op([1., 2.], [3., 4.], my_param=0.5, other_param=0.6,
              output_collections=['MY_OPS'], name='add_t1t2')
    [2.3, 3.4]
  """
  with tf.name_scope(name or "my_op"):
    tensor_in = tf.convert_to_tensor(tensor_in)
    other_tensor_in = tf.convert_to_tensor(other_tensor_in)
    result = my_param * tensor_in + other_param * other_tensor_in
    tf.add_to_collection(output_collections, result)
    return result

נוֹהָג:

output = my_op(t1, t2, my_param=0.5, other_param=0.6,
               output_collections=['MY_OPS'], name='add_t1t2')