我们从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, y_pred): """structural similarity measurement system.""" ## K1, K2 are two constants, much smaller than 1 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 = 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, 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 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[:, slice_row, slice_col, :], (-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, axis=-1, 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, axis=-1, keepdims=True) xc_2_std = K.std(xc_2_aggregate, axis=-1, 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, x, mask=None): if self.axis == -1: mean = K.mean(x, axis=[3, 2, 1, 0], keepdims=True) std = K.std(x, axis=[3, 2, 1, 0], keepdims=True) elif self.axis in (0, 1, 2, 3): all_dims = [0, 1, 2, 3] del all_dims[self.axis] mean = K.mean(x, axis=all_dims, keepdims=True) std = K.std(x, axis=all_dims, keepdims=True) return (x - mean) / (std + self.eps)
def step(self, x, 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, axis=-1, keepdims=True) std = K.std(out, axis=-1, keepdims=True) output_normed = self.activation((out - mean) / (std + self.epsilon)) output = self.gamma * output_normed + self.beta return output, [output]