initialize.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from collections.abc import Iterable
  17. import numpy as np
  18. from federatedml.statistic import statics
  19. from federatedml.util import LOGGER
  20. class Initializer(object):
  21. def zeros(self, data_shape, fit_intercept, data_instances):
  22. """
  23. If fit intercept, use the following formula to initialize b can get a faster converge rate
  24. b = log(P(1)/P(0))
  25. """
  26. inits = np.zeros(data_shape)
  27. if fit_intercept and data_instances is not None:
  28. static_obj = statics.MultivariateStatisticalSummary(data_instances, cols_index=-1)
  29. label_historgram = static_obj.get_label_histogram()
  30. LOGGER.debug("label_histogram is : {}".format(label_historgram))
  31. one_count = label_historgram.get(1)
  32. zero_count = label_historgram.get(0, 0) + label_historgram.get(-1, 0)
  33. init_intercept = np.log((one_count / zero_count))
  34. inits[-1] = init_intercept
  35. return inits
  36. def random_normal(self, data_shape):
  37. if isinstance(data_shape, Iterable):
  38. inits = np.random.randn(*data_shape)
  39. else:
  40. inits = np.random.randn(data_shape)
  41. return inits
  42. def random_uniform(self, data_shape):
  43. if isinstance(data_shape, Iterable):
  44. inits = np.random.rand(*data_shape)
  45. else:
  46. inits = np.random.rand(data_shape)
  47. return inits
  48. def constant(self, data_shape, const):
  49. inits = np.ones(data_shape) * const
  50. return inits
  51. def ones(self, data_shape):
  52. inits = np.ones(data_shape)
  53. return inits
  54. def init_model(self, model_shape, init_params, data_instance=None):
  55. init_method = init_params.init_method
  56. fit_intercept = init_params.fit_intercept
  57. random_seed = init_params.random_seed
  58. np.random.seed(random_seed)
  59. if fit_intercept:
  60. if isinstance(model_shape, int):
  61. model_shape += 1
  62. else:
  63. new_shape = []
  64. for ds in model_shape:
  65. new_shape.append(ds + 1)
  66. model_shape = tuple(new_shape)
  67. if init_method == 'random_normal':
  68. w = self.random_normal(model_shape)
  69. elif init_method == 'random_uniform':
  70. w = self.random_uniform(model_shape)
  71. elif init_method == 'ones':
  72. w = self.ones(model_shape)
  73. elif init_method == 'zeros':
  74. w = self.zeros(model_shape, fit_intercept, data_instance)
  75. elif init_method == 'const':
  76. init_const = init_params.init_const
  77. w = self.constant(model_shape, const=init_const)
  78. else:
  79. raise NotImplementedError("Initial method cannot be recognized: {}".format(init_method))
  80. # LOGGER.debug("Inited model is :{}".format(w))
  81. return w