|
| 1 | +import os |
| 2 | +import sys |
| 3 | +root_path = os.path.abspath("../../../") |
| 4 | +if root_path not in sys.path: |
| 5 | + sys.path.append(root_path) |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import tensorflow as tf |
| 9 | + |
| 10 | +from _Dist.NeuralNetworks.NNUtil import Toolbox |
| 11 | +from _Dist.NeuralNetworks.Base import Generator3d |
| 12 | +from _Dist.NeuralNetworks.c_BasicNN.NN import Basic |
| 13 | +from _Dist.NeuralNetworks.h_RNN.Cell import CellFactory |
| 14 | + |
| 15 | + |
| 16 | +class Basic3d(Basic): |
| 17 | + def _gen_batch(self, generator, n_batch, gen_random_subset=False, one_hot=False): |
| 18 | + if gen_random_subset: |
| 19 | + data, weights = generator.gen_random_subset(n_batch) |
| 20 | + else: |
| 21 | + data, weights = generator.gen_batch(n_batch) |
| 22 | + x = np.array([d[0] for d in data], np.float32) |
| 23 | + y = np.array([d[1] for d in data], np.float32) |
| 24 | + if not one_hot: |
| 25 | + return x, y, weights |
| 26 | + if self.n_class == 1: |
| 27 | + y = y.reshape([-1, 1]) |
| 28 | + else: |
| 29 | + y = Toolbox.get_one_hot(y, self.n_class) |
| 30 | + return x, y, weights |
| 31 | + |
| 32 | + |
| 33 | +class RNN(Basic3d): |
| 34 | + def __init__(self, *args, **kwargs): |
| 35 | + self.n_time_step = kwargs.pop("n_time_step", None) |
| 36 | + |
| 37 | + super(RNN, self).__init__(*args, **kwargs) |
| 38 | + self._name_appendix = "RNN" |
| 39 | + self._generator_base = Generator3d |
| 40 | + |
| 41 | + self._using_dndf_cell = False |
| 42 | + self._n_batch_placeholder = None |
| 43 | + self._cell = self._cell_name = None |
| 44 | + self.n_hidden = self.n_history = self.use_final_state = None |
| 45 | + |
| 46 | + def init_model_param_settings(self): |
| 47 | + super(RNN, self).init_model_param_settings() |
| 48 | + self._cell_name = self.model_param_settings.get("cell", "CustomLSTM") |
| 49 | + |
| 50 | + def init_model_structure_settings(self): |
| 51 | + super(RNN, self).init_model_structure_settings() |
| 52 | + self.n_hidden = self.model_structure_settings.get("n_hidden", 128) |
| 53 | + self.n_history = self.model_structure_settings.get("n_history", 0) |
| 54 | + self.use_final_state = self.model_structure_settings.get("use_final_state", True) |
| 55 | + |
| 56 | + def init_from_data(self, x, y, x_test, y_test, sample_weights, names): |
| 57 | + if self.n_time_step is None: |
| 58 | + assert len(x.shape) == 3, "n_time_step is not provided, hence len(x.shape) should be 3" |
| 59 | + self.n_time_step = x.shape[1] |
| 60 | + if len(x.shape) == 2: |
| 61 | + x = x.reshape(len(x), self.n_time_step, -1) |
| 62 | + else: |
| 63 | + assert self.n_time_step == x.shape[1], "n_time_step is set to be {}, but {} found".format( |
| 64 | + self.n_time_step, x.shape[1] |
| 65 | + ) |
| 66 | + if len(x_test.shape) == 2: |
| 67 | + x_test = x_test.reshape(len(x_test), self.n_time_step, -1) |
| 68 | + super(RNN, self).init_from_data(x, y, x_test, y_test, sample_weights, names) |
| 69 | + |
| 70 | + def _define_input_and_placeholder(self): |
| 71 | + self._is_training = tf.placeholder(tf.bool, name="is_training") |
| 72 | + self._tfx = tf.placeholder(tf.float32, [None, self.n_time_step, self.n_dim], name="X") |
| 73 | + self._tfy = tf.placeholder(tf.float32, [None, self.n_class], name="Y") |
| 74 | + |
| 75 | + def _build_model(self, net=None): |
| 76 | + self._model_built = True |
| 77 | + if net is None: |
| 78 | + net = self._tfx |
| 79 | + |
| 80 | + self._cell = CellFactory.get_cell(self._cell_name, self.n_hidden) |
| 81 | + if "DNDF" in self._cell_name: |
| 82 | + self._using_dndf_cell = True |
| 83 | + self._n_batch_placeholder = self._cell.n_batch_placeholder |
| 84 | + |
| 85 | + initial_state = self._cell.zero_state(tf.shape(net)[0], tf.float32) |
| 86 | + rnn_outputs, rnn_final_state = tf.nn.dynamic_rnn(self._cell, net, initial_state=initial_state) |
| 87 | + |
| 88 | + if self.n_history == 0: |
| 89 | + net = None |
| 90 | + elif self.n_history == 1: |
| 91 | + net = rnn_outputs[..., -1, :] |
| 92 | + else: |
| 93 | + net = rnn_outputs[..., -self.n_history:, :] |
| 94 | + net = tf.reshape(net, [-1, self.n_history * int(net.shape[2].value)]) |
| 95 | + if self.use_final_state: |
| 96 | + if net is None: |
| 97 | + net = rnn_final_state[1] |
| 98 | + else: |
| 99 | + net = tf.concat([net, rnn_final_state[1]], axis=1) |
| 100 | + return super(RNN, self)._build_model(net) |
| 101 | + |
| 102 | + def _get_feed_dict(self, x, y=None, weights=None, is_training=False): |
| 103 | + feed_dict = super(RNN, self)._get_feed_dict(x, y, weights, is_training) |
| 104 | + if self._using_dndf_cell: |
| 105 | + feed_dict[self._n_batch_placeholder] = len(x) |
| 106 | + return feed_dict |
| 107 | + |
| 108 | + def _define_py_collections(self): |
| 109 | + super(RNN, self)._define_py_collections() |
| 110 | + self.py_collections.append("_using_dndf_cell") |
| 111 | + |
| 112 | + def _define_tf_collections(self): |
| 113 | + super(RNN, self)._define_tf_collections() |
| 114 | + self.tf_collections.append("_n_batch_placeholder") |
0 commit comments