def generate_objset(task, n_epoch=30, distractor=True): objset = sg.ObjectSet(n_epoch=n_epoch) # Guess objects for epoch_now in range(n_epoch): if distractor: objset.add(sg.Object(when='now', deletable=True), epoch_now) objset = task.get_expected_input(objset, epoch_now) return objset
def generate_objset(self, n_epoch, n_distractor=1, average_memory_span=2): """Generate object set.""" if n_epoch < 4: raise ValueError( 'Number of epoch {:d} is less than 4'.format(n_epoch)) shape1, shape2 = sg.sample_shape(2) shape3 = sg.random_shape() objset = sg.ObjectSet(n_epoch=n_epoch) sample1 = sg.Object([self._color1, shape1], when='now') distractor1 = sg.Object([self._color3, shape3], when='now') test1 = sg.Object([self._color2, shape1], when='now') test2 = sg.Object([self._color2, shape2], when='now') objset.add(sample1, epoch_now=1) # sample epoch objset.add(distractor1, epoch_now=2) # delay epoch objset.add(test1, epoch_now=3) # test epoch objset.add(test2, epoch_now=3) # test epoch return objset
def generate_objset(self, n_epoch, n_distractor=1, average_memory_span=2): if n_epoch < 4: raise ValueError( 'Number of epoch {:d} is less than 4'.format(n_epoch)) objset = sg.ObjectSet(n_epoch=n_epoch) n_sample = 2 sample_shapes = sg.sample_shape(n_sample) for i in range(n_sample): obj = sg.Object([self._color1, sample_shapes[i]], when='now') objset.add(obj, epoch_now=1) obj = sg.Object([sg.another_color(self._color1)], when='now') objset.add(obj, epoch_now=1) # distractor if random.random() < 0.5: shape3 = random.choice(sample_shapes) else: shape3 = sg.another_shape(sample_shapes) test1 = sg.Object([self._color1, shape3], when='now') objset.add(test1, epoch_now=3) # test epoch return objset
def get_expected_input(self, should_be): if self.objs.when != 'now': raise ValueError(""" Guess objset is not supported for the Exist class for when other than now""") if should_be is None: should_be = random.random() > 0.5 if should_be: should_be = [sg.Object()] else: should_be = [] return should_be
def generate_objset(self, n_epoch, n_distractor=1, average_memory_span=2): """Generate object set. The task has 4 epochs: Fixation, Sample, Delay, and Test. During sample, one sample object is shown. During test, two test objects are shown, one of them will match the color of the sample object Args: n_epoch: int Returns: objset: ObjectSet instance. Raises: ValueError: when n_epoch is less than 4, the minimum epoch number for this task """ if n_epoch < 4: raise ValueError( 'Number of epoch {:d} is less than 4'.format(n_epoch)) color1, color2 = sg.sample_color(2) color3 = sg.random_color() objset = sg.ObjectSet(n_epoch=n_epoch) sample1 = sg.Object([color1, self._shape1], when='now') distractor1 = sg.Object([color3, self._shape3], when='now') test1 = sg.Object([color1, self._shape2], when='now') test2 = sg.Object([color2, self._shape2], when='now') objset.add(sample1, epoch_now=1) # sample epoch objset.add(distractor1, epoch_now=2) # delay epoch objset.add(test1, epoch_now=3) # test epoch objset.add(test2, epoch_now=3) # test epoch return objset
def generate_objset(self, n_epoch, n_distractor=1, average_memory_span=2): """Manual generate objset. By design this function will not be balanced because the network always answer False during the sample epoch. """ if n_epoch < 4: raise ValueError( 'Number of epoch {:d} is less than 4'.format(n_epoch)) objset = sg.ObjectSet(n_epoch=n_epoch) n_sample = random.choice([1, 2, 3, 4]) sample_attrs = sg.sample_colorshape(n_sample + 1) for attrs in sample_attrs[:n_sample]: obj = sg.Object(attrs, when='now') objset.add(obj, epoch_now=1) if random.random() < 0.5: attr = random.choice(sample_attrs[:n_sample]) else: attr = sample_attrs[-1] test1 = sg.Object(attr, when='now') objset.add(test1, epoch_now=3) # test epoch return objset
def get_expected_input(self, should_be): if should_be is None: should_be = sg.random_attr(self.attr_type) objs = sg.Object([should_be]) return [objs]
def get_expected_input(self, should_be, objset, epoch_now): """Guess objset for Select operator. Optionally modify the objset based on the target output, should_be, and pass down the supposed input attributes There are two sets of attributes to compute: (1) The attributes of the expected input, i.e. the attributes to select (2) The attributes of new object being added to the objset There are two main scenarios: (1) the target output is empty (2) the target output is not empty, then it has to be a list with a single Object instance in it When (1) the target output is empty, the goal is to make sure attr_newobject and attr_expectedinput differ by a single attribute When (2) the target output is not empty, then attr_newobject and attr_expectedinput are the same We first decide the attributes of the expected inputs. If target output is empty, then expected inputs is determined solely based on its actual inputs. If the actual inputs can already be computed, then use it, and skip the traversal of following nodes. Otherwise, use a random allowed value. If target output is not empty, again, if the actual inputs can be computed, use it. If the actual input can not be computed, but the attribute is specified by the target output, use that. Otherwise, use a random value. Then we determine the new object being added. If target output is empty, then randomly change one of the attributes that is not None. For self.space attribute, if it is an operator, then add the object at the opposite side of space If target output is not empty, use the attributes of the expected input Args: should_be: a list of Object instances objset: objset instance epoch_now: int, the current epoch Returns: objset: objset instance space: supposed input color: supposed input shape: supposed input Raises: TypeError: when should_be is not a list of Object instances NotImplementedError: when should_be is a list and has length > 1 """ # print(should_be[0]) if should_be is None: raise NotImplementedError('This should not happen.') if should_be: # Making sure should_be is a length-1 list of Object instance for s in should_be: if not isinstance(s, sg.Object): raise TypeError('Wrong type for items in should_be: ' + str(type(s))) if len(should_be) > 1: for s in should_be: print(s) raise NotImplementedError() obj_should_be = should_be[0] attr_new_object = list() attr_type_not_fixed = list() # First evaluate the inputs that can be evaluated for attr_type in ['loc', 'color', 'shape']: a = getattr(self, attr_type) attr = a(objset, epoch_now) # If the input is successfully evaluated if attr != const.INVALID and attr.has_value: if attr_type == 'loc': attr = attr.get_space_to(self.space_type) attr_new_object.append(attr) else: attr_type_not_fixed.append(attr_type) # Add an object based on these attributes # Note that, objset.add() will implicitly select the objset obj = sg.Object(attr_new_object, when=self.when) obj = objset.add(obj, epoch_now, add_if_exist=False) if obj is None: return objset, Skip(), Skip(), Skip() # If some attributes of the object are given by should_be, use them for attr_type in attr_type_not_fixed: a = getattr(obj_should_be, attr_type) if a.has_value: setattr(obj, attr_type, a) # If an attribute of select is an operator, then the expected input is # the value of obj attr_expected_in = list() for attr_type in ['loc', 'color', 'shape']: a = getattr(self, attr_type) if isinstance(a, Operator): # Only operators need expected_in if attr_type == 'loc': space = obj.loc.get_opposite_space_to(self.space_type) attr = space.sample() else: attr = getattr(obj, attr_type) attr_expected_in.append(attr) else: attr_expected_in.append(Skip()) if not should_be: # First determine the attributes to flip later attr_type_to_flip = list() for attr_type in ['loc', 'color', 'shape']: a = getattr(self, attr_type) # If attribute is operator or is a specified value if isinstance(a, Operator) or a.has_value: attr_type_to_flip.append(attr_type) # Now generate expected input attributes attr_expected_in = list() attr_new_object = list() for attr_type in ['loc', 'color', 'shape']: a = getattr(self, attr_type) attr = a(objset, epoch_now) if isinstance(a, Operator): if attr == const.INVALID: # Can not be evaluated yet, then randomly choose one attr = sg.random_attr(attr_type) attr_expected_in.append(attr) else: attr_expected_in.append(Skip()) if attr_type in attr_type_to_flip: # Candidate attribute values for the new object attr_new_object.append(attr) # Randomly pick one attribute to flip attr_type = random.choice(attr_type_to_flip) i = attr_type_to_flip.index(attr_type) if attr_type == 'loc': # If flip location, place it in the opposite direction loc = attr_new_object[i] attr_new_object[i] = loc.get_opposite_space_to(self.space_type) else: # Select a different attribute attr_new_object[i] = sg.another_attr(attr_new_object[i]) # Not flipping loc, so place it in the correct direction if 'loc' in attr_type_to_flip: j = attr_type_to_flip.index('loc') loc = attr_new_object[j] attr_new_object[j] = loc.get_space_to(self.space_type) # Add an object based on these attributes obj = sg.Object(attr_new_object, when=self.when) obj = objset.add(obj, epoch_now, add_if_exist=False) # Return the expected inputs return [objset] + attr_expected_in