def __get_layer(self, layer_num): layer_size = self.layer_sizes[layer_num] layer = None lstm = self.layer_types[layer_num] == "lstm" if lstm: if self.first_layer: self.first_layer = False if self.multilayer: layer = LSTM( layer_size, return_sequences=True, input_shape=(None, self.input_size), bias_initializer='ones', kernel_initializer=initializers.random_uniform( minval=-1.5, maxval=1.5)) else: layer = LSTM( layer_size, input_shape=(1, self.input_size), bias_initializer='ones', kernel_initializer=initializers.random_uniform( minval=-1.5, maxval=1.5)) elif not self.last_layer: self.last_layer == (layer_num + 1) == self.layers layer = LSTM(layer_size, return_sequences=True, bias_initializer='ones', kernel_initializer=initializers.random_uniform( minval=-1.5, maxval=1.5)) else: print("no layer match.") return layer
def build(self, input_shape): # kernel.shape was W_shape # kernel_shape = (150, 450, 1, 1) print(self.kernel_size) #kernel_shape = (3, 3, 1, 1) kernel_shape = self.kernel_size + (self.num_channels, self.num_channels) # TODO: fiddle with dimensions or better make them automatic/parrams self.kernel = self.add_weight(name='kernel', shape=kernel_shape, initializer=initializers.random_uniform(minval=-1.0, maxval=1.0), trainable=True) self.mask = np.zeros(self.kernel.shape) # W_shape must be inherit from Conv2D or layer assert self.mask.shape[0] == self.mask.shape[1] # assert that mask height = width filter_size = self.mask.shape[0] filter_center = filter_size/2 # unmask everything before the center self.mask[:math.floor(filter_center)] = 1 # unmask rows above self.mask[:math.ceil(filter_center), :math.floor(filter_center)] = 1 # unmask cols to the left in same row if not self.mask_current: self.mask[math.ceil(filter_center), math.ceil(filter_center)] = 1 self.mask = K.variable(self.mask) self.bias = None self.built = True
def get_weight_initializer(initializer=None, seed=None): if initializer is None: return initializers.he_normal() elif initializer == "lstm": return initializers.random_uniform(minval=-0.1, maxval=0.1) else: return initializer()
def __init__(self, observation, action, state_size, action_size, seed, fcs1_units=256, fc2_units=256, fc3_units=128): model = Sequential() lim1 = 1. / np.sqrt(state_size) lim2 = 1. / np.sqrt(fcs1_units + action_size) lim3 = 1. / np.sqrt(fc2_units) model.add( Dense(fcs1_units, activation='relu', input_shape=(state_size, ), kernel_initializer=initializers.random_uniform(minval=-lim1, maxval=lim1, seed=seed))) ac = Input((action_size, )) #print(model.type) out = Concatenate([model, ac]) model.add( Dense(fc2_units, activation='relu', input_shape=(fcs1_units + action_size, ), kernel_initializer=initializers.random_uniform(minval=-lim2, maxval=lim2, seed=seed))) model.add( Dense(fc3_units, activation='relu', input_shape=(fc2_units, ), kernel_initializer=initializers.random_uniform(minval=-lim3, maxval=lim3, seed=seed))) model.add( Dense(1, activation='tanh', input_shape=(fc3_units, ), kernel_initializer=initializers.random_uniform(minval=-3e-3, maxval=3e-3, seed=seed))) self.model = Model(inputs=[action, observation], outputs=model)
def _make_prd_deepleaning_model(self, n_hidden=1, n_unit=5, keep_drop=1.0): """ディープラーニング回帰モデル作成""" from keras.models import Sequential from keras.layers.core import Dense, Activation, Dropout from keras.layers.advanced_activations import PReLU from keras.initializers import random_uniform # 四捨五入関数設定 round = lambda x: (x * 2 + 1) // 2 """ディープラーニングモデル設定""" estimator = Sequential() estimator.add(Dense(n_unit, input_dim=self.X_train.shape[1], kernel_initializer=random_uniform(seed=0), bias_initializer='zeros')) # estimator.add(BatchNormalization()) estimator.add(PReLU()) estimator.add(Dropout(keep_drop)) # 隠れ層を下るごとに減らすユニット数 n_minus_of_unit = 0 if n_hidden != 0: n_minus_of_unit = n_unit / n_hidden if n_minus_of_unit == 0: n_minus_of_unit = 1 for n in range(n_hidden): estimator.add(Dense(n_unit, kernel_initializer=random_uniform(seed=0), bias_initializer='zeros')) estimator.add(PReLU()) # estimator.add(ActivityRegularization(l1=0.01, l2=0.01)) # estimator.add(BatchNormalization()) estimator.add(Dropout(keep_drop)) """次階層で使用するユニット数を減らす""" n_unit = int(round(n_unit - n_minus_of_unit)) if n_unit < 1: n_unit = 1 estimator.add(Dense(units=1, kernel_initializer=random_uniform(seed=0), bias_initializer='zeros')) # estimator.add(BatchNormalization()) estimator.add(Activation('linear')) estimator.compile(loss='mean_squared_error', optimizer='adam') return estimator
def __init__(self, state_size, action_size, seed, fc_units=256): model = Sequential() lim = 1. / np.sqrt(state_size) model.add( Dense(fc_units, activation='relu', input_shape=(state_size, ), kernel_initializer=initializers.random_uniform(minval=-lim, maxval=lim, seed=seed))) model.add( Dense(action_size, activation='tanh', input_shape=(fc_units, ), kernel_initializer=initializers.random_uniform(minval=-3e-3, maxval=3e-3, seed=seed))) self.model = model
def __init__(self, num_capsule, dim_vector, num_routing, **kwargs): self.num_capsule = num_capsule self.dim_vector = dim_vector self.num_routing = num_routing #self.kernel_initializer = initializers.get('glorot_uniform') self.kernel_initializer = initializers.random_uniform( -1, 1) # With too small weights loss will be nan super(CapsuleLayer, self).__init__(**kwargs)
def build(self, input_shape): assert len(input_shape) == 2 input_dim = input_shape[1] self.gamma_elm = self.add_weight( name='gamma_elm', shape=(1, ), initializer=initializers.random_uniform(-2, -1)) super(GaussianKernel2, self).build(input_shape) # Be sure to call this somewhere!
def build(self, input_shape): self.kernel = self.add_weight(name='kernel', shape=(self.kernel_size, input_shape[-1], self.filters), initializer=initializers.random_uniform(seed=self.seed), trainable=True) self.bias = self.add_weight(name='bias', shape=(self.filters,), initializer=initializers.Zeros(), trainable=True) super(ConvLayer, self).build(input_shape)
def build_network(self): with tf.variable_scope(self.name): self.input = tf.placeholder(tf.float32, shape=[None, *self.input_dimensions], name='inputs') self.action_gradient = tf.placeholder( tf.float32, shape=[None, self.action_numbers]) f1 = 1 / np.sqrt(self.function1_dimensions) dense1 = tf.layers.dense(self.input, units=self.function1_dimensions, kernel_initializer=random_uniform( -f1, f1), bias_initializer=random_uniform(-f1, f1)) batch1 = tf.layers.batch_normalization(dense1) layer1_activation = tf.nn.relu(batch1) f2 = 1 / np.sqrt(self.function2_dimensions) dense2 = tf.layers.dense(layer1_activation, units=self.function2_dimensions, kernel_initializer=random_uniform( -f2, f2), bias_initializer=random_uniform(-f2, f2)) batch2 = tf.layers.batch_normalization(dense2) f3 = 0.003 mu = tf.layers.dense(layer1_activation, units=self.action_numbers, activation='tanh', kernel_initializer=random_uniform(-f3, f3), bias_initializer=random_uniform(-f3, f3)) self.mu = tf.multiply(mu, self.action_bound)
def build_model(self): """Build an actor (policy) network that maps states -> actions.""" # Define input layer (states) states = layers.Input(shape=(self.state_size, ), name='states') # Add hidden layers net = layers.Dense(units=256, activation=None)(states) net = layers.BatchNormalization()(net) net = layers.Activation('relu')(net) net = layers.Dense(units=256, activation=None)(net) net = layers.BatchNormalization()(net) net = layers.Activation('relu')(net) raw_actions = layers.Dense( units=self.action_size, activation='sigmoid', kernel_initializer=initializers.random_uniform(minval=-1e-3, maxval=1e-3), kernel_regularizer=regularizers.l2(0.02), name='raw_actions')(net) # Scale [0, 1] output for each action dimension to proper range actions = layers.Lambda(lambda x: (x * self.action_range) + self.action_low, name='actions')(raw_actions) # Create Keras model self.model = models.Model(inputs=states, outputs=actions) # Define loss function using action value (Q value) gradients action_gradients = layers.Input(shape=(self.action_size, )) loss = K.mean(-action_gradients * actions) # Incorporate any additional losses here (e.g. from regularizers) # Define optimizer and training function optimizer = optimizers.Adam(lr=0.00001) updates_op = optimizer.get_updates(params=self.model.trainable_weights, loss=loss) self.train_fn = K.function( inputs=[self.model.input, action_gradients, K.learning_phase()], outputs=[], updates=updates_op)
def agent_init(): optimizer_map = { 'Adam': Adam(lr=a_globs.ALPHA), 'RMSprop': RMSprop(lr=a_globs.ALPHA), 'Adagrad': Adagrad(lr=a_globs.ALPHA), 'SGD': SGD(lr=a_globs.ALPHA) } initializer_map = { 'random': random_uniform(), 'glorot': glorot_normal(), 'he': he_normal() } a_globs.cur_epsilon = a_globs.EPSILON #The main buffer contains all of the sub buffers used to store different types of states, to support biased sampling a_globs.generic_buffer = [] a_globs.buffer_container = [a_globs.generic_buffer] #Initialize the neural network a_globs.model = Sequential() init_weights = initializer_map[a_globs.INIT] a_globs.model.add( Dense(a_globs.NUM_NERONS_LAYER_1, activation='relu', kernel_initializer=init_weights, input_shape=(a_globs.FEATURE_VECTOR_SIZE, ))) a_globs.model.add( Dense(a_globs.NUM_NERONS_LAYER_2, activation='relu', kernel_initializer=init_weights)) a_globs.model.add( Dense(a_globs.NUM_ACTIONS, activation='linear', kernel_initializer=init_weights)) a_globs.model.compile(loss='mse', optimizer=optimizer_map[a_globs.OPTIMIZER]) summarize_model(a_globs.model, a_globs.AGENT) #Create the target network a_globs.target_network = clone_model(a_globs.model) a_globs.target_network.set_weights(a_globs.model.get_weights())
def build_model(self): """Build a critic (value) network that maps (state, action) pairs -> Q-values.""" # Define input layers states = layers.Input(shape=(self.state_size, ), name='states') actions = layers.Input(shape=(self.action_size, ), name='actions') # Add hidden layer(s) for state pathway net_states = layers.Dense(units=32, activation='relu')(states) net_states = layers.Dense(units=64, activation='relu')(net_states) # Add hidden layer(s) for action pathway net_actions = layers.Dense(units=32, activation='relu')(actions) net_actions = layers.Dense(units=64, activation='relu')(net_actions) # Try different layer sizes, activations, add batch normalization, regularizers, etc. # Combine state and action pathways net = layers.Add()([net_states, net_actions]) net = layers.Activation('relu')(net) # Add more layers to the combined network if needed net = layers.normalization.BatchNormalization()(net) # Add final output layer to prduce action values (Q values) Q_values = layers.Dense(units=1, name='q_values', kernel_initializer=initializers.random_uniform( minval=-0.0003, maxval=0.0003))(net) # Create Keras model self.model = models.Model(inputs=[states, actions], outputs=Q_values) # Define optimizer and compile model for training with built-in loss function optimizer = optimizers.Adam(lr=0.001) self.model.compile(optimizer=optimizer, loss='mse') # Compute action gradients (derivative of Q values w.r.t. to actions) action_gradients = K.gradients(Q_values, actions) # Define an additional function to fetch action gradients (to be used by actor model) self.get_action_gradients = K.function( inputs=[*self.model.input, K.learning_phase()], outputs=action_gradients)
def __init__(self, filters, kernel_size, sum_constant=1.0, max_constant=0.5, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=False, kernel_initializer=initializers.random_uniform(minval=0.0, maxval=1.0), bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.01), bias_regularizer=None, activity_regularizer=None, bias_constraint=None, **kwargs): super(Cooc2D, self).__init__(rank=2, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, kernel_constraint=SumNorm(sum_const=sum_constant, max_const=max_constant, axis=[0, 1, 2]), bias_constraint=bias_constraint, **kwargs) self.input_spec = InputSpec(ndim=4)
def build_model(self): #Input layers states = layers.Input(shape=(self.state_size, ), name='states') actions = layers.Input(shape=(self.action_size, ), name='actions') #Hidden layers for states h_states = layers.Dense(units=32, activation='relu')(states) h_states = layers.Dense(units=64, activation='relu')(h_states) #Hidden layers for actions h_actions = layers.Dense(units=32, activation='relu')(actions) h_actions = layers.Dense(units=64, activation='relu')(h_actions) #Combine states and actions network = layers.Add()([h_states, h_actions]) network = layers.Activation('relu')(network) #Add batch normlization layer network = layers.normalization.BatchNormalization()(network) #Add output layer to produce action values Q_values = layers.Dense(units=1, name='q_values', kernel_initializer=initializers.random_uniform( minval=-0.0005, maxval=0.0005))(network) #Create Model self.model = models.Model(inputs=[states, actions], outputs=Q_values) #Compile Model self.model.compile(optimizer=optimizers.Adam(lr=0.001), loss='mse') #compute action gradients action_gradients = kb.gradients(Q_values, actions) self.get_action_gradients = kb.function( inputs=[*self.model.input, kb.learning_phase()], outputs=action_gradients)
def build_network(self): with tf.variable_scope(self.name): self.input = tf.placeholder(tf.float32, shape=[None, *self.input_dimensions], name='inputs') self.actions = tf.placeholder(tf.float32, shape=[None, self.action_numbers], name='actions') self.q_target = tf.placeholder(tf.float32, shape=[None, 1], name='targets') f1 = 1 / np.sqrt(self.function1_dimensions) dense1 = tf.layers.dense(self.input, units=self.function1_dimensions, kernel_initializer=random_uniform( -f1, f1), bias_initializer=random_uniform(-f1, f1)) batch1 = tf.layers.batch_normalization(dense1) layer1_activation = tf.nn.relu(batch1) f2 = 1 / np.sqrt(self.function2_dimensions) dense2 = tf.layers.dense(layer1_activation, units=self.function2_dimensions, kernel_initializer=random_uniform( -f2, f2), bias_initializer=random_uniform(-f2, f2)) batch2 = tf.layers.batch_normalization(dense2) action_in = tf.layers.dense(self.actions, units=self.function2_dimensions, activation='relu') state_actions = tf.add(batch2, action_in) state_actions = tf.nn.relu(state_actions) f3 = 0.003 self.q = tf.layers.dense( state_actions, units=1, kernal_initializer=random_uniform(-f3, f3), bias_initializer=random_uniform(-f3, f3), kernel_regularizer=tf.keras.regularizers.l2(0.01)) self.loss = tf.losses.mean_squared_error(self.q_target, self.q)
def create_model(optimizer=Adam(lr=0.01), kernel_initializer=random_uniform(seed=12), dropout=0.1, TIME_PERIODS=24, num_channels=65): model = Sequential() model.add( Conv1D(32, 3, activation='relu', input_shape=(TIME_PERIODS, num_channels), padding='same', strides=1)) model.add(Conv1D(32, 3, activation='relu', padding='same', strides=1)) model.add(Flatten()) model.add(Dropout(0.1)) model.add( Dense(16, activation='relu', kernel_initializer=kernel_initializer)) model.add(Dropout(dropout)) model.add(Dense(1, kernel_initializer=kernel_initializer)) model.compile(loss=huber_loss, optimizer=optimizer, metrics=['mae']) return model
def build_model(X, y, width, depth, epochs, select): model = Sequential() if select is 0: activ = 'relu' init = initializers.he_normal(seed=None) elif select is 1: activ = 'tanh' init = initializers.glorot_normal(seed=None) elif select is 2: activ = 'sigmoid' init = initializers.random_uniform(seed=None) model.add(Dense(width, input_dim=X.shape[1], activation=activ, kernel_initializer=init)) for i in range(1, depth): model.add(Dense(width, activation=activ, kernel_initializer=init)) model.add(Dense(1, activation='sigmoid', kernel_initializer=init)) model.compile(optimizer='adam', loss = 'binary_crossentropy', metrics = ['accuracy']) return model.fit(X, y, epochs=epochs), model
def train_tiny_imagenet(hardware='cpu', batch_size=100, num_epochs=25, num_classes=10, lr=0.001, decay=0.00, wnids='', resize='False', load='', normalize='False'): # Load data x_train, y_train, x_val, y_val, wnids_path = process_images(wnids, resize, num_classes, normalize) # Choose seleted hardware, default to CPU if hardware == 'gpu': devices = ['/gpu:0'] elif hardware == '2gpu': devices = ['/gpu:0', '/gpu:1'] else: devices = ['/cpu:0'] # Run on chosen processors for d in devices: with tf.device(d): # Load saved model and check its accuracy if optional arg passed if load != '': model = load_model(load) # Run validation set through loaded network score = model.evaluate(x_val, y_val, batch_size=batch_size) #print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100)) return str(score[1]*100) # Otherwise train new network else: model = Sequential() """Block 1""" model.add(Conv2D(32, (5, 5), strides=(1,1), padding='same', kernel_initializer=initializers.random_uniform(minval=-0.01, maxval=0.01), bias_initializer='zeros', input_shape=x_train.shape[1:])) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')) model.add(Activation('relu')) """Block 2""" model.add(Conv2D(32, (5, 5), strides=(1,1), padding='same', kernel_initializer=initializers.random_uniform(minval=-0.05, maxval=0.05), bias_initializer='zeros')) model.add(Activation('relu')) model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')) """Block 3""" model.add(Conv2D(64, (5, 5), strides=(1,1), padding='same', kernel_initializer=initializers.random_uniform(minval=-0.05, maxval=0.05), bias_initializer='zeros')) model.add(Activation('relu')) model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')) """Fully Connected Layer and ReLU""" model.add(Flatten()) model.add(Activation('relu')) """Output Layer""" model.add(Dense(num_classes, kernel_initializer=initializers.random_uniform(minval=-0.05, maxval=0.05), bias_initializer='zeros')) """Loss Layer""" model.add(Activation('softmax')) """Optimizer""" model.compile(loss=losses.categorical_crossentropy, optimizer=optimizers.adam(lr=lr, decay=decay), metrics=['accuracy']) # check model checkpointing callback which saves only the "best" network according to the 'criteria' optional argument sets_index = wnids_path.find('sets') outpath = os.path.join(train_path, wnids_path[sets_index:]) # Naming file by hyperparameters resize = resize.title() normalize = normalize.title() prefix = '%d_%d_%d_%.5f_%.2f_%s_%s_best_%s_' % (batch_size, num_epochs, num_classes, lr, decay, resize, normalize, criteria) model_outfile = os.path.join(outpath, prefix + 'model.hdf5') csv_outfile = os.path.join(outpath, prefix + 'log.csv') if not os.path.exists(outpath): os.makedirs(outpath) # Save network state from the best <criteria> of all epochs ran model_checkpoint = ModelCheckpoint(model_outfile, monitor=criteria, save_best_only=True) # Log information from each epoch to a csv file logger = CSVLogger(csv_outfile) callback_list = [model_checkpoint, logger] if not data_augmentation: print('Not using data augmentation.') # Use the defined 'model_checkpoint' callback model.fit(x_train, y_train, batch_size=batch_size, epochs=num_epochs, validation_data=(x_val, y_val), shuffle=True, callbacks=callback_list) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for feature-wise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). # Use the defined 'model_checkpoint' callback model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=num_epochs, validation_data=(x_val, y_val), callbacks=callbacks) return 'New network trained!'
from keras import optimizers from keras import initializers import numpy as np from numpy import genfromtxt import matplotlib.pyplot as plt import math number_input = 64 number_classes = 10 hidden_layers = 1 index_layer = 0 neurons_hidden = 53 funct_activation = 'relu' funct_activation_output = 'softmax' initializer_kernel = initializers.random_uniform() initializer_bias = 'ones' learning_rate = 0.005 loss_function = 'categorical_crossentropy' net_metrics = ['accuracy'] epochs_number = 4 validation_split = 3 ##################READ DATABASE - TRAIN##################### train_read = genfromtxt('dataset/train.csv', delimiter=',') tmp_train_data = np.array([]) tmp_train_label = np.array([]) for i in range(0, train_read.shape[0]): last = train_read[i][-1]
initializers.RandomNormal(0.1), dict(class_name="random_normal", mean=0.1, stddev=0.05, seed=None), id="rn_0", ), pytest.param( initializers.random_normal(mean=0.2, stddev=0.003, seed=42), dict(class_name="random_normal", mean=0.2, stddev=0.003, seed=42), id="rn_1", ), pytest.param( initializers.RandomUniform(maxval=0.1), dict(class_name="random_uniform", minval=-0.05, maxval=0.1, seed=None), id="ru_0", ), pytest.param( initializers.random_uniform(minval=-0.2, seed=42), dict(class_name="random_uniform", minval=-0.2, maxval=0.05, seed=42), id="ru_1", ), pytest.param( initializers.TruncatedNormal(0.1), dict(class_name="truncated_normal", mean=0.1, stddev=0.05, seed=None), id="tn_0", ), pytest.param( initializers.truncated_normal(mean=0.2, stddev=0.003, seed=42), dict(class_name="truncated_normal", mean=0.2, stddev=0.003, seed=42), id="tn_1", ), pytest.param( initializers.Orthogonal(1.1),
# Feature Scaling sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) # Reshape data in 3 dimensions (height width, canal = 1) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1) #print("X_train shape:", X_train.shape[0:3]) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) #print("X_test shape:", X_test.shape[0:3]) #hyperparameters input_dimension = 30 learning_rate = 0.0025 momentum = 0.85 hidden_initializer = random_uniform(seed=SEED) dropout_rate = 0.15 kernel_initializer = 'uniform' kernel_regularizer = regularizers.l2(0.0001) # define path to save model model_path = 'fm_cnn_BN.h5' #CNN modele defini dans l'article def create_model(): conv = Sequential() conv.add( Conv1D(nb_filter=15, filter_length=3,
from keras.layers import Input, Dense, Lambda, concatenate from keras.constraints import non_neg, unit_norm from keras.models import Model from keras.initializers import random_uniform RND_UNI = random_uniform(minval=0.05, maxval=1.05) class EAR(object): def __init__(self, args): super(EAR, self).__init__() self.look_back = args['look_back'] def make_model(self): x = Input(shape=(self.look_back, )) ar_output = Dense(units=1, kernel_initializer='uniform', kernel_constraint=unit_norm(), name='ar-weights')(x) pre_point = Lambda(lambda k: k[:, -1:])(x) merged_output = concatenate([ar_output, pre_point]) outputs = Dense(units=1, kernel_initializer=RND_UNI, use_bias=False, kernel_constraint=non_neg(), name='contrib-weights')(merged_output)
def get_initializer(self, min_val, max_val, inputs=10): if Config.WEIGHT_INITIALIZER == "uniform": return random_uniform(min_val, max_val) if Config.WEIGHT_INITIALIZER == "normal": return random_normal(min_val, 1 / np.sqrt(inputs)) # Stddev = 1/sqrt(inputs)
def create_model(): initializer = initializers.random_uniform(minval=0.00, maxval=1.00, seed=None) model = Sequential() model.add( Conv2D(filters=32, kernel_size=(2, 2), padding='same', input_shape=(1, 25, 25), kernel_initializer=initializer, bias_initializer='zero', kernel_regularizer=regularizers.l2(0.001), activity_regularizer=regularizers.l2(0.001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add( Conv2D(filters=32, kernel_size=(2, 2), padding='same', kernel_regularizer=regularizers.l2(0.001), activity_regularizer=regularizers.l2(0.001))) model.add(BatchNormalization()) model.add(Activation('relu')) # model.add(Dropout(0.25)) # model.add(MaxPooling2D((2, 2), padding='same')) # # model.add(Conv2D(filters=32, kernel_size=(2, 2), padding='same', # kernel_regularizer=regularizers.l2(0.001), # activity_regularizer=regularizers.l2(0.001) # )) # model.add(BatchNormalization()) # model.add(Activation('relu')) # # model.add(Conv2D(filters=32, kernel_size=(2, 2), padding='same', # kernel_regularizer=regularizers.l2(0.001), # activity_regularizer=regularizers.l2(0.001) # )) # model.add(BatchNormalization()) # model.add(Activation('relu')) # # model.add(Dropout(0.25)) # # model.add(MaxPooling2D((2, 2), padding='same')) model.add(Flatten()) # model.add(Dropout(0.25)) model.add( Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.001), activity_regularizer=regularizers.l2(0.001))) # model.add(Dropout(0.25)) model.add( Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.001), activity_regularizer=regularizers.l2(0.001))) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer=optimizers.adam(lr=0.001), metrics=['accuracy']) return model
def __init__(self, model=None, valorDama=None, listaSigmas=None, geracao=0, genealogia=[], debug=False): self.listaSigmas = [] listaWeights = [] self.currentPoints = 0 self.totalPoints = 0 self.numeroDeGeracoesVivo = 1 self.geracao = geracao if (valorDama is None): self.valorDama = 2.0 else: self.valorDama = valorDama if (model is None): initializer = initializers.random_uniform(minval=(-1) * Jogador.peakVal, maxval=Jogador.peakVal) self.model = Sequential() self.model.add( Dense(40, input_dim=32, kernel_initializer=initializer, activation='tanh')) # self.model.add (Dense (40, kernel_initializer=initializer, activation = 'tanh')) self.model.add( Dense(10, kernel_initializer=initializer, activation='tanh')) self.model.add( Dense(1, kernel_initializer=initializer, activation='tanh')) self.model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) self.model._make_predict_function() else: self.model = model self.model._make_predict_function() if (listaSigmas is None): for layerIndex in range(self.numeroDeLayers): layer = self.model.get_layer(index=layerIndex) layerWeights = layer.get_weights() arrayWeights = np.zeros( (layerWeights[0].shape[0], layerWeights[0].shape[1])) + self.sigma arrayBiases = np.zeros(layerWeights[1].shape[0]) + self.sigma listaWeights.clear() listaWeights.append(arrayWeights) listaWeights.append(arrayBiases) self.listaSigmas.append(copy.deepcopy(listaWeights)) else: self.listaSigmas = listaSigmas self.nomeJogador = "Jogador_" + str(uuid.uuid4()) + ".h5" self.genealogia = copy.deepcopy(genealogia) listaNomeJogador = [] listaNomeJogador.append(self.nomeJogador) listaGeracaoJogador = [] listaGeracaoJogador.append(self.geracao) listaGeral = [] listaGeral.append(listaNomeJogador) listaGeral.append(listaGeracaoJogador) self.genealogia.append(listaGeral) self.debug = debug
def keras_cnn_1d(self, data): """ Description: setup Keras hyper_parameters, Kernel_initializer 's parameter and find regression_value for (multiple) label(s) :param data: pandas.core.frame.DataFrame :return: Reference: https://keras.io/optimizers/ https://keras.io/initializers/ https://keras.io/layers/core/#dropout :return: """ # Convert to the right dimension row, column, 1-channel in Numpy array X_data = data.drop(self.dependent_label, axis=1) Y_data = data[self.dependent_label] # Normalizing data scaler = MinMaxScaler() scaler.fit(X_data) X_data = pd.DataFrame(data=scaler.transform(X_data), columns=X_data.columns, index=X_data.index) # Split data into training_data and evaluation_data x_train, x_test, y_train, y_test = train_test_split(X_data, Y_data, test_size=0.2, random_state=101) # Number of feature columns in training_data input_dimension = len(x_train.columns) # input_dimension = self.input_units x_train = x_train.values.reshape(x_train.shape[0], x_train.shape[1], 1) y_train = y_train.as_matrix() # Init hyper parameter np.random.seed(self.seed) hidden_initializer = random_uniform(seed=self.seed) # self.n_filters = 32 # reset number of filters (default is 32) # Create model and add hidden layers model = Sequential() # Conv1D needs 2 dimension -> input_shape=(input_dimension, 1), similarly Conv2D needs 3 dim, Conv3D needs 4 dim # Output dim has the same size as Input dim(default is 'same') model.add(Conv1D(input_shape=(input_dimension, 1), activation=self.activation_fn, filters=self.n_filters, kernel_size=self.kernel_size, padding='same')) model.add(Conv1D(activation=self.activation_fn, filters=self.n_filters, kernel_size=self.kernel_size)) # Flattens the input model.add(Flatten()) # Regularization technique to prevent Neural Networks from Overfitting model.add(Dropout(rate=self.drop_out_rate)) # Use output of CNN as input of ANN, e.g. units = np.array([1000, 500, 250, 3]) model.add(Dense(units=self.input_units, input_dim=input_dimension, kernel_initializer=hidden_initializer, activation=self.activation_fn)) # Hidden layers, number of layers and neurons in every layers (in numpy array e.g. [1000, 500, 250, 3]) for unit in self.hidden_units: model.add(Dense(units=unit, kernel_initializer=hidden_initializer, activation=self.activation_fn)) # Output signal = output_units # output_activation_fn = 'softmax', no need activation in this case? model.add(Dense(units=self.output_units, kernel_initializer=hidden_initializer)) # reset optimizer sgd = SGD(lr=self.learning_rate, momentum=self.momentum, decay=self.decay_rate) self.optimizer = sgd # compile and train model with training_data model.compile(loss=self.loss, optimizer=self.optimizer, metrics=['accuracy']) model.fit(x_train, y_train, epochs=self.nr_epochs, batch_size=self.batch_size) # Evaluate the model on the training and testing set scores = model.evaluate(x_train, y_train, verbose=0) print("\nTraining Accuracy: Model %s: Scores: %.2f%%" % (model.metrics_names[1], scores[1] * 100)) scores = model.evaluate(x_test, y_test, verbose=0) print("\nTesting Accuracy: Model %s: Scores: %.2f%%" % (model.metrics_names[1], scores[1] * 100)) # Predict value y_pred = pd.DataFrame(data=model.predict(x_test.values.reshape(x_test.shape[0], x_test.shape[1], 1)), columns=y_test.columns) return model, y_test, y_pred
def create_actor_network(self, state_size, action_size): print('now we build the actor model') # left_branch = Sequential() # left_branch.add(Dense(300,input_dim=state_size,activation='relu')) # # right_branch = Sequential() # right_branch.add(Dense(300,input_dim=fenli_size,activation='relu')) # # merged = Merge([left_branch,right_branch],mode='concat') # # model = Sequential() # model.add(merged) # model.add(Dense(300,activation='relu')) # model.add(BatchNormalization()) # model.add(Dropout(0.5)) # model.add(Dense(action_size,activation='softmax')) # adam = Adam(lr=self.LEARNING_RATE) # model.compile(loss='mse',optimizer=adam) # model = Sequential() # model.add(Dense(600, input_dim=state_size, init='uniform')) # model.add(Activation('sigmoid')) # model.add(Dense(300,init='uniform')) # model.add(Activation('relu')) # model.add(Dense(action_size, init='uniform')) # model.add(Activation('sigmoid')) # S = Input(shape=[state_size]) # h0 = Dense(16, init='uniform', activation='tanh')(S) # h1 = Dense(16, init='uniform', activation='tanh')(h0) # h2 = Dense(16, init='uniform', activation='tanh')(h1) # V = Dense(action_size,activation='sigmoid')(h2) # model = Model(input=S,output=V) # S = Input(shape=[state_size]) # # x = Embedding(input_dim=state_size,output_dim=1024,input_length=257,init='normal')(S) # x1 = LSTM(output_dim=512,input_shape=(4,257), activation='tanh')(S) # x2 = LSTM(output_dim=512,activation='sigmoid',init='normal')(x1) # x3 = Dense(action_size,activation='sigmoid')(x2) # model = Model(input=S, output=x3) S = Input(shape=[3, state_size // 3]) bl1 = Bidirectional( LSTM(100, return_sequences=True, input_shape=(3, 257), kernel_initializer=initializers.random_uniform(minval=-0.5, maxval=0)))(S) # bl1 = Bidirectional(LSTM(100, return_sequences=True, input_shape=(3, 257), kernel_initializer='lecun_uniform'))(S) # kernel_initializer='random_uniform' bl2 = Bidirectional( LSTM(100, return_sequences=False, input_shape=(3, 257), kernel_initializer=initializers.random_uniform( minval=0, maxval=0.5)))(bl1) # bl2 = Bidirectional(LSTM(100, return_sequences=False, input_shape=(3, 257), kernel_initializer='lecun_uniform'))(bl1) # bl2 = BatchNormalization(beta_initializer='random_uniform', # gamma_initializer='random_uniform', # moving_mean_initializer='random_uniform', # moving_variance_initializer='random_uniform')(bl2) # output_mask_layer = Dense(action_size, kernel_initializer='lecun_uniform')(bl2) output_mask_layer = Dense( action_size, kernel_initializer=initializers.random_normal(-1, 0.5))(bl2) # kernel_initializer=initializers.random_normal(stddev=0.05) # output_mask_layer = BatchNormalization()(output_mask_layer) output_mask_layer = Activation('sigmoid')(output_mask_layer) model = Model(input=S, output=output_mask_layer) return model, model.trainable_weights, S
def keras_sequential(self, data, output_signals=1): """ Description: setup Keras and run the Sequential method to predict value References: https://keras.io/getting-started/sequential-model-guide/ https://stackoverflow.com/questions/44747343/keras-input-explanation-input-shape-units-batch-size-dim-etc# :param data: pandas.core.frame.DataFrame :param output_signals: default 1 (when there's only 1 categorical column) :return: Keras-Sequential object, the actual (true) value, the predicted value """ # split data x_train, x_test, y_train, y_test = StartMod.split_data(data, dependent_label=self.dependent_label) # Initialising the ANN model = Sequential() # model = models.Sequential() # tbd: use parameter tuning to find the exact number of nodes in the hidden-layer # Number of layers and neurons in every layers (in numpy array e.g. [1000, 500, 250, 3]) hidden_units = self.hidden_units # Init hyper parameter # np.random.seed(10) hidden_initializer = random_uniform(seed=self.seed) # initializers.RandomUniform(seed=self.seed) input_dimension = self.input_units # default = number of features, input_signals = x_train.shape[1] = hidden_units[0] # Adding the input layer and the first hidden layer, activation function as rectifier function model.add(Dense(units=self.input_units, input_dim=input_dimension, kernel_initializer=hidden_initializer, activation=self.activation_fn, bias_initializer=self.bias_initializer)) # Adding the second hidden layer, activation function as rectifier function # print(hidden_units) for i in range(len(hidden_units)): # print(i, hidden_units[i]) model.add(Dense(activation=self.activation_fn, units=hidden_units[i], kernel_initializer=hidden_initializer)) # Adding the output layer (in case of there's only one dependent_label), # n_classes = 2 (binary), then activation function = sigmoid # n_classes > 2 (not binary), then activation function = softmax if self.n_classes == 2: output_activation = "sigmoid" else: output_activation = "softmax" model.add(Dense(units=self.output_units, kernel_initializer=hidden_initializer, activation=output_activation)) # Compiling the ANN: optional optimizer='adam', loss='categorical_crossentropy' model.compile(optimizer=self.optimizer, loss=self.loss, metrics=['accuracy']) # Fit the keras_model to the training_data and see the real time training of model on data with result of loss # and accuracy. The smaller batch_size and higher epochs, the better the result. However, slow_computing! print(x_train.shape, y_train.shape) model.fit(x_train, y_train, validation_split=0.3, batch_size=self.batch_size, epochs=self.nr_epochs) # Predictions and evaluating the model y_pred = model.predict(x_test) # Evaluate the model scores, accuracy = model.evaluate(x_train, y_train) print("\nModel %s: Scores: %.2f%%, Accuracy: %.2f%%" % (model, scores*100, accuracy*100)) # make class predictions with the model predictions = model.predict_classes(x_test) # summarize the first 5 cases for i in range(5): print('%s => %d (expected %d)' % (x_test[i].tolist(), predictions[i], y_test[i])) return model, y_test, y_pred