aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/data/kernel_tests/zip_dataset_op_test.py
blob: 55933118b9633704e9d68e4b8a4d6010c61895ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for the experimental input pipeline ops."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.ops import array_ops
from tensorflow.python.platform import test


class ZipDatasetTest(test.TestCase):

  def testZipDataset(self):
    component_placeholders = [
        array_ops.placeholder(dtypes.int64),
        array_ops.placeholder(dtypes.int64),
        array_ops.placeholder(dtypes.float64)
    ]

    datasets = tuple([
        dataset_ops.Dataset.from_tensor_slices(component_placeholder)
        for component_placeholder in component_placeholders
    ])
    zipped = dataset_ops.Dataset.zip(datasets)

    iterator = zipped.make_initializable_iterator()
    init_op = iterator.initializer
    get_next = iterator.get_next()

    with self.test_session() as sess:
      equal_length_components = [
          np.tile(np.array([[1], [2], [3], [4]]), 20),
          np.tile(np.array([[12], [13], [14], [15]]), 22),
          np.array([37.0, 38.0, 39.0, 40.0])
      ]
      sess.run(init_op, feed_dict={ph: value for ph, value in zip(
          component_placeholders, equal_length_components)})
      for i in range(4):
        results = sess.run(get_next)
        for component, result_component in zip(
            equal_length_components, results):
          self.assertAllEqual(component[i], result_component)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)

      variable_length_components = [[1, 2, 3, 4], [1, 2, 3, 4, 5], [1.0, 2.0]]
      sess.run(init_op, feed_dict={ph: value for ph, value in zip(
          component_placeholders, variable_length_components)})
      for i in range(2):
        results = sess.run(get_next)
        for component, result_component in zip(
            variable_length_components, results):
          self.assertAllEqual(component[i], result_component)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)

  def testNestedZipDataset(self):
    component_placeholders = [
        array_ops.placeholder(dtypes.int64, shape=[4, 20]),
        array_ops.placeholder(dtypes.int64, shape=[4, 22]),
        array_ops.placeholder(dtypes.float64, shape=[4])
    ]

    datasets = [
        dataset_ops.Dataset.from_tensor_slices(component_placeholder)
        for component_placeholder in component_placeholders
    ]
    zipped = dataset_ops.Dataset.zip((datasets[0], (datasets[1], datasets[2])))

    iterator = zipped.make_initializable_iterator()
    init_op = iterator.initializer
    get_next = iterator.get_next()

    self.assertEqual([20], get_next[0].shape)
    self.assertEqual([22], get_next[1][0].shape)
    self.assertEqual([], get_next[1][1].shape)

    with self.test_session() as sess:
      equal_length_components = [
          np.tile(np.array([[1], [2], [3], [4]]), 20),
          np.tile(np.array([[12], [13], [14], [15]]), 22),
          np.array([37.0, 38.0, 39.0, 40.0])
      ]
      sess.run(init_op, feed_dict={ph: value for ph, value in zip(
          component_placeholders, equal_length_components)})
      for i in range(4):
        result1, (result2, result3) = sess.run(get_next)
        self.assertAllEqual(equal_length_components[0][i], result1)
        self.assertAllEqual(equal_length_components[1][i], result2)
        self.assertAllEqual(equal_length_components[2][i], result3)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(get_next)


if __name__ == "__main__":
  test.main()