def increment_n_map_entries(self): """ Add one to ``n_map`` entries and grow the bundle map as needed. """ self.n_map_entries += 1 if self.n_map_entries >= self.bundle_map_size: self.bundle_map_size *= 2 self.bundle_map_rows = tools.pad(self.bundle_map_rows, self.bundle_map_size, val=-1, dtype='int') self.bundle_map_cols = tools.pad(self.bundle_map_cols, self.bundle_map_size, val=-1, dtype='int')
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables = self.num_cables + num_new_cables features_shape = (self.num_cables, 1) transition_shape = (self.num_cables, self.num_cables) self.reward = tools.pad(self.reward, transition_shape, val=self.INITIAL_REWARD) for index in range(len(self.activity_history)): self.activity_history[index] = tools.pad( self.activity_history[index], features_shape) self.action_history[index] = tools.pad(self.action_history[index], features_shape)
def add_cables(self, num_new_cables): """ Add new cables to the hub when new blocks are created """ self.num_cables = self.num_cables + num_new_cables self.expected_reward = tools.pad(self.expected_reward, (self.num_cables, self.num_cables), val=self.INITIAL_REWARD) self.cable_activities = tools.pad(self.cable_activities, (self.num_cables, 1)) self.count = tools.pad(self.count, (self.num_cables, self.num_cables)) # All the cable activities from all the blocks, at the current time for index in range(len(self.pre)): self.pre[index] = tools.pad(self.pre[index], (self.num_cables, 1)) self.post[index] = tools.pad(self.post[index], (self.num_cables, 1))
def step_down(self, bundle_goals): """ Find cable_activity_goals, given a set of bundle_goals """ bundle_goals = tools.pad(bundle_goals, (self.max_bundles, 1)) cable_goals = np.zeros((self.max_cables, 1)) self.surprise = np.zeros((self.max_cables, 1)) # Process the downward pass of each of the cogs in the level cog_index = 0 for cog in self.cogs: # Gather the goal inputs for each cog cog_bundle_goals = bundle_goals[ cog_index * self.max_bundles_per_cog:cog_index + 1 * self.max_bundles_per_cog, :] # Update the downward outputs for the level cable_goals_by_cog = cog.step_down(cog_bundle_goals) cog_cable_indices = self.ziptie.get_index_projection( cog_index).ravel().astype(bool) cable_goals[cog_cable_indices] = np.maximum( cable_goals_by_cog, cable_goals[cog_cable_indices]) #self.reaction[cog_cable_indices] = np.maximum( # tools.pad(cog.reaction, (cog_cable_indices[0].size, 0)), # self.reaction[cog_cable_indices]) self.surprise[cog_cable_indices] = np.maximum( cog.surprise, self.surprise[cog_cable_indices]) cog_index += 1 #self.hub_cable_goals = tools.bounded_sum([self.hub_cable_goals, # cable_goals]) return self.hub_cable_goals
def step_down(self, bundle_goals): """ Find cable_activity_goals, given a set of bundle_goals """ bundle_goals = tools.pad(bundle_goals, (self.max_bundles, 1)) cable_goals = np.zeros((self.max_cables, 1)) self.surprise = np.zeros((self.max_cables, 1)) # Process the downward pass of each of the cogs in the level cog_index = 0 for cog in self.cogs: # Gather the goal inputs for each cog cog_bundle_goals = bundle_goals[ cog_index * self.max_bundles_per_cog: cog_index + 1 * self.max_bundles_per_cog,:] # Update the downward outputs for the level cable_goals_by_cog = cog.step_down(cog_bundle_goals) cog_cable_indices = self.ziptie.get_index_projection( cog_index).ravel().astype(bool) cable_goals[cog_cable_indices] = np.maximum( cable_goals_by_cog, cable_goals[cog_cable_indices]) #self.reaction[cog_cable_indices] = np.maximum( # tools.pad(cog.reaction, (cog_cable_indices[0].size, 0)), # self.reaction[cog_cable_indices]) self.surprise[cog_cable_indices] = np.maximum( cog.surprise, self.surprise[cog_cable_indices]) cog_index += 1 #self.hub_cable_goals = tools.bounded_sum([self.hub_cable_goals, # cable_goals]) return self.hub_cable_goals
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables += num_new_cables features_shape = (self.num_cables, 1) self.time_since_seen = tools.pad(self.time_since_seen, features_shape, val=tools.BIG)
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables = self.num_cables + num_new_cables transition_shape = (self.num_cables, self.num_actions) #self.value = tools.pad(self.value, transition_shape) self.reward = tools.pad(self.reward, transition_shape)
def __init__(self, events, print_summary=True, model=None, load=None, njets=10): """ A class for neural networks that take raw pt, eta, phi values """ print("INITIALIZING NN") # get events, split into subsets self.njets = njets self.events = tools.pad(events, length=self.njets) self.train, self.val, self.test = tools.splitTVT(self.events, trainfrac=0.7, testfrac=0.2) # create network if load: jsonfile, h5file = load print("loading model. \nUsing architecture:", jsonfile, "and weights:", h5file) with open(jsonfile, 'rb') as f: model_json = f.read() self.model = model_from_json(model_json) self.model.load_weights(h5file) elif model is None: print("creating default model") self.model = Sequential([ Dense(3 * (njets - 3), input_dim=3 * (njets - 3), kernel_initializer='normal', activation='relu'), Dense(700, activation='relu'), Dropout(0.1), Dense(500, activation='relu'), Dropout(0.1), Dense(300, activation='relu'), Dropout(0.1), Dense(100, activation='relu'), Dropout(0.1), Dense(50, activation='relu'), Dense( njets - 3 + 1, # - 3 correctly tagged + 1 for no jet kernel_initializer='normal', activation='softmax') ]) optimizer = Adam(lr=5e-5) self.model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc']) else: print("using input model") self.model = model if print_summary: self.model.summary() # format inputs to NN by scaling self.s_in = tools.scale_nn_input(self.events, chop=3, pad=self.njets)
def update(self, cable_activities, reward): """ Train the daisychain using the current cable_activities and reward """ self.num_cables = np.maximum(self.num_cables, cable_activities.size) # Pad the incoming cable_activities array out to its full size cable_activities = tools.pad(cable_activities, (self.max_num_cables, 0)) self.current_reward = reward # The pre is a weighted sum of previous cable_activities, with the most # recent cable_activities being weighted the highest # debug #self.pre = tools.bounded_sum([ # self.post, self.pre * (1 - self.PRE_DECAY_RATE)]) self.pre = self.post self.post = cable_activities chain_activities = self.pre * self.post.T chain_activities[np.nonzero(np.eye(self.pre.size))] = 0. update_rate_raw = (chain_activities * ((1 - self.CHAIN_UPDATE_RATE) / (self.count + tools.EPSILON) + self.CHAIN_UPDATE_RATE)) update_rate = np.minimum(0.5, update_rate_raw) self.count += chain_activities self.count -= 1 / (self.AGING_TIME_CONSTANT * self.count + tools.EPSILON) self.count = np.maximum(self.count, 0) reward_difference = np.abs(reward - self.reward_value) self.reward_value += (reward - self.reward_value) * update_rate self.reward_uncertainty += (reward_difference - self.reward_uncertainty) * update_rate update_rate_raw_post = (self.pre * ((1 - self.CHAIN_UPDATE_RATE) / (self.pre_count + tools.EPSILON) + self.CHAIN_UPDATE_RATE)) update_rate_post = np.minimum(0.5, update_rate_raw_post) self.pre_count += self.pre self.pre_count -= 1 / (self.AGING_TIME_CONSTANT * self.pre_count + tools.EPSILON) self.pre_count = np.maximum(self.pre_count, 0) post_difference = np.abs(self.pre * self.post.T - self.expected_post) self.expected_post += (self.pre * self.post.T - self.expected_post) * update_rate_post self.post_uncertainty += (post_difference - self.post_uncertainty) * update_rate_post # Reaction is the expected post, turned into a deliberation_vote self.reaction = tools.weighted_average(self.expected_post, self.pre) # Surprise is the difference between the expected post and # the actual one self.surprise = tools.weighted_average( np.abs(self.post.T - self.expected_post), self.pre / (self.post_uncertainty + tools.EPSILON)) #self.surprise = tools.weighted_average( # np.abs((self.post.T - self.expected_post) / # (self.post_uncertainty + tools.EPSILON)), # self.pre / (self.post_uncertainty + tools.EPSILON)) # Reshape chain activities into a single column return chain_activities.ravel()[:,np.newaxis]
def grow(self, increment): """ Grow the ``Amygdala``. Parameters ---------- increment : int The number of features to add. """ self.reward_by_feature = tools.pad(self.reward_by_feature, -increment)
def grow(self, increment): """ Grow the ``Ganglia``. Parameters ---------- increment : int The number of features to add. """ self.num_features += increment self.num_elements += increment self.goals = tools.pad(self.goals, self.num_features)
def grow(self, increment): """ Grow the ``Cerebellum``. Parameters ---------- increment : int The number of features to add. Returns ------- None """ self.num_elements += increment self.num_features += increment _2D_size = (self.num_features, self.num_elements) _3D_size = (self.num_features, self.num_elements, self.num_features) self.observations = tools.pad(self.observations, _3D_size) self.opportunities = tools.pad(self.opportunities, _2D_size, val=tools.epsilon) self.curiosities = tools.pad(self.curiosities, _2D_size) self.live_elements = tools.pad(self.live_elements, self.num_elements) for i in np.arange(len(self.features_history)): self.features_history[i] = tools.pad(self.features_history[i], self.num_features) for i in np.arange(len(self.goals_history)): if self.goals_history[i] is not None: self.goals_history[i] = tools.pad(self.goals_history[i], self.num_elements)
def data_encry_pack(self,data_pack): key_iv = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] key_iv_pack = struct.pack('%dB'%len(key_iv), *key_iv) key_pack= struct.pack('%dB'%len(self.sess_key), *self.sess_key) #print '***********sess_key_pack',binascii.hexlify(key_pack) ackBody_AES = AES.new(key_pack, AES.MODE_CBC, key_iv_pack) # print 'ack_Packet_data before AES',binascii.hexlify(data_pack) data_encryed=ackBody_AES.encrypt(tools.pad(data_pack)) # print 'ack_Packet_data after AES',binascii.hexlify(data_encryed) self.msg_len=len(data_encryed) # print 'ack_Packet msg_len after AES',self.msg_len return data_encryed
def step_down(self, bundle_goals): """ Project the bundle goal values to the appropriate cables Multiply the bundle goals across the cables that contribute to them, and perform a bounded sum over all bundles to get the estimated activity associated with each cable. """ if bundle_goals.size > 0: bundle_goals = tools.pad(bundle_goals, (self.max_num_bundles, 0)) cable_activity_goals = tools.bounded_sum(self.bundle_map * bundle_goals, axis=0) else: cable_activity_goals = np.zeros((self.max_num_cables, 1)) return cable_activity_goals
def recognize(self, images, detection_kwargs=None, recognition_kwargs=None): """Run the pipeline on one or multiples images. Args: images: The images to parse (can be a list of actual images or a list of filepaths) detection_kwargs: Arguments to pass to the detector call recognition_kwargs: Arguments to pass to the recognizer call Returns: A list of lists of (text, box) tuples. """ # Make sure we have an image array to start with. if not isinstance(images, np.ndarray): images = [tools.read(image) for image in images] # This turns images into (image, scale) tuples temporarily images = [ tools.resize_image(image, max_scale=self.scale, max_size=self.max_size) for image in images ] max_height, max_width = np.array( [image.shape[:2] for image, scale in images]).max(axis=0) scales = [scale for _, scale in images] images = np.array([ tools.pad(image, width=max_width, height=max_height) for image, _ in images ]) if detection_kwargs is None: detection_kwargs = {} if recognition_kwargs is None: recognition_kwargs = {} box_groups = self.detector.detect(images=images, **detection_kwargs) prediction_groups = self.recognizer.recognize_from_boxes( images=images, box_groups=box_groups, **recognition_kwargs) box_groups = [ tools.adjust_boxes( boxes=boxes, boxes_format='boxes', scale=1 / scale) if scale != 1 else boxes for boxes, scale in zip(box_groups, scales) ] return [ list(zip(predictions, boxes)) for predictions, boxes in zip(prediction_groups, box_groups) ]
def step_up(self, cable_activities, enough_cables): """ cable_activities percolate upward through daisychain and ziptie """ # TODO: fix this so that cogs can gracefully handle more cables # or else never be assigned them in the first place if cable_activities.size > self.max_cables: cable_activities = cable_activities[:self.max_cables, :] print '----- Number of max cables exceeded in', self.name, \ ' -----' chain_activities = self.daisychain.step_up(cable_activities) self.surprise = self.daisychain.get_surprise() if enough_cables is True: bundle_activities = self.ziptie.step_up(chain_activities) else: bundle_activities = np.zeros((0,1)) bundle_activities = tools.pad(bundle_activities, (self.max_bundles, 0)) return bundle_activities
def step_up(self, cable_activities, enough_cables): """ Cable_activities percolate upward through daisychain and ziptie """ # TODO: fix this so that cogs can gracefully handle more cables # or else never be assigned them in the first place if cable_activities.size > self.max_cables: cable_activities = cable_activities[:self.max_cables, :] print '----- Number of max cables exceeded in', self.name, \ ' -----' chain_activities = self.daisychain.step_up(cable_activities) self.surprise = self.daisychain.get_surprise() if enough_cables is True: bundle_activities = self.ziptie.step_up(chain_activities) else: bundle_activities = np.zeros((0, 1)) bundle_activities = tools.pad(bundle_activities, (self.max_bundles, 0)) return bundle_activities
def step_up(self, new_cable_activities): """ Find bundle_activities that result from new_cable_activities """ # Condition the cable activities to fall between 0 and 1 if new_cable_activities.size < self.max_cables: new_cable_activities = tools.pad(new_cable_activities, (self.max_cables, 1)) # Update cable_activities self.raw_cable_activities *= 1. - (self.ACTIVITY_DECAY_RATE / float(self.step_multiplier)) self.raw_cable_activities += new_cable_activities self.step_counter += 1 if self.step_counter < self.step_multiplier: return self.bundle_activities self.min_vals = np.minimum(self.raw_cable_activities, self.min_vals) self.max_vals = np.maximum(self.raw_cable_activities, self.max_vals) spread = self.max_vals - self.min_vals self.cable_activities = ((self.raw_cable_activities - self.min_vals) / (self.max_vals - self.min_vals + tools.EPSILON)) self.min_vals += spread * self.RANGE_DECAY_RATE self.max_vals -= spread * self.RANGE_DECAY_RATE cluster_training_activities = np.maximum( self.previous_cable_activities, self.cable_activities) self.previous_cable_activities = self.cable_activities.copy() # Update the map from self.cable_activities to cogs self.ziptie.step_up(cluster_training_activities) # Process the upward pass of each of the cogs in the gearbox self.bundle_activities = np.zeros((0, 1)) for cog_index in range(len(self.cogs)): # Pick out the cog's cable_activities, process them, # and assign the results to gearbox's bundle_activities cog_cable_activities = self.cable_activities[ self.ziptie.get_index_projection( cog_index).astype(bool)] # Cogs are only allowed to start forming bundles once # the number of cables exceeds the fill_fraction_threshold enough_cables = (self.ziptie.cable_fraction_in_bundle(cog_index) > self.fill_fraction_threshold) cog_bundle_activities = self.cogs[cog_index].step_up( cog_cable_activities, enough_cables) self.bundle_activities = np.concatenate((self.bundle_activities, cog_bundle_activities)) # Goal fulfillment and decay self.hub_cable_goals -= self.cable_activities self.hub_cable_goals *= self.ACTIVITY_DECAY_RATE self.hub_cable_goals = np.maximum(self.hub_cable_goals, 0.) return self.bundle_activities
def step_up(self, new_cable_activities): """ Find bundle_activities that result from new_cable_activities """ # Condition the cable activities to fall between 0 and 1 if new_cable_activities.size < self.max_cables: new_cable_activities = tools.pad(new_cable_activities, (self.max_cables, 1)) # Update cable_activities self.raw_cable_activities *= 1. - (self.ACTIVITY_DECAY_RATE / float(self.step_multiplier)) self.raw_cable_activities += new_cable_activities self.step_counter += 1 if self.step_counter < self.step_multiplier: return self.bundle_activities self.min_vals = np.minimum(self.raw_cable_activities, self.min_vals) self.max_vals = np.maximum(self.raw_cable_activities, self.max_vals) spread = self.max_vals - self.min_vals self.cable_activities = ( (self.raw_cable_activities - self.min_vals) / (self.max_vals - self.min_vals + tools.EPSILON)) self.min_vals += spread * self.RANGE_DECAY_RATE self.max_vals -= spread * self.RANGE_DECAY_RATE cluster_training_activities = np.maximum( self.previous_cable_activities, self.cable_activities) self.previous_cable_activities = self.cable_activities.copy() # Update the map from self.cable_activities to cogs self.ziptie.step_up(cluster_training_activities) # Process the upward pass of each of the cogs in the gearbox self.bundle_activities = np.zeros((0, 1)) for cog_index in range(len(self.cogs)): # Pick out the cog's cable_activities, process them, # and assign the results to gearbox's bundle_activities cog_cable_activities = self.cable_activities[ self.ziptie.get_index_projection(cog_index).astype(bool)] # Cogs are only allowed to start forming bundles once # the number of cables exceeds the fill_fraction_threshold enough_cables = (self.ziptie.cable_fraction_in_bundle(cog_index) > self.fill_fraction_threshold) cog_bundle_activities = self.cogs[cog_index].step_up( cog_cable_activities, enough_cables) self.bundle_activities = np.concatenate( (self.bundle_activities, cog_bundle_activities)) # Goal fulfillment and decay self.hub_cable_goals -= self.cable_activities self.hub_cable_goals *= self.ACTIVITY_DECAY_RATE self.hub_cable_goals = np.maximum(self.hub_cable_goals, 0.) return self.bundle_activities
def step_up(self, cable_activities): """ Train the daisychain using the current cable_activities """ self.num_cables = np.maximum(self.num_cables, cable_activities.size) # Pad the incoming cable_activities array out to its full size cable_activities = tools.pad(cable_activities, (self.max_num_cables, 0)) # Update cable_activities self.pre = self.cable_activities.copy() self.cable_activities = cable_activities.copy() self.post = self.cable_activities.copy() chain_activities = self.pre * self.post.T self.count += chain_activities #self.count -= 1 / (self.AGING_TIME_CONSTANT * self.count + # tools.EPSILON) self.count = np.maximum(self.count, 0) update_rate_raw_post = (self.pre * ((1 - self.CHAIN_UPDATE_RATE) / (self.pre_count + tools.EPSILON) + self.CHAIN_UPDATE_RATE)) update_rate_post = np.minimum(0.5, update_rate_raw_post) self.pre_count += self.pre #self.pre_count -= 1 / (self.AGING_TIME_CONSTANT * self.pre_count + # tools.EPSILON) self.pre_count = np.maximum(self.pre_count, 0) #post_difference = np.abs(self.pre * self.post.T - # self.expected_cable_activities) self.expected_cable_activities += update_rate_post * ( self.pre * self.post.T - self.expected_cable_activities) #self.post_uncertainty += (post_difference - # self.post_uncertainty) * update_rate_post # Reaction is the expected post, turned into a deliberation_vote #self.reaction = tools.weighted_average(self.expected_cable_activities, # self.pre) # Surprise is the difference between the expected post and # the actual one #surprise_weights = (self.pre / (self.post_uncertainty + tools.EPSILON) # + tools.EPSILON) #self.surprise = tools.weighted_average( # np.abs(self.post.T - self.expected_cable_activities), # surprise_weights) # Reshape chain activities into a single column #return (chain_activities.ravel()[:,np.newaxis], # self.reaction[:self.num_cables]) return chain_activities.ravel()[:,np.newaxis]
def _normalize(self, cable_activities): """ Normalize activities so that they are predictably distrbuted. Use a running estimate of the maximum of each cable activity. Scale it so that the max would fall at 1. Normalization has several benefits. 1. It makes for fewer constraints on worlds and sensors. Any sensor can return any range of values. 2. Gradual changes in sensors and the world can be adapted to. 3. It makes the bundle creation heuristic more robust and predictable. The approximate distribution of cable activities is known and can be designed for. Parameters ---------- cable_activities : array of floats The current activity levels of the cables. Returns ------- normalized_cable_activities : array of floats The normalized activity levels of the cables. """ if cable_activities.size < self.max_num_cables: cable_activities = tools.pad(cable_activities, self.max_num_cables) self.cable_max += ((cable_activities - self.cable_max) / self.cable_max_decay_time) i_lo = np.where(cable_activities > self.cable_max) self.cable_max[i_lo] += ( (cable_activities[i_lo] - self.cable_max[i_lo]) / self.cable_max_grow_time) cable_activities = cable_activities / (self.cable_max + tools.epsilon) cable_activities = np.maximum(0., cable_activities) cable_activities = np.minimum(1., cable_activities) # Sparsify the cable activities to speed up processing. cable_activities[np.where( cable_activities < self.activity_threshold)] = 0. normalized_cable_activities = cable_activities.copy() return normalized_cable_activities
def _normalize(self, cable_activities): """ Normalize activities so that they are predictably distrbuted. Use a running estimate of the maximum of each cable activity. Scale it so that the max would fall at 1. Normalization has several benefits. 1. It makes for fewer constraints on worlds and sensors. Any sensor can return any range of values. 2. Gradual changes in sensors and the world can be adapted to. 3. It makes the bundle creation heuristic more robust and predictable. The approximate distribution of cable activities is known and can be designed for. Parameters ---------- cable_activities : array of floats The current activity levels of the cables. Returns ------- normalized_cable_activities : array of floats The normalized activity levels of the cables. """ if cable_activities.size < self.max_num_cables: cable_activities = tools.pad(cable_activities, self.max_num_cables) self.cable_max += ( (cable_activities - self.cable_max) / self.cable_max_decay_time ) i_lo = np.where(cable_activities > self.cable_max) self.cable_max[i_lo] += ( (cable_activities[i_lo] - self.cable_max[i_lo]) / self.cable_max_grow_time ) cable_activities = cable_activities / (self.cable_max + tools.epsilon) cable_activities = np.maximum(0., cable_activities) cable_activities = np.minimum(1., cable_activities) # Sparsify the cable activities to speed up processing. cable_activities[np.where(cable_activities < self.activity_threshold)] = 0. normalized_cable_activities = cable_activities.copy() return normalized_cable_activities
def step_up(self, new_cable_activities): """ Find bundle_activities that result from new_cable_activities """ # Condition the cable activities to fall between 0 and 1 if new_cable_activities.size < self.max_cables: new_cable_activities = tools.pad(new_cable_activities, (self.max_cables, 1)) self.min_vals = np.minimum(new_cable_activities, self.min_vals) self.max_vals = np.maximum(new_cable_activities, self.max_vals) spread = self.max_vals - self.min_vals new_cable_activities = ( (new_cable_activities - self.min_vals) / (self.max_vals - self.min_vals + tools.EPSILON)) self.min_vals += spread * self.RANGE_DECAY_RATE self.max_vals -= spread * self.RANGE_DECAY_RATE # Update cable_activities, incorporating sensing dynamics self.cable_activities = tools.bounded_sum([ new_cable_activities, self.cable_activities * (1. - self.ACTIVITY_DECAY_RATE) ]) # Update the map from self.cable_activities to cogs self.ziptie.step_up(self.cable_activities) # Process the upward pass of each of the cogs in the block self.bundle_activities = np.zeros((0, 1)) for cog_index in range(len(self.cogs)): # Pick out the cog's cable_activities, process them, # and assign the results to block's bundle_activities cog_cable_activities = self.cable_activities[ self.ziptie.get_index_projection(cog_index).ravel().astype( bool)] # Cogs are only allowed to start forming bundles once # the number of cables exceeds the fill_fraction_threshold enough_cables = (self.ziptie.cable_fraction_in_bundle(cog_index) > self.fill_fraction_threshold) cog_bundle_activities = self.cogs[cog_index].step_up( cog_cable_activities, enough_cables) self.bundle_activities = np.concatenate( (self.bundle_activities, cog_bundle_activities)) # Goal fulfillment and decay self.hub_cable_goals -= self.cable_activities self.hub_cable_goals *= self.ACTIVITY_DECAY_RATE self.hub_cable_goals = np.maximum(self.hub_cable_goals, 0.) return self.bundle_activities
def step_up(self, cable_activities): """ Train the daisychain using the current cable_activities """ self.num_cables = np.maximum(self.num_cables, cable_activities.size) # Pad the incoming cable_activities array out to its full size cable_activities = tools.pad(cable_activities, (self.max_num_cables, 0)) # Update cable_activities, incorporating sensing dynamics self.pre = self.cable_activities.copy() self.cable_activities = cable_activities.copy() self.post = self.cable_activities.copy() chain_activities = self.pre * self.post.T self.count += chain_activities self.count -= 1 / (self.AGING_TIME_CONSTANT * self.count + tools.EPSILON) self.count = np.maximum(self.count, 0) update_rate_raw_post = (self.pre * ( (1 - self.CHAIN_UPDATE_RATE) / (self.pre_count + tools.EPSILON) + self.CHAIN_UPDATE_RATE)) update_rate_post = np.minimum(0.5, update_rate_raw_post) self.pre_count += self.pre self.pre_count -= 1 / (self.AGING_TIME_CONSTANT * self.pre_count + tools.EPSILON) self.pre_count = np.maximum(self.pre_count, 0) post_difference = np.abs(self.pre * self.post.T - self.expected_cable_activities) self.expected_cable_activities += update_rate_post * ( self.pre * self.post.T - self.expected_cable_activities) self.post_uncertainty += (post_difference - self.post_uncertainty) * update_rate_post # Reaction is the expected post, turned into a deliberation_vote self.reaction = tools.weighted_average(self.expected_cable_activities, self.pre) # Surprise is the difference between the expected post and # the actual one surprise_weights = (self.pre / (self.post_uncertainty + tools.EPSILON) + tools.EPSILON) self.surprise = tools.weighted_average( np.abs(self.post.T - self.expected_cable_activities), surprise_weights) # Reshape chain activities into a single column return chain_activities.ravel()[:, np.newaxis]
def step_up(self, new_cable_activities): """ Find bundle_activities that result from new_cable_activities """ # Condition the cable activities to fall between 0 and 1 if new_cable_activities.size < self.max_cables: new_cable_activities = tools.pad(new_cable_activities, (self.max_cables, 1)) self.min_vals = np.minimum(new_cable_activities, self.min_vals) self.max_vals = np.maximum(new_cable_activities, self.max_vals) spread = self.max_vals - self.min_vals new_cable_activities = ((new_cable_activities - self.min_vals) / (self.max_vals - self.min_vals + tools.EPSILON)) self.min_vals += spread * self.RANGE_DECAY_RATE self.max_vals -= spread * self.RANGE_DECAY_RATE # Update cable_activities, incorporating sensing dynamics self.cable_activities = tools.bounded_sum([ new_cable_activities, self.cable_activities * (1. - self.ACTIVITY_DECAY_RATE)]) # Update the map from self.cable_activities to cogs self.ziptie.step_up(self.cable_activities) # Process the upward pass of each of the cogs in the block self.bundle_activities = np.zeros((0, 1)) for cog_index in range(len(self.cogs)): # Pick out the cog's cable_activities, process them, # and assign the results to block's bundle_activities cog_cable_activities = self.cable_activities[ self.ziptie.get_index_projection( cog_index).ravel().astype(bool)] # Cogs are only allowed to start forming bundles once # the number of cables exceeds the fill_fraction_threshold enough_cables = (self.ziptie.cable_fraction_in_bundle(cog_index) > self.fill_fraction_threshold) cog_bundle_activities = self.cogs[cog_index].step_up( cog_cable_activities, enough_cables) self.bundle_activities = np.concatenate((self.bundle_activities, cog_bundle_activities)) # Goal fulfillment and decay self.hub_cable_goals -= self.cable_activities self.hub_cable_goals *= self.ACTIVITY_DECAY_RATE self.hub_cable_goals = np.maximum(self.hub_cable_goals, 0.) return self.bundle_activities
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables = self.num_cables + num_new_cables features_shape = (self.num_cables, 1) transition_shape = (self.num_cables, self.num_actions) self.reward = tools.pad(self.reward, transition_shape) self.count = tools.pad(self.count, transition_shape) self.running_activity = tools.pad(self.running_activity, transition_shape) self.mask = tools.pad(self.mask, features_shape) for index in range(len(self.activity_history)): self.activity_history[index] = tools.pad( self.activity_history[index], features_shape) self.action_history[index] = tools.pad( self.action_history[index], (self.num_actions, 1))
def step_up(self, new_cable_activities, reward): """ Find bundle_activities that result from new_cable_activities """ new_cable_activities = tools.pad(new_cable_activities, (self.max_cables, 1)) ''' # Condition the new_cable_activities to fall between 0 and 1 self.min_vals = np.minimum(new_cable_activities, self.min_vals) self.max_vals = np.maximum(new_cable_activities, self.max_vals) spread = self.max_vals - self.min_vals new_cable_activities = ((new_cable_activities - self.min_vals) / (self.max_vals - self.min_vals + tools.EPSILON)) self.min_vals += spread * self.RANGE_DECAY_RATE self.max_vals -= spread * self.RANGE_DECAY_RATE ''' # Update cable_activities, incorporating sensing dynamics self.cable_activities = tools.bounded_sum([ new_cable_activities, self.cable_activities * (1. - self.ACTIVITY_DECAY_RATE)]) # debug #print self.name, 'ca', self.cable_activities.shape #print self.cable_activities.ravel() # Update the map from self.cable_activities to cogs self.ziptie.update(self.cable_activities) # Process the upward pass of each of the cogs in the block self.bundle_activities = np.zeros((0, 1)) for cog_index in range(len(self.cogs)): # Pick out the cog's cable_activities, process them, # and assign the results to block's bundle_activities cog_cable_activities = self.cable_activities[ self.ziptie.get_projection(cog_index).ravel().astype(bool)] enough_cables = (self.ziptie.cable_fraction_in_bundle(cog_index) > 0.7) cog_bundle_activities = self.cogs[cog_index].step_up( cog_cable_activities, reward, enough_cables) self.bundle_activities = np.concatenate((self.bundle_activities, cog_bundle_activities)) return self.bundle_activities
def step_up(self, cable_activities, enough_cables): """ Cable_activities percolate upward through daisychain and ziptie """ # TODO: fix this so that cogs can gracefully handle more cables # or else never be assigned them in the first place. # Enable them to grow dynamically, rather than preallocated, # if that can be shown to improve performance. if cable_activities.size > self.max_cables: cable_activities = cable_activities[:self.max_cables, :] print ''.join(['----- Number of max cables exceeded in', self.name, ' -----']) chain_activities = self.daisychain.step_up(cable_activities) # Wait to start training the cog's bundles until the cog's # input cables are adequately populated. This is the job # of the gearbox's ziptie. if enough_cables is True: bundle_activities = self.ziptie.step_up(chain_activities) else: bundle_activities = np.zeros((0,1)) # TODO: Check whether this is necessary bundle_activities = tools.pad(bundle_activities, (self.max_bundles, 0)) return bundle_activities
from Crypto.Cipher import AES from tools import create_parser, join, pad from base64 import b64encode if __name__ == '__main__': parser = create_parser() namespace = parser.parse_args() key = namespace.password.encode() key = pad(key, AES.block_size) cipher = AES.new(key, AES.MODE_CBC) try: # try to open file with passwords file = open(namespace.sourcefile, 'r') except FileNotFoundError: # if file dose note found, close the program print('Source file not found') raise SystemExit(1) passwords = file.read().encode() file.close() encrypted_bytes = cipher.encrypt(pad(passwords, AES.block_size)) path = namespace.encryptedfile name = namespace.name iv = b64encode(cipher.iv).decode('utf-8') encrypted = b64encode(encrypted_bytes).decode('utf-8')
import numpy as np import tools as tl import time bl_size = 32 src = 'source/park.png' dst = 'fig' processor = 'gpu' start = time.time() img = tl.read_file(src) img = tl.pad(img, bl_size) shape = np.shape(img) block = tl.break_block(img, bl_size) predict, mode_predict = tl.inpainting(block, bl_size, processor = processor) time_total = time.time() - start print 'predict size: ', np.shape(predict) print 'block size: ', np.shape(predict[0]) print 'max: ', np.max(predict) print 'min: ', np.min(predict) print time_total predict = tl.group_block(predict, bl_size, shape) #predict = np.uint8(predict * (255.0 / np.max(predict))) #predict = np.uint8(predict) #plt.imshow(predict, cmap = cm.Greys_r) np.save(dst, predict) print 'Done'
import numpy as np import tools as tl import time bl_size = 8 src = 'source/park.png' dst = 'fig' processor = 'gpu' start = time.time() img = tl.read_file(src) img = tl.pad(img, bl_size) shape = np.shape(img) block = tl.break_block(img, bl_size) predict, mode_predict = tl.inpainting(block, bl_size, processor=processor) time_total = time.time() - start print 'predict size: ', np.shape(predict) print 'block size: ', np.shape(predict[0]) print 'max: ', np.max(predict) print 'min: ', np.min(predict) print time_total predict = tl.group_block(predict, bl_size, shape) #predict = np.uint8(predict * (255.0 / np.max(predict))) #predict = np.uint8(predict) #plt.imshow(predict, cmap = cm.Greys_r) np.save(dst, predict) print 'Done'
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables = self.num_cables + num_new_cables transition_shape = (self.num_cables, self.num_cables) self.value = tools.pad(self.value, transition_shape) self.reward = tools.pad(self.reward, transition_shape)
def add_cables(self, num_new_cables): """ Add new cables to the hub when new gearboxes are created """ self.num_cables += num_new_cables features_shape = (self.num_cables, 1) self.time_since_seen = tools.pad(self.time_since_seen, features_shape, val=self.INITIAL_TIME)
os.environ["CUDA_VISIBLE_DEVICES"] = "3" ''' Max English sentence: 642 Max Spanish sentence: 632 ''' max_spanish_sequence_length = 632 max_english_sequence_lengh = 642 en_tokenized = load_clean_sentences( "/home/8id/scriptTesting/translateData/english_tokenized") sp_tokenized = load_clean_sentences( "/home/8id/scriptTesting/translateData/spanish_tokenized") sp_tokenizer = loadTokenizer("/home/8id/scriptTesting/translateData/sp") en_tokenizer = loadTokenizer("/home/8id/scriptTesting/translateData/en") en_tokenized_padded = pad(en_tokenized, 642) sp_tokenized_padded = pad(sp_tokenized, 632) sp_tokenized_padded = sp_tokenized_padded.reshape((-1, 632, 1)) spanish_vocab_size = len(sp_tokenizer.word_index) english_vocab_size = len(en_tokenizer.word_index) print("English vocabulary size:", english_vocab_size) print("Spanish vocabulary size:", spanish_vocab_size) print("----") print(sp_tokenized_padded.shape) print(en_tokenized_padded.shape) print("read This:::::::", sp_tokenized_padded.shape[-2]) # Reshaping the input to work with a basic RNN tmp_x = pad(en_tokenized_padded, 632) tmp_x = tmp_x.reshape((-1, sp_tokenized_padded.shape[-2], 1))
def step_up(self, new_cable_activities): """ Update co-activity estimates and calculate bundle activity """ if new_cable_activities.size < self.max_num_cables: new_cable_activities = tools.pad(new_cable_activities, (self.max_num_cables, 1)) # debug: don't adapt cable activities self.cable_activities = new_cable_activities self.cable_activities = tools.bounded_sum2(new_cable_activities, self.cable_activities * (1. - self.ACTIVITY_DECAY_RATE)) ''' """ Find bundle activities by taking the generalized mean of the signals with a negative exponent. The negative exponent weights the lower signals more heavily. Make a first pass at the bundle activation levels by multiplying across the bundle map. """ initial_bundle_activities = tools.generalized_mean( self.cable_activities, self.bundle_map.T, self.MEAN_EXPONENT) bundle_contribution_map = np.zeros(self.bundle_map.shape) bundle_contribution_map[np.nonzero(self.bundle_map)] = 1. # Use aggressive lateral inhibition between bundles so that # cables' activity is monopolized by the strongest-activated bundle. activated_bundle_map = (initial_bundle_activities * bundle_contribution_map) # Add just a little noise to break ties activated_bundle_map += 1e-4 * np.random.random_sample( activated_bundle_map.shape) # Find the largest bundle activity that each input contributes to max_activation = (np.max(activated_bundle_map, axis=0) + tools.EPSILON) # Divide the energy that each input contributes to each bundle input_inhibition_map = np.power(activated_bundle_map / max_activation, self.ACTIVATION_WEIGHTING_EXPONENT) # Find the effective strength of each cable to each bundle # after inhibition. inhibited_cable_activities = (input_inhibition_map * self.cable_activities.T) final_bundle_activities = tools.generalized_mean( inhibited_cable_activities.T, self.bundle_map.T, self.MEAN_EXPONENT) self.bundle_activities = final_bundle_activities ''' """ Find bundle activities by taking the minimum input value in the set of cables in the bundle. """ #self.bundle_map[np.where(self.bundle_map==0.)] = np.nan bundle_components = self.bundle_map * self.cable_activities.T # TODO: consider other ways to calculate bundle energies self.bundle_energies = np.nansum(bundle_components, axis=1)[:,np.newaxis] self.bundle_energies[np.where(np.isnan(self.bundle_energies))] = 0. #print 'be', self.bundle_energies.T energy_index = np.argsort(self.bundle_energies.ravel())[:,np.newaxis] #print 'ei', energy_index.T max_energy = self.bundle_energies[energy_index[-1]] #print 'max_e', max_energy mod_energies = self.bundle_energies - ( max_energy * ( (self.num_bundles - energy_index) / (self.num_bundles + tools.EPSILON) )) #print 'me', mod_energies.T self.bundle_activities = np.nanmin(bundle_components, axis=1)[:,np.newaxis] #print 'ba before', self.bundle_activities.T self.bundle_activities[np.where(mod_energies < 0.)] = 0. self.bundle_activities = np.nan_to_num(self.bundle_activities) #self.bundle_activities[np.where(np.isnan(self.bundle_activities))] = 0. #print 'ba', self.bundle_activities.T self.reconstruction = np.nanmax(self.bundle_activities * self.bundle_map, axis=0)[:, np.newaxis] self.reconstruction[np.isnan(self.reconstruction)] = 0. self.nonbundle_activities = self.cable_activities - self.reconstruction #print 'recon', self.reconstruction.T #print 'nba', self.nonbundle_activities.T ''' # Calculate how much energy each input has left to contribute # to the co-activity estimate. final_activated_bundle_map = (final_bundle_activities * bundle_contribution_map) combined_weights = np.sum(final_activated_bundle_map, axis=0)[:,np.newaxis] ''' ''' if self.in_gearbox: self.nonbundle_activities = (cable_activities * 2 ** -np.sum(self.bundle_map, axis=0)[:,np.newaxis]) else: self.nonbundle_activities = np.maximum(0., cable_activities - combined_weights) self.nonbundle_activities = np.maximum(0., self.cable_activities - combined_weights) #self.cable_activities = cable_activities ''' # As appropriate update the co-activity estimate and # create new bundles if not self.exploit: if not self.bundles_full: self._create_new_bundles() self._grow_bundles() return self.bundle_activities
from base64 import b64decode if __name__ == '__main__': parser: ArgumentParser = create_parser() namespace = parser.parse_args() try: file = open(namespace.encryptedfile, 'r') except: print('Encrypted file path error') raise SystemExit(1) crypto: str = file.read() file.close() key: bytes = namespace.password.encode() key = pad(key, AES.block_size) iv = b64decode(crypto[0:24]) ct = b64decode(crypto[24:]) cipher: AES = AES.new(key, AES.MODE_CBC, iv) try: data: bytes = unpad(cipher.decrypt(ct), AES.block_size) except ValueError: print("Error password") raise SystemExit(1) try: passwords = data.decode() except UnicodeDecodeError: