def KerasCost(self, y_true, y_pred): # create a random subsampling of the target instances for the test set # This is rarely going to hit the last entry sample = K.cast( K.round( K.random_uniform_variable( shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetTrainSize - 1)), IntType) # this is a subset operation (not a very pretty way to do it) MMDTargetSampleTrain = K.gather(self.MMDTargetTrain, sample) # do the same for the validation set sample = K.cast( K.round( K.random_uniform_variable( shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetValidationSize - 1)), IntType) # and the subset operation MMDTargetSampleValidation = K.gather(self.MMDTargetValidation, sample) # create the sample based on whether we are in training or validation steps MMDtargetSample = K.in_train_phase(MMDTargetSampleTrain, MMDTargetSampleValidation) # return the MMD cost for this subset ret = self.cost(self.MMDLayer, MMDtargetSample) # pretty dumb but y_treu has to be in the cost for keras to not barf when cleaning up ret = ret + 0 * K.sum(y_pred) + 0 * K.sum(y_true) return ret
def KerasCost(self, y_true, y_pred): # y_true.shape == y_pred.shape but y_true is labels # so we only get the first column source_labels = y_true[:, 0] # get target train sample sample = K.cast( K.round( K.random_uniform_variable( shape=tuple([self.target_sample_size]), low=0, high=self.target_train_size - 1)), IntType) target_label_sample = K.gather(self.target_train_labels, sample) target_sample = K.gather(self.target_train, sample) # get target validation sample val_sample = K.cast( K.round( K.random_uniform_variable( shape=tuple([self.target_sample_size]), low=0, high=self.target_validation_size - 1)), IntType) target_val_label_sample = K.gather(self.target_validation_labels, val_sample) target_val_sample = K.gather(self.target_validation, val_sample) target = [] source = [] # TODO: change to handle num_labels for i in range(3): # split target train sample by label ts = K.gather( target_sample, K.squeeze(K.tf.where(K.tf.equal(target_label_sample, i)), axis=-1)) # split target validation sample by label tvs = K.gather( target_val_sample, K.squeeze(K.tf.where(K.tf.equal(target_val_label_sample, i)), axis=-1)) # add to target depending on if train or validation phase target.append(K.in_train_phase(ts, tvs)) # split the source by label source.append( K.gather( self.output_layer, K.squeeze(K.tf.where(K.tf.equal(source_labels, i)), axis=-1))) # calculate the costs between each set of labels in the source and target costs = [] for i in range(3): costs.append(self.cost(source[i], target[i])) # keras does not like it is you don't use y_ ret = K.sum(costs) + 0 * K.sum(y_pred) + 0 * K.sum(y_true) return ret
def cal_df_gp(): def cal_gp(gradients): gradients_sqr = K.square(gradients[0]) gradients_sqr_sum = K.sum(gradients_sqr, axis=np.arange( 1, len(gradients_sqr.shape))) gradient_l2_norm = K.sqrt(gradients_sqr_sum) gradient_penalty = K.mean(K.square(1 - gradient_l2_norm)) return gradient_penalty alpha = K.random_uniform_variable(shape=(1, ), low=0, high=1) mix_tar = alpha * self.img_a + (1 - alpha) * self.img_a2b mix_outputs_a2b = self.d_model( [self.img_a, mix_tar, self.vec_ab_pos]) mix_outputs_a2ab = self.d_model( [self.img_a, self.img_a2ab, self.vec_ab_pos]) gradients_a2b = K.gradients([mix_outputs_a2b[0]], [mix_tar]) gradients_a2ab = K.gradients([mix_outputs_a2ab[2]], [self.img_a2ab]) df_gp = cal_gp(gradients_a2b) + cal_gp(gradients_a2ab) return df_gp
def build(self, input_shape): # m2m_shape =input_shape[0] n_in = input_shape[2] n_out = 1 lim = np.sqrt(6. / (n_in + n_out)) # tanh initializer xavier self.W = K.random_uniform_variable((n_in, n_out), -lim, lim, name='{}_W'.format(self.name)) self.b = K.zeros((n_out, ), name='{}_b'.format(self.name)) self.trainable_weights = [self.W, self.b] self.regularizer = [] if self.W_regularizer is not None: self.add_loss(self.W_regularizer(self.W)) if self.b_regularizer is not None: self.add_loss(self.b_regularizer(self.b)) self.build = True
def ret(pi): gumbel_softmax_arg = (pi - K.log(-K.log(K.random_uniform_variable( shape, 0., 1.))+K.epsilon()))/tau y = K.softmax(K.reshape(gumbel_softmax_arg, (-1,) + shape)) return y
def call(self, x): inp = x kernel = K.random_uniform_variable(shape=(self.kernel_size[0], self.kernel_size[1], self.out_shape[-1], int(x.get_shape()[-1])), low=0, high=1) deconv = K.conv2d_transpose(x, kernel=kernel, strides=self.strides, output_shape=self.out_shape, padding='same') biases = K.zeros(shape=(self.out_shape[-1])) deconv = K.reshape(K.bias_add(deconv, biases), deconv.get_shape()) deconv = LeakyReLU()(deconv) g = K.conv2d_transpose(inp, kernel, output_shape=self.out_shape, strides=self.strides, padding='same') biases2 = K.zeros(shape=(self.out_shape[-1])) g = K.reshape(K.bias_add(g, biases2), deconv.get_shape()) g = K.sigmoid(g) deconv = tf.multiply(deconv, g) outputs = [deconv, g] output_shapes = self.compute_output_shape(x.shape) for output, shape in zip(outputs, output_shapes): output._keras_shape = shape return [deconv, g]
def gan_loss(self, d_logit_real, d_logit_fake): """ define loss function """ d_target_real = K.ones_like(d_logit_real) d_target_fake = K.zeros_like(d_logit_fake) if self.label_flipping > 0: # some idx replace to zeros flip_val = K.random_binomial(K.get_variable_shape(d_logit_real), p=self.label_flipping) d_target_real -= flip_val # some idx replace t0 ones flip_val = K.random_binomial(K.get_variable_shape(d_logit_fake), p=self.label_flipping) d_target_fake += flip_val #if self.oneside_smooth is True: if self.oneside_smooth: # some idx replace 0.9-1.0 smooth_val = K.random_uniform_variable( K.get_variable_shape(d_logit_real), low=0.9, high=0.99) d_target_real *= smooth_val # some idx replace 0.9-1.0 (When label_flipping = 0, no processing ) smooth_val = K.random_uniform_variable( K.get_variable_shape(d_logit_fake), low=0.9, high=0.99) d_target_fake *= smooth_val d_loss_real = K.mean(K.binary_crossentropy(output=d_logit_real, target=d_target_real, from_logits=True), axis=1) d_loss_fake = K.mean(K.binary_crossentropy(output=d_logit_fake, target=d_target_fake, from_logits=True), axis=1) d_loss = K.mean(d_loss_real + d_loss_fake) g_loss = K.mean( K.binary_crossentropy(output=d_logit_fake, target=K.ones_like(d_logit_fake), from_logits=True)) return d_loss, g_loss
def create_maxatt_matching_layer(self, input_dim_a, input_dim_b): """Create a max-attentive-matching layer of a model.""" inp_a = Input(shape=( input_dim_a, self.hidden_dim, )) inp_b = Input(shape=( input_dim_b, self.hidden_dim, )) W = [] for i in range(self.perspective_num): wi = K.random_uniform_variable( (1, self.hidden_dim), -1.0, 1.0, seed=self.seed if self.seed is not None else 243) W.append(wi) outp_a = Lambda(lambda x: K.l2_normalize(x, -1))(inp_a) outp_b = Lambda(lambda x: K.l2_normalize(x, -1))(inp_b) outp_b = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))(outp_b) alpha = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))( [outp_b, outp_a]) alpha = Lambda(lambda x: K.one_hot(K.argmax(x, 1), self. max_sequence_length))(alpha) hmax = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))( [alpha, outp_b]) m = [] for i in range(self.perspective_num): outp_a = Lambda(lambda x: x * W[i])(inp_a) outp_hmax = Lambda(lambda x: x * W[i])(hmax) outp_a = Lambda(lambda x: K.l2_normalize(x, -1))(outp_a) outp_hmax = Lambda(lambda x: K.l2_normalize(x, -1))(outp_hmax) outp_hmax = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))( outp_hmax) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))( [outp_hmax, outp_a]) val = np.eye(self.max_sequence_length) kcon = K.constant(value=val, dtype='float32') outp = Lambda(lambda x: K.sum(x * kcon, -1, keepdims=True))(outp) m.append(outp) if self.perspective_num > 1: persp = Lambda(lambda x: K.concatenate(x, 2))(m) else: persp = m model = Model(inputs=[inp_a, inp_b], outputs=persp) return model
def build(self, input_shape): self.dim = input_shape[1] self.nb_channel = input_shape[2] if self.mult: self.W_poly_shape = (self.nb_channel, self.nb_polynomial_order) else: self.W_poly_shape = (self.nb_polynomial_order, ) if self.learnable: self.W_poly = K.random_uniform_variable(self.W_poly_shape, -1 * self.my_init, 1 * self.my_init, name='{}_W_poly'.format( self.name)) self.trainable_weights = [self.W_poly] else: self.W_poly = K.random_uniform_variable(self.W_poly_shape, 1, 1, name='{}_W_poly'.format( self.name)) self.trainable_weights = [] self.built = True
def build(self, input_shape): if self.W is None: self.W = K.variable(np.identity(input_shape[0][2])) elif isinstance(self.W, np.ndarray): self.W = K.variable(self.W) else: raise RuntimeError() if self.b is None: self.b = K.random_uniform_variable((input_shape[0][2],), -0.05, 0.05) elif isinstance(self.b, np.ndarray): self.b = K.variable(self.b) else: raise RuntimeError() self.trainable_weights = [self.W, self.b]
def build(self, input_shape): if self.W is None: self.W = K.variable(np.identity(input_shape[0][2])) elif isinstance(self.W, np.ndarray): self.W = K.variable(self.W) else: raise RuntimeError() if self.b is None: self.b = K.random_uniform_variable((input_shape[0][2], ), -0.05, 0.05) elif isinstance(self.b, np.ndarray): self.b = K.variable(self.b) else: raise RuntimeError() self.trainable_weights = [self.W, self.b]
def create_full_matching_layer_b(self, input_dim_a, input_dim_b): """Create a full-matching layer of a model.""" inp_a = Input(shape=( input_dim_a, self.hidden_dim, )) inp_b = Input(shape=( input_dim_b, self.hidden_dim, )) W = [] for i in range(self.perspective_num): wi = K.random_uniform_variable( (1, self.hidden_dim), -1.0, 1.0, seed=self.seed if self.seed is not None else 243) W.append(wi) val = np.concatenate((np.ones( (1, 1)), np.zeros((self.max_sequence_length - 1, 1))), axis=0) kcon = K.constant(value=val, dtype='float32') inp_b_perm = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))( inp_b) last_state = Lambda(lambda x: K.permute_dimensions( K.dot(x, kcon), (0, 2, 1)))(inp_b_perm) m = [] for i in range(self.perspective_num): outp_a = Lambda(lambda x: x * W[i])(inp_a) outp_last = Lambda(lambda x: x * W[i])(last_state) outp_a = Lambda(lambda x: K.l2_normalize(x, -1))(outp_a) outp_last = Lambda(lambda x: K.l2_normalize(x, -1))(outp_last) outp_last = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))( outp_last) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))( [outp_last, outp_a]) outp = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))(outp) m.append(outp) if self.perspective_num > 1: persp = Lambda(lambda x: K.concatenate(x, 2))(m) else: persp = m model = Model(inputs=[inp_a, inp_b], outputs=persp) return model
def call(self, x, training=None): tensorListT = [] tensorListV = [] for i in self.modality_nums: tensorListT.append( K.repeat_elements(K.cast( K.random_uniform(shape=(1, )) > self.thres, 'float32'), i, axis=0)) tensorListV.append( K.repeat_elements(K.cast( K.random_uniform_variable(shape=(1, ), low=0, high=1) > self.thres, 'float32'), i, axis=0)) maskT = x * K.concatenate(tensorListT) maskV = x * K.concatenate(tensorListV) return K.in_train_phase(maskT, maskV, training=training)
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) shapes = [K.int_shape(p) for p in params] accumulators = [K.random_uniform_variable(shape, low=-0.1, high=0.1, seed=123) for shape in shapes] self.updates = [K.update_add(self.iterations, 1)] lr = self.lr self.weights = accumulators mu = self.mu c = self.c l1 = c * K.pow(lr, 0.5 + mu) * K.pow(K.cast(self.iterations, K.floatx()), mu) for p, g, a in zip(params, grads, accumulators): new_a = a - lr * g # Gradient Step self.updates.append(K.update(a, new_a)) new_a_l1 = K.abs(new_a) - l1 new_p = tf.sign(new_a) * K.maximum(new_a_l1, K.zeros(K.int_shape(new_a))) self.updates.append(K.update(p, new_p)) return self.updates
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) shapes = [K.int_shape(p) for p in params] accumulators = [ K.random_uniform_variable(shape, low=-0.1, high=0.1, seed=1) for shape in shapes ] self.updates = [K.update_add(self.iterations, 1)] lr = self.lr mu = self.mu c = self.c l1 = c * K.pow(lr, 0.5 + mu) * K.pow( K.cast(self.iterations, K.floatx()) + 1, mu) for p, g, a in zip(params, grads, accumulators): new_a = a - lr * g self.updates.append(K.update(a, new_a)) new_p = K.softthreshold(new_a, l1) self.updates.append(K.update(p, new_p)) return self.updates
def create_maxpool_matching_layer(self, input_dim_a, input_dim_b): """Create a maxpooling-matching layer of a model.""" inp_a = Input(shape=( input_dim_a, self.hidden_dim, )) inp_b = Input(shape=( input_dim_b, self.hidden_dim, )) W = [] for i in range(self.perspective_num): wi = K.random_uniform_variable( (1, self.hidden_dim), -1.0, 1.0, seed=self.seed if self.seed is not None else 243) W.append(wi) m = [] for i in range(self.perspective_num): outp_a = Lambda(lambda x: x * W[i])(inp_a) outp_b = Lambda(lambda x: x * W[i])(inp_b) outp_a = Lambda(lambda x: K.l2_normalize(x, -1))(outp_a) outp_b = Lambda(lambda x: K.l2_normalize(x, -1))(outp_b) outp_b = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))( outp_b) outp = Lambda(lambda x: K.batch_dot(x[0], x[1], axes=[1, 2]))( [outp_b, outp_a]) outp = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))(outp) outp = Lambda(lambda x: K.max(x, -1, keepdims=True))(outp) m.append(outp) if self.perspective_num > 1: persp = Lambda(lambda x: K.concatenate(x, 2))(m) else: persp = m model = Model(inputs=[inp_a, inp_b], outputs=persp) return model
def KerasCost(self, y_true, y_pred): ret = 0 for t in range(self.num_tissues): # sample from target tissues low = self.target_train_ranges[t, 0] high = self.target_train_ranges[t, 1] sample_size = floor(self.sample_ratio * self.target_train_counts[t]) sample_train = K.cast( K.round( K.random_uniform_variable(shape=tuple([sample_size]), low=low, high=high)), IntType) sample_target_train = K.gather(self.target_train, sample_train) # select validation samples (use all) low = self.target_validate_ranges[t, 0] high = self.target_validate_ranges[t, 1] sample_validate = K.cast( np.fromiter((i for i in range(low, high)), dtype=IntType), IntType) sample_target_validate = K.gather(self.target_validate, sample_validate) source_labels = K.eval(y_true) source_index = np.where(np.isin(source_labels, t))[0] source = K.eval(self.output_layer)[source_index] # source = K.cast_to_floatx(source) target = K.in_train_phase(sample_target_train, sample_target_validate) # target = sample_target_train ret += self.cost(source, target, t) ret = ret + 0 * K.cast(K.sum(y_pred), FloatType) + 0 * K.cast( K.sum(y_true), FloatType) return ret
def call(self, logits): # logits: [batch_size, d, 1] logits_ = K.permute_dimensions(logits, (0, 2, 1)) # [batch_size, 1, d] d = int(logits_.get_shape()[2]) unif_shape = [batch_size, self.k, d] uniform = K.random_uniform_variable( shape=unif_shape, low=np.finfo(tf.float32.as_numpy_dtype).tiny, high=1.0) gumbel = -K.log(-K.log(uniform)) noisy_logits = (gumbel + logits_) / self.tau0 samples = K.softmax(noisy_logits) samples = K.max(samples, axis=1) logits = tf.reshape(logits, [-1, d]) threshold = tf.expand_dims( tf.nn.top_k(logits, self.k, sorted=True)[0][:, -1], -1) discrete_logits = tf.cast(tf.greater_equal(logits, threshold), tf.float32) output = K.in_train_phase(samples, discrete_logits) return tf.expand_dims(output, -1)
def build(self, input_shape): if self.multiple_inputs: n_in = input_shape[1][-1] else: n_in = input_shape[-1] print(n_in) #sda #n_in=n_in+1 n_out = self.attention_dim lim = np.sqrt(6. / (n_in + n_out)) W = K.random_uniform_variable((n_in, n_out), -lim, lim, name='{}_W'.format(self.name)) b = K.zeros((n_out, ), name='{}_b'.format(self.name)) self.W = W self.b = b self.v = K.random_normal_variable(shape=(n_out, 1), mean=0, scale=0.1, name='{}_v'.format(self.name)) self.trainable_weights = [self.W, self.v, self.b]
def call(self, x): m = K.random_uniform_variable((NUM_OF_NEURONS, NUM_OF_NEURONS), 0, 1, seed=1) # x = Dropout(rate=0.4)(x) # x = Dropout(rate=0.4)(x) a = layers.Dense(NUM_OF_NEURONS, activation='relu')(x) b = layers.Dense(NUM_OF_NEURONS, activation='relu')(x) b = K.permute_dimensions(b, (0, 2, 1)) # print(K.int_shape(a)) K.expand_dims(a, axis=-1) # print(K.int_shape(a)) # print('a : ', a, '\n') # print('b : ', b, '\n') # print('m : ', m, '\n\n\n') x = K.dot(a, m) x = K.batch_dot(x, b) # print('x : ', x, '\n\n\n') x = Lambda(find_softmax, output_shape=output_of_lambda)(x) # print('x : ', x, '\n\n\n') return x
def _merge_function(self, inputs): weights = K.random_uniform_variable((1, 1, 1), low=0, high=1) return ((weights * inputs[0]) + ((1 - weights) * inputs[-1]))
def init_hidden_layer(shape, name=None): return K.random_uniform_variable(shape=shape, low=-0.01/shape[0], high=0.01/shape[0], name=name)
# defining the placeholders to feed the input and target data input_tensor = K.placeholder(shape=(batch_size, input_dim), dtype='float32') target_tensor = K.placeholder(shape=(batch_size, num_classes), dtype='float32') num_units.append(num_classes) weights = [] biases = [] inp_layer_dim = input_dim # defining the weight and the bias variables for layer in range(num_layers + 1): outp_layer_dim = num_units[layer] weight_variable = K.random_uniform_variable(shape=(inp_layer_dim, outp_layer_dim), low=-1., high=1., dtype='float32') bias_variable = K.zeros(shape=(outp_layer_dim, ), dtype='float32') weights.append(weight_variable) biases.append(bias_variable) inp_layer_dim = outp_layer_dim # defining the sigmoid output tensor inp = input_tensor for layer in range(num_layers + 1): output_tensor = K.dot(inp, weights[0]) + biases[0] if layer == num_layers + 1: output_tensor = K.softmax(output_tensor)
num_units = [100, 100] # build the model here # provide the train_function which takes in the input and target, and outputs # the loss, while updating the necessary variables underneath # please provide the test_function which takes in the input and target , and # outputs a tuple of (accuracy, prediction) # >>>>> PUT YOUR CODE HERE <<<<< # defining the placeholders to feed the input and target data input_tensor = K.placeholder(shape=(batch_size, input_dim), dtype='float32') target_tensor = K.placeholder(shape=(batch_size, num_classes), dtype='float32') # defining the weight and the bias variables for input to first hidden layer weight_var_tupel = (K.random_uniform_variable(shape=(input_dim, num_units[1]), low=-1., high=1., dtype='float32'), ) bias_var_tupel = (K.zeros(shape=(num_units[1], ), dtype='float32'), ) # defining the weight and the bias variables for hidden layers for l in range(1, num_layers): weight_variable = (K.random_uniform_variable(shape=(num_units[l - 1], num_units[l]), low=-1., high=1., dtype='float32'), ) bias_variable = (K.zeros(shape=(num_units[l], ), dtype='float32'), ) weight_var_tupel += weight_variable bias_var_tupel += bias_variable # defining the weight and the bias variables for last hidden to output layer
def init_embeddings(shape, name=None): return K.random_uniform_variable(shape=shape, low=-0.01, high=0.01, name=name)
# -*- coding: utf-8 -*- """ Created on Fri Sep 14 13:10:30 2018 @author: Youssef """ from keras import backend as K inputs = K.placeholder(shape=(2, 4, 5)) # also works: inputs = K.placeholder(ndim=3) import numpy as np x = np.random.random((3, 4, 5)) y = K.variable(value=x) # Initializing Tensors with Random Numbers b = K.random_uniform_variable(shape=(3, 4), low=0, high=1) # Uniform distribution c = K.random_normal_variable(shape=(3, 4), mean=0, scale=1) # Gaussian distribution d = K.random_normal_variable(shape=(3, 4), mean=0, scale=1) a = b + c * K.abs(d) c = K.dot(a, K.transpose(b)) a = K.sum(b, axis=1) a = K.softmax(b) a = K.concatenate([b, c], axis=-1)
num_units = [100, 100] # build the model here # provide the train_function which takes in the input and target, and outputs # the loss, while updating the necessary variables underneath # please provide the test_function which takes in the input and target , and # outputs a tuple of (accuracy, prediction) # >>>>> PUT YOUR CODE HERE <<<<< input_tensor = K.placeholder(shape=(batch_size, input_dim), dtype='float32') target_tensor = K.placeholder(shape=(batch_size, 10), dtype='float32') l1_tensor = K.placeholder(shape=(num_units[0], )) l2_tensor = K.placeholder(shape=(num_units[1], )) output = K.placeholder(shape=(10, )) # TODO # define weight and bias weight_variable_1 = K.random_uniform_variable(shape=(input_dim, num_units[0]), low=-1., high=1., dtype='float32') weight_variable_2 = K.random_uniform_variable(shape=(num_units[0], num_units[1]), low=-1., high=1., dtype='float32') weight_variable_3 = K.random_uniform_variable(shape=(num_units[1], 10), low=-1., high=1., dtype='float32') bias_variable_1 = K.zeros(shape=(num_units[0], ), dtype='float32') bias_variable_2 = K.zeros(shape=(num_units[1], ), dtype='float32') bias_variable_3 = K.zeros(shape=(10, ), dtype='float32') # define sigmoid output tensor
def init_uniform(shape, lb, ub, name=None): return K.random_uniform_variable(shape, lb, ub, name=name)
lr = 0.1 # defining the number of layers and number of hidden units in each layer num_layers = 2 num_units = [100, 100] # build the model here # provide the train_function which takes in the input and target, and outputs # the loss, while updating the necessary variables underneath # please provide the test_function which takes in the input and target , and # outputs a tuple of (accuracy, prediction) # >>>>> PUT YOUR CODE HERE <<<<< input_tensor = K.placeholder(shape=(batch_size, input_dim), dtype='float32') weight_list = (K.random_uniform_variable(shape=(input_dim, num_units[0]), low=-1., high=1., dtype='float32'),) bias_list = (K.zeros(shape=(num_units[0],), dtype='float32'),) hl_tensor = K.relu(K.dot(input_tensor, weight_list[0]) + bias_list[0], alpha=0.0) for i in range(num_layers - 1): weight_tensor = (K.random_uniform_variable(shape=(num_units[i], num_units[i + 1]), low=-1., high=1., dtype='float32'),) weight_list += weight_tensor bias_tensor = (K.zeros(shape=(num_units[i + 1],), dtype='float32'),) bias_list += bias_tensor
discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=learnrate), metrics=['accuracy']) # Create GAN gan = make_gan(model, discriminator) # Train train_loss_disc = np.zeros(epochs) test_loss_disc = np.zeros(epochs) for epoch in range(epochs): # Train generator and discriminator simultaneously ## Generate samples train_synth_ones = K.eval( model(K.random_uniform_variable((train_size, latent_dimension), -1, 1))) permutation = np.random.permutation(train_size) for i in range(num_batches): start = time.time() print("Epoch " + str(epoch + 1) + "/" + str(epochs) + ": Batch " + str(i + 1) + "/" + str(num_batches)) train_mixed_ones, train_mixed_labels = grab_discriminator_samples( train_ones, batch_size, i, permutation, model) disc_mets = discriminator.train_on_batch(train_mixed_ones, train_mixed_labels) train_noise = 2 * np.random.random([batch_size, latent_dimension]) - 1 train_gan_labels = np.ones(batch_size) model_mets = gan.train_on_batch(train_noise, train_gan_labels) time_taken = time.time() - start # print("Discriminator accuracy:" +str(disc_mets[1]) + "\t Generator Fool Rate:" + str(model_mets[1]) + "\t Estimated time per epoch:" + str(int(time_taken * num_batches)) + " seconds") print(
# defining the batch size and epochs batch_size = 128 num_epochs = 40 # defining the learning rate lr = 0.1 # building the model # defining the placeholders to feed the input and target data input_tensor = K.placeholder(shape=(batch_size, input_dim), dtype='float32') target_tensor = K.placeholder(shape=(batch_size, 1), dtype='float32') # defining the weight and the bias variables weight_variable = K.random_uniform_variable(shape=(input_dim, 1), low=-1., high=1., dtype='float32') bias_variable = K.zeros(shape=(1, ), dtype='float32') # defining the sigmoid output tensor output_tensor = K.dot(input_tensor, weight_variable) + bias_variable output_tensor = K.sigmoid(output_tensor) # defining the mean loss tensor loss_tensor = K.mean(K.binary_crossentropy(target_tensor, output_tensor)) # getting the gradients of the mean loss with respect to the weight and bias gradient_tensors = K.gradients(loss=loss_tensor, variables=[weight_variable, bias_variable]) # creating the updates based on stochastic gradient descent rule