我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用keras.layers.multiply()。
def prep_model(inputs, N, s0pad, s1pad, c, granlevels=1): # LSTM lstm = LSTM(N, return_sequences=True, implementation=2, kernel_regularizer=l2(c['l2reg']), recurrent_regularizer=l2(c['l2reg']), bias_regularizer=l2(c['l2reg'])) x1 = inputs[0] x2 = inputs[1] h1 = lstm(x1) h2 = lstm(x2) W_x = Dense(N, kernel_initializer='glorot_uniform', use_bias=True, kernel_regularizer=l2(c['l2reg'])) W_h = Dense(N, kernel_initializer='orthogonal', use_bias=True, kernel_regularizer=l2(c['l2reg'])) sigmoid = Activation('sigmoid') a1 = multiply([x1, sigmoid( add([W_x(x1), W_h(h1)]) )]) a2 = multiply([x2, sigmoid( add([W_x(x2), W_h(h2)]) )]) # Averaging avg = Lambda(function=lambda x: K.mean(x, axis=1), output_shape=lambda shape: (shape[0], ) + shape[2:]) gran1 = avg(a1) gran2 = avg(a2) return [gran1, gran2], N
def test_tiny_mul_random(self): np.random.seed(1988) input_dim = 10 num_channels = 6 # Define a model input_tensor = Input(shape = (input_dim, )) x1 = Dense(num_channels)(input_tensor) x2 = Dense(num_channels)(x1) x3 = Dense(num_channels)(x1) x4 = multiply([x2, x3]) x5 = Dense(num_channels)(x4) model = Model(inputs=[input_tensor], outputs=[x5]) # Set some random weights model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()]) # Get the coreml model self._test_keras_model(model)
def test_dense_elementwise_params(self): options = dict( modes = [add, multiply, concatenate, average, maximum] ) def build_model(mode): x1 = Input(shape=(3,)) x2 = Input(shape=(3,)) y1 = Dense(4)(x1) y2 = Dense(4)(x2) z = mode([y1, y2]) model = Model([x1,x2], z) return mode, model product = itertools.product(*options.values()) args = [build_model(p[0]) for p in product] print("Testing a total of %s cases. This could take a while" % len(args)) for param, model in args: self._run_test(model, param)
def RHN(input_dim, hidden_dim, depth): # Wrapped model inp = Input(batch_shape=(batch_size, input_dim)) state = Input(batch_shape=(batch_size, hidden_dim)) drop_mask = Input(batch_shape=(batch_size, hidden_dim)) # To avoid all zero mask causing gradient to vanish inverted_drop_mask = Lambda(lambda x: 1.0 - x, output_shape=lambda s: s)(drop_mask) drop_mask_2 = Lambda(lambda x: x + 0., output_shape=lambda s: s)(inverted_drop_mask) dropped_state = multiply([state, inverted_drop_mask]) y, new_state = RHNCell(units=hidden_dim, recurrence_depth=depth, kernel_initializer=weight_init, kernel_regularizer=l2(weight_decay), kernel_constraint=max_norm(gradient_clip), bias_initializer=Constant(transform_bias), recurrent_initializer=weight_init, recurrent_regularizer=l2(weight_decay), recurrent_constraint=max_norm(gradient_clip))([inp, dropped_state]) return RecurrentModel(input=inp, output=y, initial_states=[state, drop_mask], final_states=[new_state, drop_mask_2]) # lr decay Scheduler
def QRNcell(): xq = Input(batch_shape=(batch_size, embedding_dim * 2)) # Split into context and query xt = Lambda(lambda x, dim: x[:, :dim], arguments={'dim': embedding_dim}, output_shape=lambda s: (s[0], s[1] / 2))(xq) qt = Lambda(lambda x, dim: x[:, dim:], arguments={'dim': embedding_dim}, output_shape=lambda s: (s[0], s[1] / 2))(xq) h_tm1 = Input(batch_shape=(batch_size, embedding_dim)) zt = Dense(1, activation='sigmoid', bias_initializer=Constant(2.5))(multiply([xt, qt])) zt = Lambda(lambda x, dim: K.repeat_elements(x, dim, axis=1), arguments={'dim': embedding_dim})(zt) ch = Dense(embedding_dim, activation='tanh')(concatenate([xt, qt], axis=-1)) rt = Dense(1, activation='sigmoid')(multiply([xt, qt])) rt = Lambda(lambda x, dim: K.repeat_elements(x, dim, axis=1), arguments={'dim': embedding_dim})(rt) ht = add([multiply([zt, ch, rt]), multiply([Lambda(lambda x: 1 - x, output_shape=lambda s: s)(zt), h_tm1])]) return RecurrentModel(input=xq, output=ht, initial_states=[h_tm1], final_states=[ht], return_sequences=True) # # Load data #
def cosine_dist(self, inputs): """Define a function for a lambda layer of a model.""" input1, input2 = inputs a = K.abs(input1-input2) b = multiply(inputs) return K.concatenate([a, b])
def cosine_dist(self, inputs): input1, input2 = inputs a = K.abs(input1-input2) b = multiply(inputs) return K.concatenate([a, b])
def eltwise(layer, layer_in, layerId): out = {} if (layer['params']['layer_type'] == 'Multiply'): # This input reverse is to handle visualization out[layerId] = multiply(layer_in[::-1]) elif (layer['params']['layer_type'] == 'Sum'): out[layerId] = add(layer_in[::-1]) elif (layer['params']['layer_type'] == 'Average'): out[layerId] = average(layer_in[::-1]) elif (layer['params']['layer_type'] == 'Dot'): out[layerId] = dot(layer_in[::-1], -1) else: out[layerId] = maximum(layer_in[::-1]) return out
def mlp_ptscorer(inputs, Ddim, N, l2reg, pfx='out', Dinit='glorot_uniform', sum_mode='sum', extra_inp=[]): """ Element-wise features from the pair fed to an MLP. """ linear = Activation('linear') if sum_mode == 'absdiff': absdiff = Lambda(function=lambda x: K.abs(x[0] - x[1]), output_shape=lambda shape: shape[0]) # model.add_node(name=pfx+'sum', layer=absdiff_merge(model, inputs)) mlp_inputs = absdiff(inputs) elif sum_mode == 'sum': outsum = linear(add(inputs)) outmul = linear(multiply(inputs)) mlp_inputs = [outsum, outmul] + extra_inp def mlp_args(mlp_inputs): """ return model.add_node() args that are good for mlp_inputs list of both length 1 and more than 1. """ if isinstance(mlp_inputs, list): mlp_inputs = concatenate(mlp_inputs) return mlp_inputs # Ddim may be either 0 (no hidden layer), scalar (single hidden layer) or # list (multiple hidden layers) if Ddim == 0: mlp_inputs = mlp_args(mlp_inputs) Ddim = [] elif not isinstance(Ddim, list): Ddim = [Ddim] if Ddim: for i, D in enumerate(Ddim): mlp_inputs = Dense(int(N*D), activation='tanh', kernel_initializer=Dinit, kernel_regularizer=l2(l2reg))(mlp_args(mlp_inputs)) # model.add_node(name=pfx+'hdn[%d]'%(i,), # layer=Dense(output_dim=int(N*D), W_regularizer=l2(l2reg), activation='tanh', init=Dinit), # **mlp_args(mlp_inputs)) # mlp_inputs = [pfx+'hdn[%d]'%(i,)] outmlp = Dense(1, kernel_regularizer=l2(l2reg))(mlp_inputs) return outmlp
def build_generator(self): model = Sequential() model.add(Dense(128 * 7 * 7, activation="relu", input_dim=100)) model.add(Reshape((7, 7, 128))) model.add(BatchNormalization(momentum=0.8)) model.add(UpSampling2D()) model.add(Conv2D(128, kernel_size=3, padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(momentum=0.8)) model.add(UpSampling2D()) model.add(Conv2D(64, kernel_size=3, padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(self.channels, kernel_size=3, padding='same')) model.add(Activation("tanh")) model.summary() noise = Input(shape=(100,)) label = Input(shape=(1,), dtype='int32') label_embedding = Flatten()(Embedding(self.num_classes, 100)(label)) input = multiply([noise, label_embedding]) img = model(input) return Model([noise, label], img)
def RWA(input_dim, output_dim): x = Input((input_dim, )) h_tm1 = Input((output_dim, )) n_tm1 = Input((output_dim, )) d_tm1 = Input((output_dim, )) x_h = concatenate([x, h_tm1]) u = Dense(output_dim)(x) g = Dense(output_dim, activation='tanh')(x_h) a = Dense(output_dim, use_bias=False)(x_h) e_a = Lambda(lambda x: K.exp(x))(a) z = multiply([u, g]) nt = add([n_tm1, multiply([z, e_a])]) dt = add([d_tm1, e_a]) dt = Lambda(lambda x: 1.0 / x)(dt) ht = multiply([nt, dt]) ht = Activation('tanh')(ht) return RecurrentModel(input=x, output=ht, initial_states=[h_tm1, n_tm1, d_tm1], final_states=[ht, nt, dt], state_initializer=[initializers.random_normal(stddev=1.0)]) ##################################################################### # Settings #####################################################################
def build_generator(latent_size): # we will map a pair of (z, L), where z is a latent vector and L is a # label drawn from P_c, to image space (..., 1, 28, 28) cnn = Sequential() cnn.add(Dense(1024, input_dim=latent_size, activation='relu')) cnn.add(Dense(128 * 7 * 7, activation='relu')) cnn.add(Reshape((128, 7, 7))) # upsample to (..., 14, 14) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Conv2D(256, 5, padding='same', activation='relu', kernel_initializer='glorot_normal')) # upsample to (..., 28, 28) cnn.add(UpSampling2D(size=(2, 2))) cnn.add(Conv2D(128, 5, padding='same', activation='relu', kernel_initializer='glorot_normal')) # take a channel axis reduction cnn.add(Conv2D(1, 2, padding='same', activation='tanh', kernel_initializer='glorot_normal')) # this is the z space commonly refered to in GAN papers latent = Input(shape=(latent_size, )) # this will be our label image_class = Input(shape=(1,), dtype='int32') # 10 classes in MNIST cls = Flatten()(Embedding(10, latent_size, embeddings_initializer='glorot_normal')(image_class)) # hadamard product between z-space and a class conditional embedding h = layers.multiply([latent, cls]) fake_image = cnn(h) return Model([latent, image_class], fake_image)
def _augment_model(self, model, score, reweighting): # Extract some info from the model loss = model.loss optimizer = model.optimizer.__class__(**model.optimizer.get_config()) output_shape = K.int_shape(model.output)[1:] if isinstance(loss, str) and loss.startswith("sparse"): output_shape = output_shape[:-1] + (1,) # Make sure that some stuff look ok assert not isinstance(loss, list) # We need to create two more inputs # 1. the targets # 2. the predicted scores y_true = Input(shape=output_shape) pred_score = Input(shape=(reweighting.weight_size,)) # Create a loss layer and a score layer loss_tensor = LossLayer(loss)([y_true, model.output]) last_layer = -2 if isinstance(model.layers[-1], Activation) else -1 score_tensor = _get_scoring_layer( score, y_true, model.output, loss, model.layers[last_layer] ) # Create the sample weights weights = reweighting.weight_layer()([score_tensor, pred_score]) # Create the output weighted_loss = multiply([loss_tensor, weights]) # Create the metric layers metrics = model.metrics or [] metrics = [ MetricLayer(metric)([y_true, model.output]) for metric in metrics ] # Finally build, compile, return new_model = TransparentModel( inputs=_tolist(model.input) + [y_true, pred_score], outputs=[weighted_loss], observed_tensors=[loss_tensor, weighted_loss, score_tensor] + metrics ) new_model.compile( optimizer=optimizer, loss=lambda y_true, y_pred: y_pred ) return new_model
def create(cls, classes, maximum_tokens, embedding_size, lstm_units, dropout, bidirectional): """ Create a model that labels semantic relationships between text pairs. The text pairs are passed in as two aligned matrices of size (batch size, maximum embedding tokens, embedding size). They are generated by TextPairEmbeddingGenerator. :param classes: the number of distinct classes to categorize :type classes: int :param maximum_tokens: maximum number of embedded tokens :type maximum_tokens: int :param embedding_size: size of the embedding vector :type embedding_size: int :param lstm_units: number of hidden units in the shared LSTM :type lstm_units: int :param dropout: dropout rate or None for no dropout :type dropout: float or None :param bidirectional: should the shared LSTM be bidirectional? :type bidirectional: bool :return: the created model :rtype: TextPairClassifier """ # Create the model geometry. input_shape = (maximum_tokens, embedding_size) # Input two sets of aligned text pairs. input_1 = Input(input_shape) input_2 = Input(input_shape) # Apply the same LSTM to each. if bidirectional: lstm = Bidirectional(LSTM(lstm_units), name="lstm") else: lstm = LSTM(lstm_units, name="lstm") r1 = lstm(input_1) r2 = lstm(input_2) # Concatenate the embeddings with their product and squared difference. p = multiply([r1, r2]) negative_r2 = Lambda(lambda x: -x)(r2) d = add([r1, negative_r2]) q = multiply([d, d]) v = [r1, r2, p, q] lstm_output = concatenate(v) if dropout is not None: lstm_output = Dropout(dropout, name="dropout")(lstm_output) # A single-layer perceptron maps the concatenated vector to the labels. It has a number of hidden states equal # to the square root of the length of the concatenated vector. m = sum(t.shape[1].value for t in v) perceptron = Dense(math.floor(math.sqrt(m)), activation="relu")(lstm_output) logistic_regression = Dense(classes, activation="softmax", name="softmax")(perceptron) model = Model([input_1, input_2], logistic_regression, "Text pair classifier") model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) return cls(model)