本文整理匯總了Python中tensorflow.keras.backend.concatenate方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.concatenate方法的具體用法?Python backend.concatenate怎麽用?Python backend.concatenate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.concatenate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def call(self, inputs):
features = inputs[0]
fltr = inputs[1]
# Enforce sparse representation
if not K.is_sparse(fltr):
fltr = ops.dense_to_sparse(fltr)
# Propagation
indices = fltr.indices
N = tf.shape(features, out_type=indices.dtype)[0]
indices = ops.sparse_add_self_loops(indices, N)
targets, sources = indices[:, -2], indices[:, -1]
messages = tf.gather(features, sources)
aggregated = self.aggregate_op(messages, targets, N)
output = K.concatenate([features, aggregated])
output = ops.dot(output, self.kernel)
if self.use_bias:
output = K.bias_add(output, self.bias)
output = K.l2_normalize(output, axis=-1)
if self.activation is not None:
output = self.activation(output)
return output
示例2: _rotation_matrix_zyz
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def _rotation_matrix_zyz(self, params):
phi = params[0] * 2 * np.pi - np.pi; theta = params[1] * 2 * np.pi - np.pi; psi_t = params[2] * 2 * np.pi - np.pi;
loc_r = params[3:6] * 2 - 1
a1 = self._rotation_matrix_axis(2, psi_t) # first rotate about z axis for angle psi_t
a2 = self._rotation_matrix_axis(1, theta)
a3 = self._rotation_matrix_axis(2, phi)
rm = K.dot(K.dot(a3,a2),a1)
rm = tf.transpose(rm)
c = K.dot(-rm, K.expand_dims(loc_r))
rm = K.flatten(rm)
theta = K.concatenate([rm[:3], c[0], rm[3:6], c[1], rm[6:9], c[2]])
return theta
示例3: _mask_rotation_matrix_zyz
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def _mask_rotation_matrix_zyz(self, params):
phi = params[0] * 2 * np.pi - np.pi; theta = params[1] * 2 * np.pi - np.pi; psi_t = params[2] * 2 * np.pi - np.pi;
loc_r = params[3:6] * 0 # magnitude of Fourier transformation is translation-invariant
a1 = self._rotation_matrix_axis(2, psi_t)
a2 = self._rotation_matrix_axis(1, theta)
a3 = self._rotation_matrix_axis(2, phi)
rm = K.dot(K.dot(a3,a2),a1)
rm = tf.transpose(rm)
c = K.dot(-rm, K.expand_dims(loc_r))
rm = K.flatten(rm)
theta = K.concatenate([rm[:3], c[0], rm[3:6], c[1], rm[6:9], c[2]])
return theta
示例4: surv_likelihood
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def surv_likelihood(n_intervals):
"""Create custom Keras loss function for neural network survival model.
Arguments
n_intervals: the number of survival time intervals
Returns
Custom loss function that can be used with Keras
"""
def loss(y_true, y_pred):
"""
Required to have only 2 arguments by Keras.
Arguments
y_true: Tensor.
First half of the values is 1 if individual survived that interval, 0 if not.
Second half of the values is for individuals who failed, and is 1 for time interval during which failure occured, 0 for other intervals.
See make_surv_array function.
y_pred: Tensor, predicted survival probability (1-hazard probability) for each time interval.
Returns
Vector of losses for this minibatch.
"""
cens_uncens = 1. + y_true[:,0:n_intervals] * (y_pred-1.) #component for all individuals
uncens = 1. - y_true[:,n_intervals:2*n_intervals] * y_pred #component for only uncensored individuals
return K.sum(-K.log(K.clip(K.concatenate((cens_uncens,uncens)),K.epsilon(),None)),axis=-1) #return -log likelihood
return loss
示例5: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def call(self, x, **kwargs):
assert isinstance(x, list)
inp_a, inp_b = x
last_state = K.expand_dims(inp_b[:, -1, :], 1)
m = []
for i in range(self.output_dim):
outp_a = inp_a * self.W[i]
outp_last = last_state * self.W[i]
outp_a = K.l2_normalize(outp_a, -1)
outp_last = K.l2_normalize(outp_last, -1)
outp = K.batch_dot(outp_a, outp_last, axes=[2, 2])
m.append(outp)
if self.output_dim > 1:
persp = K.concatenate(m, 2)
else:
persp = m[0]
return [persp, persp]
示例6: _find_maxima
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def _find_maxima(x, coordinate_scale=1, confidence_scale=255.0):
x = K.cast(x, K.floatx())
col_max = K.max(x, axis=1)
row_max = K.max(x, axis=2)
maxima = K.max(col_max, 1)
maxima = K.expand_dims(maxima, -2) / confidence_scale
cols = K.cast(K.argmax(col_max, -2), K.floatx())
rows = K.cast(K.argmax(row_max, -2), K.floatx())
cols = K.expand_dims(cols, -2) * coordinate_scale
rows = K.expand_dims(rows, -2) * coordinate_scale
maxima = K.concatenate([cols, rows, maxima], -2)
return maxima
示例7: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def call(self, x):
output = self._spectrogram_mono(x[:, 0:1, :])
if self.is_mono is False:
for ch_idx in range(1, self.n_ch):
output = K.concatenate(
(output, self._spectrogram_mono(x[:, ch_idx : ch_idx + 1, :])),
axis=self.ch_axis_idx,
)
if self.power_spectrogram != 2.0:
output = K.pow(K.sqrt(output), self.power_spectrogram)
if self.return_decibel_spectrogram:
output = backend_keras.amplitude_to_decibel(output)
return output
示例8: rel_to_abs
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def rel_to_abs(self, x):
shape = K.shape(x)
shape = [shape[i] for i in range(3)]
B, Nh, L, = shape
col_pad = K.zeros(K.stack([B, Nh, L, 1]))
x = K.concatenate([x, col_pad], axis=3)
flat_x = K.reshape(x, [B, Nh, L * 2 * L])
flat_pad = K.zeros(K.stack([B, Nh, L - 1]))
flat_x_padded = K.concatenate([flat_x, flat_pad], axis=2)
final_x = K.reshape(flat_x_padded, [B, Nh, L + 1, 2 * L - 1])
final_x = final_x[:, :, :L, L - 1:]
return final_x
示例9: augmented_conv2d
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def augmented_conv2d(ip, filters, kernel_size=(3, 3), strides=(1, 1),
depth_k=0.2, depth_v=0.2, num_heads=8, relative_encodings=True):
"""
Builds an Attention Augmented Convolution block.
Args:
ip: keras tensor.
filters: number of output filters.
kernel_size: convolution kernel size.
strides: strides of the convolution.
depth_k: float or int. Number of filters for k.
Computes the number of filters for `v`.
If passed as float, computed as `filters * depth_k`.
depth_v: float or int. Number of filters for v.
Computes the number of filters for `k`.
If passed as float, computed as `filters * depth_v`.
num_heads: int. Number of attention heads.
Must be set such that `depth_k // num_heads` is > 0.
relative_encodings: bool. Whether to use relative
encodings or not.
Returns:
a keras tensor.
"""
# input_shape = K.int_shape(ip)
channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
depth_k, depth_v = _normalize_depth_vars(depth_k, depth_v, filters)
conv_out = _conv_layer(filters - depth_v, kernel_size, strides)(ip)
# Augmented Attention Block
qkv_conv = _conv_layer(2 * depth_k + depth_v, (1, 1), strides)(ip)
attn_out = AttentionAugmentation2D(depth_k, depth_v, num_heads, relative_encodings)(qkv_conv)
attn_out = _conv_layer(depth_v, kernel_size=(1, 1))(attn_out)
output = concatenate([conv_out, attn_out], axis=channel_axis)
output = BatchNormalization()(output)
return output
示例10: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def call(self, inputs):
"""Execute this layer on input tensors.
Parameters
----------
inputs: list
List of two tensors (X, Xp). X should be of shape (n_test,
n_feat) and Xp should be of shape (n_support, n_feat) where
n_test is the size of the test set, n_support that of the support
set, and n_feat is the number of per-atom features.
Returns
-------
list
Returns two tensors of same shape as input. Namely the output
shape will be [(n_test, n_feat), (n_support, n_feat)]
"""
if len(inputs) != 2:
raise ValueError("AttnLSTMEmbedding layer must have exactly two parents")
# x is test set, xp is support set.
x, xp = inputs
# Get initializations
q = self.q_init
states = self.states_init
for d in range(self.max_depth):
# Process using attention
# Eqn (4), appendix A.1 of Matching Networks paper
e = _cosine_dist(x + q, xp)
a = tf.nn.softmax(e)
r = backend.dot(a, xp)
# Generate new attention states
y = backend.concatenate([q, r], axis=1)
q, states = self.lstm([y] + states)
return [x + q, xp]
示例11: build
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def build(self, input_shape):
init = initializers.get(self.init)
self.U = init((2 * self.n_hidden, 4 * self.n_hidden))
self.b = tf.Variable(
np.concatenate((np.zeros(self.n_hidden), np.ones(self.n_hidden),
np.zeros(self.n_hidden), np.zeros(self.n_hidden))),
dtype=tf.float32)
self.built = True
示例12: message
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def message(self, X, **kwargs):
X_i = self.get_i(X)
X_j = self.get_j(X)
return self.mlp(K.concatenate((X_i, X_j - X_i)))
示例13: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def call(self, inputs, **kwargs):
X, A, E = self.get_inputs(inputs)
edge_weight = A.values
output = [X]
for k in range(self.K):
output.append(self.propagate(X, A, E, edge_weight=edge_weight))
output = K.concatenate(output)
return self.linear(output)
示例14: message
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def message(self, X, E=None):
X_i = self.get_i(X)
X_j = self.get_j(X)
Z = K.concatenate((X_i, X_j, E), axis=-1)
output = self.dense_s(Z) * self.dense_f(Z)
return output
示例15: _concat
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import concatenate [as 別名]
def _concat(lists, dim):
if lists[0].size == 0:
lists = lists[1:]
return np.concatenate(lists, dim)