Scope
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Quản lý các nhóm thuộc tính liên quan khi tạo Hoạt động Tensorflow, chẳng hạn như tiền tố tên chung.
Scope
là nơi chứa các thuộc tính chung được áp dụng cho TensorFlow Ops. Mã người dùng thông thường khởi tạo Scope
và cung cấp nó cho các lớp xây dựng Hoạt động. Ví dụ:
Scope scope = new Scope(graph);
Constant c = Constant.create(scope, 42);
Lớp xây dựng Hoạt động có được Phạm vi và sử dụng phạm vi đó để đặt các thuộc tính trên các hoạt động Tensorflow cơ bản. Ví dụ:
// An operator class that adds a constant.
public class Constant {
public static Constant create(Scope scope, ...) {
scope.graph().opBuilder(
"Const", scope.makeOpName("Const"))
.setAttr(...)
.build()
...
}
}
Phân cấp phạm vi:
Scope
cung cấp nhiều phương thức with()
khác nhau để tạo ra một phạm vi mới. Phạm vi mới thường có một thuộc tính được thay đổi trong khi các thuộc tính khác được kế thừa từ phạm vi chính.
Một ví dụ sử dụng Constant
được triển khai như trước:
Scope root = new Scope(graph);
// The linear subscope will generate names like linear/...
Scope linear = Scope.withSubScope("linear");
// This op name will be "linear/W"
Constant.create(linear.withName("W"), ...);
// This op will be "linear/Const", using the default
// name provided by Constant
Constant.create(linear, ...);
// This op will be "linear/Const_1", using the default
// name provided by Constant and making it unique within
// this scope
Constant.create(linear, ...);
Các đối tượng phạm vi không an toàn theo luồng.
Phương pháp công khai
OperationBuilder | |
Thực thiMôi trường | env () Trả về môi trường thực thi được phạm vi này sử dụng. |
Sợi dây | makeOpName (Tên mặc định chuỗi) Tạo tên duy nhất cho toán tử, sử dụng tên mặc định được cung cấp nếu cần. |
Phạm vi | withControlDependency (điều khiển Iterable< Toán hạng <?>>) Trả về một phạm vi mới trong đó các hoạt động được thêm vào sẽ có các phần phụ thuộc điều khiển được cung cấp. |
Phạm vi | withName (Chuỗi opName) Trả về một phạm vi mới sử dụng tên được cung cấp cho một op. |
Phạm vi | withSubScope (Chuỗi childScopeName) Trả về một phạm vi mới trong đó các hoạt động được thêm vào sẽ có tiền tố tên được cung cấp. |
Phương pháp kế thừa
Từ lớp java.lang.Object boolean | bằng (Đối tượng arg0) |
Lớp cuối cùng<?> | getClass () |
int | mã băm () |
khoảng trống cuối cùng | thông báo () |
khoảng trống cuối cùng | thông báoTất cả () |
Sợi dây | toString () |
khoảng trống cuối cùng | chờ đã (arg0 dài, int arg1) |
khoảng trống cuối cùng | chờ đã (arg0 dài) |
khoảng trống cuối cùng | Chờ đợi () |
Nhà xây dựng công cộng
Tạo một phạm vi cấp cao nhất mới.
Thông số
env | Môi trường thực thi được phạm vi sử dụng. |
---|
Phương pháp công khai
Thêm từng Toán hạng trong controlDependency làm đầu vào điều khiển cho trình tạo được cung cấp.
Thông số
người xây dựng | OperationBuilder để thêm đầu vào điều khiển vào |
---|
Trả về môi trường thực thi được phạm vi này sử dụng.
Chuỗi công khai makeOpName (Tên mặc định chuỗi)
Tạo tên duy nhất cho toán tử, sử dụng tên mặc định được cung cấp nếu cần.
Điều này thường chỉ được gọi bởi các lớp xây dựng toán tử.
Phương thức này tạo ra một tên duy nhất, phù hợp với phạm vi tên được kiểm soát bởi phiên bản này. Mã xây dựng toán tử điển hình có thể trông giống như
scope.env().opBuilder("Const", scope.makeOpName("Const"))...
Lưu ý: nếu bạn cung cấp một lớp xây dựng toán tử tổng hợp (tức là một lớp tạo ra một tập hợp các phép toán liên quan bằng cách gọi mã xây dựng toán tử khác), thì tên được cung cấp sẽ đóng vai trò như một phạm vi con cho tất cả các toán tử cơ bản.
Thông số
tên mặc định | tên cho toán tử cơ bản. |
---|
Trả lại
- tên duy nhất cho người vận hành.
Ném
Đối số bất hợp phápNgoại lệ | nếu tên mặc định không hợp lệ. |
---|
Phạm vi công khai vớiControlDependency (điều khiển Iterable< Operand <?>>)
Trả về một phạm vi mới trong đó các hoạt động được thêm vào sẽ có các phần phụ thuộc điều khiển được cung cấp.
Các hoạt động được tạo bằng phạm vi này sẽ có cạnh điều khiển từ mỗi điều khiển được cung cấp. Tất cả các thuộc tính khác được kế thừa từ phạm vi hiện tại.
Thông số
điều khiển | kiểm soát các phần phụ thuộc cho các hoạt động được tạo bằng phạm vi được trả về |
---|
Trả lại
- một phạm vi mới với các phần phụ thuộc điều khiển được cung cấp
Trả về một phạm vi mới sử dụng tên được cung cấp cho một op.
Các hoạt động được tạo trong phạm vi này sẽ có tên dạng name/opName[_suffix]
. Điều này cho phép bạn đặt tên cho một toán tử cụ thể có ý nghĩa hơn.
Tên phải khớp với biểu thức chính quy [A-Za-z0-9.][A-Za-z0-9_.\-]*
Thông số
tên op | tên cho một toán tử trong phạm vi trả về |
---|
Trả lại
- một Phạm vi mới sử dụng opName cho các hoạt động.
Ném
Đối số bất hợp phápNgoại lệ | nếu tên không hợp lệ |
---|
phạm vi công khai withSubScope (Chuỗi childScopeName)
Trả về một phạm vi mới trong đó các hoạt động được thêm vào sẽ có tiền tố tên được cung cấp.
Các hoạt động được tạo với phạm vi này sẽ có tiền tố name/childScopeName/
. Tên thực tế sẽ là duy nhất trong phạm vi được trả về. Tất cả các thuộc tính khác được kế thừa từ phạm vi hiện tại.
Tên phạm vi con phải khớp với biểu thức chính quy [A-Za-z0-9.][A-Za-z0-9_.\-]*
Thông số
tên phạm vi con | tên cho phạm vi con mới |
---|
Ném
Đối số bất hợp phápNgoại lệ | nếu tên không hợp lệ |
---|
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để xem chi tiết, vui lòng tham khảo Chính sách trên trang web của Google Developers. Java là một nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết của Oracle. Một số nội dung được cấp phép theo giấy phép numpy.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Scope\n\npublic final class **Scope** \nManages groups of related properties when creating Tensorflow Operations, such as a common name\nprefix.\n\nA `Scope` is a container for common properties applied to TensorFlow Ops. Normal user\ncode initializes a `Scope` and provides it to Operation building classes. For example:\n\n Scope scope = new Scope(graph);\n Constant c = Constant.create(scope, 42);\n \nAn Operation building class acquires a Scope, and uses it to set properties on the underlying\nTensorflow ops. For example:\n\n // An operator class that adds a constant.\n public class Constant {\n public static Constant create(Scope scope, ...) {\n scope.graph().opBuilder(\n \"Const\", scope.makeOpName(\"Const\"))\n .setAttr(...)\n .build()\n ...\n }\n }\n \n**Scope hierarchy:**\n\nA `Scope` provides various `with()` methods that create a new scope. The new scope\ntypically has one property changed while other properties are inherited from the parent scope.\n\nAn example using `Constant` implemented as before:\n\n Scope root = new Scope(graph);\n\n // The linear subscope will generate names like linear/...\n Scope linear = Scope.withSubScope(\"linear\");\n\n // This op name will be \"linear/W\"\n Constant.create(linear.withName(\"W\"), ...);\n\n // This op will be \"linear/Const\", using the default\n // name provided by Constant\n Constant.create(linear, ...);\n\n // This op will be \"linear/Const_1\", using the default\n // name provided by Constant and making it unique within\n // this scope\n Constant.create(linear, ...);\n \nScope objects are **not** thread-safe.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Public Constructors\n\n|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#Scope(org.tensorflow.ExecutionEnvironment))([ExecutionEnvironment](/versions/r2.11/api_docs/java/org/tensorflow/ExecutionEnvironment) env) Create a new top-level scope. |\n\n### Public Methods\n\n|-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [OperationBuilder](/versions/r2.11/api_docs/java/org/tensorflow/OperationBuilder) | [applyControlDependencies](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#applyControlDependencies(org.tensorflow.OperationBuilder))([OperationBuilder](/versions/r2.11/api_docs/java/org/tensorflow/OperationBuilder) builder) Adds each Operand in controlDependencies as a control input to the provided builder. |\n| [ExecutionEnvironment](/versions/r2.11/api_docs/java/org/tensorflow/ExecutionEnvironment) | [env](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#env())() Returns the execution environment used by this scope. |\n| String | [makeOpName](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#makeOpName(java.lang.String))(String defaultName) Create a unique name for an operator, using a provided default if necessary. |\n| [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope) | [withControlDependencies](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#withControlDependencies(java.lang.Iterable\u003corg.tensorflow.Operand\u003c?\u003e\u003e))(Iterable\\\u003c[Operand](/versions/r2.11/api_docs/java/org/tensorflow/Operand)\\\u003c?\\\u003e\\\u003e controls) Returns a new scope where added operations will have the provided control dependencies. |\n| [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope) | [withName](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#withName(java.lang.String))(String opName) Return a new scope that uses the provided name for an op. |\n| [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope) | [withSubScope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope#withSubScope(java.lang.String))(String childScopeName) Returns a new scope where added operations will have the provided name prefix. |\n\n### Inherited Methods\n\nFrom class java.lang.Object \n\n|------------------|---------------------------|\n| boolean | equals(Object arg0) |\n| final Class\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| String | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nPublic Constructors\n-------------------\n\n#### public\n**Scope**\n([ExecutionEnvironment](/versions/r2.11/api_docs/java/org/tensorflow/ExecutionEnvironment) env)\n\nCreate a new top-level scope. \n\n##### Parameters\n\n| env | The execution environment used by the scope. |\n|-----|----------------------------------------------|\n\nPublic Methods\n--------------\n\n#### public [OperationBuilder](/versions/r2.11/api_docs/java/org/tensorflow/OperationBuilder)\n**applyControlDependencies**\n([OperationBuilder](/versions/r2.11/api_docs/java/org/tensorflow/OperationBuilder) builder)\n\nAdds each Operand in controlDependencies as a control input to the provided builder. \n\n##### Parameters\n\n| builder | OperationBuilder to add control inputs to |\n|---------|-------------------------------------------|\n\n#### public [ExecutionEnvironment](/versions/r2.11/api_docs/java/org/tensorflow/ExecutionEnvironment)\n**env**\n()\n\nReturns the execution environment used by this scope. \n\n#### public String\n**makeOpName**\n(String defaultName)\n\nCreate a unique name for an operator, using a provided default if necessary.\n\nThis is normally called only by operator building classes.\n\nThis method generates a unique name, appropriate for the name scope controlled by this\ninstance. Typical operator building code might look like\n\n scope.env().opBuilder(\"Const\", scope.makeOpName(\"Const\"))...\n \n**Note:** if you provide a composite operator building class (i.e, a class that creates a\nset of related operations by calling other operator building code), the provided name will act\nas a subscope to all underlying operators.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| defaultName | name for the underlying operator. |\n|-------------|-----------------------------------|\n\n##### Returns\n\n- unique name for the operator. \n\n##### Throws\n\n| IllegalArgumentException | if the default name is invalid. |\n|--------------------------|---------------------------------|\n\n#### public [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope)\n**withControlDependencies**\n(Iterable\\\u003c[Operand](/versions/r2.11/api_docs/java/org/tensorflow/Operand)\\\u003c?\\\u003e\\\u003e controls)\n\nReturns a new scope where added operations will have the provided control dependencies.\n\nOps created with this scope will have a control edge from each of the provided controls. All\nother properties are inherited from the current scope.\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| controls | control dependencies for ops created with the returned scope |\n|----------|--------------------------------------------------------------|\n\n##### Returns\n\n- a new scope with the provided control dependencies \n\n#### public [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope)\n**withName**\n(String opName)\n\nReturn a new scope that uses the provided name for an op.\n\nOperations created within this scope will have a name of the form `name/opName[_suffix]`. This lets you name a specific operator more meaningfully.\n\nNames must match the regular expression `[A-Za-z0-9.][A-Za-z0-9_.\\-]*`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| opName | name for an operator in the returned scope |\n|--------|--------------------------------------------|\n\n##### Returns\n\n- a new Scope that uses opName for operations. \n\n##### Throws\n\n| IllegalArgumentException | if the name is invalid |\n|--------------------------|------------------------|\n\n#### public [Scope](/versions/r2.11/api_docs/java/org/tensorflow/op/Scope)\n**withSubScope**\n(String childScopeName)\n\nReturns a new scope where added operations will have the provided name prefix.\n\nOps created with this scope will have `name/childScopeName/` as the prefix. The actual\nname will be unique in the returned scope. All other properties are inherited from the current\nscope.\n\nThe child scope name must match the regular expression `[A-Za-z0-9.][A-Za-z0-9_.\\-]*`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n##### Parameters\n\n| childScopeName | name for the new child scope |\n|----------------|------------------------------|\n\n##### Returns\n\n- a new subscope \n\n##### Throws\n\n| IllegalArgumentException | if the name is invalid |\n|--------------------------|------------------------|"]]