def generate_resnet_model_advance_and_density(classes_len: int): """ Function to create a ResNet50 model pre-trained with custom FC Layers. If the "advanced" command line argument is selected, adds an extra convolutional layer with extra filters to support larger images. :param classes_len: The number of classes (labels). :return: The ResNet50 model. """ # Reconfigure single channel input into a greyscale 3 channel input img_input = Input(shape=(config.VGG_IMG_SIZE['HEIGHT'], config.VGG_IMG_SIZE['WIDTH'], 1)) # Add convolution and pooling layers model = Sequential() model.add(img_input) for i in range(0, config.CONV_CNT): model.add(Conv2D(3, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) # Generate a ResNet50 model with pre-trained ImageNet weights, input as given above, excluded fully connected layers. model_base = ResNet50(include_top=False, weights='imagenet') # Start with base model consisting of convolutional layers model.add(model_base) # Flatten layer to convert each input into a 1D array (no parameters in this layer, just simple pre-processing). model.add(Flatten()) # Possible dropout for regularisation can be added later and experimented with: if config.DROPOUT != 0: model.add(Dropout(config.DROPOUT, name='Dropout_Regularization_1')) # Add fully connected hidden layers. model.add(Dense(units=512, activation='relu', name='Dense_Intermediate_1')) model.add(Dense(units=32, activation='relu', name='Dense_Intermediate_2')) model_density = Sequential() model_density.add( Dense(int(config.model.split('-')[1]), input_shape=(int(config.model.split('-')[1]), ), activation='relu')) model_concat = concatenate([model.output, model_density.output], axis=-1) # Final output layer that uses softmax activation function (because the classes are exclusive). if classes_len == 2: model_concat = Dense(1, activation='sigmoid', name='Output')(model_concat) else: model_concat = Dense(classes_len, activation='softmax', name='Output')(model_concat) model_combine = Model(inputs=[model.input, model_density.input], outputs=model_concat) # Print model details if running in debug mode. if config.verbose_mode: print(model_combine.summary()) return model_combine
def _define_composite(generators, discriminators): model_list = [] for i in range(len(discriminators)): g_models, d_models = generators[i], discriminators[i] # straight-through model d_models[0].trainable = False model1 = Sequential() model1.add(g_models[0]) model1.add(d_models[0]) model1.compile(loss=wasserstein_loss, optimizer=Adam(lr=0.001, beta_1=0, beta_2=0.99, epsilon=10e-8)) # fade-in model d_models[1].trainable = False model2 = Sequential() model2.add(g_models[1]) model2.add(d_models[1]) model2.compile(loss=wasserstein_loss, optimizer=Adam(lr=0.001, beta_1=0, beta_2=0.99, epsilon=10e-8)) # store model_list.append([model1, model2]) return model_list
def _make_layer(self, name, block, out_channels, num_blocks, stride=1, dilate=False): ## Constructs a layer of blocks downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if stride != 1 or self.inplanes != out_channels * block.expansion: downsample = Sequential([ layers.Conv2D(out_channels * block.expansion, (1, 1), stride), layers.BatchNormalization() ]) self.inplanes = out_channels * block.expansion blocks = [ block(out_channels=out_channels, stride=stride, downsample=downsample, base_width=self.base_width, dilation=previous_dilation) ] for _ in range(1, num_blocks): blocks.append( block(out_channels=out_channels, base_width=self.base_width, dilation=self.dilation)) return Sequential(layers=blocks, name=name)
def create_resnet50_model(num_classes: int): """ Function to create a ResNet50 model pre-trained with custom FC Layers. If the "advanced" command line argument is selected, adds an extra convolutional layer with extra filters to support larger images. :param num_classes: The number of classes (labels). :return: The ResNet50 model. """ # Reconfigure single channel input into a greyscale 3 channel input img_input = Input(shape=(config.RESNET_IMG_SIZE['HEIGHT'], config.RESNET_IMG_SIZE['WIDTH'], 1)) img_conc = Concatenate()([img_input, img_input, img_input]) # Generate a ResNet50 model with pre-trained ImageNet weights, input as given above, excluding fully connected # layers. model_base = ResNet50(include_top=False, weights="imagenet", input_tensor=img_conc) # Add fully connected layers model = Sequential() # Start with base model consisting of convolutional layers model.add(model_base) # Flatten layer to convert each input into a 1D array (no parameters in this layer, just simple pre-processing). model.add(Flatten()) fully_connected = Sequential(name="Fully_Connected") # Fully connected layers. fully_connected.add(Dropout(0.2, seed=config.RANDOM_SEED, name="Dropout_1")) fully_connected.add(Dense(units=512, activation='relu', name='Dense_1')) # fully_connected.add(Dropout(0.2, name="Dropout_2")) fully_connected.add(Dense(units=32, activation='relu', name='Dense_2')) # Final output layer that uses softmax activation function (because the classes are exclusive). if num_classes == 2: fully_connected.add( Dense(1, activation='sigmoid', kernel_initializer="random_uniform", name='Output')) else: fully_connected.add( Dense(num_classes, activation='softmax', kernel_initializer="random_uniform", name='Output')) model.add(fully_connected) # Print model details if running in debug mode. if config.verbose_mode: print("CNN Model used:") print(model.summary()) print("Fully connected layers:") print(fully_connected.summary()) return model
def __init__( self, dim: int, layer_dims: List[Union[float, int]] = [0.5, 0.5, 1, 1, 2, 2, 4], ): super().__init__() dims = [int(dim * f) for f in layer_dims] layers = [self._make_layers(dim, i) for i, dim in enumerate(dims)] layers += [ConvBlock(dim * 4, 4, 2, False)] self.layer = Sequential(layers) self.head = Sequential([Flatten(), Dense(1)])
def make_model_no_reg(classes, points_per_sample, channel_mode='channels_last'): # creates the Time Distributed CNN for range Doppler heatmap ########################## mmw_rdpl_input = (int(points_per_sample),) + rd_shape + (1,) if channel_mode == 'channels_last' else (points_per_sample, 1) + rd_shape mmw_rdpl_TDCNN = Sequential() mmw_rdpl_TDCNN.add( TimeDistributed( Conv2D(filters=8, kernel_size=(3, 3), data_format=channel_mode, kernel_initializer='random_uniform'), input_shape=mmw_rdpl_input)) mmw_rdpl_TDCNN.add(TimeDistributed(tf.keras.layers.LeakyReLU(alpha=0.1))) mmw_rdpl_TDCNN.add(TimeDistributed(BatchNormalization())) mmw_rdpl_TDCNN.add(TimeDistributed( Conv2D(filters=16, kernel_size=(3, 3), ))) mmw_rdpl_TDCNN.add(TimeDistributed(tf.keras.layers.LeakyReLU(alpha=0.1))) mmw_rdpl_TDCNN.add(TimeDistributed(BatchNormalization())) mmw_rdpl_TDCNN.add(TimeDistributed(MaxPooling2D(pool_size=2))) mmw_rdpl_TDCNN.add(TimeDistributed(Flatten())) # this should be where layers meets # creates the Time Distributed CNN for range Azimuth heatmap ########################### mmw_razi_input = (int(points_per_sample),) + ra_shape + (1,) if channel_mode == 'channels_last' else (points_per_sample, 1) + ra_shape mmw_razi_TDCNN = Sequential() mmw_razi_TDCNN.add( TimeDistributed( Conv2D(filters=8, kernel_size=(3, 3), kernel_initializer='random_uniform'), input_shape=mmw_razi_input)) mmw_razi_TDCNN.add(TimeDistributed(tf.keras.layers.LeakyReLU(alpha=0.1))) mmw_razi_TDCNN.add(TimeDistributed(BatchNormalization())) mmw_razi_TDCNN.add(TimeDistributed( Conv2D(filters=16, kernel_size=(3, 3), data_format=channel_mode, ))) mmw_razi_TDCNN.add(TimeDistributed(tf.keras.layers.LeakyReLU(alpha=0.1))) mmw_razi_TDCNN.add(TimeDistributed(BatchNormalization())) mmw_razi_TDCNN.add(TimeDistributed(MaxPooling2D(pool_size=2))) mmw_razi_TDCNN.add(TimeDistributed(Flatten())) # this should be where layers meets merged = concatenate([mmw_rdpl_TDCNN.output, mmw_razi_TDCNN.output]) # concatenate two feature extractors regressive_tensor = LSTM(units=32, return_sequences=True, kernel_initializer='random_uniform', )(merged) regressive_tensor = Dropout(rate=0.5)(regressive_tensor) regressive_tensor = LSTM(units=32, return_sequences=False, kernel_initializer='random_uniform', )(regressive_tensor) regressive_tensor = Dropout(rate=0.5)(regressive_tensor) regressive_tensor = Dense(units=256, )(regressive_tensor) regressive_tensor = Dropout(rate=0.5)(regressive_tensor) regressive_tensor = Dense(len(classes), activation='softmax', kernel_initializer='random_uniform')(regressive_tensor) model = Model(inputs=[mmw_rdpl_TDCNN.input, mmw_razi_TDCNN.input], outputs=regressive_tensor) adam = tf.keras.optimizers.Adam(lr=5e-5, decay=1e-7) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) return model
def test_with_shared_layer(self): class LayerWithLoss(layers.Layer): def call(self, inputs): self.add_loss(math_ops.reduce_sum(inputs), inputs) return inputs * 2 shared_layer = LayerWithLoss() m = Sequential([shared_layer]) m2 = Sequential([shared_layer, m]) m2(array_ops.constant([1, 2, 3])) self.assertEqual(len(m2.losses), 2) self.assertAllClose(m2.losses, [6, 12])
def prepare_model(self, model: Model) -> Model: assert isinstance(model.layers[-1], Softmax) self.teacher_model.trainable = False inputs = Input(shape=model.input.shape[1:]) return Model( inputs=inputs, outputs=concatenate([ Sequential(self.teacher_model.layers[:-1])(inputs), Sequential(model.layers[:-1])(inputs) ]), )
def get_multilayer_model(self, pre_embeddings, dp_rate=0.0, use_lstm=False): """ construct 3-layer LSTM or GRU models :param pre_embeddings: :param dp_rate: drop out rate :param use_lstm: utilize LSTM or GRU unit :return: the model """ # Embedding part can try multichannel as same as origin paper embedding_layer = Embedding(self.max_features, # 字典长度 self.embedding_dims, # 词向量维度 weights=[pre_embeddings], # 预训练的词向量 input_length=self.maxlen, # 每句话的最大长度 trainable=False # 是否在训练过程中更新词向量 ) model = Sequential() model.add(embedding_layer) if use_lstm: model.add(LSTM(RNN_DIM, return_sequences=True)) # returns a sequence of vectors of dimension RNN_DIM model.add(LSTM(RNN_DIM, return_sequences=True)) # returns a sequence of vectors of dimension RNN_DIM model.add(LSTM(RNN_DIM, recurrent_dropout=dp_rate)) # return a single vector of dimension RNN_DIM else: model.add(GRU(RNN_DIM, return_sequences=True)) # returns a sequence of vectors of dimension RNN_DIM model.add(GRU(RNN_DIM, return_sequences=True)) # returns a sequence of vectors of dimension RNN_DIM model.add(GRU(RNN_DIM, recurrent_dropout=dp_rate)) # return a single vector of dimension 128 # model.add(Dropout(dp_rate)) model.add(Dense(self.class_num, activation=self.last_activation)) return model
def as_sequential(self): '''Converts the QPGA instance into a sequential model for easier inspection''' model = Sequential() model.num_qubits = self.num_qubits model.complex_inputs = self.complex_inputs if not self.complex_inputs: model.add(Input(shape=( 2, self.input_dim, ), dtype='float64')) model.add( Lambda(lambda x: k_to_tf_complex(x), output_shape=(self.input_dim, ))) else: model.add(Input(shape=(self.input_dim, ), dtype='complex128')) model.add(self.input_layer) for cphase_layer, single_qubit_layer in zip(self.cphase_layers, self.single_qubit_layers): model.add(cphase_layer) model.add(single_qubit_layer) model.complex_outputs = self.complex_outputs if not self.complex_outputs: model.add(Lambda(lambda x: tf_to_k_complex(x))) return model
def get_bidirectional_model(self, pre_embeddings, dp_rate=0.0, use_lstm=False): """ follow the common model construction step shown in keras manual :param pre_embeddings: :param dp_rate: drop out rate :param use_lstm: utilize LSTM or GRU unit :return: the model """ # Embedding part can try multichannel as same as origin paper embedding_layer = Embedding(self.max_features, # 字典长度 self.embedding_dims, # 词向量维度 weights=[pre_embeddings], # 预训练的词向量 input_length=self.maxlen, # 每句话的最大长度 trainable=False # 是否在训练过程中更新词向量 ) model = Sequential() model.add(embedding_layer) if use_lstm: model.add(Bidirectional(LSTM(RNN_DIM, recurrent_dropout=dp_rate))) else: model.add(Bidirectional(GRU(RNN_DIM, recurrent_dropout=dp_rate))) # model.add(Dropout(dp_rate)) model.add(Dense(self.class_num, activation=self.last_activation)) return model
def train(self): batch_size = 64 units = 100 embedding_matrix = np.zeros((self.vocab_size, 100)) for word, index in self.tk.word_index.items(): embedding_vector = self.word2vec.get(word) if embedding_vector is not None: embedding_matrix[index] = embedding_vector self.model = Sequential() self.model.add( Embedding(self.vocab_size, units, weights=[embedding_matrix], trainable=False)) self.model.add( Bidirectional(LSTM(units, return_sequences=True, dropout=0.2))) self.model.add(Bidirectional(LSTM(units, dropout=0.2))) self.model.add(Dense(self.output_size, activation='sigmoid')) print(self.model.summary()) self.model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc']) history = self.model.fit(self.X_train, self.y_train, epochs=100, batch_size=batch_size, verbose=1)
def dense_binary_classifier(input_dim: int, hidden_layers_units: List[int], batch_norm: bool = False, dropout: float = 0., l2_regularizer_param: int = 0.01): model = Sequential() if len(hidden_layers_units): model.add(Dense(hidden_layers_units[0], input_dim=input_dim, kernel_initializer='normal', kernel_regularizer=l2(l2_regularizer_param), bias_regularizer=l2(l2_regularizer_param), activation='relu')) if dropout != 0.: model.add(Dropout(dropout)) if batch_norm: model.add(BatchNormalization(axis=1)) for layer_units in hidden_layers_units[1:]: model.add(Dense(layer_units, kernel_initializer='normal', kernel_regularizer=l2(l2_regularizer_param), bias_regularizer=l2(l2_regularizer_param), activation='relu')) if dropout < 1: model.add(Dropout(dropout)) if batch_norm: model.add(BatchNormalization(axis=1)) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def upsample(units, input_shape=None, apply_dropout=False, layer_type='dense', output_padding=(1, 1)): initializer = random_normal_initializer(0., 0.02) seq = Sequential() if layer_type == 'dense': seq.add( layers.Dense(units, input_shape=[ input_shape, ], kernel_initializer=initializer, use_bias=False)) elif layer_type == 'conv': seq.add( layers.Conv2DTranspose(filters=units, kernel_size=3, strides=(2, 2), padding='same', input_shape=input_shape, kernel_initializer=initializer, use_bias=False, output_padding=output_padding)) else: raise ValueError('wrong layer_type!') seq.add(layers.BatchNormalization()) if apply_dropout: seq.add(layers.Dropout(0.5)) seq.add(layers.ReLU()) return seq
def keras_model_fn(hyperparameters): model = Sequential() model.add( Conv2D(64, kernel_size=(3, 3), input_shape=(HEIGHT, WIDTH, DEPTH), activation="relu", name="inputs", padding="same")) model.add(MaxPooling2D()) model.add(Conv2D(64, kernel_size=(3, 3), activation="relu", padding="same")) model.add(MaxPooling2D()) model.add(Conv2D(96, kernel_size=(3, 3), activation="relu", padding="same")) model.add(MaxPooling2D()) model.add(Conv2D(96, kernel_size=(3, 3), activation="relu", padding="same")) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(256, activation="relu")) model.add(Dense(2, activation="softmax")) opt = RMSPropOptimizer(learning_rate=hyperparameters['learning_rate'], decay=hyperparameters['decay']) model.compile(loss='binary_crossentropy', optimizer=opt, metrics=["accuracy"]) return model
def init_cem(env, nb_actions): """ Initialize the CEM agent using the keras-rl package. :param env: the environment to be played, required to determine the input size :param nb_actions: number of actions :return: CEM agent """ # Option 2: deep network model = Sequential() model.add(Flatten(input_shape=(1, ) + env.observation_space.shape)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('softmax')) # compile agent memory = EpisodeParameterMemory(limit=1000, window_length=1) cem = CEMAgent(model=model, nb_actions=nb_actions, memory=memory, batch_size=50, nb_steps_warmup=2000, train_interval=50, elite_frac=0.05) cem.model_name = "CEM" cem.compile() return cem
def learn_model(x_train, y_train, x_test, y_test, take_components, save_path=None, do_pca=False): # pca select main features if do_pca: pca = PCA(n_components=take_components) print("Compute pca relevant features with " + str(take_components) + " percent of variance") previous_dims = len(x_train[0]) x_train = pca.fit_transform(x_train) x_test = pca.transform(x_test) print(str(len(x_train[0])) + " dims are used from initially " + str(previous_dims)) # expand dims x_train = np.expand_dims(x_train, axis=2) x_test = np.expand_dims(x_test, axis=2) # change label to categorical y_train = to_categorical(y_train) y_test = to_categorical(y_test) # build model model = Sequential() model.add(Conv1D(256, 8, padding='same', input_shape=(x_train.shape[1], 1))) model.add(Activation('relu')) model.add(Conv1D(256, 8, padding='same')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.6)) model.add(Conv1D(128, 8, padding='same')) model.add(Activation('relu')) model.add(Conv1D(128, 8, padding='same')) model.add(Activation('relu')) model.add(Conv1D(128, 8, padding='same')) model.add(Activation('relu')) model.add(Conv1D(128, 8, padding='same')) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dropout(0.6)) model.add(Conv1D(64, 8, padding='same')) model.add(Activation('relu')) model.add(Conv1D(64, 8, padding='same')) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.6)) model.add(Dense(2)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0), metrics=['acc']) # fit network model.fit(x_train, y_train, batch_size=16, epochs=33) # evaluate model _, accuracy = model.evaluate(x_test, y_test) # save model if save_path is not None: model.save(save_path) return accuracy
def build_model(): model = Sequential() model.add(Conv2D(64, (5, 5), (1, 1), "SAME", activation="relu", input_shape=(306, 408, 3))) model.add(MaxPool2D((3, 3), (2, 2), 'same')) model.add(Conv2D(64, (5, 5), (1, 1), "SAME", activation="relu")) model.add(MaxPool2D((3, 3), (2, 2), 'same')) model.add(Conv2D(64, (5, 5), padding="SAME", activation='relu')) model.add(MaxPool2D((3, 3), (2, 2), 'same')) model.add(Conv2D(16, (5, 5), padding="SAME", activation='relu')) model.add(MaxPool2D((3, 3), (2, 2), 'same')) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(512, activation='relu')) model.add(Dense(8, activation='relu')) optimizer = Adadelta() model.compile(optimizer, loss=mean_squared_error) print(model.summary()) train_X, train_y = GET_DATA.get_batches_data() cost_values = [] for step in range(1000): cost = model.train_on_batch(train_X, train_y) cost_values.append(cost) if step % 10 == 0: print("step %d , cost value is %.3f" % (step, cost)) model.save("./model1.h5") plt.plot(cost_values) plt.show()
def make_model(input_shape): ret = Sequential() ret.add(Dense(10, input_shape=input_shape, activation='sigmoid')) # ret.add(Dense(3, input_shape=input_shape, activation='sigmoid')) # ret.add(Dense(3, activation='relu')) ret.add(Dense(1, activation='linear')) return ret
def __init__(self): super(Network, self).__init__() self.mylayers = [ # unit1 layers.Conv2D(filters=32, kernel_size=[5, 5], padding='same', activation=nn.relu), layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'), # unit2 layers.Conv2D(filters=64, kernel_size=[5, 5], padding='same', activation=nn.relu), layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same'), # flatten the tensor layers.Flatten(), # 2 full-connected layers layers.Dense(512, activation=nn.relu), layers.Dense(10, activation=nn.softmax) # layers.Dense(10, activation=None) ] # 根据tensorflow的版本确定网络的最后一层要不要加activation=nn.softmax # 如果tf版本低于1.13: # 则需要添加, # 训练时在model.compile中设置loss=keras.losses.categorical_crossentropy # 如果tf版本等于或者高于1.13: # 则不需要添加, # 训练时在model.compile中设置loss=keras.losses.CategoricalCrossentropy(from_logits=True) self.net = Sequential(self.mylayers)
def create_model(): # Create model # Building CNN model = Sequential() model.add( Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1))) model.add(MaxPooling2D((2, 2))) model.add( Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform')) model.add( Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform')) model.add(MaxPooling2D((2, 2))) model.add(Flatten()) model.add(Dense(100, activation='relu', kernel_initializer='he_uniform')) model.add(Dense(10, activation='softmax')) # model.summary() # compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def define_gan(self): self.discriminator.trainable = False model = Sequential() model.add(self.generator) model.add(self.discriminator) model.compile(loss='binary_crossentropy', optimizer='adam') return model
def init_dqn(env, nb_actions): """ Initialize the DQN agent using the keras-rl package. :param env: the environment to be played, required to determine the input size :param nb_actions: number of actions :return: DQN Agent """ # Next, we build a very simple model. model = Sequential() model.add(Flatten(input_shape=(1, ) + env.observation_space.shape)) model.add(Dense(16)) model.add(Activation('relu')) model.add(Dense(16)) model.add(Activation('relu')) model.add(Dense(16)) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) print(model.summary()) # compile agent memory = SequentialMemory(limit=50000, window_length=1) policy = BoltzmannQPolicy() dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=10, target_model_update=1e-2, policy=policy) dqn.model_name = f"DQN" dqn.compile(Adam(lr=1e-3), metrics=['mae']) return dqn
def create_model(): model = Sequential() model.add(Dense(1, input_shape=(3, ), activation='sigmoid')) # model.add(Dense(NUMBER_OF_ACTIONS, activation='sigmoid')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss="mse", optimizer=sgd, metrics=["accuracy"]) return model
def init_sarsa(env, nb_actions, lr=1e-3): """ Initialize the Sarsa agent using the keras-rl package. :param env: the environment to be played, required to determine the input size :param nb_actions: number of actions :param lr: learning rate :return: Sarsa Agent """ # Next, we build a very simple model. model = Sequential() model.add(Flatten(input_shape=(1, ) + env.observation_space.shape)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) # SARSA does not require a memory. policy = BoltzmannQPolicy() sarsa = SARSAAgent(model=model, nb_actions=nb_actions, nb_steps_warmup=10, policy=policy) sarsa.model_name = f"SARSA" sarsa.compile(Adam(lr=lr), metrics=['mae']) return sarsa
def downsample(units, input_shape=None, apply_batchnorm=True, layer_type='dense'): initializer = random_normal_initializer(0., 0.02) seq = Sequential() if layer_type == 'dense': seq.add( layers.Dense(units, input_shape=[ input_shape, ], kernel_initializer=initializer, use_bias=False)) elif layer_type == 'conv': seq.add( layers.Conv2D(filters=units, kernel_size=3, strides=(2, 2), padding='same', input_shape=input_shape, kernel_initializer=initializer, use_bias=False)) else: raise ValueError('wrong layer type!') if apply_batchnorm: seq.add(layers.BatchNormalization()) seq.add(layers.LeakyReLU()) return seq
def build_model(params): model = Sequential() if params['pretrained']: model.add(Embedding(params['vocab_size'], params['embedding_dim'], weights=[glove.custom_embedding_matrix], input_length=params['max_answer_len'], trainable=False)) else: model.add(Embedding(params['vocab_size'], params['embedding_dim'], input_length=params['max_answer_len'])) model.add(Dropout(params['dropout'])) if params['flatten']: model.add(Flatten()) model.add(Reshape((1, params['embedding_dim'] * params['max_answer_len']))) if params['lstm_dim_2']: model.add(LSTM(params['lstm_dim_1'], return_sequences=True)) model.add(LSTM(params['lstm_dim_2'], return_sequences=False)) else: model.add(LSTM(params['lstm_dim_1'], return_sequences=False)) model.add(Dropout(params['dropout'])) model.add(Dense(1, activation="linear")) # compile the model optimizer = AdamOptimizer() model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['acc']) print(model.summary()) return model
def build_ffn_model(): """ Simple 2 Dense layer model w/ LeakyReLU""" return Sequential([ layers.Dense(HIDDEN_UNITS_DIM), layers.LeakyReLU(), layers.Dense(ACTION_OUTPUT_DIM, activation='softmax') ])
def keras_model_fn(input_dim, output_dim, return_sequences, hyperparameters): model = Sequential() model.add( LSTM(input_shape=(None, input_dim), units=output_dim, return_sequences=return_sequences, name="inputs")) model.add(Dropout(0.2)) model.add(LSTM(128, return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(units=1)) model.add(Activation('linear')) opt = hyperparameters['optimizer'], loss = hyperparameters['loss'], eval_metric = hyperparameters['eval_metric'], model.compile(loss=loss, optimizer=opt, metrics=eval_metric) print(model.summary()) return model
def DeepQNetwork(lr, num_actions, input_dims, fc1): q_net = Sequential() q_net.add( Conv2D(filters=32, kernel_size=8, strides=4, activation='relu', input_shape=(*input_dims, ), data_format='channels_first')) q_net.add( Conv2D(filters=64, kernel_size=4, strides=2, activation='relu', data_format='channels_first')) q_net.add( Conv2D(filters=64, kernel_size=3, strides=1, activation='relu', data_format='channels_first')) q_net.add(Flatten()) q_net.add(Dense(fc1, activation='relu')) q_net.add(Dense(num_actions, activation=None)) q_net.compile(optimizer=Adam(learning_rate=lr), loss='mse') return q_net