def cnn_rnn_pre_int_net(window_len, n_iterations): input_state_shape = (10, ) pre_int_shape = (window_len, 3) imu_input_shape = (window_len, 7, 1) b_norm = False # Input layers. Don't change names imu_in = layers.Input(imu_input_shape, name="imu_input") state_in = layers.Input(input_state_shape, name="state_input") gyro, acc, dt_vec = custom_layers.PreProcessIMU()(imu_in) # Convolution features channels = [2**i for i in range(2, 2 + n_iterations + 1)] final_shape = (pre_int_shape[0], pre_int_shape[1], channels[-1]) gyro_feat_vec = down_scaling_loop(gyro, n_iterations, 0, channels, window_len, final_shape, n_iterations, b_norm) acc_feat_vec = down_scaling_loop(acc, n_iterations, 0, channels, window_len, final_shape, n_iterations, b_norm) # Pre-integrated rotation x = layers.GRU(64, return_sequences=True)(gyro_feat_vec) x = layers.TimeDistributed(layers.Dense(50, activation='tanh'))(x) rot_prior = layers.TimeDistributed(layers.Dense(pre_int_shape[1]), name="pre_integrated_R")(x) # Pre-integrated velocity x = custom_layers.PreIntegrationForwardDense(pre_int_shape)(rot_prior) rot_contrib = norm_activate(x, 'leakyRelu', b_norm) v_feat_vec = layers.Concatenate()( [gyro_feat_vec, acc_feat_vec, rot_contrib]) x = layers.GRU(64, return_sequences=True)(v_feat_vec) x = layers.TimeDistributed(layers.Dense(50, activation='tanh'))(x) v_prior = layers.TimeDistributed(layers.Dense(pre_int_shape[1]), name="pre_integrated_v")(x) # Pre-integrated position x = custom_layers.PreIntegrationForwardDense(pre_int_shape)(rot_prior) rot_contrib = norm_activate(x, 'leakyRelu', b_norm) x = custom_layers.PreIntegrationForwardDense(pre_int_shape)(v_prior) vel_contrib = norm_activate(x, 'leakyRelu', b_norm) pos_in = layers.Concatenate()( [gyro_feat_vec, acc_feat_vec, rot_contrib, vel_contrib]) x = layers.GRU(64, return_sequences=True)(pos_in) x = layers.TimeDistributed(layers.Dense(50, activation='tanh'))(x) p_prior = layers.TimeDistributed(layers.Dense(pre_int_shape[1]), name="pre_integrated_p")(x) rot_prior_, v_prior_, p_prior_ = custom_layers.DifferenceRegularizer( 0.005)((rot_prior, v_prior, p_prior)) state_out = custom_layers.IntegratingLayer(name="state_output")( [state_in, rot_prior_, v_prior_, p_prior_, dt_vec]) return Model(inputs=(imu_in, state_in), outputs=(rot_prior, v_prior, p_prior)), \ Model(inputs=(imu_in, state_in), outputs=(rot_prior, v_prior, p_prior, state_out))
def define_rnn_network(hidden_state_dim, timepoints, timepoint_step): rnn_input = layers.Input(shape=(40, ), name="g_input") x = layers.Dense(64, activation="relu")(rnn_input) x = layers.Dense(64, activation="relu")(x) # The last output predicts the initial state for the RNN. gru_state = layers.Dense(hidden_state_dim, activation="relu")(x) # The RNN state is passed through a separate regressor to obtain the (mean, # scale) for each of the 4 dimensions. gru = layers.GRU(hidden_state_dim) intermediate_regressor = layers.Dense(64, activation="relu") mean_and_scale_regressor = layers.Dense(8, activation="linear", name="mean_and_scale_regressor") # Ensure the regressed scale is positive; also reshape to Bx8 -> Bx1x8. kSigmaMin = 1e-3 # avoid poor conditioning absolute_scale_op = layers.Lambda(lambda x: K.concatenate( (x[:, :4], K.abs(x[:, 4:]) + kSigmaMin), axis=1)[:, tf.newaxis, :]) output_regressor = lambda x: \ absolute_scale_op(mean_and_scale_regressor(intermediate_regressor(x))) current_output = output_regressor(gru_state) # predict the first output t = timepoint_step next_timepoint_idx = 0 outputs = [] while t <= timepoints[-1] or np.isclose(t, timepoints[-1]): if np.isclose(t, timepoints[next_timepoint_idx]): outputs.append(current_output) t = timepoints[next_timepoint_idx] # avoid any accumulative drift next_timepoint_idx += 1 if next_timepoint_idx == len(timepoints): break #current_output_with_time = layers.Lambda( # lambda x: K.concatenate((x, K.zeros_like(x)[:,:,:1] + t)))( # current_output) gru_state = gru(current_output, initial_state=gru_state) current_output = output_regressor(gru_state) t += timepoint_step assert (len(outputs) == len(timepoints)) # Join the T [Bx1x8] outputs into a Bx8xT tensor, then split in half down # the first axis and reform into a Bx4xTx2 tensor. def rejoin_op(x): x = K.stack(x, axis=-1) return K.stack((x[:, 0, :4, :], x[:, 0, 4:, :]), axis=-1) output = layers.Lambda(rejoin_op, name="transforms")(outputs) M = models.Model(inputs=[rnn_input], outputs=[output], name='rnn_regressor') return M
def __init__(self, action_size, ip_shape=(84, 84, 3)): super(CnnGru, self).__init__() self.action_size = action_size self.ip_shape = ip_shape # CNN - spatial dependencies # (20, 20, 32) self.conv1 = layers.Conv2D(filters=16, kernel_size=(8, 8), strides=(4, 4), activation=tf.keras.activations.relu, data_format='channels_last', input_shape=self.ip_shape ) self.bn1 = layers.BatchNormalization() # (9, 9, 64) self.conv2 = layers.Conv2D(filters=32, kernel_size=(4, 4), strides=(2, 2), activation=tf.keras.activations.relu, data_format='channels_last' ) self.bn2 = layers.BatchNormalization() # (7, 7, 64) self.conv3 = layers.Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation=tf.keras.activations.relu, data_format='channels_last' ) self.bn3 = layers.BatchNormalization() # reshape self.flatten = layers.Flatten() self.fc1 = layers.Dense(units=256, activation=tf.keras.activations.relu ) # RNN - temporal dependencies self.gru = layers.GRU(256) # policy output layer (Actor) self.policy_logits = layers.Dense(units=self.action_size, activation=tf.nn.softmax, name='policy_logits') # value output layer (Critic) self.values = layers.Dense(units=1, name='value')
def _get_keras_model(self) -> models.Model: I = layers.Input(shape=(None, self._embedding_size), dtype='float32', name=base_model.TOKENS_FEATURE_KEY) # Bidirectional GRU H = I for num_units in self.hparams().gru_units: H = layers.Bidirectional( layers.GRU(num_units, return_sequences=True))(I) # Attention last_gru_units = self.hparams( ).gru_units[-1] * 2 # x2 because bidirectional A = layers.TimeDistributed(layers.Dense(self.hparams().attention_units, activation='relu'), input_shape=(None, last_gru_units))(H) A = layers.TimeDistributed(layers.Dense(1))(A) A = layers.Flatten()(A) A = layers.Activation('softmax')(A) # Dense X = layers.Dot((1, 1))([H, A]) X = layers.Flatten()(X) for num_units in self.hparams().dense_units: X = layers.Dense(num_units, activation='relu')(X) X = layers.Dropout(self.hparams().dropout_rate)(X) # Outputs outputs = [] for label in self._labels: outputs.append( layers.Dense(1, activation='sigmoid', name=label)(X)) model = models.Model(inputs=I, outputs=outputs) model.compile( optimizer=optimizers.Adam(lr=self.hparams().learning_rate), loss='binary_crossentropy', metrics=['binary_accuracy', super().roc_auc]) tf.logging.info(model.summary()) return model
def __init__(self, units, use_bias=True, kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', dropout=0., return_sequences=False, return_state=False, go_backwards=False, stateful=False, num_layers=1, bidirectional=False, **kwargs): super(GRU, self).__init__(**kwargs) assert num_layers == 1, "Only support single layer for CuDNN RNN in keras" self._rnn = layers.GRU( # cuDNN requirement activation='tanh', recurrent_activation='sigmoid', recurrent_dropout=0, unroll=False, use_bias=use_bias, reset_after=True, # free arguments units=units, kernel_initializer=kernel_initializer, recurrent_initializer=recurrent_initializer, bias_initializer=bias_initializer, dropout=dropout, return_sequences=return_sequences, return_state=return_state, go_backwards=go_backwards, stateful=stateful, **kwargs) if bidirectional: self._rnn = layers.Bidirectional( self._rnn, merge_mode='concat', )
def testSupports_KerasRNNLayers(self): self.assertTrue(self.quantize_registry.supports(l.LSTM(10))) self.assertTrue(self.quantize_registry.supports(l.GRU(10)))
lookback=lookback, delay=delay, min_index=300001, max_index=None, step=step, batch_size=batch_size) # How many steps to draw from test, val to see the entire respective set val_steps = (300000 - 200001 - lookback) test_steps = (len(float_data) - 300001 - lookback) # Model mdl = Sequential() mdl.add( layers.GRU(32, dropout=0.2, recurrent_dropout=0.2, input_shape=(None, float_data.shape[-1]))) mdl.add(layers.Dense(1)) mdl.compile(optimizer=RMSprop(), loss='mae') hist = mdl.fit_generator(train_gen, steps_per_epoch=500, epochs=20, validation_data=val_gen, validation_steps=val_steps) # Plotting results loss = hist.history['loss'] val_loss = hist.history['val_loss'] epochs = range(1, len(loss) + 1) plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'bo', label='Validation loss')
test_gen = generator(float_data, lookback=lookback, delay=delay, min_index=300001, max_index=None, step=step, batch_size=batch_size) # How many steps to draw from test, val to see the entire respective set val_steps = (300000 - 200001 - lookback) test_steps = (len(float_data) - 300001 - lookback) # Model mdl = Sequential() mdl.add(layers.GRU(32, input_shape=(None, float_data.shape[-1]))) mdl.add(layers.Dense(1)) mdl.compile(optimizer=RMSprop(), loss='mae') hist = mdl.fit_generator(train_gen, steps_per_epoch=500, epochs=20, validation_data=val_gen, validation_steps=val_steps) # Plotting results loss = hist.history['loss'] val_loss = hist.history['val_loss'] epochs = range(1, len(loss) + 1) plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'bo', label='Validation loss') plt.title('Training and validation loss')
y_train = np.asarray(train_y).astype('float32') y_test = np.asarray(test_y).astype('float32') y_validation = np.asarray(validation_y).astype('float32') x_train_t = x_train.reshape(x_train.shape[0], 2957, 1) x_test_t = x_test.reshape(x_test.shape[0], 2957, 1) x_validation_t = x_validation.reshape(x_validation.shape[0], 2957, 1) #########MODEL############# print("====모델생성====") #모델 구성 model = models.Sequential() model.add( layers.GRU(64, input_shape=(2957, 1), activation='relu', return_sequences=True, dropout=0.8)) model.add(layers.GRU(64, activation='tanh', return_sequences=True)) model.add(layers.GRU(64, activation='tanh', return_sequences=True)) model.add(layers.Flatten()) model.add(layers.Dense(1, activation='sigmoid')) #relu성능 최하 쓰지말자 output에 model.compile( optimizer=optimizers.Adam(lr=0.002), #모델 학급과정 설정 loss=losses.binary_crossentropy, metrics=['accuracy']) #학습 batch_size/2 => epochs/2 model.fit(x_train_t, y_train,