本文整理汇总了Python中keras.backend.in_train_phase方法的典型用法代码示例。如果您正苦于以下问题:Python backend.in_train_phase方法的具体用法?Python backend.in_train_phase怎么用?Python backend.in_train_phase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.in_train_phase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.output_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
if 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, input_dim))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
return constants
示例2: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def call(self,x,training=None):
deta1 = 0.3
deta2 = 0.3
deta3 = 0.3
seed = np.random.randint(1, 10e6)
rng = RandomStreams(seed=seed)
theta1 = rng.uniform(size=(x.shape[0],1),low=-deta1,high=deta1,dtype='float32')
theta2 = rng.uniform(size=(x.shape[0],1),low=-deta2,high=deta2,dtype='float32')
theta3 = rng.uniform(size=(x.shape[0],1),low=-deta3,high=deta3,dtype='float32')
theta = K.concatenate([theta1,theta2,theta3],axis=-1)
theta = K.tile(theta,x.shape[1])
theta = theta.reshape((x.shape[0], x.shape[1], 3))
theta = theta.reshape((theta.shape[0]*theta.shape[1], theta.shape[2]))
M = _fusion(theta)
output = _transform_rot(M, x)
return K.in_train_phase(output,x,training = training)
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:20,代码来源:transform_rnn.py
示例3: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.hidden_recurrent_dim))
B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones)
constants.append(B_U)
else:
constants.append(K.cast_to_floatx(1.))
if self.consume_less == 'cpu' and 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, input_dim))
B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones)
constants.append(B_W)
else:
constants.append(K.cast_to_floatx(1.))
return constants
示例4: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.input_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
if 0 < self.dropout_W < 1:
input_shape = K.int_shape(x)
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, int(input_dim)))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
return constants
示例5: sample_h_given_x
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def sample_h_given_x(self, x):
h_pre = K.dot(x, self.Wrbm) + self.bh
h_sigm = self.activation(self.scaling_h_given_x * h_pre)
# drop out noise
#if(0.0 < self.p < 1.0):
# noise_shape = self._get_noise_shape(h_sigm)
# h_sigm = K.in_train_phase(K.dropout(h_sigm, self.p, noise_shape), h_sigm)
if(self.hidden_unit_type == 'binary'):
h_samp = K.random_binomial(shape=h_sigm.shape, p=h_sigm)
# random sample
# \hat{h} = 1, if p(h=1|x) > uniform(0, 1)
# 0, otherwise
elif(self.hidden_unit_type == 'nrlu'):
h_samp = nrlu(h_pre)
else:
h_samp = h_sigm
if(0.0 < self.p < 1.0):
noise_shape = self._get_noise_shape(h_samp)
h_samp = K.in_train_phase(K.dropout(h_samp, self.p, noise_shape), h_samp)
return h_samp, h_pre, h_sigm
示例6: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.output_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
if 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, int(input_dim)))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
return constants
示例7: preprocess_input
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def preprocess_input(self, inputs, training=None):
if self.window_size > 1:
inputs = K.temporal_padding(inputs, (self.window_size - 1, 0))
inputs = K.expand_dims(inputs, 2) # add a dummy dimension
output = K.conv2d(inputs, self.kernel, strides=self.strides,
padding='valid',
data_format='channels_last')
output = K.squeeze(output, 2) # remove the dummy dimension
if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
if self.dropout is not None and 0. < self.dropout < 1.:
z = output[:, :, :self.units]
f = output[:, :, self.units:2 * self.units]
o = output[:, :, 2 * self.units:]
f = K.in_train_phase(1 - _dropout(1 - f, self.dropout), f, training=training)
return K.concatenate([z, f, o], -1)
else:
return output
示例8: _compute_probabilities
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def _compute_probabilities(self, energy, previous_attention=None):
if self.is_monotonic:
# add presigmoid noise to encourage discreteness
sigmoid_noise = K.in_train_phase(1., 0.)
noise = K.random_normal(K.shape(energy), mean=0.0, stddev=sigmoid_noise)
# encourage discreteness in train
energy = K.in_train_phase(energy + noise, energy)
p = K.in_train_phase(K.sigmoid(energy),
K.cast(energy > 0, energy.dtype))
p = K.squeeze(p, -1)
p_prev = K.squeeze(previous_attention, -1)
# monotonic attention function from tensorflow
at = K.in_train_phase(
tf.contrib.seq2seq.monotonic_attention(p, p_prev, 'parallel'),
tf.contrib.seq2seq.monotonic_attention(p, p_prev, 'hard'))
at = K.expand_dims(at, -1)
else:
# softmax
at = keras.activations.softmax(energy, axis=1)
return at
示例9: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def call(self, X, mask=None):
if mask is not None:
assert K.ndim(mask) == 2, 'Input mask to CRF must have dim 2 if not None'
if self.test_mode == 'viterbi':
test_output = self.viterbi_decoding(X, mask)
else:
test_output = self.get_marginal_prob(X, mask)
self.uses_learning_phase = True
if self.learn_mode == 'join':
train_output = K.zeros_like(K.dot(X, self.kernel))
out = K.in_train_phase(train_output, test_output)
else:
if self.test_mode == 'viterbi':
train_output = self.get_marginal_prob(X, mask)
out = K.in_train_phase(train_output, test_output)
else:
out = test_output
return out
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def call(self, inputs, training=None):
"""
The computation logic of DecayingDropoutLayer.
:param inputs: an input tensor.
"""
noise_shape = self._get_noise_shape(inputs)
t = tf.cast(self._iterations, K.floatx()) + 1
p = t / float(self._decay_interval)
keep_rate = self._initial_keep_rate * tf.pow(self._decay_rate, p)
def dropped_inputs():
update_op = self._iterations.assign_add([1])
with tf.control_dependencies([update_op]):
return tf.nn.dropout(inputs, 1 - keep_rate[0], noise_shape,
seed=self._seed)
return K.in_train_phase(dropped_inputs, inputs, training=training)
示例11: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.concatenate([ones] * self.output_dim, 1)
B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones)
constants.append(B_U)
else:
constants.append(K.cast_to_floatx(1.))
if self.consume_less == 'cpu' and 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.concatenate([ones] * input_dim, 1)
B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones)
constants.append(B_W)
else:
constants.append(K.cast_to_floatx(1.))
return constants
示例12: Orthonorm
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def Orthonorm(x, name=None):
'''
Builds keras layer that handles orthogonalization of x
x: an n x d input matrix
name: name of the keras layer
returns: a keras layer instance. during evaluation, the instance returns an n x d orthogonal matrix
if x is full rank and not singular
'''
# get dimensionality of x
d = x.get_shape().as_list()[-1]
# compute orthogonalizing matrix
ortho_weights = orthonorm_op(x)
# create variable that holds this matrix
ortho_weights_store = K.variable(np.zeros((d,d)))
# create op that saves matrix into variable
ortho_weights_update = tf.assign(ortho_weights_store, ortho_weights, name='ortho_weights_update')
# switch between stored and calculated weights based on training or validation
l = Lambda(lambda x: K.in_train_phase(K.dot(x, ortho_weights), K.dot(x, ortho_weights_store)), name=name)
l.add_update(ortho_weights_update)
return l
示例13: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.output_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
if 0 < self.dropout_W < 1:
input_shape = K.int_shape(x)
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, int(input_dim)))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
return constants
示例14: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def get_constants(self, inputs, training=None):
constants = []
'''if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.units))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
if 0 < self.dropout_W < 1:
input_shape = K.int_shape(x)
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, int(input_dim)))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)]
constants.append(B_W)
else:'''
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
return constants
示例15: preprocess_input
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import in_train_phase [as 别名]
def preprocess_input(self, inputs, training=None):
if self.window_size > 1:
inputs = K.temporal_padding(inputs, (self.window_size-1, 0))
inputs = K.expand_dims(inputs, 2) # add a dummy dimension
output = K.conv2d(inputs, self.kernel, strides=self.strides,
padding='valid',
data_format='channels_last')
output = K.squeeze(output, 2) # remove the dummy dimension
if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
if self.dropout is not None and 0. < self.dropout < 1.:
z = output[:, :, :self.units]
f = output[:, :, self.units:2 * self.units]
o = output[:, :, 2 * self.units:]
f = K.in_train_phase(1 - _dropout(1 - f, self.dropout), f, training=training)
return K.concatenate([z, f, o], -1)
else:
return output