def modelC256P3C256P3f32_conc_f64(inputLength, inputDim):
inputA = Input(shape=(inputLength,), dtype='int32')
inputB = Input(shape=(inputLength,), dtype='int32')
# One hot encoding
oheInputA = Lambda(K.one_hot, arguments={
'num_classes': inputDim}, output_shape=(inputLength, inputDim))(inputA)
oheInputB = Lambda(K.one_hot, arguments={
'num_classes': inputDim}, output_shape=(inputLength, inputDim))(inputB)
net = netC256P3C256P3f32(inputLength, inputDim)
processedA = net(oheInputA)
processedB = net(oheInputB)
conc = Concatenate()([processedA, processedB])
x = BatchNormalization()(conc)
x = Dense(64, activation='relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model([inputA, inputB], predictions)
return model
python类Lambda()的实例源码
testSigmoidSmaller.py 文件源码
项目:kaggle-quora-question-pairs
作者: voletiv
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
testSigmoidSmaller.py 文件源码
项目:kaggle-quora-question-pairs
作者: voletiv
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def modelC256P3C256P3f64_conc_f64(inputLength, inputDim):
inputA = Input(shape=(inputLength,), dtype='int32')
inputB = Input(shape=(inputLength,), dtype='int32')
# One hot encoding
oheInputA = Lambda(K.one_hot, arguments={
'num_classes': inputDim}, output_shape=(inputLength, inputDim))(inputA)
oheInputB = Lambda(K.one_hot, arguments={
'num_classes': inputDim}, output_shape=(inputLength, inputDim))(inputB)
net = netC256P3C256P3f64(inputLength, inputDim)
processedA = net(oheInputA)
processedB = net(oheInputB)
conc = Concatenate()([processedA, processedB])
x = BatchNormalization()(conc)
x = Dense(64, activation='relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model([inputA, inputB], predictions)
return model
# To encode questions into char indices
def register(self, info_tensor, param_tensor):
self.info_tensor = info_tensor #(128,1)
if self.stddev_fix:
self.param_tensor = param_tensor
mean = K.clip(param_tensor[:, 0].dimshuffle(0, 'x'), self.min, self.max)
std = 1.0
else:
self.param_tensor = param_tensor # 2
mean = K.clip(param_tensor[:, 0].dimshuffle(0, 'x'), self.min, self.max)
# std = K.maximum( param_tensor[:, 1].dimshuffle(0, 'x'), 0)
std = K.sigmoid( param_tensor[:, 1].dimshuffle(0, 'x') )
e = (info_tensor-mean)/(std + K.epsilon())
self.log_Q_c_given_x = \
K.sum(-0.5*np.log(2*np.pi) -K.log(std+K.epsilon()) -0.5*(e**2), axis=1) * self.lmbd
# m = Sequential([ Activation('softmax', input_shape=(self.n,)), Lambda(lambda x: K.log(x), lambda x: x) ])
return K.reshape(self.log_Q_c_given_x, (-1, 1))
def get_transformer_net(X, weights=None):
input_ = Input(tensor=X, shape=(3, 256, 256))
y = conv_layer(input_, 32, 9)
y = conv_layer(y, 64, 3, subsample=2)
y = conv_layer(y, 128, 3, subsample=2)
y = residual_block(y)
y = residual_block(y)
y = residual_block(y)
y = residual_block(y)
y = residual_block(y)
y = conv_layer(y, 64, 3, upsample=2)
y = conv_layer(y, 32, 3, upsample=2)
y = conv_layer(y, 3, 9, only_conv=True)
y = Activation("tanh")(y)
y = Lambda(lambda x: x * 150, output_shape=(3, None, None))(y)
net = Model(input=input_, output=y)
if weights is not None:
try:
net.load_weights(weights)
except OSError as e:
print(e)
sys.exit(1)
return net
def yolo_body(inputs, num_anchors, num_classes):
"""Create YOLO_V2 model CNN body in Keras."""
darknet = Model(inputs, darknet_body()(inputs))
conv20 = compose(
DarknetConv2D_BN_Leaky(1024, (3, 3)),
DarknetConv2D_BN_Leaky(1024, (3, 3)))(darknet.output)
conv13 = darknet.layers[43].output
conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13)
# TODO: Allow Keras Lambda to use func arguments for output_shape?
conv21_reshaped = Lambda(
space_to_depth_x2,
output_shape=space_to_depth_x2_output_shape,
name='space_to_depth')(conv21)
x = concatenate([conv21_reshaped, conv20])
x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x)
x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x)
return Model(inputs, x)
def test_saving_lambda_custom_objects():
input = Input(shape=(3,))
x = Lambda(lambda x: square_fn(x), output_shape=(3,))(input)
output = Dense(3)(x)
model = Model(input, output)
model.compile(loss=objectives.MSE,
optimizer=optimizers.RMSprop(lr=0.0001),
metrics=[metrics.categorical_accuracy])
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
out = model.predict(x)
_, fname = tempfile.mkstemp('.h5')
save_model(model, fname)
model = load_model(fname, custom_objects={'square_fn': square_fn})
os.remove(fname)
out2 = model.predict(x)
assert_allclose(out, out2, atol=1e-05)
def build_model(args):
"""
Modified NVIDIA model
"""
model = Sequential()
model.add(Lambda(lambda x: x/127.5-1.0, input_shape=INPUT_SHAPE))
model.add(Conv2D(24, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(36, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(48, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(64, 3, 3, activation='elu'))
model.add(Conv2D(64, 3, 3, activation='elu'))
model.add(Dropout(args.keep_prob))
model.add(Flatten())
model.add(Dense(100, activation='elu'))
model.add(Dense(50, activation='elu'))
model.add(Dense(10, activation='elu'))
model.add(Dense(1))
model.summary()
return model
def createLayers():
x = Input(shape=env.observation_space.shape, name='x')
u = Input(shape=env.action_space.shape, name='u')
if args.batch_norm:
h = BatchNormalization()(x)
else:
h = x
for i in xrange(args.layers):
h = Dense(args.hidden_size, activation=args.activation, name='h'+str(i+1),
W_constraint=W_constraint, W_regularizer=regularizer())(h)
if args.batch_norm and i != args.layers - 1:
h = BatchNormalization()(h)
v = Dense(1, name='v', W_constraint=W_constraint, W_regularizer=regularizer())(h)
m = Dense(num_actuators, name='m', W_constraint=W_constraint, W_regularizer=regularizer())(h)
l0 = Dense(num_actuators * (num_actuators + 1)/2, name='l0',
W_constraint=W_constraint, W_regularizer=regularizer())(h)
l = Lambda(_L, output_shape=(num_actuators, num_actuators), name='l')(l0)
p = Lambda(_P, output_shape=(num_actuators, num_actuators), name='p')(l)
a = merge([m, p, u], mode=_A, output_shape=(num_actuators,), name="a")
q = merge([v, a], mode=_Q, output_shape=(num_actuators,), name="q")
return x, u, m, v, q, p, a
def createLayers():
x = Input(shape=env.observation_space.shape, name='x')
u = Input(shape=env.action_space.shape, name='u')
if args.batch_norm:
h = BatchNormalization()(x)
else:
h = x
for i in xrange(args.layers):
h = Dense(args.hidden_size, activation=args.activation, name='h'+str(i+1),
kernel_constraint=W_constraint, kernel_regularizer=kernel_regularizer)(h)
if args.batch_norm and i != args.layers - 1:
h = BatchNormalization()(h)
v = Dense(1, name='v', kernel_constraint=W_constraint, \
kernel_regularizer=kernel_regularizer)(h)
m = Dense(num_actuators, name='m', kernel_constraint=W_constraint, \
kernel_regularizer=kernel_regularizer)(h)
l0 = Dense(num_actuators * (num_actuators + 1)/2, name='l0',
kernel_constraint=W_constraint, kernel_regularizer=kernel_regularizer)(h)
l = Lambda(_L, output_shape=(num_actuators, num_actuators), name='l')(l0)
p = Lambda(_P, output_shape=(num_actuators, num_actuators), name='p')(l)
#a = merge([m, p, u], mode=_A, output_shape=(None, num_actuators,), name="a")
a = merge([m, p, u], mode=_A, output_shape=(num_actuators,), name="a")
#q = merge([v, a], mode=_Q, output_shape=(None, num_actuators,), name="q")
q = add([v, a], name="q")
return x, u, m, v, q, p, a
def createLayers():
x = Input(shape=env.observation_space.shape)
if args.batch_norm:
h = BatchNormalization()(x)
else:
h = x
for i in xrange(args.layers):
h = Dense(args.hidden_size, activation=args.activation)(h)
if args.batch_norm and i != args.layers - 1:
h = BatchNormalization()(h)
y = Dense(env.action_space.n + 1)(h)
if args.advantage == 'avg':
z = Lambda(lambda a: K.expand_dims(a[:, 0], dim=-1) + a[:, 1:] - K.mean(a[:, 1:], keepdims=True), output_shape=(env.action_space.n,))(y)
elif args.advantage == 'max':
z = Lambda(lambda a: K.expand_dims(a[:, 0], dim=-1) + a[:, 1:] - K.max(a[:, 1:], keepdims=True), output_shape=(env.action_space.n,))(y)
elif args.advantage == 'naive':
z = Lambda(lambda a: K.expand_dims(a[:, 0], dim=-1) + a[:, 1:], output_shape=(env.action_space.n,))(y)
else:
assert False
return x, z
def arch_attention(embedding_layer, sequence_length, classes):
tweet_input = Input(shape=(sequence_length,), dtype='int32')
embedded_tweet = embedding_layer(tweet_input)
activations = LSTM(128, return_sequences=True, name='recurrent_layer')(embedded_tweet)
attention = TimeDistributed(Dense(1, activation='tanh'))(activations)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(128)(attention)
attention = Permute([2, 1], name='attention_layer')(attention)
sent_representation = merge([activations, attention], mode='mul')
sent_representation = Lambda(lambda xin: K.sum(xin, axis=1), name='merged_layer')(sent_representation)
tweet_output = Dense(classes, activation='softmax', name='predictions')(sent_representation)
tweetnet = Model(tweet_input, tweet_output)
tweetnet.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return tweetnet
def arch_attention36(embedding_layer, sequence_length, classes):
tweet_input = Input(shape=(sequence_length,), dtype='int32')
embedded_tweet = embedding_layer(tweet_input)
activations = LSTM(36, return_sequences=True, name='recurrent_layer')(embedded_tweet)
attention = TimeDistributed(Dense(1, activation='tanh'))(activations)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(36)(attention)
attention = Permute([2, 1], name='attention_layer')(attention)
sent_representation = merge([activations, attention], mode='mul')
sent_representation = Lambda(lambda xin: K.sum(xin, axis=1), name='merged_layer')(sent_representation)
tweet_output = Dense(classes, activation='softmax', name='output_layer')(sent_representation)
tweetnet = Model(tweet_input, tweet_output)
tweetnet.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return tweetnet
def lin_interpolation_2d(inp, dim):
num_rows, num_cols, num_filters = K.int_shape(inp)[1:]
conv = SeparableConv2D(num_filters, (num_rows, num_cols), use_bias=False)
x = conv(inp)
w = conv.get_weights()
w[0].fill(0)
w[1].fill(0)
linspace = linspace_2d(num_rows, num_cols, dim=dim)
for i in range(num_filters):
w[0][:,:, i, 0] = linspace[:,:]
w[1][0, 0, i, i] = 1.
conv.set_weights(w)
conv.trainable = False
x = Lambda(lambda x: K.squeeze(x, axis=1))(x)
x = Lambda(lambda x: K.squeeze(x, axis=1))(x)
x = Lambda(lambda x: K.expand_dims(x, axis=-1))(x)
return x
def inception_resnet_v2_B(input, scale_residual=True):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
# Input is relu activation
init = input
ir1 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(160, 1, 7, activation='relu', border_mode='same')(ir2)
ir2 = Convolution2D(192, 7, 1, activation='relu', border_mode='same')(ir2)
ir_merge = merge([ir1, ir2], mode='concat', concat_axis=channel_axis)
ir_conv = Convolution2D(1152, 1, 1, activation='linear', border_mode='same')(ir_merge)
if scale_residual: ir_conv = Lambda(lambda x: x * 0.1)(ir_conv)
out = merge([init, ir_conv], mode='sum')
out = BatchNormalization(axis=channel_axis)(out)
out = Activation("relu")(out)
return out
def inception_resnet_v2_C(input, scale_residual=True):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
# Input is relu activation
init = input
ir1 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(192, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(224, 1, 3, activation='relu', border_mode='same')(ir2)
ir2 = Convolution2D(256, 3, 1, activation='relu', border_mode='same')(ir2)
ir_merge = merge([ir1, ir2], mode='concat', concat_axis=channel_axis)
ir_conv = Convolution2D(2144, 1, 1, activation='linear', border_mode='same')(ir_merge)
if scale_residual: ir_conv = Lambda(lambda x: x * 0.1)(ir_conv)
out = merge([init, ir_conv], mode='sum')
out = BatchNormalization(axis=channel_axis)(out)
out = Activation("relu")(out)
return out
def inception_resnet_B(input, scale_residual=True):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
# Input is relu activation
init = input
ir1 = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(input)
ir2 = Convolution2D(128, 1, 7, activation='relu', border_mode='same')(ir2)
ir2 = Convolution2D(128, 7, 1, activation='relu', border_mode='same')(ir2)
ir_merge = merge([ir1, ir2], mode='concat', concat_axis=channel_axis)
ir_conv = Convolution2D(896, 1, 1, activation='linear', border_mode='same')(ir_merge)
if scale_residual: ir_conv = Lambda(lambda x: x * 0.1)(ir_conv)
out = merge([init, ir_conv], mode='sum')
out = BatchNormalization(axis=channel_axis)(out)
out = Activation("relu")(out)
return out
def __init__(self,
vocab_size,
sequence_size,
setting=None,
checkpoint_path="",
temperature=10,
tying=False):
super().__init__(vocab_size, sequence_size, setting, checkpoint_path)
self.temperature = temperature
self.tying = tying
self.gamma = self.setting.gamma
if tying:
self.model.pop() # remove activation
self.model.pop() # remove projection (use self embedding)
self.model.add(Lambda(lambda x: K.dot(x, K.transpose(self.embedding.embeddings))))
self.model.add(Activation("softmax"))
def to_multi_gpu(model, n_gpus=2):
if n_gpus ==1:
return model
with tf.device('/cpu:0'):
x = Input(model.input_shape[1:])
towers = []
for g in range(n_gpus):
with tf.device('/gpu:' + str(g)):
slice_g = Lambda(slice_batch, lambda shape: shape, arguments={'n_gpus':n_gpus, 'part':g})(x)
towers.append(model(slice_g))
with tf.device('/cpu:0'):
# Deprecated
#merged = merge(towers, mode='concat', concat_axis=0)
merged = Concatenate(axis=0)(towers)
return Model(inputs=[x], outputs=merged)
def loss_net(self) -> Model:
"""Returns the network that yields a loss given both input spectrograms and labels. Used for training."""
input_batch = self._input_batch_input
label_batch = Input(name=Wav2Letter.InputNames.label_batch, shape=(None,), dtype='int32')
label_lengths = Input(name=Wav2Letter.InputNames.label_lengths, shape=(1,), dtype='int64')
asg_transition_probabilities_variable = backend.variable(value=self.asg_transition_probabilities,
name="asg_transition_probabilities")
asg_initial_probabilities_variable = backend.variable(value=self.asg_initial_probabilities,
name="asg_initial_probabilities")
# Since Keras doesn't currently support loss functions with extra parameters,
# we define a custom lambda layer yielding one single real-valued CTC loss given the grapheme probabilities:
loss_layer = Lambda(Wav2Letter._asg_lambda if self.use_asg else Wav2Letter._ctc_lambda,
name='asg_loss' if self.use_asg else 'ctc_loss',
output_shape=(1,),
arguments={"transition_probabilities": asg_transition_probabilities_variable,
"initial_probabilities": asg_initial_probabilities_variable} if self.use_asg else None)
# ([asg_transition_probabilities_variable, asg_initial_probabilities_variable] if self.use_asg else [])
# This loss layer is placed atop the predictive network and provided with additional arguments,
# namely the label batch and prediction/label sequence lengths:
loss = loss_layer(
[self.predictive_net(input_batch), label_batch, self._prediction_lengths_input, label_lengths])
loss_net = Model(inputs=[input_batch, label_batch, self._prediction_lengths_input, label_lengths],
outputs=[loss])
# Since loss is already calculated in the last layer of the net, we just pass through the results here.
# The loss dummy labels have to be given to satify the Keras API.
loss_net.compile(loss=lambda dummy_labels, ctc_loss: ctc_loss, optimizer=self.optimizer)
return loss_net
def decoding_net(self):
decoding_layer = Lambda(self._decode_lambda, name='ctc_decode')
prediction_batch = self.predictive_net(self._input_batch_input)
decoded = decoding_layer([prediction_batch, self._prediction_lengths_input])
return Model(inputs=[self._input_batch_input, self._prediction_lengths_input], outputs=[decoded])