此页面包含TensorFlow的开发人员和用户应遵循的风格决策,以增加其代码的可读性,减少错误数量并提高一致性。
一般遵循 PEP8 Python风格指南,除了使用2个空格。
from
__future__ import
absolute_import
from
__future__ import
division
from
__future__ import
print_functio
six
写兼容代码(例如six.moves.range
)。TensorFlow使用Bazel构建系统并执行下一个要求:
# Description:
# <...>
package(
default_visibility =
["//visibility:private"],
features =
["-parse_headers"],
)
licenses(["notice"]) # Apache 2.0
exports_files(["LICENSE"])
filegroup(
name =
"all_files",
srcs =
glob(
["**/*"],
exclude =
[
"**/METADATA",
"**/OWNERS",
],
),
visibility =
["//third_party/tensorflow:__subpackages__"],
)
tensorflow/BUILD
到all_opensource_files
目标文件中。
"//third_party/tensorflow/<directory>:all_files",
srcs_version =
"PY2AND3",
甲的Python操作是一个函数,给定输入张量和参数,创建图的一部分,并返回输出张量。
name
的默认值为None
。如果操作需要将一些Tensor
s 保存到Graph集合,则将参数与集合名称一起放在name
参数之前。
assert_proper_iterable
。
convert_to_tensor
如果使用C ++操作,将张量作为参数的操作应调用将非张量输入转换为张量。请注意,参数仍被描述为文档中Tensor
特定dtype的对象。
op_scope
如下所示。传递输入张量列表,name
并将op的默认名称作为参数。
例:
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, "my_op", [tensor_in, other_tensor_in]): 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_collections(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') |
---|
阿层是结合可变创建和/或一个或多个其他图形操作一个Python操作。遵循与常规Python操作相同的要求。
initializers
:可选地允许指定变量的初始值。regularizers
:可选地允许为变量指定正则化程序。trainable
:哪个控制他们的变量是否可训练。scope
:VariableScope
该变量将被放在下面的对象。reuse
:bool
指示变量如果存在于范围内,应重新使用。
is_training
:bool
指示是否建立训练图。例:
def conv2d(inputs, num_filters_out, kernel_size, stride=1, padding='SAME', activation_fn=tf.nn.relu, normalization_fn=add_bias, normalization_params=None, initializers=None, regularizers=None, trainable=True, scope=None, reuse=None): ... see implementation at tensorflow/contrib/layers/python/layers/layers.py ... |
---|