def __init__(self, gymEnvironment, useRender=False): dataManager = DataManager(['episodes', 'steps']) dataManager.subDataManager.addDataEntry('rewards', 1) dataManager.subDataManager.addDataEntry('isTerminalState', 1, dataType=DataType.discrete) if isinstance(gymEnvironment.observation_space, spaces.Box): low = gymEnvironment.observation_space.low high = gymEnvironment.observation_space.high dataManager.addDataEntry('states', low.shape, low, high, level=1) elif isinstance(gymEnvironment.observation_space, spaces.Discrete): dataManager.addDataEntry('states', 1, np.array([1]), np.array( [gymEnvironment.observation_space.n]), dataType=DataType.discrete, level=1) if isinstance(gymEnvironment.action_space, spaces.Box): low = gymEnvironment.action_space.low high = gymEnvironment.action_space.high dataManager.addDataEntry('actions', low.shape, low, high, level=1) elif isinstance(gymEnvironment.observation_space, spaces.Discrete): dataManager.addDataEntry('actions', 1, np.array([1]), np.array([gymEnvironment.action_space.n]), dataType=DataType.discrete, level=1) # get one step to check the info gymEnvironment.reset() observation, reward, done, info = gymEnvironment.step( gymEnvironment.action_space.sample()) self.outputArgumentsStep = ['nextStates', 'rewards', 'isTerminalState'] for key, value in info.items(): if (isinstance(value, np.ndarray)): dataManager.addDataEntry(key, value.shape, level=1) self.outputArgumentsStep.append(key) super().__init__(self, dataManager) self.gymEnvironment = gymEnvironment self._parallelSampling = False
def test_mergeDataFront(self): dataManager = DataManager("manager") subDataManager = DataManager("subDataManager") dataManager.subDataManager = subDataManager dataManager.addDataEntry("entry1", 2) subDataManager.addDataEntry("entry2", 3) data1 = dataManager.createDataObject([10, 10]) data1.setDataEntry("entry1", ..., np.ones((10, 2))) data1.setDataEntry("entry2", ..., np.ones((100, 3))) data2 = dataManager.createDataObject([5, 5]) data2.setDataEntry("entry1", ..., np.zeros((5, 2))) data2.setDataEntry("entry2", ..., np.zeros((25, 3))) data1.mergeData(data2, True) self.assertEqual(data1.getNumElements("entry1"), 15) self.assertEqual(len(data1.dataStructure["subDataManager"]), 15) self.assertTrue((data1.getDataEntry("entry1", ...) == np.vstack( (np.zeros((5, 2)), np.ones((10, 2))))).all())
import numpy as np from pypost.data import DataManager ''' In this example we are going to see how data managers work. We will create a data manager, fill it with some random data and then learn how to retrieve it. ''' # Initialization of the manager # create data managers with 3 hierarchical layers (episodes, steps, subSteps) dataManager = DataManager('episodes') subDataManager = DataManager('steps') # do the same with the sub-manager: we have states and actions as data for # each steps subDataManager.addDataEntry('states', 1, -np.ones(1), np.ones(1)) subDataManager.addDataEntry('actions', 2, -np.ones(2), np.ones(2)) # now we only need to connect the data managers and finalize them dataManager.subDataManager = subDataManager # Initialization of the data # so far we have defined the structure of our data # now we want to create new data # here we create new data object with 100 episodes, 10 steps # this method will also finalize the dataManager myData = dataManager.createDataObject([100, 10]) # show all states (1000)
def test_setgetDataEntry(self): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager subSubDataManager.addDataEntry('subActions', 2, -10, 10) myData = dataManager.createDataObject([10, 5, 3]) # set the data for all subActions of all episodes, steps and subSteps myData.setDataEntry(['steps', 'subSteps', 'subActions'], [], np.ones((150, 2))) # all subActions in the 3rd subSteps of all steps of the 2nd episode # should have different parameters myData.setDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., 2], np.array([[2, 1], [2, 2], [2, 3], [2, 4], [2, 5]])) self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0] ['subActions'][0] == np.array([1, 1])).all()) self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][1] ['subActions'][1] == np.array([1, 1])).all()) self.assertTrue((myData.dataStructure['steps'][9]['subSteps'][4] ['subActions'][2] == np.array([1, 1])).all()) self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4] ['subActions'][1] == np.array([1, 1])).all()) self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][0] ['subActions'][2] == np.array([2, 1])).all()) self.assertTrue((myData.dataStructure['steps'][1]['subSteps'][4] ['subActions'][2] == np.array([2, 5])).all()) # tests for getDataEntry self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], [0, 0]) == myData.dataStructure['steps'][0] ['subSteps'][0]['subActions']).all()) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], 1) == np.array([[1, 1], [1, 1], [2, 1], [1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [2, 3], [1, 1], [1, 1], [2, 4], [1, 1], [1, 1], [2, 5]])).all()) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., 2]) == np.array([[2, 1], [2, 2], [2, 3], [2, 4], [2, 5]])).all()) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, 3, 2]) == np.array([[2, 4]])).all()) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], [2, 3, 2]) == np.array([[1, 1]])).all()) # all subActions in every second subStep of all steps of the 2nd episode # should have different parameters myData.setDataEntry(['steps', 'subSteps', 'subActions'], [1, slice(0, 5, 2), 2], np.array([[3, 1], [3, 2], [3, 3]])) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], 1) == np.array([[1, 1], [1, 1], [3, 1], [1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [3, 2], [1, 1], [1, 1], [2, 4], [1, 1], [1, 1], [3, 3]])).all()) # all subActions in the first 3 subSteps of all steps of the 2nd episode # should have different parameters myData.setDataEntry(['steps', 'subSteps', 'subActions'], [1, slice(0, -2), 2], np.array([[4, 1], [4, 2], [4, 3]])) self.assertTrue( (myData.getDataEntry(['steps', 'subSteps', 'subActions'], 1) == np.array([[1, 1], [1, 1], [4, 1], [1, 1], [1, 1], [4, 2], [1, 1], [1, 1], [4, 3], [1, 1], [1, 1], [2, 4], [1, 1], [1, 1], [3, 3]])).all()) self.assertRaises(ValueError, myData.setDataEntry, ['steps', 'subSteps', 'subActions'], [1, 2, slice(0, 1)], np.array([[5, 1], [5, 2]])) self.assertRaises(ValueError, myData.setDataEntry, ['steps', 'subSteps', 'subActions'], [1, ..., 0], np.array([[5, 1], [5, 2], [5, 3]])) self.assertRaises(ValueError, myData.setDataEntry, ['steps', 'subSteps', 'subActions'], [1, slice(0, -2), 2], np.array([[5, 1], [5, 2]])) self.assertRaises(ValueError, myData.setDataEntry, ['steps', 'subSteps', 'subActions'], [1, slice(0, -2), 2], np.array([])) self.assertRaises(ValueError, myData.setDataEntry, ['steps', 'subSteps', 'subActions'], [1, slice(0, -2), 2], np.array([[5, 1], [5, 2], [5, 3], [5, 4]]))
def test_getDataEntryDimensions(self): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager dataManager.addDataEntry('parameters', 5) subDataManager.addDataEntry('states', 1) subSubDataManager.addDataEntry('subStates', 1) subSubDataManager.addDataEntry('subActions', 2) myData = dataManager.createDataObject([10, 5, 3]) self.assertEqual(myData.getDataEntry(['parameters']).shape[0], 10) self.assertEqual( myData.getDataEntry(['parameters'], [...]).shape[0], 10) self.assertEqual(myData.getDataEntry(['parameters'], [1]).shape[0], 1) self.assertEqual( myData.getDataEntry(['steps', 'states'], [1]).shape[0], 5) self.assertEqual( myData.getDataEntry(['steps', 'states'], [3]).shape[0], 5) self.assertEqual( myData.getDataEntry(['steps', 'states'], [..., 1]).shape[0], 10) self.assertEqual( myData.getDataEntry(['steps', 'states'], [..., ...]).shape[0], 50) self.assertEqual(myData.getDataEntry(['steps', 'states']).shape[0], 50) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [..., ..., ...]).shape[0], 150) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions']).shape[0], 150) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [..., ..., 1]).shape[0], 50) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [..., 1, ...]).shape[0], 30) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [..., 1]).shape[0], 30) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [..., 1, 1]).shape[0], 10) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., ...]).shape[0], 15) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1]).shape[0], 15) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], 1).shape[0], 15) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, ..., 1]).shape[0], 5) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, 1, ...]).shape[0], 3) self.assertEqual( myData.getDataEntry(['steps', 'subSteps', 'subActions'], [1, 1, 1]).shape[0], 1)
def test_addDataEntry_after_finalize(self): dataManager = DataManager('episodes') myData = dataManager.createDataObject(10) self.assertRaises(RuntimeError, dataManager.addDataEntry, 'parameters', 5)
def test_getDataObject(self): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 2) subDataManager.addDataEntry('states', 1) subDataManager.addDataEntry('actions', 2) subSubDataManager.addDataEntry('subStates', 1) subSubDataManager.addDataEntry('subActions', 2) myData = dataManager.createDataObject([10, 5, 1]) self.assertTrue(dataManager.finalized) self.assertEqual(len(myData.dataStructure['parameters']), 10) self.assertEqual(len(myData.dataStructure['contexts']), 10) self.assertEqual(len(myData.dataStructure['parameters'][0]), 5) self.assertEqual(len(myData.dataStructure['parameters'][9]), 5) self.assertEqual(len(myData.dataStructure['contexts'][0]), 2) self.assertEqual(len(myData.dataStructure['contexts'][9]), 2) self.assertEqual(len(myData.dataStructure['steps']), 10) self.assertEqual(len(myData.dataStructure['steps'][0]), 3) self.assertEqual(len(myData.dataStructure['steps'][0]['states']), 5) self.assertEqual(len(myData.dataStructure['steps'][0]['states'][0]), 1) self.assertEqual(len(myData.dataStructure['steps'][0]['actions']), 5) self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][0]), 2) self.assertEqual(len(myData.dataStructure['steps'][0]['actions'][1]), 2) self.assertEqual(len(myData.dataStructure['steps'][0]['subSteps']), 5) self.assertEqual( len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']), 1) self.assertEqual( len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions']), 1) self.assertEqual( len(myData.dataStructure['steps'][0]['subSteps'][0]['subActions'] [0]), 2)
import numpy as np import time from pypost.data import DataManager from pypost.data import DataManagerTimeSeries ''' In this example we are going to see how data managers work. We will create a data manager, fill it with some random data and then learn how to retrieve it. ''' # Initialization of the manager # create data managers with 3 hierarchical layers (episodes, steps, subSteps) dataManager = DataManager('episodes') subDataManager = DataManagerTimeSeries('steps') # do the same with the sub-manager: we have states and actions as data for # each steps subDataManager.addDataEntry('states', (5, 5), -np.ones((5, 5)), np.ones( (5, 5))) subDataManager.addDataEntry('actions', 1, -np.ones(1), np.ones(1)) # now we only need to connect the data managers and finalize them dataManager.subDataManager = subDataManager # Initialization of the data # so far we have defined the structure of our data # now we want to create new data numTimeSteps = 1000 # here we create new data object with 1 episodes, 10000 steps
import numpy as np from pypost.data import DataManager ''' In this example we are going to see how data managers work. We will create a data manager, fill it with some random data and then learn how to retrieve it. ''' # Initialization of the manager # create data managers with 3 hierarchical layers (episodes, steps, subSteps) dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') # add data entries to each of the layers # here we add an entry named 'parameters' with 5 elements in range [-2,2] dataManager.addDataEntry('parameters', 5, -2*np.ones(5), 2*np.ones(5)) # here we add an entry named 'context' with 2 elements in range [-1,1] # (this is the default for minRange and maxRange) dataManager.addDataEntry('context', 2) # do the same with the sub-manager: we have states and actions as data for # each steps subDataManager.addDataEntry('states', 1, -np.ones(1), np.ones(1)) subDataManager.addDataEntry('actions', 2, -np.ones(2), np.ones(2)) # now we only need to connect the data managers and finalize them dataManager.subDataManager = subDataManager
from pypost.data import DataManager import time ''' In this example we are going to see how to use aliases. ''' # create data manager dataManager = DataManager('episodes') # add data entries dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 5) # add an alias # parameterAlias points to the first two dimensions of parameters dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) # it's also possible to create an alias that points to another alias # 'aliasAlias' will now be the aquivalent to 'parameters' dataManager.addDataAlias('aliasAlias', [('parameterAlias', ...), ('contexts', slice(2, 5))]) dataManager.addDataAlias('parametersAliasVertical', [('parameters', slice(0, 2)), ('parameters', slice(2, 4))], useConcatVertical=True) # create the data object myData = dataManager.createDataObject([3]) # set the entries of the 'parameters' entry. This will also affect the alias # that points to 'parameters' parameters = myData[...].parameters
def setUp(self): self.dataManager = DataManager('episodes')
import numpy as np from pypost.data import DataManager from pypost.common.SettingsManager import * settings = getDefaultSettings() settings.setProperty('optionalEntry', np.array([[-2, -2]])) settings.setProperty('optionalEntry2', 1) dataManager = DataManager('episodes') # Create optional entry where default is not to use it dataManager.addOptionalDataEntry('optionalEntry', False, 2, np.array([[-1, -1]]), np.array([[5, 5]])) # Create optional entry where default is not to use, but we overwrite this default before settings.setProperty('useOptionalEntry2', True) dataManager.addOptionalDataEntry('optionalEntry2', False, 1, np.array([[-1]]), np.array([[5]])) data = dataManager.createDataObject(3) # Optional entry is now [-2 -2] print('Optional Entry\n', data[...].optionalEntry, '\n\n') dataManager.settings.setProperty('optionalEntry', np.array([[-5, -5]])) # Optional entry is now [-5 -5] print('Optional Entry\n', data[...].optionalEntry, '\n\n') # We can not put data in optional entry try:
import time from pypost.learner import LinearGaussianMLLearner from pypost.learner.InputOutputLearner import LogLikeGradientLearner from pypost.learner.InputOutputLearner import L2GradientLearner from pypost.common import getDefaultSettings from pypost.optimizer import TFOptimizerType from pypost.mappings.Gaussian import LinearFullGaussian num_cpu = 1 tf_config = tf.ConfigProto(inter_op_parallelism_threads=num_cpu, intra_op_parallelism_threads=num_cpu) session = tf.Session(config=tf_config) session.__enter__() dataManager = DataManager('data') dataManager.addDataEntry('states', 4) dataManager.addDataEntry('actions', 6) settings = getDefaultSettings() gaussian = LinearFullGaussian(dataManager, ['states'], ['actions']) data = dataManager.createDataObject([10000]) data[...].states = np.random.normal(0, 1, data[...].states.shape) A = np.random.normal(0, 1, (dataManager.getNumDimensions('actions'), dataManager.getNumDimensions('actions'))) covMat = A.dot(A.transpose()) gaussian.param_stdmat = np.linalg.cholesky(covMat) gaussian.param_final_w = np.random.normal(0, 1, gaussian.param_final_w.shape)
inputVariables = self.inputVariables clone = MLPClassifier(self.dataManager, inputVariables, self.outputVariables, hiddenNodes=self.hiddenNodes, name=name) clone.params = self.params return clone if __name__ == "__main__": num_cpu = 1 tf_config = tf.ConfigProto(inter_op_parallelism_threads=num_cpu, intra_op_parallelism_threads=num_cpu) session = tf.Session(config=tf_config) session.__enter__() dataManager = DataManager('data') dataManager.addDataEntry('states', 10) dataManager.addDataEntry('predictions', 1) generatorMean = tfutils.continuous_MLP_generator([100, 100]) generatorLogStd = tfutils.diagional_log_std_generator() function = LinearClassifier(dataManager, ['states'], ['predictions']) data = dataManager.createDataObject([10]) data[...].states = np.random.normal(0, 1, data[...].states.shape)
def test_getSubDataManagerForDepth(self): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager) self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager) self.assertEqual(dataManager.getDataManagerForLevel(0), dataManager) self.assertEqual(dataManager.getDataManagerForLevel(1), subDataManager) self.assertEqual(dataManager.getDataManagerForLevel(2), subSubDataManager) self.assertEqual(dataManager.getDataManagerForLevel(3), None)
import numpy as np import time from pypost.data import DataManager from pypost.data import DataManagerTimeSeries ''' In this example we are going to see how data managers work. We will create a data manager, fill it with some random data and then learn how to retrieve it. ''' # Initialization of the manager # create data managers with 3 hierarchical layers (episodes, steps, subSteps) dataManager = DataManager('episodes') subDataManager = DataManagerTimeSeries('steps') # do the same with the sub-manager: we have states and actions as data for # each steps subDataManager.addDataEntry('states', 5, -np.ones(5), np.ones(5)) subDataManager.addDataEntry('actions', 1, -np.ones(1), np.ones(1)) subDataManager.addDataWindowAlias('historyStates', [('states', ...)], -3, 1, dropBoundarySamples=False) # now we only need to connect the data managers and finalize them dataManager.subDataManager = subDataManager # Initialization of the data # so far we have defined the structure of our data # now we want to create new data
def test_reserveStorage(self): dataManager = DataManager('episodes') dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 2) subDataManager = DataManager('steps') subDataManager.addDataEntry('states', 2) subDataManager.addDataEntry('actions', 2) dataManager.subDataManager = subDataManager data = dataManager.createDataObject([100, 20]) data.reserveStorage([20, 20]) self.assertEqual(data.dataStructure['contexts'].shape[0], 20) self.assertEqual(data.dataStructure['parameters'].shape[0], 20) for i in range(0, 20): self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0], 20) self.assertEqual( data.dataStructure['steps'][i]['actions'].shape[0], 20) data.reserveStorage([50, 100]) self.assertEqual(data.dataStructure['contexts'].shape[0], 50) self.assertEqual(data.dataStructure['parameters'].shape[0], 50) for i in range(0, 50): self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0], 100) self.assertEqual( data.dataStructure['steps'][i]['actions'].shape[0], 100) data.reserveStorage([50, 0]) self.assertEqual(data.dataStructure['contexts'].shape[0], 50) self.assertEqual(data.dataStructure['parameters'].shape[0], 50) for i in range(0, 50): self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0], 0) self.assertEqual( data.dataStructure['steps'][i]['actions'].shape[0], 0) data.reserveStorage(15, ...) self.assertEqual(data.dataStructure['contexts'].shape[0], 50) self.assertEqual(data.dataStructure['parameters'].shape[0], 50) for i in range(0, 50): self.assertEqual(data.dataStructure['steps'][i]['states'].shape[0], 15) self.assertEqual( data.dataStructure['steps'][i]['actions'].shape[0], 15)
outputVariables='X') @Mapping.MappingMethod() def computeSomething(self, X): return X + 1 @DataManipulator.DataMethod(inputArguments=[], outputArguments=['X'], takesNumElements=True) def reset(self, numElements): return np.zeros((numElements, 2)) # Create a dataManager that can handle the input (X) and output (Y) of a 1 dimensional # function dataManager = DataManager('values') dataManager.addDataEntry('X', 2) data = dataManager.createDataObject([10]) mapping = DummyMapping(dataManager) print(data[...].X) # apply mapping data[...] >> mapping >> data print(data[...].X) mapping.reset >> data # Applying the >> to the mapping and writing the result back to data. Using the >> operator for the second operation returns the data object
def test_addDataEntry_name_conflict(self): dataManager = DataManager('episodes') dataManager.addDataEntry('parameters', 5) dataManager.addDataAlias('conflict', [('parameters', ...)]) self.assertRaises(ValueError, dataManager.addDataEntry, 'conflict', 0)
def setUp(self): self.dataManager = DataManager('testmngr') self.ia2s = TerminationFunction(self.dataManager, None)
def test_init(self): dataManager = DataManager('episodes') self.assertIsInstance(dataManager, DataManager) self.assertEqual(dataManager.name, 'episodes')
def test_addDataAlias(self): dataManager = DataManager('episodes') self.assertIsInstance(dataManager.dataAliases, dict) dataManager.addDataEntry('parameters', 5, -1, 1) dataManager.addDataEntry('contexts', 5, -1, 1) # Add alias dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 1))]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 1))]) # Replace entry of same alias dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 2))]) # Add another entry to alias dataManager.addDataAlias('parameterAlias', [('contexts', ...)]) self.assertEqual(dataManager.dataAliases['parameterAlias'].entryList, [('parameters', slice(0, 2)), ('contexts', ...)]) # Recursive alias dataManager.addDataAlias('aliasToParameterAlias', [('parameterAlias', ...)]) self.assertEqual( dataManager.dataAliases['aliasToParameterAlias'].entryList, [('parameterAlias', ...)]) # Alias cycle dataManager.addDataAlias('badAlias', [('aliasToParameterAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'aliasToParameterAlias', [('badAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'badAlias', [('badAlias', ...)]) self.assertRaises(ValueError, dataManager.addDataAlias, 'contexts', [('contexts', ...)])
def test_getDataEntryDeepCopy(self): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager dataManager.addDataEntry('parameters', 5) subDataManager.addDataEntry('states', 1) subSubDataManager.addDataEntry('subActions', 2, -10, 10) myData = dataManager.createDataObject([1, 1, 1]) # set the data for all subActions of all episodes, steps and subSteps myData.setDataEntry('subActions', [], np.ones((1, 2))) self.assertTrue((myData.dataStructure['steps'][0]['subSteps'][0] ['subActions'][0] == np.array([1, 1])).all()) data1 = myData.getDataEntry('subActions', []) data2 = myData.getDataEntry('subActions', [], True) data3 = myData.getDataEntry('subActions', [], False) data4 = myData.getDataEntry('subActions', [], False) self.assertTrue((data1 == data2).all()) self.assertTrue((data2 == data3).all()) self.assertTrue((data3 == data4).all()) data2[0, 1] = 7 # This should NOT have any impact on any other data self.assertTrue((data2 != data1).any()) self.assertTrue((data2 != data3).any()) self.assertTrue((data2 != data4).any()) data5 = myData.getDataEntry('subActions', []) data3[0, 0] = 8 # This MAY impact the data in the data structure, # but should not impact data1 or data2 self.assertTrue((data5 == data1).all()) self.assertTrue((data3 != data1).any()) self.assertTrue((data3 == data4).any()) myData.setDataEntry('subActions', [], data2) # still no impact on data1 self.assertTrue((data5 == data1).all()) self.assertTrue(((myData.getDataEntry('subActions', [])) == [1, 7]).all())
def test_addDataAlias_missing_data_entry(self): dataManager = DataManager('episodes') self.assertRaises(ValueError, dataManager.addDataAlias, 'alias', [('none', ...)])
def test_getNumElementsForIndex(self): manager = DataUtil.createTestManager() data = manager.createDataObject([3, 4, 5]) self.assertEqual(data.getNumElementsForIndex(0), 3) self.assertEqual(data.getNumElementsForIndex(0, [...]), 3) self.assertEqual(data.getNumElementsForIndex(1), 12) self.assertEqual(data.getNumElementsForIndex(1, [...]), 12) self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12) self.assertEqual(data.getNumElementsForIndex(1, [..., slice(0, 2)]), 6) self.assertEqual( data.getNumElementsForIndex(1, [slice(2, 3), slice(0, 2)]), 2) self.assertEqual(data.getNumElementsForIndex(2), 60) self.assertEqual( data.getNumElementsForIndex(2, ...), 60) # test implicit cast to array of second argument (deprecated) self.assertEqual(data.getNumElementsForIndex(2, [...]), 60) # this should be the standart way self.assertEqual(data.getNumElementsForIndex(2, [..., ...]), 60) self.assertEqual(data.getNumElementsForIndex(2, [..., ..., ...]), 60) self.assertEqual( data.getNumElementsForIndex(2, [..., ..., slice(0, 2)]), 24) self.assertEqual( data.getNumElementsForIndex( 2, [..., slice(1, 4), slice(0, 2)]), 18) self.assertEqual( data.getNumElementsForIndex( 2, [slice(0, 1), slice(2, 3), slice(0, 2)]), 2) dataManager = DataManager('episodes') subDataManager = DataManager('steps') dataManager.subDataManager = subDataManager dataManager.addDataEntry('parameters', 5) dataManager.addDataAlias('pAlias', [('parameters', ...)]) dataManager.addDataEntry('contexts', 2) data = dataManager.createDataObject([3, 4, 5]) self.assertEqual(data.getNumElementsForIndex(0), 3) self.assertEqual(data.getNumElementsForIndex(0, [...]), 3) self.assertEqual(data.getNumElementsForIndex(1), 12) self.assertEqual(data.getNumElementsForIndex(1, [...]), 12) self.assertEqual(data.getNumElementsForIndex(1, [..., ...]), 12) self.assertEqual(data.getNumElementsForIndex(1, [slice(2, 3)]), 4)
def test_addDataAlias_after_finalize(self): dataManager = DataManager('episodes') myData = dataManager.createDataObject([10]) self.assertRaises(RuntimeError, dataManager.addDataAlias, 'alias', [])
@TFMapping.TensorMethod() def constantTerm(self): return tf.get_variable('constTerm', shape=[1], initializer=tf.zeros_initializer()) if __name__ == "__main__": num_cpu = 1 tf_config = tf.ConfigProto(inter_op_parallelism_threads=num_cpu, intra_op_parallelism_threads=num_cpu) session = tf.Session(config=tf_config) session.__enter__() dataManager = DataManager('data') dataManager.addDataEntry('states', 10) dataManager.addDataEntry('actions', 5) dataManager.addDataEntry('rewards', 1) function = LinearFunction(dataManager, ['states'], ['actions']) quadraticFeatures = QuadraticFeatureExpansion(dataManager, ['actions']) data = dataManager.createDataObject([10]) data[...].states = np.random.normal(0, 1, data[...].states.shape) quadraticFunction = QuadraticFunction(dataManager, ['actions'], ['rewards']) print('Hello')
def test_get_alias_data(self): dataManager = DataManager('episodes') dataManager.addDataEntry('parameters', 5) dataManager.addDataEntry('contexts', 5) dataManager.addDataAlias('parameterAlias', [('parameters', slice(0, 2))]) dataManager.addDataAlias('twoAlias', [('parameters', slice(0, 2)), ('contexts', slice(2, 5))]) myData = dataManager.createDataObject([10, 5, 1]) myData.dataStructure['parameters'] = np.ones((10, 5)) myData.dataStructure['contexts'] = np.ones((10, 5)) * 2 paramAlias = myData.dataStructure['parameterAlias'] paramAlias[:] = np.ones(2) * 3 paramAlias[0][1] = 10 myData.dataStructure['parameterAlias'] = paramAlias self.assertEqual(myData.dataStructure['parameters'][0][1], 10) self.assertEqual(myData.dataStructure['parameters'][0][2], 1) self.assertEqual(myData.dataStructure['parameters'][3][1], 3) self.assertEqual(myData.dataStructure['parameters'][5][3], 1) twoAlias = myData.dataStructure['twoAlias'] twoAlias[4] = np.ones(5) * 4 twoAlias[5] = np.ones(5) * 5 twoAlias[6] = np.ones(5) * 6 twoAlias[-1] = np.ones(5) * 9 myData.dataStructure['twoAlias'] = twoAlias self.assertEqual(myData.dataStructure['twoAlias'][0][3], 2) self.assertEqual(myData.dataStructure['twoAlias'][-1][3], 9) self.assertEqual(myData.dataStructure['twoAlias'][4][2], 4) self.assertEqual(myData.dataStructure['twoAlias'][5][2], 5) self.assertEqual(myData.dataStructure['parameters'][0][0], 3) self.assertEqual(myData.dataStructure['parameters'][0][1], 10) self.assertEqual(myData.dataStructure['parameters'][0][2], 1) self.assertEqual(myData.dataStructure['parameters'][4][2], 1) self.assertEqual(myData.dataStructure['parameters'][5][2], 1) self.assertEqual(myData.dataStructure['contexts'][1][3], 2) self.assertEqual(myData.dataStructure['contexts'][6][3], 6) self.assertEqual(myData.dataStructure['contexts'][-1][3], 9) self.assertEqual(myData.dataStructure['contexts'][4][0], 2) self.assertEqual(myData.dataStructure['contexts'][5][1], 2) self.assertEqual(myData.dataStructure['contexts'][4][2], 4) self.assertEqual(myData.dataStructure['contexts'][5][2], 5)
def test_getExpectationAndSigma_NotImplementedError(self): dataManager = DataManager("TestDataManager") distribution = DistributionWithMeanAndVariance(dataManager) self.assertRaises(NotImplementedError, distribution.getExpectationAndSigma, 1, [])
def createTestManager(): dataManager = DataManager('episodes') subDataManager = DataManager('steps') subSubDataManager = DataManager('subSteps') dataManager.subDataManager = subDataManager subDataManager.subDataManager = subSubDataManager dataManager.addDataEntry('parameters', 5, -100, 100) dataManager.addDataEntry('contexts', 1, -100, 100) subDataManager.addDataEntry('states', 1, -100, 100) subDataManager.addDataEntry('actions', 2) subSubDataManager.addDataEntry('subStates', 1) subSubDataManager.addDataEntry('subActions', 2) return dataManager