python - Keras + tf.Dataset + 预测

标签 python tensorflow machine-learning keras

我使用的数据集包含数字+分类数据。 在训练和评估之前,我将分类特征转换为数字,最终得到相同数量的特征,只是数字:

基于此笔记本:

https://www.kaggle.com/jannesklaas/explaining-income-classification-with-keras/notebook

我的模型使用 keras + Estimator (model_to_estimator),它定义了训练和服务函数。

总功能:14

  • 我的训练数据集包含 14 个。
  • 我的评估数据集包含 14 个。

输入功能:

  def input_fn(features, labels, shuffle, num_epochs, batch_size):
    dataset = tf.data.Dataset.from_tensor_slices((features, labels))    
    if shuffle:
        dataset = dataset.shuffle(buffer_size=256)
    dataset = dataset.repeat(num_epochs)
    dataset = dataset.batch(batch_size)
    return dataset

如果我想做真正的预测,数据需要转换为数字吗?

25, Private, 226802, 11th, 7, Never-married, Machine-op-inspct, Own-child, Black, Male, 0, 0, 40, United-States, <=50K

这是估计器服务函数:

train_input_fn = lambda: input_fn(features=X_train, 
                    labels=Y_train, 
                    shuffle=True, 
                    num_epochs=10, 
                    batch_size=40)

estimator.train(train_input_fn)

引用here

如何创建一个能够传递预测的函数?

最佳答案

数据应该是数字。估计器方法为其关联的输入函数构建图形,因此您可以使当前的 input_fn 更通用,以便能够通过仅提供 X_test 和 Y_test 来返回测试数据集,或者您可以编写一个新方法来执行此操作。目标可以传递给预测方法,因为仅当输入采用元组形式时,它才会采用第一项。

关于python - Keras + tf.Dataset + 预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53603775/

相关文章:

Python Scipy : scipy. stats.spearmanr 返回 nans

Python 生成器 : Errors visible only after commenting

python - 我怎样才能使用 tensorflow 对象检测来只检测人?

c++ - 从张量读取设备不可知的方式?

tensorflow - Keras TimeDistributed Not Masking CNN模型

machine-learning - MCTS 搜索游戏中不同开始状态的树

python - Pandas :TypeError: float() argument must be a string or a number, 不是 'pandas._libs.interval.Interval'

python - 我在训练(和测试)我的自动编码器时损失非常高

python - 属性错误 : 'Settings' object has no attribute 'MERCHANT_SETTINGS'

Python Pandas : Doing cumsum for each day for months