具有用户指定头部的 TensorFlow RNN 模型的 Estimator。
警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。
继承自:Estimator
用法
tf.estimator.experimental.RNNEstimator(
head, sequence_feature_columns, context_feature_columns=None, units=None,
cell_type=USE_DEFAULT, rnn_cell_fn=None, return_sequences=False, model_dir=None,
optimizer='Adagrad', config=None
)参数
-
headHead实例。这指定要优化的模型的输出和损失函数。 -
sequence_feature_columns包含表示顺序输入的FeatureColumn的可迭代对象。集合中的所有项目都应该是序列列(例如sequence_numeric_column)或从一个列构造(例如embedding_column与sequence_categorical_column_*作为输入)。 -
context_feature_columns包含用于上下文输入的FeatureColumn的迭代。这些列表示的数据将在每个时间步被复制并提供给 RNN。这些列必须是派生自DenseColumn的类的实例,例如numeric_column,而不是顺序变体。 -
units每个 RNN 层的整数个隐藏单元的迭代。如果设置,还必须指定cell_type并且rnn_cell_fn必须是None。 -
cell_type生成 RNN 单元的类或指定单元类型的字符串。支持的字符串是:'simple_rnn','lstm'和'gru'。如果设置,还必须指定units并且rnn_cell_fn必须是None。 -
rnn_cell_fn一个返回 RNN 单元实例的函数,该实例将用于构造 RNN。如果设置,则无法设置units和cell_type。这适用于需要在units和cell_type之外进行额外自定义的高级用户。请注意,堆叠 RNN 需要tf.keras.layers.StackedRNNCells。 -
return_sequences一个布尔值,指示是返回输出序列中的最后一个输出,还是返回完整序列。 -
model_dir保存模型参数、图形等的目录。这也可用于将检查点从目录加载到估计器中,以继续训练先前保存的模型。 -
optimizertf.Optimizer的实例或指定优化器类型的字符串。默认为 Adagrad 优化器。 -
configRunConfig对象来配置运行时设置。
抛出
-
ValueError如果units,cell_type和rnn_cell_fn不兼容。
属性
-
config -
model_dir -
model_fn返回绑定到self.params的model_fn。 -
params
例子:
token_sequence = sequence_categorical_column_with_hash_bucket(...)
token_emb = embedding_column(categorical_column=token_sequence, ...)
estimator = RNNEstimator(
head=tf.estimator.RegressionHead(),
sequence_feature_columns=[token_emb],
units=[32, 16], cell_type='lstm')
# Or with custom RNN cell:
def rnn_cell_fn(_):
cells = [ tf.keras.layers.LSTMCell(size) for size in [32, 16] ]
return tf.keras.layers.StackedRNNCells(cells)
estimator = RNNEstimator(
head=tf.estimator.RegressionHead(),
sequence_feature_columns=[token_emb],
rnn_cell_fn=rnn_cell_fn)
# Input builders
def input_fn_train:# returns x, y
pass
estimator.train(input_fn=input_fn_train, steps=100)
def input_fn_eval:# returns x, y
pass
metrics = estimator.evaluate(input_fn=input_fn_eval, steps=10)
def input_fn_predict:# returns x, None
pass
predictions = estimator.predict(input_fn=input_fn_predict)
train 和 evaluate 的输入应具有以下特征,否则会出现 KeyError :
- 如果头部的
weight_column不是None,则具有key=weight_column的特征,其值为Tensor。 - 对于每个
column在sequence_feature_columns:- 具有
key=column.name的函数,其value是SparseTensor。
- 具有
- 对于每个
column在context_feature_columns:- 如果
column是CategoricalColumn,则具有key=column.name的特征,其value是SparseTensor。 - 如果
column是WeightedCategoricalColumn,则有两个特征:第一个具有keyid 列名,第二个具有key权重列名。两个函数的value必须是SparseTensor。 - 如果
column是DenseColumn,则具有key=column.name的特征,其value是Tensor。
- 如果
损失和预测输出由指定的头部确定。
相关用法
- Python tf.estimator.experimental.RNNClassifier用法及代码示例
- Python tf.estimator.experimental.stop_if_lower_hook用法及代码示例
- Python tf.estimator.experimental.stop_if_no_increase_hook用法及代码示例
- Python tf.estimator.experimental.LinearSDCA用法及代码示例
- Python tf.estimator.experimental.make_early_stopping_hook用法及代码示例
- Python tf.estimator.experimental.stop_if_higher_hook用法及代码示例
- Python tf.estimator.experimental.InMemoryEvaluatorHook用法及代码示例
- Python tf.estimator.experimental.stop_if_no_decrease_hook用法及代码示例
- Python tf.estimator.TrainSpec用法及代码示例
- Python tf.estimator.LogisticRegressionHead用法及代码示例
- Python tf.estimator.MultiHead用法及代码示例
- Python tf.estimator.PoissonRegressionHead用法及代码示例
- Python tf.estimator.WarmStartSettings用法及代码示例
- Python tf.estimator.RunConfig用法及代码示例
- Python tf.estimator.MultiLabelHead用法及代码示例
- Python tf.estimator.BaselineEstimator用法及代码示例
- Python tf.estimator.DNNLinearCombinedEstimator用法及代码示例
- Python tf.estimator.Estimator用法及代码示例
- Python tf.estimator.LinearRegressor用法及代码示例
- Python tf.estimator.LinearEstimator用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.estimator.experimental.RNNEstimator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
