Python keras.initializers.constant() Examples
The following are 7
code examples of keras.initializers.constant().
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 also want to check out all available functions/classes of the module
keras.initializers
, or try the search function
.
Example #1
Source File: HandWritingRecognition.py From Jtyoui with MIT License | 6 votes |
def nn_model(): (x_train, y_train), _ = mnist.load_data() # 归一化 x_train = x_train.reshape(x_train.shape[0], -1) / 255. # one-hot y_train = np_utils.to_categorical(y=y_train, num_classes=10) # constant(value=1.)自定义常数,constant(value=1.)===one() # 创建模型:输入784个神经元,输出10个神经元 model = Sequential([ Dense(units=200, input_dim=784, bias_initializer=constant(value=1.), activation=tanh), Dense(units=100, bias_initializer=one(), activation=tanh), Dense(units=10, bias_initializer=one(), activation=softmax), ]) opt = SGD(lr=0.2, clipnorm=1.) # 优化器 model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc', 'mae']) # 编译 model.fit(x_train, y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()]) model_save(model, './model.h5')
Example #2
Source File: capslayers.py From deepcaps with MIT License | 6 votes |
def build(self, input_shape): assert len(input_shape) == 5, "The input Tensor should have shape=[None, input_height, input_width," \ " input_num_capsule, input_num_atoms]" self.input_height = input_shape[1] self.input_width = input_shape[2] self.input_num_capsule = input_shape[3] self.input_num_atoms = input_shape[4] # Transform matrix self.W = self.add_weight(shape=[self.input_num_atoms, self.kernel_size, self.kernel_size, 1, self.num_capsule * self.num_atoms], initializer=self.kernel_initializer, name='W') self.b = self.add_weight(shape=[self.num_capsule, self.num_atoms, 1, 1], initializer=initializers.constant(0.1), name='b') self.built = True
Example #3
Source File: capsule_layers.py From SegCaps with Apache License 2.0 | 6 votes |
def build(self, input_shape): assert len(input_shape) == 5, "The input Tensor should have shape=[None, input_height, input_width," \ " input_num_capsule, input_num_atoms]" self.input_height = input_shape[1] self.input_width = input_shape[2] self.input_num_capsule = input_shape[3] self.input_num_atoms = input_shape[4] # Transform matrix self.W = self.add_weight(shape=[self.kernel_size, self.kernel_size, self.input_num_atoms, self.num_capsule * self.num_atoms], initializer=self.kernel_initializer, name='W') self.b = self.add_weight(shape=[1, 1, self.num_capsule, self.num_atoms], initializer=initializers.constant(0.1), name='b') self.built = True
Example #4
Source File: capsule_layers.py From SegCaps with Apache License 2.0 | 5 votes |
def build(self, input_shape): assert len(input_shape) == 5, "The input Tensor should have shape=[None, input_height, input_width," \ " input_num_capsule, input_num_atoms]" self.input_height = input_shape[1] self.input_width = input_shape[2] self.input_num_capsule = input_shape[3] self.input_num_atoms = input_shape[4] # Transform matrix if self.upsamp_type == 'subpix': self.W = self.add_weight(shape=[self.kernel_size, self.kernel_size, self.input_num_atoms, self.num_capsule * self.num_atoms * self.scaling * self.scaling], initializer=self.kernel_initializer, name='W') elif self.upsamp_type == 'resize': self.W = self.add_weight(shape=[self.kernel_size, self.kernel_size, self.input_num_atoms, self.num_capsule * self.num_atoms], initializer=self.kernel_initializer, name='W') elif self.upsamp_type == 'deconv': self.W = self.add_weight(shape=[self.kernel_size, self.kernel_size, self.num_capsule * self.num_atoms, self.input_num_atoms], initializer=self.kernel_initializer, name='W') else: raise NotImplementedError('Upsampling must be one of: "deconv", "resize", or "subpix"') self.b = self.add_weight(shape=[1, 1, self.num_capsule, self.num_atoms], initializer=initializers.constant(0.1), name='b') self.built = True
Example #5
Source File: matching_tensor_layer.py From MatchZoo with Apache License 2.0 | 4 votes |
def build(self, input_shape: list): """ Build the layer. :param input_shape: the shapes of the input tensors, for MatchingTensorLayer we need two input tensors. """ # Used purely for shape validation. if not isinstance(input_shape, list) or len(input_shape) != 2: raise ValueError('A `MatchingTensorLayer` layer should be called ' 'on a list of 2 inputs.') self._shape1 = input_shape[0] self._shape2 = input_shape[1] for idx in (0, 2): if self._shape1[idx] != self._shape2[idx]: raise ValueError( 'Incompatible dimensions: ' f'{self._shape1[idx]} != {self._shape2[idx]}.' f'Layer shapes: {self._shape1}, {self._shape2}.' ) if self._init_diag: interaction_matrix = np.float32( np.random.uniform( -0.05, 0.05, [self._channels, self._shape1[2], self._shape2[2]] ) ) for channel_index in range(self._channels): np.fill_diagonal(interaction_matrix[channel_index], 0.1) self.interaction_matrix = self.add_weight( name='interaction_matrix', shape=(self._channels, self._shape1[2], self._shape2[2]), initializer=constant(interaction_matrix), trainable=True ) else: self.interaction_matrix = self.add_weight( name='interaction_matrix', shape=(self._channels, self._shape1[2], self._shape2[2]), initializer='uniform', trainable=True ) super(MatchingTensorLayer, self).build(input_shape)
Example #6
Source File: capslayers.py From deepcaps with MIT License | 4 votes |
def update_routing(votes, biases, logit_shape, num_dims, input_dim, output_dim, num_routing): if num_dims == 6: votes_t_shape = [3, 0, 1, 2, 4, 5] r_t_shape = [1, 2, 3, 0, 4, 5] elif num_dims == 4: votes_t_shape = [3, 0, 1, 2] r_t_shape = [1, 2, 3, 0] else: raise NotImplementedError('Not implemented') votes_trans = tf.transpose(votes, votes_t_shape) _, _, _, height, width, caps = votes_trans.get_shape() def _body(i, logits, activations): """Routing while loop.""" # route: [batch, input_dim, output_dim, ...] a,b,c,d,e = logits.get_shape() a = logit_shape[0] b = logit_shape[1] c = logit_shape[2] d = logit_shape[3] e = logit_shape[4] print(logit_shape) logit_temp = tf.reshape(logits, [a,b,-1]) route_temp = tf.nn.softmax(logit_temp, dim=-1) route = tf.reshape(route_temp, [a, b, c, d, e]) preactivate_unrolled = route * votes_trans preact_trans = tf.transpose(preactivate_unrolled, r_t_shape) preactivate = tf.reduce_sum(preact_trans, axis=1) + biases # activation = _squash(preactivate) activation = squash(preactivate, axis=[-1, -2, -3]) activations = activations.write(i, activation) act_3d = K.expand_dims(activation, 1) tile_shape = np.ones(num_dims, dtype=np.int32).tolist() tile_shape[1] = input_dim act_replicated = tf.tile(act_3d, tile_shape) distances = tf.reduce_sum(votes * act_replicated, axis=3) logits += distances return (i + 1, logits, activations) activations = tf.TensorArray( dtype=tf.float32, size=num_routing, clear_after_read=False) logits = tf.fill(logit_shape, 0.0) i = tf.constant(0, dtype=tf.int32) _, logits, activations = tf.while_loop( lambda i, logits, activations: i < num_routing, _body, loop_vars=[i, logits, activations], swap_memory=True) a = K.cast(activations.read(num_routing - 1), dtype='float32') return K.cast(activations.read(num_routing - 1), dtype='float32')
Example #7
Source File: capsule_layers.py From SegCaps with Apache License 2.0 | 4 votes |
def update_routing(votes, biases, logit_shape, num_dims, input_dim, output_dim, num_routing): if num_dims == 6: votes_t_shape = [5, 0, 1, 2, 3, 4] r_t_shape = [1, 2, 3, 4, 5, 0] elif num_dims == 4: votes_t_shape = [3, 0, 1, 2] r_t_shape = [1, 2, 3, 0] else: raise NotImplementedError('Not implemented') votes_trans = tf.transpose(votes, votes_t_shape) _, _, _, height, width, caps = votes_trans.get_shape() def _body(i, logits, activations): """Routing while loop.""" # route: [batch, input_dim, output_dim, ...] route = tf.nn.softmax(logits, dim=-1) preactivate_unrolled = route * votes_trans preact_trans = tf.transpose(preactivate_unrolled, r_t_shape) preactivate = tf.reduce_sum(preact_trans, axis=1) + biases activation = _squash(preactivate) activations = activations.write(i, activation) act_3d = K.expand_dims(activation, 1) tile_shape = np.ones(num_dims, dtype=np.int32).tolist() tile_shape[1] = input_dim act_replicated = tf.tile(act_3d, tile_shape) distances = tf.reduce_sum(votes * act_replicated, axis=-1) logits += distances return (i + 1, logits, activations) activations = tf.TensorArray( dtype=tf.float32, size=num_routing, clear_after_read=False) logits = tf.fill(logit_shape, 0.0) i = tf.constant(0, dtype=tf.int32) _, logits, activations = tf.while_loop( lambda i, logits, activations: i < num_routing, _body, loop_vars=[i, logits, activations], swap_memory=True) return K.cast(activations.read(num_routing - 1), dtype='float32')