13.6. GTDR

Examples

13.6.1. example_gtdr.py

  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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# coding: utf-8
# Copyright (C) pSeven SAS, 2010-present
#


from da.p7core import gtdr
from da.p7core.loggers import StreamLogger, LogLevel
from da.p7core.blackbox import Blackbox
import random

def rms(x_one, x_two):
  """
  calculate root mean square error
  """
  import math
  tmp = [pow(x_one[i] - x_two[i], 2.) for i in range(len(x_one))]
  return math.sqrt(sum(tmp) / len(tmp))

class ExampleBlackbox(Blackbox):
  """
  Problem representation for GTDR builder in blackbox mode
  """
  def prepare_blackbox(self):
    # add new variable in problem
    self.add_variable((0, 1))
    self.add_variable((0, 1))
    self.add_variable((0, 1))
    # add new response in problem
    self.add_response()

  def evaluate(self, queryx):
    result = []
    for x in queryx:
      result.append(sum(x)**2)
    return result

def reduction_by_dim():
  """
  Example reduction dimension procedure with reduced dimension value specified by the User
  """
  # create data
  orig_dim = 5
  sample_size = 20
  data = [[random.random() for j in range(orig_dim)] for i in range(sample_size)]

  # create builder
  builder = gtdr.Builder()
  # set logger, by default output -- to sys.stdout
  builder.set_logger(StreamLogger())
  # create model
  model = builder.build(x=data, dim=2, options={'GTDR/MinImprove': '0.1', 'GTDR/LogLevel': 'Info'})
  #print info about model
  print(str(model))

  # print original and compressed size
  print("Original dim: %s" % model.original_dim)
  print("Compressed dim: %s" % model.compressed_dim)
  # usage of model
  original = [random.random() for j in range(orig_dim)]
  print("original: %s" % original)
  compressed = model.compress(original)
  print("compressed: %s" % compressed)
  decompressed = model.decompress(compressed)
  print("decompressed: %s" % decompressed)
  error = rms(original, decompressed)
  print("error: %s" % error)

  # save model to file
  model.save("GtdrModelByDim.dr")
  # load model from file
  loaded_model = gtdr.Model('GtdrModelByDim.dr')

def reduction_by_err():
  """
  Example reduction dimension procedure with automatic selection of reduced dimension value based on reconstruction error specified by the User
  """
  orig_dim = 5
  sample_size = 20
  data = [[random.random() for j in range(orig_dim)] for i in range(sample_size)]

  builder = gtdr.Builder()
  # alternate method how to set options
  builder.options.set( {'GTDR/MinImprove': '0.1', 'GTDR/LogLevel': 'Debug'} )
  desired_error = 0.2
  model = builder.build(x=data, error=desired_error)

  original = [random.random() for j in range(orig_dim)]
  print("original: %s" % original)
  compressed = model.compress(original)
  print("compressed: %s" % compressed)
  decompressed = model.decompress(compressed)
  print("decompressed: %s" % decompressed)
  error = rms(original, decompressed)
  print("error: %s" % error)
  err = rms([rms(x, model.decompress(model.compress(x))) for x in data], [0 for x in data])
  print('rms err = %s' % err)

  # save model to file
  model.save("GtdrModelByErr.dr")
  # load model from file
  loaded_model = gtdr.Model('GtdrModelByErr.dr')

def feature_extraction():
  """
  Construction of such dimension reduction procedure,
  which aims at keeping outputs for initial inputs and outputs for reconstructed inputs as close as possible
  """
  # prepare data
  orig_dim = 3

  sample_size = 50
  X = [[random.random() for j in range(orig_dim)] for i in range(sample_size)]
  def f(x):
    assert(len(x) == 3)
    linear_combination = sum(x)
    return [5 * linear_combination, linear_combination * linear_combination]
  F = [f(x) for x in X]

  builder = gtdr.Builder()
  model = builder.build(x=X, y=F, options={'GTDR/LogLevel': 'Debug'})

  original_x = [random.random() for j in range(orig_dim)]
  original_f = f(original_x)

  # use model
  compr_dim = 1
  compressed_x = model.compress(original_x, compr_dim)
  decompressed_x = model.decompress(compressed_x)
  decompressed_f = f(decompressed_x)
  print('compressed dimensionality: %d' % compr_dim)
  print("error by x: %g" % rms(original_x, decompressed_x))
  print("error by f: %g" % rms(original_f, decompressed_f))

  compr_dim = 2
  # new feature: default dimensionality for FE
  model = builder.build(x=X, y=F, dim=compr_dim, options={'GTDR/LogLevel': 'Debug'})
  assert(model.compressed_dim == compr_dim)

  # use model
  compressed_x = model.compress(original_x)
  decompressed_x = model.decompress(compressed_x)
  decompressed_f = f(decompressed_x)
  print('compressed dimensionality: %d' % compr_dim)
  print("error by x: %g" % rms(original_x, decompressed_x))
  print("error by f: %g" % rms(original_f, decompressed_f))

  # save model to file
  model.save("GtdrModelFE.dr")
  # load model from file
  loaded_model = gtdr.Model('GtdrModelFE.dr')

def feature_extraction_bb():
  """
  Based on blackbox input, constructs such dimension reduction procedure,
  that aims at keeping outputs for initial inputs and outputs for reconstructed inputs as close as possible
  """
  # prepare data
  bb = ExampleBlackbox()
  orig_dim = bb.size_x()
  compr_dim = 1

  original_x = [random.random() for j in range(orig_dim)]
  original_f = [bb.evaluate([original_x])[0]]

  builder = gtdr.Builder()
  # new feature: default dimensionality for FE
  model = builder.build(blackbox=bb, budget=1000, dim=compr_dim, options={'GTDR/LogLevel': 'Debug'})
  assert(model.compressed_dim == compr_dim)

  # use model
  compressed_x = model.compress(original_x)
  decompressed_x = model.decompress(compressed_x)
  decompressed_f = [bb.evaluate([decompressed_x])[0]]
  print('compressed dimensionality: %d' % compr_dim)
  print("error by x: %g" % rms(original_x, decompressed_x))
  print("error by f: %g" % rms(original_f, decompressed_f))

  # save model to file
  model.save("GtdrModelFEBB.dr")
  # load model from file
  loaded_model = gtdr.Model('GtdrModelFEBB.dr')

def main():
  """
  Example of GTDR usage.
  """
  random.seed(100)

  print("\n\n")
  print("="*60)
  print("\nReduction by dimension\n")
  reduction_by_dim()

  print("\n\n")
  print("="*60)
  print("\nReduction by error\n")
  reduction_by_err()

  print("\n\n")
  print("="*60)
  print("\nFeature extraction - blackbox mode\n")
  feature_extraction_bb()

  print("\n\n")
  print("="*60)
  print("\nFeature extraction\n")
  feature_extraction()

if __name__ == "__main__":
  main()