(self)
| 30 | self._built_RNN() |
| 31 | |
| 32 | def _built_RNN(self): |
| 33 | with tf.variable_scope('inputs'): |
| 34 | self._xs = tf.placeholder(tf.float32, [self._batch_size, self._time_steps, self._input_size], name='xs') |
| 35 | self._ys = tf.placeholder(tf.float32, [self._batch_size, self._time_steps, self._output_size], name='ys') |
| 36 | with tf.name_scope('RNN'): |
| 37 | with tf.variable_scope('input_layer'): |
| 38 | l_in_x = tf.reshape(self._xs, [-1, self._input_size], name='2_2D') # (batch*n_step, in_size) |
| 39 | # Ws (in_size, cell_size) |
| 40 | Wi = self._weight_variable([self._input_size, self._cell_size]) |
| 41 | print(Wi.name) |
| 42 | # bs (cell_size, ) |
| 43 | bi = self._bias_variable([self._cell_size, ]) |
| 44 | # l_in_y = (batch * n_steps, cell_size) |
| 45 | with tf.name_scope('Wx_plus_b'): |
| 46 | l_in_y = tf.matmul(l_in_x, Wi) + bi |
| 47 | l_in_y = tf.reshape(l_in_y, [-1, self._time_steps, self._cell_size], name='2_3D') |
| 48 | |
| 49 | with tf.variable_scope('cell'): |
| 50 | cell = tf.contrib.rnn.BasicLSTMCell(self._cell_size) |
| 51 | with tf.name_scope('initial_state'): |
| 52 | self._cell_initial_state = cell.zero_state(self._batch_size, dtype=tf.float32) |
| 53 | |
| 54 | self.cell_outputs = [] |
| 55 | cell_state = self._cell_initial_state |
| 56 | for t in range(self._time_steps): |
| 57 | if t > 0: tf.get_variable_scope().reuse_variables() |
| 58 | cell_output, cell_state = cell(l_in_y[:, t, :], cell_state) |
| 59 | self.cell_outputs.append(cell_output) |
| 60 | self._cell_final_state = cell_state |
| 61 | |
| 62 | with tf.variable_scope('output_layer'): |
| 63 | # cell_outputs_reshaped (BATCH*TIME_STEP, CELL_SIZE) |
| 64 | cell_outputs_reshaped = tf.reshape(tf.concat(self.cell_outputs, 1), [-1, self._cell_size]) |
| 65 | Wo = self._weight_variable((self._cell_size, self._output_size)) |
| 66 | bo = self._bias_variable((self._output_size,)) |
| 67 | product = tf.matmul(cell_outputs_reshaped, Wo) + bo |
| 68 | # _pred shape (batch*time_step, output_size) |
| 69 | self._pred = tf.nn.relu(product) # for displacement |
| 70 | |
| 71 | with tf.name_scope('cost'): |
| 72 | _pred = tf.reshape(self._pred, [self._batch_size, self._time_steps, self._output_size]) |
| 73 | mse = self.ms_error(_pred, self._ys) |
| 74 | mse_ave_across_batch = tf.reduce_mean(mse, 0) |
| 75 | mse_sum_across_time = tf.reduce_sum(mse_ave_across_batch, 0) |
| 76 | self._cost = mse_sum_across_time |
| 77 | self._cost_ave_time = self._cost / self._time_steps |
| 78 | |
| 79 | with tf.variable_scope('trian'): |
| 80 | self._lr = tf.convert_to_tensor(self._lr) |
| 81 | self.train_op = tf.train.AdamOptimizer(self._lr).minimize(self._cost) |
| 82 | |
| 83 | @staticmethod |
| 84 | def ms_error(y_target, y_pre): |
no test coverage detected