คู่มือสไตล์โค้ด TensorFlow
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
สไตล์หลาม
ปฏิบัติตาม คำแนะนำสไตล์ PEP 8 Python ยกเว้น TensorFlow จะใช้ช่องว่าง 2 ช่องแทน 4 โปรดปฏิบัติตาม คำแนะนำสไตล์ Google Python และใช้ pylint เพื่อตรวจสอบการเปลี่ยนแปลง Python ของคุณ
ไพลินท์
วิธีติดตั้ง 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++ และ รายละเอียดสไตล์เฉพาะของ TensorFlow ใช้ clang-format
เพื่อตรวจสอบการเปลี่ยนแปลง C/C++ ของคุณ
หากต้องการติดตั้งบน Ubuntu 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 เป็นฟังก์ชันที่เทนเซอร์อินพุตที่กำหนดส่งคืนเทนเซอร์เอาต์พุต (หรือเพิ่ม op ให้กับกราฟเมื่อสร้างกราฟ)
- อาร์กิวเมนต์แรกควรเป็นเทนเซอร์ ตามด้วยพารามิเตอร์ Python พื้นฐาน อาร์กิวเมนต์สุดท้ายคือ
name
ที่มีค่าเริ่มต้นเป็น None
- อาร์กิวเมนต์เทนเซอร์ควรเป็นเทนเซอร์ตัวเดียวหรือเทนเซอร์ที่ทำซ้ำได้ นั่นคือ "เทนเซอร์หรือรายชื่อเทนเซอร์" นั้นกว้างเกินไป ดู
assert_proper_iterable
- การดำเนินการที่ใช้เทนเซอร์เป็นอาร์กิวเมนต์ควรเรียก
convert_to_tensor
เพื่อแปลงอินพุตที่ไม่ใช่เทนเซอร์เป็นเทนเซอร์หากพวกเขากำลังใช้การดำเนินการ C ++ โปรดทราบว่าอาร์กิวเมนต์ยังคงอธิบายว่าเป็นวัตถุ Tensor
ของ dtype เฉพาะในเอกสารประกอบ - แต่ละการดำเนินการของ Python ควรมี
name_scope
ตามที่เห็นด้านล่าง ให้ส่งชื่อของ op เป็นสตริง - การดำเนินการควรมีความคิดเห็น Python ที่ครอบคลุมพร้อมการประกาศ Args และ Returns ที่อธิบายทั้งประเภทและความหมายของแต่ละค่า ควรระบุรูปร่าง ประเภท หรืออันดับที่เป็นไปได้ในคำอธิบาย ดูรายละเอียดเอกสาร
- เพื่อการใช้งานที่เพิ่มขึ้น ให้รวมตัวอย่างการใช้งานกับอินพุต / เอาท์พุตของ 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')
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-01-14 UTC
[null,null,["อัปเดตล่าสุด 2025-01-14 UTC"],[],[],null,["# TensorFlow code style guide\n\nPython style\n------------\n\nFollow the [PEP 8 Python style\nguide](https://www.python.org/dev/peps/pep-0008/), except TensorFlow uses 2\nspaces instead of 4. Please conform to the\n[Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md),\nand use [pylint](https://www.pylint.org/) to check your Python changes.\n\n### pylint\n\nTo install `pylint`: \n\n $ pip install pylint\n\nTo check a file with `pylint` from the TensorFlow source code root directory: \n\n $ pylint --rcfile=tensorflow/tools/ci_build/pylintrc tensorflow/python/keras/losses.py\n\n### Supported Python versions\n\nFor supported Python versions, see the TensorFlow\n[installation guide](https://www.tensorflow.org/install).\n\nSee the TensorFlow\n[continuous build status](https://github.com/tensorflow/tensorflow/blob/master/README.md#continuous-build-status)\nfor official and community supported builds.\n\nC++ coding style\n----------------\n\nChanges to TensorFlow C++ code should conform to the [Google C++ Style\nGuide](https://google.github.io/styleguide/cppguide.html) and [TensorFlow specific style details](https://github.com/tensorflow/community/blob/master/governance/cpp-style.md). Use `clang-format` to check your C/C++ changes.\n\nTo install on Ubuntu 16+, do: \n\n $ apt-get install -y clang-format\n\nYou can check the format of a C/C++ file with the following: \n\n $ clang-format \u003cmy_cc_file\u003e --style=google \u003e /tmp/my_cc_file.cc\n $ diff \u003cmy_cc_file\u003e /tmp/my_cc_file.cc\n\nOther languages\n---------------\n\n- [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)\n- [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)\n- [Google Shell Style Guide](https://google.github.io/styleguide/shell.xml)\n- [Google Objective-C Style Guide](https://google.github.io/styleguide/objcguide.html)\n\nTensorFlow conventions and special uses\n---------------------------------------\n\n### Python operations\n\nA TensorFlow *operation* is a function that, given input tensors returns output\ntensors (or adds an op to a graph when building graphs).\n\n- The first argument should be tensors, followed by basic Python parameters. The last argument is `name` with a default value of `None`.\n- Tensor arguments should be either a single tensor or an iterable of tensors. That is, a \"Tensor or list of Tensors\" is too broad. See `assert_proper_iterable`.\n- Operations that take tensors as arguments should call `convert_to_tensor` to convert non-tensor inputs into tensors if they are using C++ operations. Note that the arguments are still described as a `Tensor` object of a specific dtype in the documentation.\n- Each Python operation should have a `name_scope`. As seen below, pass the name of the op as a string.\n- Operations should contain an extensive Python comment with Args and Returns declarations that explain both the type and meaning of each value. Possible shapes, dtypes, or ranks should be specified in the description. See documentation details.\n- For increased usability, include an example of usage with inputs / outputs of the op in Example section.\n- Avoid making explicit use of [`tf.Tensor.eval`](https://www.tensorflow.org/api_docs/python/tf/Tensor#eval) or `tf.Session.run`. For example, to write logic that depends on the Tensor value, use the TensorFlow control flow. Alternatively, restrict the operation to only run when eager execution is enabled ([`tf.executing_eagerly()`](https://www.tensorflow.org/api_docs/python/tf/executing_eagerly)).\n\nExample: \n\n def my_op(tensor_in, other_tensor_in, my_param, other_param=0.5,\n output_collections=(), name=None):\n \"\"\"My operation that adds two tensors with given coefficients.\n\n Args:\n tensor_in: `Tensor`, input tensor.\n other_tensor_in: `Tensor`, same shape as `tensor_in`, other input tensor.\n my_param: `float`, coefficient for `tensor_in`.\n other_param: `float`, coefficient for `other_tensor_in`.\n output_collections: `tuple` of `string`s, name of the collection to\n collect result of this op.\n name: `string`, name of the operation.\n\n Returns:\n `Tensor` of same shape as `tensor_in`, sum of input values with coefficients.\n\n Example:\n \u003e\u003e\u003e my_op([1., 2.], [3., 4.], my_param=0.5, other_param=0.6,\n output_collections=['MY_OPS'], name='add_t1t2')\n [2.3, 3.4]\n \"\"\"\n with tf.name_scope(name or \"my_op\"):\n tensor_in = tf.convert_to_tensor(tensor_in)\n other_tensor_in = tf.convert_to_tensor(other_tensor_in)\n result = my_param * tensor_in + other_param * other_tensor_in\n tf.add_to_collection(output_collections, result)\n return result\n\nUsage: \n\n output = my_op(t1, t2, my_param=0.5, other_param=0.6,\n output_collections=['MY_OPS'], name='add_t1t2')"]]