本文整理汇总了Python中keras.backend.prod方法的典型用法代码示例。如果您正苦于以下问题:Python backend.prod方法的具体用法?Python backend.prod怎么用?Python backend.prod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.prod方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_output
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def get_output(self, train=False):
def format_shape(shape):
if K._BACKEND == 'tensorflow':
def trf(x):
try:
return int(x)
except TypeError:
return x
return map(trf, shape)
return shape
X = self.get_input(train)
in_shape = format_shape(K.shape(X))
batch_flatten_len = K.prod(in_shape[:2])
cast_in_shape = (batch_flatten_len, ) + tuple(in_shape[i] for i in range(2, K.ndim(X)))
pre_outs = self.layer(K.reshape(X, cast_in_shape))
out_shape = format_shape(K.shape(pre_outs))
cast_out_shape = (in_shape[0], in_shape[1]) + tuple(out_shape[i] for i in range(1, K.ndim(pre_outs)))
outputs = K.reshape(pre_outs, cast_out_shape)
return outputs
示例2: tf_normal
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def tf_normal(y_true, mu, sigma, pi):
rollout_length = K.shape(y_true)[1]
y_true = K.tile(y_true,(1,1,GAUSSIAN_MIXTURES))
y_true = K.reshape(y_true, [-1, rollout_length, GAUSSIAN_MIXTURES,Z_DIM])
oneDivSqrtTwoPI = 1 / math.sqrt(2*math.pi)
result = y_true - mu
# result = K.permute_dimensions(result, [2,1,0])
result = result * (1 / (sigma + 1e-8))
result = -K.square(result)/2
result = K.exp(result) * (1/(sigma + 1e-8))*oneDivSqrtTwoPI
result = result * pi
result = K.sum(result, axis=2) #### sum over gaussians
#result = K.prod(result, axis=2) #### multiply over latent dims
return result
示例3: inst_weight
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def inst_weight(output_y, output_x, output_dr, output_dl, config=None):
dy = output_y[:,2:,2:]-output_y[:, :-2,2:] + \
2*(output_y[:,2:,1:-1]- output_y[:,:-2,1:-1]) + \
output_y[:,2:,:-2]-output_y[:,:-2,:-2]
dx = output_x[:,2:,2:]- output_x[:,2:,:-2] + \
2*( output_x[:,1:-1,2:]- output_x[:,1:-1,:-2]) +\
output_x[:,:-2,2:]- output_x[:,:-2,:-2]
ddr= (output_dr[:,2:,2:]-output_dr[:,:-2,:-2] +\
output_dr[:,1:-1,2:]-output_dr[:,:-2,1:-1]+\
output_dr[:,2:,1:-1]-output_dr[:,1:-1,:-2])*K.constant(2)
ddl= (output_dl[:,2:,:-2]-output_dl[:,:-2,2:] +\
output_dl[:,2:,1:-1]-output_dl[:,1:-1,2:]+\
output_dl[:,1:-1,:-2]-output_dl[:,:-2,1:-1])*K.constant(2)
dpred = K.concatenate([dy,dx,ddr,ddl],axis=-1)
dpred = K.spatial_2d_padding(dpred)
weight_fg = K.cast(K.all(dpred>K.constant(config.GRADIENT_THRES), axis=3,
keepdims=True), K.floatx())
weight = K.clip(K.sqrt(weight_fg*K.prod(dpred, axis=3, keepdims=True)),
config.WEIGHT_AREA/config.CLIP_AREA_HIGH,
config.WEIGHT_AREA/config.CLIP_AREA_LOW)
weight +=(1-weight_fg)*config.WEIGHT_AREA/config.BG_AREA
weight = K.conv2d(weight, K.constant(config.GAUSSIAN_KERNEL),
padding='same')
return K.stop_gradient(weight)
示例4: get_iou
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def get_iou(bbox_base, bbox_target):
"""2つのBoundingBoxのIoU(Intersection Over Union)を取得する。
https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
Args:
bbox_base (ndarray): 基準になるBoudingBox。
Its shape is :math:`(N, 4)`.
2軸目に以下の順でBBoxの座標を保持する。
:math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
bbox_target (ndarray): BoudingBox。
Its shape is :math:`(K, 4)`.
2軸目に以下の順でBBoxの座標を保持する。
:math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
bbox_baseの各Box毎にbbox_targetを適用し、IoUを求める。
Returns:
ndarray:
IoU(0 <= IoU <= 1)
形状は以下の通り。
:math:`(N, K)`.
"""
if bbox_base.shape[1] != 4 or bbox_target.shape[1] != 4:
raise IndexError
# 交差領域の左上の座標
# bbox_base[:, None, :]のより次元を増やすことで、
# bbox_baseとbbox_targetを総当りで評価出来る。
# (N, K, 2)の座標が得られる
tl = np.maximum(bbox_base[:, None, :2], bbox_target[:, :2])
# 交差領域の右下の座標
# (N, K, 2)の座標が得られる
br = np.minimum(bbox_base[:, None, 2:], bbox_target[:, 2:])
# 右下-左下=交差領域の(h, w)が得られる。
# h*wで交差領域の面積。ただし、交差領域がない(右下 <= 左上)ものは除くため0とする。
area_i = np.prod(br - tl, axis=2) * \
np.all(br > tl, axis=2).astype('float32')
area_base = np.prod(bbox_base[:, 2:] - bbox_base[:, :2], axis=1)
area_target = np.prod(bbox_target[:, 2:] - bbox_target[:, :2], axis=1)
return area_i / (area_base[:, None] + area_target - area_i)
示例5: get_iou_K
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def get_iou_K(bbox_base, bbox_target):
"""2つのBoundingBoxのIoU(Intersection Over Union)を取得する。
https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
Args:
bbox_base (tensor): 基準になるBoudingBox。
Its shape is :math:`(N, 4)`.
2軸目に以下の順でBBoxの座標を保持する。
:math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
bbox_target (tensor): BoudingBox。
Its shape is :math:`(K, 4)`.
2軸目に以下の順でBBoxの座標を保持する。
:math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
bbox_baseの各Box毎にbbox_targetを適用し、IoUを求める。
Returns:
tensor:
IoU(0 <= IoU <= 1)
形状は以下の通り。
:math:`(N, K)`.
"""
if bbox_base.shape[1] != 4 or bbox_target.shape[1] != 4:
raise IndexError
# 交差領域の左上の座標
# bbox_base[:, None, :]のより次元を増やすことで、
# bbox_baseとbbox_targetを総当りで評価出来る。
# (N, K, 2)の座標が得られる
tl = K.maximum(bbox_base[:, None, :2], bbox_target[:, :2])
# 交差領域の右下の座標
# (N, K, 2)の座標が得られる
br = K.minimum(bbox_base[:, None, 2:], bbox_target[:, 2:])
# 右下-左下=交差領域の(h, w)が得られる。
# h*wで交差領域の面積。ただし、交差領域がない(右下 <= 左上)ものは除くため0とする。
area_i = K.prod(br - tl, axis=2) * \
K.cast(K.all(br > tl, axis=2), 'float32')
area_base = K.prod(bbox_base[:, 2:] - bbox_base[:, :2], axis=1)
area_target = K.prod(bbox_target[:, 2:] - bbox_target[:, :2], axis=1)
return area_i / (area_base[:, None] + area_target - area_i)
示例6: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def call(self, inputs, mask=None):
# shape: (batch size, ..., num_probs, ...)
probabilities = inputs
if mask is not None:
probabilities *= K.cast(mask, "float32")
noisy_probs = self.noise_parameter * probabilities
# shape: (batch size, ..., num_probs, ...)
noisy_probs = 1.0 - noisy_probs
# shape: (batch size, ..., ...)
probability_product = K.prod(noisy_probs, axis=self.axis)
return 1.0 - probability_product
示例7: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def call(self, x, mask=None):
# theano.scan doesn't seem to work when intermediate results' changes in shape -- Alexander
res = x
# core_arr_idx = 0 # oroginal implementation, removed
for k in range(self.num_dim - 1, -1, -1):
"""
These are the original codes by Alexander, which calculate the shapes ad-hoc.
Feel free to switch these codes on if one is interested in comparing performances.
At least I only observe very small differences.
# res is of size o_k+1 x ... x o_d x batch_size x i_1 x ... x i_k-1 x i_k x r_k+1
curr_shape = (self.tt_input_shape[k] * self.tt_ranks[k + 1], self.tt_ranks[k] * self.tt_output_shape[k])
curr_core = self.W[core_arr_idx:core_arr_idx+K.prod(curr_shape)].reshape(curr_shape)
res = K.dot(res.reshape((-1, curr_shape[0])), curr_core)
# res is of size o_k+1 x ... x o_d x batch_size x i_1 x ... x i_k-1 x r_k x o_k
res = K.transpose(res.reshape((-1, self.tt_output_shape[k])))
# res is of size o_k x o_k+1 x ... x o_d x batch_size x i_1 x ... x i_k-1 x r_k
core_arr_idx += K.prod(curr_shape)
"""
# New one, in order to avoid calculating the indices in every iteration
res = K.dot(K.reshape(res, (-1, self.shapes[k][0])), # of shape (-1, m_k*r_{k+1})
K.reshape(self.cores[k], self.shapes[k]) # of shape (m_k*r_{k+1}, r_k*n_k)
)
res = K.transpose(
K.reshape(res, (-1, self.tt_output_shape[k]))
)
# res is of size o_1 x ... x o_d x batch_size # by Alexander
res = K.transpose(K.reshape(res, (-1, K.shape(x)[0])))
if self.use_bias:
res = K.bias_add(res, self.bias)
if self.activation is not None:
res =self.activation(res)
return res
示例8: get_output_shape_for
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def get_output_shape_for(self, input_shape):
return (input_shape[0], np.prod(self.tt_output_shape))
示例9: compute_output_shape
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def compute_output_shape(self, input_shape):
# assert input_shape and len(input_shape) >= 2
# assert input_shape[-1]
# output_shape = list(input_shape)
# output_shape[-1] = self.units
return (input_shape[0], np.prod(self.tt_output_shape))
示例10: ncc
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def ncc(self, I, J):
# get dimension of volume
# assumes I, J are sized [batch_size, *vol_shape, nb_feats]
ndims = len(I.get_shape().as_list()) - 2
assert ndims in [1, 2, 3], "volumes should be 1 to 3 dimensions. found: %d" % ndims
# set window size
if self.win is None:
self.win = [9] * ndims
# get convolution function
conv_fn = getattr(tf.nn, 'conv%dd' % ndims)
# compute CC squares
I2 = I*I
J2 = J*J
IJ = I*J
# compute filters
sum_filt = tf.ones([*self.win, 1, 1])
strides = 1
if ndims > 1:
strides = [1] * (ndims + 2)
padding = 'SAME'
# compute local sums via convolution
I_sum = conv_fn(I, sum_filt, strides, padding)
J_sum = conv_fn(J, sum_filt, strides, padding)
I2_sum = conv_fn(I2, sum_filt, strides, padding)
J2_sum = conv_fn(J2, sum_filt, strides, padding)
IJ_sum = conv_fn(IJ, sum_filt, strides, padding)
# compute cross correlation
win_size = np.prod(self.win)
u_I = I_sum/win_size
u_J = J_sum/win_size
cross = IJ_sum - u_J*I_sum - u_I*J_sum + u_I*u_J*win_size
I_var = I2_sum - 2 * u_I * I_sum + u_I*u_I*win_size
J_var = J2_sum - 2 * u_J * J_sum + u_J*u_J*win_size
cc = cross*cross / (I_var*J_var + self.eps)
# return negative cc.
return tf.reduce_mean(cc)
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def call(self, x, mask=None):
N_DECISION = (2 ** (self.n_depth)) - 1 # Number of decision nodes
N_LEAF = 2 ** (self.n_depth + 1) # Number of leaf nodes
flat_decision_p_e = []
leaf_p_e = []
for w_d, w_l in zip(self.w_d_ensemble, self.w_l_ensemble):
decision_p = K.sigmoid((K.dot(x, w_d)))
leaf_p = K.softmax(w_l)
decision_p_comp = 1 - decision_p
decision_p_pack = K.concatenate([decision_p, decision_p_comp])
flat_decision_p_e.append(decision_p_pack)
leaf_p_e.append(leaf_p)
#Construct tiling pattern for decision probability matrix
#Could be done in TF, but I think it's better statically
tiling_pattern = np.zeros((N_LEAF, self.n_depth), dtype=np.int32)
comp_offset = N_DECISION
dec_idx = 0
for n in xrange(self.n_depth):
j = 0
for depth_idx in xrange(2**n):
repeat_times = 2 ** (self.n_depth - n)
for _ in xrange(repeat_times):
tiling_pattern[j][n] = dec_idx
j = j + 1
for _ in xrange(repeat_times):
tiling_pattern[j][n] = comp_offset + dec_idx
j = j + 1
dec_idx = dec_idx + 1
flat_pattern = tiling_pattern.flatten()
# iterate over each tree
tree_ret = None
for flat_decision_p, leaf_p in zip(flat_decision_p_e, leaf_p_e):
flat_mu = tf.transpose(tf.gather(tf.transpose(flat_decision_p), flat_pattern))
batch_size = tf.shape(flat_decision_p)[0]
shape = tf.pack([batch_size, N_LEAF, self.n_depth])
mu = K.reshape(flat_mu, shape)
leaf_prob = K.prod(mu, [2])
prob_label = K.dot(leaf_prob, leaf_p)
if tree_ret is None:
tree_ret = prob_label
else:
tree_ret = tree_ret + prob_label
return tree_ret/self.n_trees
示例12: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def call(self, x, mask=None):
input_vector = x[0]
target_classes = x[1]
nb_req_classes = self.input_spec[1].shape[1]
if nb_req_classes is None:
nb_req_classes = K.shape(target_classes)
if K.dtype(target_classes) != 'int32':
target_classes = K.cast(target_classes, 'int32')
if self.mode == 0:
# One giant matrix mul
input_dim = self.input_spec[0].shape[1]
nb_req_classes = self.input_spec[1].shape[1]
path_lengths = map(len, self.paths)
huffman_codes = K.variable(np.array(self.huffman_codes))
req_nodes = K.gather(self.class_path_map, target_classes)
req_W = K.gather(self.W, req_nodes)
y = K.batch_dot(input_vector, req_W, axes=(1, 3))
if self.bias:
req_b = K.gather(self.b, req_nodes)
y += req_b
y = K.sigmoid(y[:, :, :, 0])
req_huffman_codes = K.gather(huffman_codes, target_classes)
return K.prod(req_huffman_codes + y - 2 * req_huffman_codes * y, axis=-1) # Thug life
elif self.mode == 1:
# Many tiny matrix muls
probs = []
for i in range(len(self.paths)):
huffman_code = self.huffman_codes[i]
path = self.paths[i]
prob = 1.
for j in range(len(path)):
node = path[j]
node_index = self.node_indices[node]
p = K.dot(input_vector, self.W[node_index, :, :])[:, 0]
if self.bias:
p += self.b[node_index, :][0]
h = huffman_code[j]
p = K.sigmoid(p)
prob *= h + p - 2 * p * h
probs += [prob]
probs = K.pack(probs)
req_probs = K.gather(probs, target_classes)
req_probs = K.permute_dimensions(req_probs, (0, 2, 1))
req_probs = K.reshape(req_probs, (-1, nb_req_classes))
batch_size = K.shape(input_vector)[0]
indices = arange(batch_size * batch_size, batch_size + 1)
req_probs = K.gather(req_probs, indices)
return req_probs
示例13: init_orthogonal_tt_cores
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def init_orthogonal_tt_cores(tt_input_shape, tt_output_shape, tt_ranks):
tt_input_shape = np.array(tt_input_shape)
tt_output_shape = np.array(tt_output_shape)
tt_ranks = np.array(tt_ranks)
cores_arr_len = np.sum(tt_input_shape * tt_output_shape *
tt_ranks[1:] * tt_ranks[:-1])
cores_arr = np.zeros(cores_arr_len)
rv = 1
d = tt_input_shape.shape[0]
rng = np.random
shapes = [None] * d
tall_shapes = [None] * d
cores = [None] * d
counter = 0
for k in range(tt_input_shape.shape[0]):
# Original implementation
# shape = [ranks[k], input_shape[k], output_shape[k], ranks[k+1]]
shapes[k] = [tt_ranks[k], tt_input_shape[k], tt_output_shape[k], tt_ranks[k + 1]]
# Original implementation
# tall_shape = (np.prod(shape[:3]), shape[3])
tall_shapes[k] = (np.prod(shapes[k][:3]), shapes[k][3])
# Original implementation
# curr_core = np.dot(rv, np.random.randn(shape[0], np.prod(shape[1:])) )
cores[k] = np.dot(rv, rng.randn(shapes[k][0], np.prod(shapes[k][1:])))
# Original implementation
# curr_core = curr_core.reshape(tall_shape)
cores[k] = cores[k].reshape(tall_shapes[k])
if k < tt_input_shape.shape[0] - 1:
# Original implementation
# curr_core, rv = np.linalg.qr(curr_core)
cores[k], rv = np.linalg.qr(cores[k])
# Original implementation
# cores_arr[cores_arr_idx:cores_arr_idx+curr_core.size] = curr_core.flatten()
# cores_arr_idx += curr_core.size
cores_arr[counter:(counter + cores[k].size)] = cores[k].flatten()
counter += cores[k].size
glarot_style = (np.prod(tt_input_shape) * np.prod(tt_ranks)) ** (1.0 / tt_input_shape.shape[0])
return (0.1 / glarot_style) * cores_arr
示例14: _generate_orthogonal_tt_cores
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import prod [as 别名]
def _generate_orthogonal_tt_cores(self):
cores_arr_len = np.sum(self.tt_input_shape * self.tt_output_shape *
self.tt_ranks[1:] * self.tt_ranks[:-1])
cores_arr = np.zeros(cores_arr_len)
rv = 1
d = self.tt_input_shape.shape[0]
rng = np.random
shapes = [None] * d
tall_shapes = [None] * d
cores = [None] * d
counter = 0
for k in range(self.tt_input_shape.shape[0]):
# Original implementation
# shape = [ranks[k], input_shape[k], output_shape[k], ranks[k+1]]
shapes[k] = [self.tt_ranks[k], self.tt_input_shape[k], self.tt_output_shape[k], self.tt_ranks[k + 1]]
# Original implementation
# tall_shape = (np.prod(shape[:3]), shape[3])
tall_shapes[k] = (np.prod(shapes[k][:3]), shapes[k][3])
# Original implementation
# curr_core = np.dot(rv, np.random.randn(shape[0], np.prod(shape[1:])) )
cores[k] = np.dot(rv, rng.randn(shapes[k][0], np.prod(shapes[k][1:])))
# Original implementation
# curr_core = curr_core.reshape(tall_shape)
cores[k] = cores[k].reshape(tall_shapes[k])
if k < self.tt_input_shape.shape[0] - 1:
# Original implementation
# curr_core, rv = np.linalg.qr(curr_core)
cores[k], rv = np.linalg.qr(cores[k])
# Original implementation
# cores_arr[cores_arr_idx:cores_arr_idx+curr_core.size] = curr_core.flatten()
# cores_arr_idx += curr_core.size
cores_arr[counter:(counter + cores[k].size)] = cores[k].flatten()
counter += cores[k].size
glarot_style = (np.prod(self.tt_input_shape) * np.prod(self.tt_ranks)) ** (1.0 / self.tt_input_shape.shape[0])
return (0.1 / glarot_style) * cores_arr