Python keras.backend 模块,std() 实例源码
我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用keras.backend.std()。
def ssim(y_true, y_pred):#may be wrong
"""structural similarity measurement system."""
K1 = 0.04
K2 = 0.06
## mean,std,correlation
mu_x = K.mean(y_pred)
mu_y = K.mean(y_true)
sig_x = K.std(y_pred)
sig_y = K.std(y_true)
sig_xy = (sig_x * sig_y) ** 0.5
## L,number of pixels,C1,C2,two constants
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def ssim(y_true,two constants
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def ssim(y_true,two constants
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def ssim(y_true, y_pred):
"""structural similarity measurement system."""
## K1,K2 are two constants,much smaller than 1
K1 = 0.04
K2 = 0.06
## mean,correlation
mu_x = K.mean(y_pred)
mu_y = K.mean(y_true)
sig_x = K.std(y_pred)
sig_y = K.std(y_true)
sig_xy = (sig_x * sig_y) ** 0.5
## L,two constants
L = 33
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def ssim(y_true, y_pred):#may be wrong
K1 = 0.04
K2 = 0.06
mu_x = K.mean(y_pred)
mu_y = K.mean(y_true)
sig_x = K.std(y_pred)
sig_y = K.std(y_true)
sig_xy = (sig_x * sig_y) ** 0.5
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def ssim(y_true, y_pred):#this fuction may be error!
K1 = 0.04
K2 = 0.06
mu_x = K.mean(y_pred)
mu_y = K.mean(y_true)
sig_x = K.std(y_pred)
sig_y = K.std(y_true)
sig_xy = (sig_x * sig_y) ** 0.5
## L,two constants
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def center_normalize(x):
"""
Custom activation for online sample-wise center and std. normalization
"""
return (x - K.mean(x)) / K.std(x)
def center_normalize(x):
"""
Custom activation for online sample-wise center and std. normalization
"""
return (x - K.mean(x)) / K.std(x)
def call(self, x, mask=None):
input_1, input_2 = x
stride_row, stride_col = self.subsample
inp_shape = input_1._keras_shape
output_shape = self.get_output_shape_for([inp_shape, inp_shape])
output_row = inp_shape[1] - self.kernel_size + 1
output_col = inp_shape[2] - self.kernel_size + 1
xc_1 = []
xc_2 = []
for i in range(output_row):
for j in range(output_col):
slice_row = slice(i, i + self.kernel_size)
slice_col = slice(j, j + self.kernel_size)
xc_2.append(K.reshape(input_2[:, slice_row, slice_col, :],
(-1, 1, self.kernel_size**2*inp_shape[-1])))
if i % stride_row == 0 and j % stride_col == 0:
xc_1.append(K.reshape(input_1[:,
(-1, self.kernel_size**2*inp_shape[-1])))
xc_1_aggregate = K.concatenate(xc_1, axis=1) # batch_size x w'h' x (k**2*d),w': w/subsample-1
xc_1_mean = K.mean(xc_1_aggregate, axis=-1, keepdims=True)
xc_1_std = K.std(xc_1_aggregate, keepdims=True)
xc_1_aggregate = (xc_1_aggregate - xc_1_mean) / xc_1_std
xc_2_aggregate = K.concatenate(xc_2, axis=1) # batch_size x wh x (k**2*d),w: output_row
xc_2_mean = K.mean(xc_2_aggregate, keepdims=True)
xc_2_std = K.std(xc_2_aggregate, keepdims=True)
xc_2_aggregate = (xc_2_aggregate - xc_2_mean) / xc_2_std
xc_1_aggregate = K.permute_dimensions(xc_1_aggregate, (0, 2, 1))
output = K.batch_dot(xc_2_aggregate, xc_1_aggregate) # batch_size x wh x w'h'
output = K.reshape(output, (-1, output_row, output_col, output_shape[-1]))
output = self.activation(output)
return output
def call(self, mask=None):
if self.axis == -1:
mean = K.mean(x, axis=[3, 0], keepdims=True)
std = K.std(x, keepdims=True)
elif self.axis in (0, 3):
all_dims = [0, 3]
del all_dims[self.axis]
mean = K.mean(x, axis=all_dims, keepdims=True)
return (x - mean) / (std + self.eps)
def ssim(y_true, y_pred):#may be wrong
K1 = 0.04
K2 = 0.06
mu_x = K.mean(y_pred)
mu_y = K.mean(y_true)
sig_x = K.std(y_pred)
sig_y = K.std(y_true)
sig_xy = (sig_x * sig_y) ** 0.5
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
ssim = (2 * mu_x * mu_y + C1) * (2 * sig_xy * C2) * 1.0 / ((mu_x ** 2 + mu_y ** 2 + C1) * (sig_x ** 2 + sig_y ** 2 + C2))
return ssim
def step(self, states):
prev_output = states[0]
B_U = states[1]
B_W = states[2]
if self.consume_less == 'gpu':
h = K.dot(x * B_W, self.W) + self.b
else:
h = x
out = h + K.dot(prev_output * B_U, self.U)
mean = K.mean(out, keepdims=True)
std = K.std(out, keepdims=True)
output_normed = self.activation((out - mean) / (std + self.epsilon))
output = self.gamma * output_normed + self.beta
return output, [output]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。