def deep_rnn_model(input_dim, units, recur_layers, output_dim=29): """ Build a deep recurrent network for speech """ # Main acoustic input input_data = Input(name='the_input', shape=(None, input_dim)) # TODO: Add recurrent layers, each with batch normalization model_input = input_data for i in range(recur_layers): model = GRU(units, activation="relu", return_sequences=True, implementation=2, name='simp_rnn' + str(i + 1))(model_input) bg_rnn = BatchNormalization()(model) model_input = bg_rnn # TODO: Add a TimeDistributed(Dense(output_dim)) layer time_dense = TimeDistributed(Dense(output_dim))(bg_rnn) # Add softmax activation layer y_pred = Activation('softmax', name='softmax')(time_dense) # Specify the model model = Model(inputs=input_data, outputs=y_pred) model.output_length = lambda x: x print(model.summary()) return model
def train_health(dense, dimension, batch_size, hid_layer, num_epochs, x_train, y_train, x_test, y_test): look_ahead = 2 timesteps = look_ahead - 1 #top = get_rare() print('dense : ' + str(dense) + ' dimension : ' + str(dimension) + ' batch size : ' + str(batch_size) + 'hidden layer : ' + str(hid_layer) + ' num of epochs : ' + str(num_epochs)) # expected input data shape: (batch_size, timesteps, data_dim) inputs = Input(shape=( timesteps, 284, )) model = attention(inputs) model = GRU(hid_layer, input_shape=(timesteps, 284), activation='relu', dropout=0.5)(model) #model = Sequential() #model.add(GRU(hid_layer,input_shape=(timesteps, dimension))) # returns a sequence of vectors of dimension 32 #model.add(Activation('relu')) #model.add(Dropout(0.5)) #model.add(Dense(284, activation='softmax', kernel_regularizer=h_reg)) output = Dense(284, activation='softmax', kernel_regularizer=h_reg)(model) model = Model(input=[inputs], output=output) # model.add(Dense(dimension, kernel_regularizer=h_reg )) # model.compile(loss=losses.mean_squared_error, # optimizer='rmsprop') print("MODEL COMPILED. TRAINING AND VALIDATION STARTED.") model.compile(loss='binary_crossentropy', optimizer='adam') print(model.summary()) model.fit([x_train], y_train, batch_size=batch_size, epochs=num_epochs, verbose=2) return model
encoder = GRU(latent_dim, return_state=True) encoder_outputs, state_h = encoder(embed_inputs) # vae part x = Dense(intermediate_dim, activation='relu')(state_h) #only use h z_mean = Dense(vae_latent_dim, name='z_mean')(x) z_log_var = Dense(vae_latent_dim, name='z_log_var')(x) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(vae_latent_dim, ), name='z')([z_mean, z_log_var]) # instantiate encoder model encoder = Model(encoder_inputs, [z_mean, z_log_var, z], name='encoder') encoder.summary() # plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True) # build decoder model if is_train: decoder_inputs = Input(shape=(None, num_decoder_tokens), name='decoder_input') else: decoder_inputs = Input(shape=(1, num_decoder_tokens)) # decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_gru = GRU(latent_dim, return_sequences=True, return_state=True) decoder_dense = Dense(num_decoder_tokens, activation='tanh') # latent_inputs = Input(shape=(vae_latent_dim,), name='z_sampling') #used as the initial states! # h0 = Dense(latent_dim, activation='tanh')(latent_inputs) h0 = Dense(latent_dim, activation='tanh')(encoder(encoder_inputs)[2])