用于 Sagemaker Tensorflow 实例的特征列的程序生成

Procedural Generation of Feature Columns for use with Sagemaker Tensorflow Instance

在这件事上我已经筋疲力尽了,所以我们将不胜感激。

我正在尝试使用 Amazon Sagemaker 设置托管我的 tensorflow 模型,并按照找到的示例进行操作 here

此示例使用维度已知的硬编码特征列。

feature_columns = [tf.feature_column.numeric_column(INPUT_TENSOR_NAME, shape=[4])]

我需要避免这种情况,因为我的数据集经常变化。

本地机器设置

现在在我的本地机器上,我定义了一个列列表

my_feature_columns = []

采用以下策略

#Define placeholder nodes based on datatype being inserted

for key in train_x.keys():

其中 train_x 是没有标签的数据集。

'OBJECTS' 成为散列桶,因为有很多可能的类别

    if train_x[key].dtypes == 'object':

        categorical_column = tf.feature_column.categorical_column_with_hash_bucket(
                key = key,
                hash_bucket_size = len(train_x[key].unique()))

        my_feature_columns.append(tf.feature_column.embedding_column(
                categorical_column=categorical_column,
                dimension=5))

'INT64' 成为分类列,因为只有两个可能的类别(我已将布尔值重新编码为 0/1)

    elif train_x[key].dtypes == 'int64':

        categorical_column = tf.feature_column.categorical_column_with_identity(
                key=key,
                num_buckets=2)

        my_feature_columns.append(tf.feature_column.indicator_column(categorical_column))

'FLOATS'成为连续的列

    elif train_x[key].dtypes == 'float':
        my_feature_columns.append(
        tf.feature_column.numeric_column(
        key=key))

在本地机器上,这会生成一个很好的列表,其中包含我的所有功能,可以在实例化 tf.estimator.DNNClassifier 时作为参数给出。随着更多类别被添加到每个 OBJECT 列,这由

处理
hash_bucket_size = len(train_x[key].unique())

贤者

来自Docs

正在准备 TensorFlow 训练脚本 您的 TensorFlow 训练脚本必须是 Python 2.7 源文件。 SageMaker TensorFlow docker 图像通过从该脚本调用特定命名的函数来使用该脚本。

训练脚本必须包含以下内容:

恰好是以下其中一项: model_fn:定义将要训练的模型。 keras_model_fn:定义将要训练的tf.keras模型。 estimator_fn:定义将训练模型的tf.estimator.Estimator。

train_input_fn: 预处理和加载训练数据。

eval_input_fn: 预处理和加载评估数据。

同样,来自 example

def train_input_fn(training_dir, params):
"""Returns input function that would feed the model during training"""
return _generate_input_fn(training_dir, 'iris_training.csv')

此函数由sagemaker docker image调用,它为training_dir添加了自己的参数,它不是全局参数。

尝试从 estimator_fn 访问我的训练数据以构建 my_feature_columns 列表时

NameError: global name 'training_dir' is not defined

我很想能够做这样的事情。

def estimator_fn(run_config, params):

my_feature_columns = []

train_x , _ , _ , _ = datasplitter(os.path.join(training_dir, 'leads_test_frame.csv'))

for key in train_x.keys():
    if train_x[key].dtypes == 'object':

        categorical_column = tf.feature_column.categorical_column_with_hash_bucket(
                key = key,
                hash_bucket_size = len(train_x[key].unique()))

        my_feature_columns.append(tf.feature_column.embedding_column(
                categorical_column=categorical_column,
                dimension=5))

    elif train_x[key].dtypes == 'int64':

        categorical_column = tf.feature_column.categorical_column_with_identity(
                key=key,
                num_buckets=2)

        my_feature_columns.append(tf.feature_column.indicator_column(categorical_column))

    elif train_x[key].dtypes == 'float':
        my_feature_columns.append(
        tf.feature_column.numeric_column(
        key=key))

return tf.estimator.DNNClassifier(feature_columns=my_feature_columns,
                                  hidden_units=[10, 20, 10],
                                  n_classes=2,
                                  config=run_config)

感谢任何能以任何方式提供帮助的人。如果需要,会很乐意提供更多信息,但感觉 4 页可能就足够了 :-S

干杯! 克莱姆

training_dir指向您的训练频道,即/opt/ml/input/data/training。您可以在 estimation_fn.

中硬编码此位置

训练开始时,SageMaker 使通道的数据在 /opt/ml/input/data/channel_name 中可用Docker 容器中的目录。

您可以在此处找到更多信息 https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo.html#your-algorithms-training-algo-running-container