张量流自定义估计器的简单示例

时间:2020-09-17 02:11:33

标签: tensorflow

如何使用一些模拟测试数据编写一个简单的自定义tensorflow估计器的工作示例?我打算在评估模式下使用eval_ops进行测试,但设置中缺少某些内容。这段代码

import unittest
import numpy as np
import tensorflow as tf


NFEATS = 5


def model_fn(features, labels, mode):
    inputs = tf.keras.Input(shape=NFEATS)
    outputs = tf.keras.layers.Dense(1)(inputs)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    logits = model(features['dense_input'])
    loss = tf.losses.mean_squared_error(y_true=labels, y_pred=logits)
    return tf.estimator.EstimatorSpec(
        mode=mode, loss=loss, eval_metric_ops=None, evaluation_hooks=None
    )


class TestEstimator(unittest.TestCase):
    def setUp(self) -> None:
        self.N = 1000
        self.batch_size = 100
        self.X = np.random.rand(self.N, NFEATS)
        self.Y = np.random.rand(self.N, 1)

    def test_estimator(self):
        def input_fn():
            data = {
                'X': self.X,
                'Y': self.Y
            }

            def map_fn(dd):
                return ({'dense_input': dd['X']}, dd['Y'])

            dset = tf.data.Dataset.from_tensor_slices(data) \
                     .map(map_fn).repeat(1).batch(self.batch_size)
            return dset

        estimator = tf.estimator.Estimator(model_fn=model_fn)
        estimator.evaluate(input_fn=input_fn, steps=3)


if __name__ == '__main__':
    unittest.main()

产生一个很长的堆栈跟踪,其结果是试图将我的100的批量大小调整为1大小。

======================================================================
ERROR: test_estimator (test.test_wis_total_label.TestEstimator)
----------------------------------------------------------------------
Traceback (most recent call last):
...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 100 values, but the requested shape has 1
     [[{{node Reshape}}]]

During handling of the above exception, another exception occurred:

----------------------------------------------------------------------
Ran 1 test in 0.316s

FAILED (errors=1)

3 个答案:

答案 0 :(得分:0)

我知道,对于进入均方误差损失的张量,我有错误的形状,如果我这样做,这是可行的

    loss = tf.losses.mean_squared_error(y_true=tf.reshape(labels, [-1]), y_pred=tf.reshape(logits, [-1]))

绝对可以接受更简单的示例:)

答案 1 :(得分:0)

我在使用Tensorflow 1.14的计算机上测试了您的代码,并且在对以下内容进行了小幅更改后通过了您的测试:

loss = tf.losses.mean_squared_error(y_true=labels, y_pred=logits)

收件人:

loss = tf.losses.mean_squared_error(labels, logits)

答案 2 :(得分:0)

在tensorflow 2.1中

import unittest
import numpy as np
import tensorflow as tf


NFEATS = 5
BATCH_SIZE = 100


def model_fn(features, labels, mode):
    inputs = tf.keras.Input(shape=(NFEATS,), batch_size=BATCH_SIZE)
    outputs = tf.keras.layers.Dense(1)(inputs)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    logits = model(features['dense_input'])
    loss = tf.reduce_mean(tf.losses.mean_squared_error(y_true=labels, y_pred=logits))
    return tf.estimator.EstimatorSpec(
        mode=mode, loss=loss, eval_metric_ops=None, evaluation_hooks=None
    )


class TestEstimator(unittest.TestCase):
    def setUp(self) -> None:
        self.N = 1000
        self.X = np.random.rand(self.N, NFEATS)
        self.Y = np.random.rand(self.N, 1)

    def test_estimator(self):
        def input_fn():
            data = {
                'X': self.X,
                'Y': self.Y
            }

            def map_fn(dd):
                return ({'dense_input': dd['X']}, dd['Y'])

            dset = tf.data.Dataset.from_tensor_slices(data) \
                     .map(map_fn).repeat(1).batch(BATCH_SIZE)
            return dset

        estimator = tf.estimator.Estimator(model_fn=model_fn)
        estimator.evaluate(input_fn=input_fn, steps=3)


if __name__ == '__main__':
    unittest.main()

将批次大小添加到tf.keras。输入并减少损失的平均值。