Python keras.backend.zeros() Examples
The following are 30 code examples for showing how to use keras.backend.zeros(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example 1
Project: CapsNet Author: l11x0m7 File: capsule.py License: MIT License | 6 votes |
def call(self, inputs, **kwargs): # (batch_size, 1, input_num_capsule, input_dim_capsule) expand_inputs = K.expand_dims(inputs, axis=1) # (batch_size, num_capsule, input_num_capsule, input_dim_capsule) expand_inputs = K.tile(expand_inputs, (1, self.num_capsule, 1, 1)) # (batch_size, num_capsule, input_num_capsule, dim_capsule) u_hat = K.map_fn(lambda x: K.batch_dot(x, self.W, axes=[2, 3]), expand_inputs) if self.num_routing <= 0: self.num_routing = 3 # (batch_size, num_capsule, input_num_capsule) b = K.zeros((K.shape(u_hat)[0], self.num_capsule, self.input_num_capsule)) for i in xrange(self.num_routing): # (batch_size, num_capsule, input_num_capsule) c = softmax(b, axis=1) # (batch_size, num_capsule, dim_capsule) s = K.batch_dot(c, u_hat, axes=[2, 2]) squashed_s = squash(s) if i < self.num_routing - 1: # (batch_size, num_capsule, input_num_capsule) b += K.batch_dot(squashed_s, u_hat, axes=[2, 3]) return squashed_s
Example 2
Project: keras_bn_library Author: bnsnapper File: rnnrbm.py License: MIT License | 6 votes |
def reset_states(self): assert self.stateful, 'Layer must be stateful.' input_shape = self.input_spec[0].shape if not input_shape[0]: raise Exception('If a RNN is stateful, a complete ' + 'input_shape must be provided (including batch size).') if hasattr(self, 'states'): K.set_value(self.states[0], np.zeros((input_shape[0], self.hidden_recurrent_dim))) K.set_value(self.states[1], np.zeros((input_shape[0], self.input_dim))) K.set_value(self.states[2], np.zeros((input_shape[0], self.hidden_dim))) else: self.states = [K.zeros((input_shape[0], self.hidden_recurrent_dim)), K.zeros((input_shape[0], self.input_dim)), K.zeros((input_shape[0], self.hidden_dim))]
Example 3
Project: keras_bn_library Author: bnsnapper File: recurrent.py License: MIT License | 6 votes |
def build(self, input_shape): self.input_spec = [InputSpec(shape=input_shape)] self.input_dim = input_shape[2] self.W = self.init((self.output_dim, 4 * self.input_dim), name='{}_W'.format(self.name)) self.U = self.inner_init((self.input_dim, 4 * self.input_dim), name='{}_U'.format(self.name)) self.b = K.variable(np.hstack((np.zeros(self.input_dim), K.get_value(self.forget_bias_init((self.input_dim,))), np.zeros(self.input_dim), np.zeros(self.input_dim))), name='{}_b'.format(self.name)) self.A = self.init((self.input_dim, self.output_dim), name='{}_A'.format(self.name)) self.ba = K.zeros((self.output_dim,), name='{}_ba'.format(self.name)) self.trainable_weights = [self.W, self.U, self.b, self.A, self.ba] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights
Example 4
Project: keras_bn_library Author: bnsnapper File: recurrent.py License: MIT License | 6 votes |
def reset_states(self): assert self.stateful, 'Layer must be stateful.' input_shape = self.input_spec[0].shape if not input_shape[0]: raise ValueError('If a RNN is stateful, it needs to know ' 'its batch size. Specify the batch size ' 'of your input tensors: \n' '- If using a Sequential model, ' 'specify the batch size by passing ' 'a `batch_input_shape` ' 'argument to your first layer.\n' '- If using the functional API, specify ' 'the time dimension by passing a ' '`batch_shape` argument to your Input layer.') if hasattr(self, 'states'): K.set_value(self.states[0], np.zeros((input_shape[0], self.input_dim))) K.set_value(self.states[1], np.zeros((input_shape[0], self.output_dim))) else: self.states = [K.zeros((input_shape[0], self.input_dim)), K.zeros((input_shape[0], self.output_dim))]
Example 5
Project: keras-lookahead Author: CyberZHG File: optimizers.py License: MIT License | 6 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] t = K.cast(self.iterations, K.floatx()) + 1 lr_t = self.learning_rate * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] self.weights = [self.iterations] + ms + vs for p, g, m, v in zip(params, grads, ms, vs): m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) p_t = lr_t * m_t / (K.sqrt(v_t) + self.epsilon) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) self.updates.append(K.update_sub(p, p_t)) return self.updates
Example 6
Project: voxelmorph Author: voxelmorph File: layers.py License: GNU General Public License v3.0 | 6 votes |
def build(self, input_shape): # Create mean and count # These are weights because just maintaining variables don't get saved with the model, and we'd like # to have these numbers saved when we save the model. # But we need to make sure that the weights are untrainable. self.mean = self.add_weight(name='mean', shape=input_shape[1:], initializer='zeros', trainable=False) self.count = self.add_weight(name='count', shape=[1], initializer='zeros', trainable=False) # self.mean = K.zeros(input_shape[1:], name='mean') # self.count = K.variable(0.0, name='count') super(MeanStream, self).build(input_shape) # Be sure to call this somewhere!
Example 7
Project: Implementation-CVPR2015-CNN-for-ReID Author: Ning-Ding File: model_for_market1501.py License: MIT License | 6 votes |
def cmc(model): def cmc_curve(model, camera1, camera2, rank_max=50): num = camera1.shape[0] rank = [] score = [] camera_batch1 = np.zeros(camera1.shape) for i in range(num): for j in range(num): camera_batch1[j] = camera1[i] similarity_batch = model.predict_on_batch([camera_batch1, camera2]) sim_trans = similarity_batch.transpose() similarity_rate_sorted = np.argsort(sim_trans[0]) for k in range(num): if similarity_rate_sorted[k] == i: rank.append(k+1) break rank_val = 0 for i in range(rank_max): rank_val = rank_val + len([j for j in rank if i == j-1]) score.append(rank_val / float(num)) return np.array(score) a,b = get_data_for_cmc() return cmc_curve(model,a,b)
Example 8
Project: pyslam Author: luigifreda File: LRN.py License: GNU General Public License v3.0 | 6 votes |
def call(self, x, mask=None): s = K.shape(x) b = s[0] r = s[1] c = s[2] ch = s[3] half_n = self.n // 2 # half the local region input_sqr = K.square(x) # square the input extra_channels = K.zeros((b, r, c, ch + 2 * half_n)) input_sqr = K.concatenate([extra_channels[:, :, :, :half_n],input_sqr, extra_channels[:, :, :, half_n + ch:]], axis = 3) scale = self.k # offset for the scale norm_alpha = self.alpha / self.n # normalized alpha for i in range(self.n): scale += norm_alpha * input_sqr[:, :, :, i:i+ch] scale = scale ** self.beta x = x / scale return x
Example 9
Project: recurrent-attention-for-QA-SQUAD-based-on-keras Author: wentaozhu File: QnA.py License: MIT License | 6 votes |
def vectorizeData(xContext, xQuestion, xAnswerBeing, xAnswerEnd, word_index, context_maxlen, question_maxlen): '''Vectorize the words to their respective index and pad context to max context length and question to max question length. Answers vectors are padded to the max context length as well. ''' X = [] Xq = [] YBegin = [] YEnd = [] for i in xrange(len(xContext)): x = [word_index[w] for w in xContext[i]] xq = [word_index[w] for w in xQuestion[i]] # map the first and last words of answer span to one-hot representations y_Begin = np.zeros(len(xContext[i])) y_Begin[xAnswerBeing[i]] = 1 y_End = np.zeros(len(xContext[i])) y_End[xAnswerEnd[i]] = 1 X.append(x) Xq.append(xq) YBegin.append(y_Begin) YEnd.append(y_End) return pad_sequences(X, maxlen=context_maxlen, padding='post'), pad_sequences(Xq, maxlen=question_maxlen, padding='post'), pad_sequences(YBegin, maxlen=context_maxlen, padding='post'), pad_sequences(YEnd, maxlen=context_maxlen, padding='post') # Note: Need to download and unzip Glove pre-train model files into same file as this script
Example 10
Project: onto-lstm Author: pdasigi File: embedding.py License: Apache License 2.0 | 6 votes |
def build(self, input_shape): # input shape is (batch_size, num_words, num_senses, num_hyps) self.num_senses = input_shape[-2] self.num_hyps = input_shape[-1] - 1 # -1 because the last value is a word index # embedding of size 1. if self.set_sense_priors: self.sense_priors = self._get_initial_sense_priors((self.word_index_size, 1), name='{}_sense_priors'.format(self.name)) else: # OntoLSTM makes sense proabilities uniform if the passed sense parameters are zero. self.sense_priors = K.zeros((self.word_index_size, 1)) # uniform sense probs # Keeping aside the initial weights to not let Embedding set them. It wouldn't know what sense priors are. if self.initial_weights is not None: self.onto_aware_embedding_weights = self.initial_weights self.initial_weights = None # The following method will set self.trainable_weights super(OntoAwareEmbedding, self).build(input_shape) # input_shape will not be used by Embedding's build. if not self.tune_embedding: # Move embedding to non_trainable_weights self._non_trainable_weights.append(self._trainable_weights.pop()) if self.set_sense_priors: self._trainable_weights.append(self.sense_priors) if self.onto_aware_embedding_weights is not None: self.set_weights(self.onto_aware_embedding_weights)
Example 11
Project: deep-models Author: LaurentMazare File: rhn.py License: Apache License 2.0 | 5 votes |
def reset_states(self): assert self.stateful, 'Layer must be stateful.' input_shape = self.input_spec[0].shape if not input_shape[0]: raise Exception('If a RNN is stateful, a complete ' + 'input_shape must be provided (including batch size).') if hasattr(self, 'states'): K.set_value(self.states[0], np.zeros((input_shape[0], self.output_dim))) else: self.states = [K.zeros((input_shape[0], self.output_dim))]
Example 12
Project: deep-models Author: LaurentMazare File: lstm_ln.py License: Apache License 2.0 | 5 votes |
def build(self, input_shape): super(LSTM_LN, self).build(input_shape) self.gs, self.bs = [], [] for i in xrange(3): f = 1 if i == 2 else 4 self.gs += [ K.ones((f*self.output_dim,), name='{}_g%i'.format(self.name, i)) ] self.bs += [ K.zeros((f*self.output_dim,), name='{}_b%d'.format(self.name, i)) ] self.trainable_weights += self.gs + self.bs
Example 13
Project: fancy-cnn Author: textclf File: convolutions.py License: MIT License | 5 votes |
def build(self): stack_size = self.input_shape[2] dtensor5 = T.TensorType('float32', (False,)*5) self.input = dtensor5() self.W_shape = (self.nb_filter, stack_size, self.nb_row, self.nb_col) self.W = self.init(self.W_shape) self.b = shared_zeros((self.nb_filter,)) self.params = [self.W, self.b] self.regularizers = [] if self.W_regularizer: self.W_regularizer.set_param(self.W) self.regularizers.append(self.W_regularizer) if self.b_regularizer: self.b_regularizer.set_param(self.b) self.regularizers.append(self.b_regularizer) if self.activity_regularizer: self.activity_regularizer.set_layer(self) self.regularizers.append(self.activity_regularizer) if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights
Example 14
Project: keras_bn_library Author: bnsnapper File: recurrent.py License: MIT License | 5 votes |
def build(self, input_shape): self.input_spec = [InputSpec(shape=input_shape)] input_dim = input_shape[2] self.input_dim = input_dim if self.stateful: self.reset_states() else: self.states = [None, None] self.states_dim = [self.input_dim, self.output_dim] self.weight_size = self.output_dim * 4 self.W = self.add_weight((input_dim, self.weight_size), initializer=self.init, name='{}_W'.format(self.name), regularizer=self.W_regularizer) self.U = self.add_weight((input_dim, self.weight_size), initializer=self.inner_init, name='{}_U'.format(self.name), regularizer=self.U_regularizer) def b_reg(shape, name=None): return K.variable(np.hstack((np.zeros(self.output_dim), K.get_value(self.forget_bias_init((self.output_dim,))), np.zeros(self.output_dim), np.zeros(self.output_dim))), name='{}_b'.format(self.name)) self.b = self.add_weight((self.weight_size,), initializer=b_reg, name='{}_b'.format(self.name), regularizer=self.b_regularizer) if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights self.built = True
Example 15
Project: NTM-Keras Author: SigmaQuan File: lstm2ntm.py License: MIT License | 5 votes |
def reset_states(self): assert self.stateful, 'Layer must be stateful.' input_shape = self.input_spec[0].shape if not input_shape[0]: raise Exception('If a RNN is stateful, a complete ' + 'input_shape must be provided (including batch size).') if hasattr(self, 'states'): K.set_value(self.states[0], np.zeros((input_shape[0], self.output_dim))) K.set_value(self.states[1], np.zeros((input_shape[0], self.output_dim))) else: self.states = [K.zeros((input_shape[0], self.output_dim)), K.zeros((input_shape[0], self.output_dim))]
Example 16
Project: NTM-Keras Author: SigmaQuan File: memory.py License: MIT License | 5 votes |
def initial(number_of_memory_locations, memory_vector_size): return K.zeros((number_of_memory_locations, memory_vector_size))
Example 17
Project: conv_qsar_fast Author: connorcoley File: GraphEmbedding_sumAfter.py License: MIT License | 5 votes |
def build(self, input_shape): '''Builds internal weights and paramer attribute''' # NOTE: NEED TO TILE AND EVALUATE SO THAT PARAMS CAN BE VARIABLES # OTHERWISE K.GET_VALUE() DOES NOT WORK # Define template weights for inner FxF W_inner = self.init_inner((self.inner_dim, self.inner_dim)) b_inner = K.zeros((1, self.inner_dim)) # Initialize weights tensor self.W_inner = K.variable(T.tile(W_inner, (self.depth + 1, 1, 1)).eval() + \ initializations.uniform((self.depth + 1, self.inner_dim, self.inner_dim)).eval()) self.W_inner.name = 'T:W_inner' self.b_inner = K.variable(T.tile(b_inner, (self.depth + 1, 1, 1)).eval() + \ initializations.uniform((self.depth + 1, 1, self.inner_dim)).eval()) self.b_inner.name = 'T:b_inner' # # Concatenate third dimension (depth) so different layers can have # # different weights. Now, self.W_inner[#,:,:] corresponds to the # # weight matrix for layer/depth #. # Define template weights for output FxL W_output = self.init_output((self.inner_dim, self.output_dim), scale = self.scale_output) b_output = K.zeros((1, self.output_dim)) # Initialize weights tensor self.W_output = K.variable(T.tile(W_output, (self.depth + 1, 1, 1)).eval()) self.W_output.name = 'T:W_output' self.b_output = K.variable(T.tile(b_output, (self.depth + 1, 1, 1)).eval()) self.b_output.name = 'T:b_output' # # Concatenate third dimension (depth) so different layers can have # # different weights. Now, self.W_output[#,:,:] corresponds to the # # weight matrix for layer/depth #. # Pack params self.trainable_weights = [self.W_inner, self.b_inner, self.W_output, self.b_output] self.params = [self.W_inner, self.b_inner, self.W_output, self.b_output]
Example 18
Project: conv_qsar_fast Author: connorcoley File: GraphEmbedding.py License: MIT License | 5 votes |
def build(self, input_shape): '''Builds internal weights and paramer attribute''' # NOTE: NEED TO TILE AND EVALUATE SO THAT PARAMS CAN BE VARIABLES # OTHERWISE K.GET_VALUE() DOES NOT WORK # Define template weights for inner FxF W_inner = self.init_inner((self.inner_dim, self.inner_dim)) b_inner = K.zeros((1, self.inner_dim)) # Initialize weights tensor self.W_inner = K.variable(T.tile(W_inner, (self.depth + 1, 1, 1)).eval() + \ initializations.uniform((self.depth + 1, self.inner_dim, self.inner_dim)).eval()) self.W_inner.name = 'T:W_inner' self.b_inner = K.variable(T.tile(b_inner, (self.depth + 1, 1, 1)).eval() + \ initializations.uniform((self.depth + 1, 1, self.inner_dim)).eval()) self.b_inner.name = 'T:b_inner' # # Concatenate third dimension (depth) so different layers can have # # different weights. Now, self.W_inner[#,:,:] corresponds to the # # weight matrix for layer/depth #. # Define template weights for output FxL W_output = self.init_output((self.inner_dim, self.output_dim), scale = self.scale_output) b_output = K.zeros((1, self.output_dim)) # Initialize weights tensor self.W_output = K.variable(T.tile(W_output, (self.depth + 1, 1, 1)).eval()) self.W_output.name = 'T:W_output' self.b_output = K.variable(T.tile(b_output, (self.depth + 1, 1, 1)).eval()) self.b_output.name = 'T:b_output' # # Concatenate third dimension (depth) so different layers can have # # different weights. Now, self.W_output[#,:,:] corresponds to the # # weight matrix for layer/depth #. # Pack params self.trainable_weights = [self.W_inner, self.b_inner, self.W_output, self.b_output] self.params = [self.W_inner, self.b_inner, self.W_output, self.b_output]
Example 19
Project: voxelmorph Author: voxelmorph File: layers.py License: GNU General Public License v3.0 | 5 votes |
def __init__(self, filters, kernel_size, strides=(1, 1, 1), padding='valid', data_format=None, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs): super(LocallyConnected3D, self).__init__(**kwargs) self.filters = filters self.kernel_size = conv_utils.normalize_tuple( kernel_size, 3, 'kernel_size') self.strides = conv_utils.normalize_tuple(strides, 3, 'strides') self.padding = conv_utils.normalize_padding(padding) if self.padding != 'valid': raise ValueError('Invalid border mode for LocallyConnected3D ' '(only "valid" is supported): ' + padding) self.data_format = conv_utils.normalize_data_format(data_format) self.activation = activations.get(activation) self.use_bias = use_bias self.kernel_initializer = initializers.get(kernel_initializer) self.bias_initializer = initializers.get(bias_initializer) self.kernel_regularizer = regularizers.get(kernel_regularizer) self.bias_regularizer = regularizers.get(bias_regularizer) self.activity_regularizer = regularizers.get(activity_regularizer) self.kernel_constraint = constraints.get(kernel_constraint) self.bias_constraint = constraints.get(bias_constraint) self.input_spec = InputSpec(ndim=5)
Example 20
Project: DigiX_HuaWei_Population_Age_Attribution_Predict Author: WeavingWong File: models.py License: MIT License | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr *= (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /(1. - K.pow(self.beta_1, t))) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) vhat_t = K.maximum(vhat, v_t) p_t = p - lr_t * m_t / (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) self.updates.append(K.update(vhat, vhat_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
Example 21
Project: stochastic_depth_keras Author: dblN File: train.py License: MIT License | 5 votes |
def residual_drop(x, input_shape, output_shape, strides=(1, 1)): global add_tables nb_filter = output_shape[0] conv = Convolution2D(nb_filter, 3, 3, subsample=strides, border_mode="same", W_regularizer=l2(weight_decay))(x) conv = BatchNormalization(axis=1)(conv) conv = Activation("relu")(conv) conv = Convolution2D(nb_filter, 3, 3, border_mode="same", W_regularizer=l2(weight_decay))(conv) conv = BatchNormalization(axis=1)(conv) if strides[0] >= 2: x = AveragePooling2D(strides)(x) if (output_shape[0] - input_shape[0]) > 0: pad_shape = (1, output_shape[0] - input_shape[0], output_shape[1], output_shape[2]) padding = K.zeros(pad_shape) padding = K.repeat_elements(padding, K.shape(x)[0], axis=0) x = Lambda(lambda y: K.concatenate([y, padding], axis=1), output_shape=output_shape)(x) _death_rate = K.variable(death_rate) scale = K.ones_like(conv) - _death_rate conv = Lambda(lambda c: K.in_test_phase(scale * c, c), output_shape=output_shape)(conv) out = merge([conv, x], mode="sum") out = Activation("relu")(out) gate = K.variable(1, dtype="uint8") add_tables += [{"death_rate": _death_rate, "gate": gate}] return Lambda(lambda tensors: K.switch(gate, tensors[0], tensors[1]), output_shape=output_shape)([out, x])
Example 22
Project: Keras-TextClassification Author: yongzhuo File: keras_radam.py License: MIT License | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr = lr * (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 beta_1_t = K.pow(self.beta_1, t) beta_2_t = K.pow(self.beta_2, t) rho = 2 / (1 - self.beta_2) - 1 rho_t = rho - 2 * t * beta_2_t / (1 - beta_2_t) r_t = K.sqrt( K.relu(rho_t - 4) * K.relu(rho_t - 2) * rho / ((rho - 4) * (rho - 2) * rho_t) ) flag = K.cast(rho_t > 4, K.floatx()) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] self.weights = [self.iterations] + ms + vs for p, g, m, v in zip(params, grads, ms, vs): m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) mhat_t = m_t / (1 - beta_1_t) vhat_t = K.sqrt(v_t / (1 - beta_2_t)) p_t = p - lr * mhat_t * (flag * r_t / (vhat_t + self.epsilon) + (1 - flag)) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
Example 23
Project: Implementation-CVPR2015-CNN-for-ReID Author: Ning-Ding File: model_for_market1501.py License: MIT License | 5 votes |
def next(self): with self.lock: index_array, current_index, current_batch_size = next(self.index_generator) batch_x1 = np.zeros(tuple([current_batch_size * 2] + [128,64,3])) batch_x2 = np.zeros(tuple([current_batch_size * 2] + [128,64,3])) batch_y = np.zeros([current_batch_size * 2, 2]) for i, j in enumerate(index_array): x1 = np.array(Image.open(self.folder_dir + self.f[self.train_or_validation][j,0])) / 255. x2 = np.array(Image.open(self.folder_dir + self.f[self.train_or_validation][j,1])) / 255. if np.random.rand() > self.flag: x1 = self.image_data_generator.random_transform(x1.astype('float32')) if np.random.rand() > self.flag: x2 = self.image_data_generator.random_transform(x2.astype('float32')) batch_x1[2*i] = x1 batch_x2[2*i] = x2 batch_y[2*i][1] = 1 while True: index_1,index_2 = np.random.choice(self.path_list,2) if index_1[6] != index_2[6] and index_1[0:4] != index_2[0:4]: break x1 = np.array(Image.open(self.folder_dir + index_1)) / 255. x2 = np.array(Image.open(self.folder_dir + index_2)) / 255. batch_x1[2*i+1] = x1 batch_x2[2*i+1] = x2 batch_y[2*i+1][0] = 1 return [batch_x1,batch_x2], batch_y
Example 24
Project: Implementation-CVPR2015-CNN-for-ReID Author: Ning-Ding File: model_for_market1501.py License: MIT License | 5 votes |
def agumentation(self, X, rounds=1, seed=None): if seed is not None: np.random.seed(seed) X = np.copy(X) aX = np.zeros(tuple([rounds * X.shape[0]] + list(X.shape)[1:])) for r in range(rounds): for i in range(X.shape[0]): aX[i + r * X.shape[0]] = self.random_transform(X[i]) X = aX return X
Example 25
Project: keras-monotonic-attention Author: asmekal File: attention_decoder.py License: GNU Affero General Public License v3.0 | 5 votes |
def get_initial_state(self, inputs): if isinstance(inputs, list): assert len(inputs) == 2 # inputs == [encoder_outputs, y_true] encoder_outputs = inputs[0] else: encoder_outputs = inputs memory_shape = K.shape(encoder_outputs) # apply the matrix on the first time step to get the initial s0. s0 = activations.tanh(K.dot(encoder_outputs[:, 0], self.W_s)) y0 = K.zeros((memory_shape[0],), dtype='int64') + self.start_token t0 = K.zeros((memory_shape[0],), dtype='int64') initial_states = [y0, s0, t0] if self.is_monotonic: # initial attention has form: [1, 0, 0, ..., 0] for each sample in batch alpha0 = K.ones((memory_shape[0], 1)) alpha0 = K.switch(K.greater(memory_shape[1], 1), lambda: K.concatenate([alpha0, K.zeros((memory_shape[0], memory_shape[1] - 1))], axis=-1), alpha0) # like energy, attention is stored in shape (samples, time, 1) alpha0 = K.expand_dims(alpha0, -1) initial_states.append(alpha0) return initial_states
Example 26
Project: keras-contrib Author: keras-team File: lars.py License: MIT License | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) weights = self.get_weights() self.updates = [K.update_add(self.iterations, 1)] scaled_lr = self.lr w_norm = K.sqrt(K.sum([K.sum(K.square(weight)) for weight in weights])) g_norm = K.sqrt(K.sum([K.sum(K.square(grad)) for grad in grads])) scaled_lr = K.switch(K.greater(w_norm * g_norm, K.zeros([1])), K.expand_dims((self.eeta * w_norm / (g_norm + self.weight_decay * w_norm + self.epsilon)) * self.lr), K.ones([1]) * self.lr) if K.backend() == 'theano': scaled_lr = scaled_lr[0] # otherwise theano raise broadcasting error # momentum moments = [K.zeros(K.int_shape(param), dtype=K.dtype(param)) for param in params] self.weights = [self.iterations] + moments for param, grad, moment in zip(params, grads, moments): v0 = (moment * self.momentum) v1 = scaled_lr * grad # velocity veloc = v0 - v1 self.updates.append(K.update(moment, veloc)) if self.nesterov: new_param = param + (veloc * self.momentum) - v1 else: new_param = param + veloc # Apply constraints. if getattr(param, 'constraint', None) is not None: new_param = param.constraint(new_param) self.updates.append(K.update(param, new_param)) return self.updates
Example 27
Project: keras-contrib Author: keras-team File: yogi.py License: MIT License | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr = lr * (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vhats = [K.zeros(1) for _ in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): g2 = K.square(g) m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = v - (1. - self.beta_2) * K.sign(v - g2) * g2 p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
Example 28
Project: keras-contrib Author: keras-team File: ftml.py License: MIT License | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.inital_decay > 0: lr *= (1. / (1. + self.decay * self.iterations)) t = self.iterations + 1 lr_t = lr / (1. - K.pow(self.beta_1, t)) shapes = [K.int_shape(p) for p in params] zs = [K.zeros(shape) for shape in shapes] vs = [K.zeros(shape) for shape in shapes] ds = [K.zeros(shape) for shape in shapes] self.weights = [self.iterations] + zs + vs + ds for p, g, z, v, d in zip(params, grads, zs, vs, ds): v_t = self.beta_2 * v + (1. - self.beta_2) * K.square(g) d_t = (K.sqrt(v_t / (1. - K.pow(self.beta_2, t))) + self.epsilon) / lr_t sigma_t = d_t - self.beta_1 * d z_t = self.beta_1 * z + (1. - self.beta_1) * g - sigma_t * p p_t = - z_t / d_t self.updates.append(K.update(z, z_t)) self.updates.append(K.update(v, v_t)) self.updates.append(K.update(d, d_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
Example 29
Project: keras-contrib Author: keras-team File: dssim_test.py License: MIT License | 5 votes |
def test_DSSIM_channels_last(): prev_data = K.image_data_format() K.set_image_data_format('channels_last') for input_dim, kernel_size in zip([32, 33], [2, 3]): input_shape = [input_dim, input_dim, 3] X = np.random.random_sample(4 * input_dim * input_dim * 3) X = X.reshape([4] + input_shape) y = np.random.random_sample(4 * input_dim * input_dim * 3) y = y.reshape([4] + input_shape) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape, activation='relu')) model.add(Conv2D(3, (3, 3), padding='same', input_shape=input_shape, activation='relu')) adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8) model.compile(loss=DSSIMObjective(kernel_size=kernel_size), metrics=['mse'], optimizer=adam) model.fit(X, y, batch_size=2, epochs=1, shuffle='batch') # Test same x1 = K.constant(X, 'float32') x2 = K.constant(X, 'float32') dssim = DSSIMObjective(kernel_size=kernel_size) assert_allclose(0.0, K.eval(dssim(x1, x2)), atol=1e-4) # Test opposite x1 = K.zeros([4] + input_shape) x2 = K.ones([4] + input_shape) dssim = DSSIMObjective(kernel_size=kernel_size) assert_allclose(0.5, K.eval(dssim(x1, x2)), atol=1e-4) K.set_image_data_format(prev_data)
Example 30
Project: aiexamples Author: mogoweb File: training.py License: Apache License 2.0 | 5 votes |
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [(self.iterations, self.iterations + 1)] t = self.iterations + 1 lr_t = self.lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t)) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] gs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] self.weights = ms + vs flag = K.equal(t % self.accum_iters, 0) flag = K.cast(flag, dtype='float32') for p, g, m, v, gg in zip(params, grads, ms, vs, gs): gg_t = (1 - flag) * (gg + g) m_t = (self.beta_1 * m) + (1. - self.beta_1) * (gg + flag * g) / self.accum_iters v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square((gg + flag * g) / self.accum_iters) p_t = p - flag * lr_t * m_t / (K.sqrt(v_t) + self.epsilon) self.updates.append((m, flag * m_t + (1 - flag) * m)) self.updates.append((v, flag * v_t + (1 - flag) * v)) self.updates.append((gg, gg_t)) # apply constraints. new_p = p_t if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates