TensorFlow官方文档默认值和约束条件
2018-02-28 23:16
239 查看
默认值和约束条件
属性可能有默认值, 一些类型的属性可以有约束条件. 为了定义一个有约束条件的属性, 你可以使用下列的<attr-type-expr>形式:
艾伯特(http://www.aibbt.com/)国内第一家人工智能门户
{'<string1>', '<string2>'}: 属性值必须是一个字符串, 取值可以为
<string1>或
<string2>. 值的语法已经暗示了值的类型为
string, 已经暗示了. 下述语句模拟了一个枚举值:
REGISTER_OP("EnumExample") .Attr("e: {'apple', 'orange'}");
{<type1>, <type2>}: 值是
type类型, 且必须为
<type1>或
<type2>之一, 当然
<type1>和
<type2>必须都是有效的 tensor 类型. 你无须指定属性的类型为
type, 而是通过
{...}语句给出一个类型列表. 例如, 在下面的例子里, 属性
t的类型必须为
int32,
float, 或
bool:
REGISTER_OP("RestrictedTypeExample") .Attr("t: {int32, float, bool}");这里有一些常见类型约束条件的快捷方式:
numbertype: 限制类型为数字类型, 即非 string 非 bool 的类型.
realnumbertype: 与
numbertype区别是不支持复杂类型.
quantizedtype: 与
numbertype区别是只支持量化数值 (quantized number type).
这些类型的列表在
tensorflow/core/framework/types.h文件中通过函数定义 (如
NumberTypes()). 本例中属性
t必须为某种数字类型:
REGISTER_OP("NumberType") .Attr("t: numbertype");对于这个 Op:
tf.number_type(t=tf.int32) # 有效 tf.number_type(t=tf.bool) # 无效
int >= <n>: 值必须是一个整数, 且取值大于等于
<n>,
<n>是一个自然数.
例如, 下列 Op 注册操作指定了属性
a的取值至少为
2.
REGISTER_OP("MinIntExample") .Attr("a: int >= 2");
list(<type>) >= <n>: 一个
<type>类型列表, 列表长度必须大于等于
<n>.
例如, 下面的 Op 注册操作指定属性
a是一个列表, 列表中的元素类型是
int32或
float列表长度至少为3.
REGISTER_OP("TypeListExample") .Attr("a: list({int32, float}) >= 3");通过添加
= <default>到约束条件末尾, 给一个属性设置默认值 (使其在自动生成的代码里 变成可选属性), 如下:
REGISTER_OP("AttrDefaultExample") .Attr("i: int = 0");默认值支持的语法将在最终 GraphDef 定义的 protobuf 表示中被使用.http://www.aibbt.com/a/16370.html
下面是给所有类型赋予默认值的例子:
REGISTER_OP("AttrDefaultExampleForAllTypes") .Attr("s: string = 'foo'") .Attr("i: int = 0") .Attr("f: float = 1.0") .Attr("b: bool = true") .Attr("ty: type = DT_INT32") .Attr("sh: shape = { dim { size: 1 } dim { size: 2 } }") .Attr("te: tensor = { dtype: DT_INT32 int_val: 5 }") .Attr("l_empty: list(int) = []") .Attr("l_int: list(int) = [2, 3, 5, 7]");请特别注意那些类型值里面包含的
DT_*名称.
相关文章推荐
- tensorflow学习笔记十7:tensorflow官方文档学习 How to Retrain Inception's Final Layer for New Categories
- TensorFlow官方文档字词的向量表示Vector Representations of Words
- TensorFlow官方文档
- TENSORFLOW官方文档-MNIST机器学习入门-数据集准备
- TensorFlow官方文档中文版-笔记(八)
- tensorflow学习笔记十六:tensorflow官方文档学习 Mandelbrot Set
- TensorFlow 官方文档中文版
- TensorFlow官方文档递归神经网络
- tensorflow学习笔记十七:tensorflow官方文档学习 Convolutional Neural Networks
- oracle默认表空间-官方文档
- tensorflow(GPU) win10 下安装(根据tensorflow官方文档)
- TensorFlow官方文档递归神经网络
- TensorFlow 官方文档中文版
- TensorFlow官方文档中文版-笔记(二)
- tensorflow(3)---官方文档MNIST问题的实现思路解说
- 按照TensorFlow官方文档导入MNIST数据集失败的解决
- TensorFlow官方文档偏微分方程
- 浅析谷歌tensorflow官方文档中的seq2seq代码
- TensorFlow 官方文档中文版
- TensorFlow官方文档中文版