我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用keras.backend.all()。
def validate_transitions_cpu_old(transitions, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) base = setting['base'] width = pre.shape[1] // base height = pre.shape[1] // base load(width,height) pre_validation = validate_states(pre, **kwargs) suc_validation = validate_states(suc, **kwargs) results = [] for pre, suc, pre_validation, suc_validation in zip(pre, suc, pre_validation, suc_validation): if pre_validation and suc_validation: c = to_configs(np.array([pre, suc]), verbose=False) succs = successors(c[0], width, height) results.append(np.any(np.all(np.equal(succs, c[1]), axis=1))) else: results.append(False) return results
def validate_transitions(transitions, check_states=True, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) if check_states: pre_validation = validate_states(pre, verbose=False, **kwargs) suc_validation = validate_states(suc, verbose=False, **kwargs) pre_configs = to_configs(pre, verbose=False, **kwargs) suc_configs = to_configs(suc, verbose=False, **kwargs) results = [] if check_states: for pre_c, suc_c, pre_validation, suc_validation in zip(pre_configs, suc_configs, pre_validation, suc_validation): if pre_validation and suc_validation: succs = successors(pre_c) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) else: results.append(False) else: for pre_c, suc_c in zip(pre_configs, suc_configs): succs = successors(pre_c) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) return results
def contingency_table(y, z): """Compute contingency table.""" y = K.round(y) z = K.round(z) def count_matches(a, b): tmp = K.concatenate([a, b]) return K.sum(K.cast(K.all(tmp, -1), K.floatx())) ones = K.ones_like(y) zeros = K.zeros_like(y) y_ones = K.equal(y, ones) y_zeros = K.equal(y, zeros) z_ones = K.equal(z, ones) z_zeros = K.equal(z, zeros) tp = count_matches(y_ones, z_ones) tn = count_matches(y_zeros, z_zeros) fp = count_matches(y_zeros, z_ones) fn = count_matches(y_ones, z_zeros) return (tp, tn, fp, fn)
def compute_mask(self, inputs, mask): inputs_shape = K.int_shape(inputs) outputs_shape = self.compute_output_shape(inputs_shape) mask_output = K.reshape(mask, (-1, outputs_shape[1])) mask_tensor = K.all(mask_output, axis=1) return mask_tensor
def validate_transitions_cpu(transitions, check_states=True, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) base = setting['base'] width = pre.shape[1] // base height = pre.shape[1] // base load(width,height) if check_states: pre_validation = validate_states(pre, verbose=False, **kwargs) suc_validation = validate_states(suc, verbose=False, **kwargs) pre_configs = to_configs(pre, verbose=False, **kwargs) suc_configs = to_configs(suc, verbose=False, **kwargs) results = [] if check_states: for pre_c, suc_c, pre_validation, suc_validation in zip(pre_configs, suc_configs, pre_validation, suc_validation): if pre_validation and suc_validation: succs = successors(pre_c, width, height) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) else: results.append(False) else: for pre_c, suc_c in zip(pre_configs, suc_configs): succs = successors(pre_c, width, height) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) return results
def validate_states(states,verbose=True,**kwargs): base = panels.shape[1] dim = states.shape[1] - pad*2 size = dim // base def build(): states = Input(shape=(dim+2*pad,dim+2*pad)) s = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **unswirl_args) error = build_errors(s,base,pad,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) num_matches = K.sum(matches, axis=3) panels_ok = K.all(K.equal(num_matches, 1), (1,2)) panels_ng = K.any(K.not_equal(num_matches, 1), (1,2)) panels_nomatch = K.any(K.equal(num_matches, 0), (1,2)) panels_ambiguous = K.any(K.greater(num_matches, 1), (1,2)) validity = panels_ok if verbose: return Model(states, [ wrap(states, x) for x in [panels_ng, panels_nomatch, panels_ambiguous, validity]]) else: return Model(states, wrap(states, validity)) if verbose: panels_ng, panels_nomatch, panels_ambiguous, validity \ = build().predict(states, **kwargs) print(np.count_nonzero(panels_ng), "images have some panels which match 0 or >2 panels, out of which") print(np.count_nonzero(panels_nomatch), "images have some panels which are unlike any panels") print(np.count_nonzero(panels_ambiguous),"images have some panels which match >2 panels") print(np.count_nonzero(validity), "images have panels (all of them) which match exactly 1 panel each") return validity else: validity \ = build().predict(states, **kwargs) return validity
def validate_transitions(transitions, check_states=True, **kwargs): pre = np.array(transitions[0]) suc = np.array(transitions[1]) tower_height = pre.shape[1] disks = tower_height // disk_height tower_width = disks * (2*disk_inc) + base_disk_width + border towers = pre.shape[2] // tower_width if check_states: pre_validation = validate_states(pre, verbose=False, **kwargs) suc_validation = validate_states(suc, verbose=False, **kwargs) pre_configs = to_configs(pre, verbose=False, **kwargs) suc_configs = to_configs(suc, verbose=False, **kwargs) results = [] if check_states: for pre_c, suc_c, pre_validation, suc_validation in zip(pre_configs, suc_configs, pre_validation, suc_validation): if pre_validation and suc_validation: succs = successors(pre_c, disks, towers) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) else: results.append(False) else: for pre_c, suc_c in zip(pre_configs, suc_configs): succs = successors(pre_c, disks, towers) results.append(np.any(np.all(np.equal(succs, suc_c), axis=1))) return results ## patterns ##############################################################
def all_acc(y_true, y_pred): """ All Accuracy https://github.com/rasmusbergpalm/normalization/blob/master/train.py#L10 """ return K.mean( K.all( K.equal( K.max(y_true, axis=-1), K.cast(K.argmax(y_pred, axis=-1), K.floatx()) ), axis=1) )
def exact_reversal(self, y_true, y_pred): "Fraction exactly predicted qubit flips." if self.p: y_pred = undo_normcentererr(y_pred, self.p) y_true = undo_normcentererr(y_true, self.p) return K.mean(F(K.all(K.equal(y_true, K.round(y_pred)), axis=-1)))
def squash_mask(self, mask): if K.ndim(mask) == 2: return mask elif K.ndim(mask) == 3: return K.all(mask, axis=-1) return mask
def compute_mask(self, x, mask=None): if self.return_probabilities: mask2 = mask if mask is not None: mask2 = K.expand_dims(K.all(mask2, axis=-1)) return [mask, mask2] return mask
def validate_states(states, verbose=True, **kwargs): base = setting['base'] width = states.shape[1] // base height = states.shape[1] // base load(width,height) def build(): states = Input(shape=(height*base,width*base)) error = build_error(states, height, width, base) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel num_matches = K.sum(matches, axis=3) panels_ok = K.all(K.equal(num_matches, 1), (1,2)) panels_ng = K.any(K.not_equal(num_matches, 1), (1,2)) panels_nomatch = K.any(K.equal(num_matches, 0), (1,2)) panels_ambiguous = K.any(K.greater(num_matches, 1), (1,2)) panel_coverage = K.sum(matches,axis=(1,2)) # ideally, this should be [[1,1,1,1,1,1,1,1,1], ...] coverage_ok = K.all(K.less_equal(panel_coverage, 1), 1) coverage_ng = K.any(K.greater(panel_coverage, 1), 1) validity = tf.logical_and(panels_ok, coverage_ok) if verbose: return Model(states, [ wrap(states, x) for x in [panels_ok, panels_ng, panels_nomatch, panels_ambiguous, coverage_ok, coverage_ng, validity]]) else: return Model(states, wrap(states, validity)) model = build() # model.summary() if verbose: panels_ok, panels_ng, panels_nomatch, panels_ambiguous, \ coverage_ok, coverage_ng, validity = model.predict(states, **kwargs) print(np.count_nonzero(panels_ng), "images have some panels which match 0 or >2 panels, out of which") print(np.count_nonzero(panels_nomatch), "images have some panels which are unlike any panels") print(np.count_nonzero(panels_ambiguous),"images have some panels which match >2 panels") print(np.count_nonzero(panels_ok), "images have panels (all of them) which match exactly 1 panel each") print(np.count_nonzero(np.logical_and(panels_ok, coverage_ng)),"images have duplicated tiles") print(np.count_nonzero(np.logical_and(panels_ok, coverage_ok)),"images have no duplicated tiles") return validity else: validity = model.predict(states, **kwargs) return validity
def validate_states(states,verbose=True, **kwargs): tower_height = states.shape[1] disks = tower_height // disk_height tower_width = disks * (2*disk_inc) + base_disk_width + border towers = states.shape[2] // tower_width panels = get_panels(disks, tower_width) def build(): states = Input(shape=(tower_height, tower_width*towers)) error = build_error(states, disks, towers, tower_width, panels) matches = 1 - K.clip(K.sign(error - threshold),0,1) num_matches = K.sum(matches, axis=3) panels_ok = K.all(K.equal(num_matches, 1), (1,2)) panels_ng = K.any(K.not_equal(num_matches, 1), (1,2)) panels_nomatch = K.any(K.equal(num_matches, 0), (1,2)) panels_ambiguous = K.any(K.greater(num_matches, 1), (1,2)) panel_coverage = K.sum(matches,axis=(1,2)) # ideally, this should be [[1,1,1...1,1,1,disks*tower-disk], ...] ideal_coverage = np.ones(disks+1) ideal_coverage[-1] = disks*towers-disks ideal_coverage = K.variable(ideal_coverage) coverage_ok = K.all(K.equal(panel_coverage, ideal_coverage), 1) coverage_ng = K.any(K.not_equal(panel_coverage, ideal_coverage), 1) validity = tf.logical_and(panels_ok, coverage_ok) if verbose: return Model(states, [ wrap(states, x) for x in [panels_ok, panels_ng, panels_nomatch, panels_ambiguous, coverage_ok, coverage_ng, validity]]) else: return Model(states, wrap(states, validity)) model = build() # model.summary() if verbose: panels_ok, panels_ng, panels_nomatch, panels_ambiguous, \ coverage_ok, coverage_ng, validity = model.predict(states, **kwargs) print(np.count_nonzero(panels_ng), "images have some panels which match 0 or >2 panels, out of which") print(np.count_nonzero(panels_nomatch), "images have some panels which are unlike any panels") print(np.count_nonzero(panels_ambiguous),"images have some panels which match >2 panels") print(np.count_nonzero(panels_ok), "images have panels (all of them) which match exactly 1 panel each") print(np.count_nonzero(np.logical_and(panels_ok, coverage_ng)),"images have duplicated tiles") print(np.count_nonzero(np.logical_and(panels_ok, coverage_ok)),"images have no duplicated tiles") return validity else: validity = model.predict(states, **kwargs) return validity
def to_configs(states,verbose=True, **kwargs): tower_height = states.shape[1] disks = tower_height // disk_height tower_width = disks * (2*disk_inc) + base_disk_width + border towers = states.shape[2] // tower_width panels = get_panels(disks, tower_width) def build(): states = Input(shape=(tower_height, tower_width*towers)) error = build_error(states, disks, towers, tower_width, panels) matches = 1 - K.clip(K.sign(error - threshold),0,1) # assume disks=4, towers=3 # matches: a h w p # [[[00001][00001][00001]] --- all panel 4 (empty panel) # [[10000][00001][00001]] --- h,w=1,0 is panel 0, others are panel 4 (empty panel) # [[01000][00001][00001]] --- h,w=2,0 is panel 1, others are panel 4 (empty panel) # [[00010][00100][00001]]] --- h,w=3,0 is panel 3, h,w=3,1 is panel 2, h,w=3,2 is panel 4 # # target config is [0,0,1,0] # you don't need the last panel (empty panel) # a h w p # [[[0000][0000][0000]] --- all panel 4 (empty panel) # [[1000][0000][0000]] --- h,w=1,0 is panel 0, others are panel 4 (empty panel) # [[0100][0000][0000]] --- h,w=2,0 is panel 1, others are panel 4 (empty panel) # [[0001][0010][0000]]] --- h,w=3,0 is panel 3, h,w=3,1 is panel 2, h,w=3,2 is panel 4 config = matches[:, :, :, 0:-1] # you don't need the height info # a w p # [[1101][0010][0000]] config = K.sum(config, 1) # reorder to a p w # [[100][100][010][100]] config = K.permute_dimensions(config, [0,2,1]) # convert one-hot width into width position config = config * K.arange(0,towers,dtype='float32') # 1-4 # [[000][000][010][000]] config = K.sum(config, -1) # [0 0 1 0] config = K.cast(config, 'int32') return Model(states, wrap(states, config)) return build().predict(states, **kwargs)