data.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import tensorflow as tf
  2. from tensorflow.keras.utils import Sequence
  3. from sklearn.preprocessing import OneHotEncoder
  4. import numpy as np
  5. # This creates pool of cpu resources
  6. # physical_devices = tf.config.experimental.list_physical_devices("CPU")
  7. # tf.config.experimental.set_virtual_device_configuration(
  8. # physical_devices[0], [
  9. # tf.config.experimental.VirtualDeviceConfiguration(),
  10. # tf.config.experimental.VirtualDeviceConfiguration()
  11. # ])
  12. import misc
  13. class BinaryOneHotGenerator(Sequence):
  14. def __init__(self, size=1e5, shape=2):
  15. size = int(size)
  16. if size % shape:
  17. size += shape - (size % shape)
  18. self.size = size
  19. self.shape = shape
  20. self.x = None
  21. self.on_epoch_end()
  22. def on_epoch_end(self):
  23. x_train = misc.generate_random_bit_array(self.size).reshape((-1, self.shape))
  24. self.x = misc.bit_matrix2one_hot(x_train)
  25. def __len__(self):
  26. return self.size
  27. def __getitem__(self, idx):
  28. return self.x, self.x
  29. class BinaryTimeDistributedOneHotGenerator(Sequence):
  30. def __init__(self, size=1e5, cardinality=32, blocks=9):
  31. self.size = int(size)
  32. self.cardinality = cardinality
  33. self.x = None
  34. self.encoder = OneHotEncoder(
  35. handle_unknown='ignore',
  36. sparse=False,
  37. categories=[np.arange(self.cardinality)]
  38. )
  39. self.middle = int((blocks - 1) / 2)
  40. self.blocks = blocks
  41. self.on_epoch_end()
  42. def on_epoch_end(self):
  43. rand_int = np.random.randint(self.cardinality, size=(self.size * self.blocks, 1))
  44. out = self.encoder.fit_transform(rand_int)
  45. self.x = np.reshape(out, (self.size, self.blocks, self.cardinality))
  46. def __len__(self):
  47. return self.size
  48. @property
  49. def y(self):
  50. return self.x[:, self.middle, :]
  51. def __getitem__(self, idx):
  52. return self.x, self.y
  53. class BinaryGenerator(Sequence):
  54. def __init__(self, size=1e5, shape=2, dtype=tf.bool):
  55. size = int(size)
  56. if size % shape:
  57. size += shape - (size % shape)
  58. self.size = size
  59. self.shape = shape
  60. self.x = None
  61. self.dtype = dtype
  62. self.on_epoch_end()
  63. def on_epoch_end(self):
  64. x = misc.generate_random_bit_array(self.size).reshape((-1, self.shape))
  65. self.x = tf.convert_to_tensor(x, dtype=self.dtype)
  66. def __len__(self):
  67. return self.size
  68. def __getitem__(self, idx):
  69. return self.x, self.x