python - Tensorflow估计器输入函数: defining each feature or not?

标签 python tensorflow tensorflow-estimator

x是 Iris 数据的 120 x 4 特征矩阵(4 个特征)和 y是一个标签,我可以为 tf.estimator 创建一个输入函数像下面这样

def input_function(x, y):
    dict_x = {
        "sepal_length" : x[:,0],
        "sepal_width" :  x[:,1],
        "petal_length" : x[:,2],
        "petal_width" :  x[:,3]
    }

    dataset = tf.data.Dataset.from_tensor_slices((
        dict_x, y
    ))

    return dataset

然后定义特征列,如下所示:

feature_columns = [
    tf.feature_column.numeric_column(key="sepal_length"),
    tf.feature_column.numeric_column(key="sepal_width"),
    tf.feature_column.numeric_column(key="petal_length"),
    tf.feature_column.numeric_column(key="petal_width")
]

但是,我在互联网上发现(我忘记了来源,仍在搜索)我也可以定义输入函数,如下所示。与之前方法的区别在于现在仅用一个键 "x" 定义所有四个功能。 .

def input_function(x, y):
    dict_x = {
        "x" : x,
    }

    dataset = tf.data.Dataset.from_tensor_slices((
        dict_x, y
    ))

    return dataset

然后定义特征列,如下所示:

feature_columns = [
    tf.feature_column.numeric_column(key="x",shape=4),
]

我已经运行了这两种方法,并且都给出了几乎相同的结果。 我的问题:我找不到任何解释这两种方法之间差异的文档,因为乍一看 dict_x有不同的形状。它们在神经网络的输入层仍然受到平等对待吗?

我是新使用 tf.estimator ,谢谢

我的估算器代码(如果需要):

classifier = tf.estimator.DNNClassifier(
    feature_columns=feature_columns,
    hidden_units=[10],
    n_classes=3,
    optimizer=tf.train.GradientDescentOptimizer(0.001),
    activation_fn=tf.nn.relu
)

# Train the model
classifier.train(
    input_fn=lambda:input_function(xtrain, ytrain, True)
)

最佳答案

如果numeric_column具有相同的dtype,唯一的区别是结果输入的形状:

选项 1 创建形状的输入:[120,4,1]:120 个样本,每个样本由 1 个数字的 4 个向量表示。

而选项 2 创建的输入形状为:[120,1,4]:120 个样本,每个样本由一个由 4 个数字组成的向量表示。

最终,这并不重要,因为两者在输入网络之前都会被展平为 [120,4]


首先我创建了这些功能。

features1 = {
    'sepal_length' : np.random.rand(120),
    'sepal_width': np.random.rand(120),
    'petal_length': np.random.rand(120),
    'petal_width': np.random.rand(120)
}

features2 = {
    'everything' : np.random.rand(120, 4)
}

然后我准备了特征列——和你一样。

feature_columns1 = [
    tf.feature_column.numeric_column(key="sepal_length"),
    tf.feature_column.numeric_column(key="sepal_width"),
    tf.feature_column.numeric_column(key="petal_length"),
    tf.feature_column.numeric_column(key="petal_width")
]

feature_columns2 = [
    tf.feature_column.numeric_column(key="everything", shape=4),
]

现在,要查看将它们输入网络时到底做了什么,我们可以使用 feature_column.input_layer()

inputs1 = tf.feature_column.input_layer(features1, feature_columns1)
inputs2 = tf.feature_column.input_layer(features2, feature_columns2)

正如我们所看到的,两种方法产生了相同的形状。

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    res1 = sess.run(inputs1)
    res2 = sess.run(inputs2)
print(res1.shape)
print(res2.shape)
(120, 4)
(120, 4)

关于python - Tensorflow估计器输入函数: defining each feature or not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50246535/

相关文章:

tensorflow - Tensorflow 2.0 中的第一个纪元训练未完成

python - 复制列并将结果与​​另一个数据框连接

tensorflow - 在 PyCharm 插件市场中找不到 TensorFlow 开发者证书插件

Tensorflow 1.9/Object Detection : model_main. py 只评估一张图片

python-3.x - Tensorflow 错误 "UnimplementedError: Cast string to float is not supported"- 使用估计器的线性分类器模型

tensorflow - 如何控制 tensorflow 估计器保留的检查点数量?

python - 在 python 中解析数据提要

python - 如何使用python中的子进程将两个值传递给stdin

函数中的 Python 错误 [初学者]

python - 如何恢复tensorflow inceptions检查点文件(ckpt)?