def build_cnn_model(n_features: int, n_classes: int): """Build the P1FP(C) model using Keras.""" model = keras.Sequential() model.add(layers.Reshape((1, n_features, 1), input_shape=(n_features, ), name="input")) model.add(layers.Conv2D( 128, 12, activation="relu", kernel_regularizer="l2", padding="same")) model.add(layers.MaxPool2D(10, padding="same")) model.add(layers.Lambda(nn.local_response_normalization)) model.add(layers.Conv2D( 128, 12, activation="relu", kernel_regularizer="l2", padding="same")) model.add(layers.MaxPool2D(10, padding="same")) model.add(layers.Lambda(nn.local_response_normalization)) # It is flattened for the computation regardless, however tflearn retained # the flattened result whereas keras does not model.add(layers.Flatten()) model.add(layers.Dense(256, activation="tanh")) model.add(layers.Dropout(rate=0.2)) model.add(layers.Dense(n_classes, activation="softmax", name="target")) learning_rate = keras.optimizers.schedules.ExponentialDecay( 0.05, decay_steps=1000, decay_rate=0.96) model.compile( optimizer=keras.optimizers.SGD(learning_rate=learning_rate), loss="categorical_crossentropy", metrics=[keras.metrics.TopKCategoricalAccuracy(3), "accuracy"]) return model
def get_simple_rnn_model(event_dim, is_Training, temperature=1): # input_shape: (None, : different sequence lengths (per batch; every sequence in one batch does have the same dimension) # EVENT_DIM) : dimensionality of one event layer_one_args = { 'units': 128, 'input_shape': (None, event_dim), 'return_sequences': True, 'dropout': 0.5, 'recurrent_dropout': 0.5, } layer_two_args = { 'units': 128, 'return_sequences': True, 'dropout': 0.5, 'recurrent_dropout': 0.5, } # for generating if not is_Training: # we predict one by one event layer_one_args['input_shape'] = (1, event_dim) layer_one_args['batch_input_shape'] = (1, 1, event_dim) layer_one_args['stateful'] = True layer_two_args['stateful'] = True model = keras.Sequential() model.add(layers.LSTM(**layer_one_args)) # second LSTM layer model.add(layers.LSTM(**layer_two_args)) model.add(layers.Lambda(lambda x: x / temperature)) model.add(layers.Dense(units=event_dim, activation='softmax')) return model
def test_build_mdn(): """Make sure an MDN model can be constructed""" N_HIDDEN = 5 N_MIXES = 5 model = keras.Sequential() model.add( keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu')) model.add(keras.layers.Dense(N_HIDDEN, activation='relu')) model.add(mdn.MDN(1, N_MIXES)) model.compile(loss=mdn.get_mixture_loss_func(1, N_MIXES), optimizer=keras.optimizers.Adam()) assert isinstance(model, keras.Sequential)
def _build_decoder(self, num_mixtures): #Decoder for using the trained model decoder = keras.Sequential() decoder.add( keras.layers.LSTM(NUM_LSTM_UNITS, batch_input_shape=(1, 1, LATENT_VECTOR_SIZE + ACTION_DIMENSIONALITY), return_sequences=False, stateful=True, name="Input_LSTM")) decoder.add( mdn.MDN(LATENT_VECTOR_SIZE, num_mixtures, name="decoder_output_MDN")) decoder.compile(loss=mdn.get_mixture_loss_func(LATENT_VECTOR_SIZE, num_mixtures), optimizer=keras.optimizers.Adam()) decoder.summary() #decoder.load_weights(path_to_weights) return (decoder, None)
def test_save_mdn(): """Make sure an MDN model can be saved and loaded""" N_HIDDEN = 5 N_MIXES = 5 model = keras.Sequential() model.add( keras.layers.Dense(N_HIDDEN, batch_input_shape=(None, 1), activation='relu')) model.add(mdn.MDN(1, N_MIXES)) model.compile(loss=mdn.get_mixture_loss_func(1, N_MIXES), optimizer=keras.optimizers.Adam()) model.save('test_save.h5', overwrite=True, save_format="h5") m_2 = keras.models.load_model('test_save.h5', custom_objects={ 'MDN': mdn.MDN, 'mdn_loss_func': mdn.get_mixture_loss_func(1, N_MIXES) }) assert isinstance(m_2, keras.Sequential)
def _build_sequential(self, sequence_length, num_mixtures): # The RNN-mdn code from https://github.com/cpmpercussion/creative-prediction/blob/master/notebooks/7-MDN-Robojam-touch-generation.ipynb model = keras.Sequential() model.add( keras.layers.LSTM( NUM_LSTM_UNITS, input_shape=(sequence_length, LATENT_VECTOR_SIZE + ACTION_DIMENSIONALITY), return_sequences=False, name="Input_LSTM")) # TODO Return sequences returns the hidden state, and feeds that to the next layer. When I do this with the MDN, # I get an error, because it does not expect that input. I need to find a way to store the hidden state (for the # controller) without return sequences? #model.add(keras.layers.LSTM(NUM_LSTM_UNITS)) model.add(mdn.MDN(LATENT_VECTOR_SIZE, num_mixtures, name="Output_MDN")) model.compile(loss=mdn.get_mixture_loss_func(LATENT_VECTOR_SIZE, num_mixtures), optimizer=keras.optimizers.Adam()) model.summary() return (model, None)
for ex in examples: xs.append(ex[:-1]) ys.append(ex[-1]) return (xs,ys) sequences = slice_sequence_examples(microjam_corpus, SEQ_LEN+1) print("Total training examples:", len(sequences)) X, y = seq_to_singleton_format(sequences) X = np.array(X) y = np.array(y) print("X:", X.shape, "y:", y.shape) OUTPUT_DIMENSION = 3 NUMBER_MIXTURES = 5 model = keras.Sequential() model.add(keras.layers.LSTM(HIDDEN_UNITS, batch_input_shape=(None,SEQ_LEN,OUTPUT_DIMENSION), return_sequences=True)) model.add(keras.layers.LSTM(HIDDEN_UNITS)) model.add(mdn.MDN(OUTPUT_DIMENSION, NUMBER_MIXTURES)) model.compile(loss=mdn.get_mixture_loss_func(OUTPUT_DIMENSION,NUMBER_MIXTURES), optimizer=keras.optimizers.Adam()) model.summary() # Define callbacks filepath="robojam_mdrnn-E{epoch:02d}-VL{val_loss:.2f}.h5" checkpoint = keras.callbacks.ModelCheckpoint(filepath, save_weights_only=True, verbose=1, save_best_only=True, mode='min') early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10) callbacks = [keras.callbacks.TerminateOnNaN(), checkpoint, early_stopping] history = model.fit(X, y, batch_size=BATCH_SIZE, epochs=EPOCHS, callbacks=callbacks, validation_split=VAL_SPLIT) # Save the Model
def _define_model(self): self._model = keras.Sequential() self._model.add( keras.layers.Dense(150, input_shape=(self._num_states, )))
def build_model(n_features: int, n_classes: int): """Create and return the DeepFingerprinting Model.""" model = keras.Sequential() # Block1 filter_num = ['None', 32, 64, 128, 256] kernel_size = ['None', 8, 8, 8, 8] conv_stride_size = ['None', 1, 1, 1, 1] pool_stride_size = ['None', 4, 4, 4, 4] pool_size = ['None', 8, 8, 8, 8] model.add(layers.Reshape((n_features, 1), input_shape=(n_features, ))) model.add( layers.Conv1D(filters=filter_num[1], kernel_size=kernel_size[1], strides=conv_stride_size[1], padding='same', name='block1_conv1')) model.add(layers.BatchNormalization(axis=-1)) model.add(layers.ELU(alpha=1.0, name='block1_adv_act1')) model.add( layers.Conv1D(filters=filter_num[1], kernel_size=kernel_size[1], strides=conv_stride_size[1], padding='same', name='block1_conv2')) model.add(layers.BatchNormalization(axis=-1)) model.add(layers.ELU(alpha=1.0, name='block1_adv_act2')) model.add( layers.MaxPooling1D(pool_size=pool_size[1], strides=pool_stride_size[1], padding='same', name='block1_pool')) model.add(layers.Dropout(0.1, name='block1_dropout')) model.add( layers.Conv1D(filters=filter_num[2], kernel_size=kernel_size[2], strides=conv_stride_size[2], padding='same', name='block2_conv1')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block2_act1')) model.add( layers.Conv1D(filters=filter_num[2], kernel_size=kernel_size[2], strides=conv_stride_size[2], padding='same', name='block2_conv2')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block2_act2')) model.add( layers.MaxPooling1D(pool_size=pool_size[2], strides=pool_stride_size[3], padding='same', name='block2_pool')) model.add(layers.Dropout(0.1, name='block2_dropout')) model.add( layers.Conv1D(filters=filter_num[3], kernel_size=kernel_size[3], strides=conv_stride_size[3], padding='same', name='block3_conv1')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block3_act1')) model.add( layers.Conv1D(filters=filter_num[3], kernel_size=kernel_size[3], strides=conv_stride_size[3], padding='same', name='block3_conv2')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block3_act2')) model.add( layers.MaxPooling1D(pool_size=pool_size[3], strides=pool_stride_size[3], padding='same', name='block3_pool')) model.add(layers.Dropout(0.1, name='block3_dropout')) model.add( layers.Conv1D(filters=filter_num[4], kernel_size=kernel_size[4], strides=conv_stride_size[4], padding='same', name='block4_conv1')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block4_act1')) model.add( layers.Conv1D(filters=filter_num[4], kernel_size=kernel_size[4], strides=conv_stride_size[4], padding='same', name='block4_conv2')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='block4_act2')) model.add( layers.MaxPooling1D(pool_size=pool_size[4], strides=pool_stride_size[4], padding='same', name='block4_pool')) model.add(layers.Dropout(0.1, name='block4_dropout')) model.add(layers.Flatten(name='flatten')) model.add( layers.Dense(512, kernel_initializer=initializers.glorot_uniform(seed=0), name='fc1')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='fc1_act')) model.add(layers.Dropout(0.7, name='fc1_dropout')) model.add( layers.Dense(512, kernel_initializer=initializers.glorot_uniform(seed=0), name='fc2')) model.add(layers.BatchNormalization()) model.add(layers.Activation('relu', name='fc2_act')) model.add(layers.Dropout(0.5, name='fc2_dropout')) model.add( layers.Dense(n_classes, kernel_initializer=initializers.glorot_uniform(seed=0), name='fc3')) model.add(layers.Activation('softmax', name="softmax")) model.compile(loss="categorical_crossentropy", optimizer=keras.optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0), metrics=["accuracy"]) return model