【发布时间】:2019-02-05 14:33:28
【问题描述】:
在这方面已经筋疲力尽了,所以任何帮助都将不胜感激。
我正在尝试使用 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镜像调用,为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
干杯! 克莱姆
【问题讨论】:
标签: tensorflow amazon-sagemaker