def __init__(self): pyaon.setNumThreads(8) lds = [] for i in range(1): ld = pyaon.LayerDesc() ld.hiddenSize = Int3(5, 5, 16) ld.ffRadius = 5 ld.lRadius = 5 ld.pRadius = 5 ld.aRadius = 5 ld.ticksPerUpdate = 2 ld.temporalHorizon = 2 lds.append(ld) input_sizes = [Int3(4, 3, ANGLE_RESOLUTION)] input_types = [pyaon.inputTypePrediction] input_sizes.append(Int3(3, 1, COMMAND_RESOLUTION)) input_types.append(pyaon.inputTypeNone) input_sizes.append(Int3(3, 2, IMU_RESOLUTION)) input_types.append(pyaon.inputTypeNone) self.h = pyaon.Hierarchy(input_sizes, input_types, lds) self.reward = 1.0 self.direction = np.array([0.0, 0.0, 0.0]) self.average_error = 0.0 self.average_error_decay = 0.999 self.num_samples = 0 self.offsets = np.array([ -0.12295051, 0.12295051, -0.12295051, 0.12295051, 0.77062617, 0.77062617, 0.77062617, 0.77062617, -0.845151, -0.845151, -0.845151, -0.845151 ])
numActions = env.action_space.n # N actions (1 discrete value) res = 32 se = ScalarEncoder(4, 9, res) # Set the number of threads pyaon.setNumThreads(8) # Define layer descriptors: Parameters of each layer upon creation lds = [] for i in range( 2 ): # Layers with exponential memory. Not much memory is needed for Cart-Pole, so we only use 2 layers ld = pyaon.LayerDesc(hiddenSize=(3, 3, 16)) ld.eRadius = 1 ld.dRadius = 1 lds.append(ld) # Create the hierarchy: Provided with input layer sizes (a single column in this case), and input types (a single predicted layer) h = pyaon.Hierarchy() h.initRandom([ pyaon.IODesc((3, 3, res), pyaon.prediction, eRadius=1, dRadius=1), pyaon.IODesc((1, 1, numActions), pyaon.action, eRadius=0, dRadius=1, historyCapacity=64)
def main(): pyaon.setNumThreads(8) lds = [] for i in range(3): ld = pyaon.LayerDesc() ld.hiddenSize = (3, 3, 16) lds.append(ld) h = pyaon.Hierarchy() h.initRandom( [ pyaon.IODesc((2, 4, sensorRes), pyaon.none, 2, 2, 2, 32), # Priop pyaon.IODesc((2, 3, sensorRes), pyaon.none, 2, 2, 2, 32), # IMU pyaon.IODesc( (4, 4, motorRes), pyaon.action, 2, 2, 2, 32) # Motor control ], lds) iks = [] for i in range(4): iks.append(DeltaIK()) controller = ManualController(iks) angles = 8 * [0.0] kPs = 8 * [1.0] frametime = 1.0 / 30.0 print("Ready.") saveTimer = 0 errorTimer = 0 averageError = 0.0 while True: try: #s = time.time() pad_y = 1.0 controller.step(pad_y, frametime) # Map IK leg results to motors for i in range(4): angles[legMap[i] [0]] = legDirs[i][0] * (iks[i].angle - (np.pi / 2.0 - iks[i].A)) angles[legMap[i] [1]] = legDirs[i][1] * (iks[i].angle + (np.pi / 2.0 - iks[i].A)) #motors.sendCommands(angles, kPs) priopSDR = 8 * [0] for i in range(8): priopSDR[i] = np.random.randint(0, sensorRes) # Train with noise imuSDR = 6 * [0] # Random IMU training for i in range(6): imuSDR[i] = np.random.randint(0, sensorRes) # Train with commands from manual controller motorSDR = 16 * [0] for i in range(16): if i >= 8: # Kp motorSDR[i] = int(kPs[i - 8] * (motorRes - 1) + 0.5) # Train with maximum default else: # Angle motorSDR[i] = int( min(1.0, max(0.0, angles[i] / maxAngleRange * 0.5 + 0.5)) * (motorRes - 1) + 0.5) error = 0.0 for i in range(len(motorSDR)): delta = (motorSDR[i] - h.getPredictionCIs(2)[i]) / float(motorRes - 1) error += delta * delta averageError = 0.99 * averageError + 0.01 * error h.step([priopSDR, imuSDR, motorSDR], True, 0.0, True) # Show error rate occasionally if errorTimer >= 1000: errorTimer = 0 print("Error: " + str(averageError)) errorTimer += 1 # Save occasionally if saveTimer >= 10000: saveTimer = 0 print("Saving...") h.saveToFile("lorcan_mini_pretrained.ohr") print("Saved.") saveTimer += 1 #time.sleep(max(0, frametime - (time.time() - s))) except Exception as e: print(e) break print("-- Program at End --")
# Reverse transform of IEEEToCSDR def CSDRToUnorm8(csdr): return (csdr[0] | (csdr[1] << 4)) / 255.0 # This defines the resolution of the input encoding - we are using a simple single column that represents a bounded scalar through a one-hot encoding. This value is the number of "bins" numInputColumns = 2 inputColumnSize = 16 # Define layer descriptors: Parameters of each layer upon creation lds = [] for i in range(8): # Layers with exponential memory ld = pyaon.LayerDesc() ld.hiddenSize = (4, 4, 16) # Size of the encoder (SparseCoder) lds.append(ld) # Create the hierarchy h = pyaon.Hierarchy() h.initRandom([pyaon.IODesc(size=(1, 2, 16), type=pyaon.prediction)], lds) # Present the wave sequence for some timesteps iters = 3000 def wave(t): return (np.sin(t * 0.05 * 2.0 * np.pi + 0.5) *
def __init__(self, env, layerSizes=2 * [(4, 4, 16)], layerRadius=2, hiddenSize=(8, 8, 16), imageRadius=8, imageScale=1.0, obsResolution=32, actionResolution=16, rewardScale=1.0, terminalReward=0.0, infSensitivity=1.0, nThreads=8): self.env = env pyaon.setNumThreads(nThreads) self.imEnc = None self.imEncIndex = -1 self.inputSizes = [] self.inputLows = [] self.inputHighs = [] self.inputTypes = [] self.imageSizes = [] self.imgsPrev = [] self.actionIndices = [] self.rewardScale = rewardScale self.terminalReward = terminalReward self.infSensitivity = infSensitivity if type(self.env.observation_space) is gym.spaces.Discrete: self.inputSizes.append((1, 1, self.env.observation_space.n)) self.inputTypes.append(pyaon.prediction) self.inputLows.append([0.0]) self.inputHighs.append([0.0]) elif type(self.env.observation_space) is gym.spaces.Box: if len(self.env.observation_space.shape) == 1 or len( self.env.observation_space.shape) == 0: squareSize = int( np.ceil(np.sqrt(len(self.env.observation_space.low)))) self.inputSizes.append((squareSize, squareSize, obsResolution)) self.inputTypes.append(pyaon.prediction) lows = list(self.env.observation_space.low) highs = list(self.env.observation_space.high) # Detect large numbers/inf for i in range(len(lows)): if abs(lows[i]) > 100000 or abs(highs[i]) > 100000: # Indicate inf by making low greater than high lows[i] = 1.0 highs[i] = -1.0 self.inputLows.append(lows) self.inputHighs.append(highs) elif len(self.env.observation_space.shape) == 2: scaledSize = (int(self.env.observation_space.shape[0] * imageScale), int(self.env.observation_space.shape[1] * imageScale), 1) self.imageSizes.append(scaledSize) elif len(self.env.observation_space.shape) == 3: scaledSize = (int(self.env.observation_space.shape[0] * imageScale), int(self.env.observation_space.shape[1] * imageScale), 3) self.imageSizes.append(scaledSize) else: raise Exception("Unsupported Box input: Dimensions too high " + str(self.env.observation_space.shape)) else: raise Exception("Unsupported input type " + str(type(self.env.observation_space))) if len(self.imageSizes) > 0: vlds = [] for i in range(len(self.imageSizes)): vld = pyaon.ImageEncoderVisibleLayerDesc( (self.imageSizes[i][0], self.imageSizes[i][1], self.imageSizes[i][2]), imageRadius) vlds.append(vld) self.imgsPrev.append(np.zeros(self.imageSizes[i])) self.imEnc = pyaon.ImageEncoder() self.imEnc.initRandom(hiddenSize, vlds) self.imEncIndex = len(self.inputSizes) self.inputSizes.append(hiddenSize) self.inputTypes.append(pyaon.prediction) self.inputLows.append([0.0]) self.inputHighs.append([1.0]) # Actions if type(self.env.action_space) is gym.spaces.Discrete: self.actionIndices.append(len(self.inputSizes)) self.inputSizes.append((1, 1, self.env.action_space.n)) self.inputTypes.append(pyaon.action) self.inputLows.append([0.0]) self.inputHighs.append([0.0]) elif type(self.env.action_space) is gym.spaces.Box: if len(self.env.action_space.shape) < 3: if len(self.env.action_space.shape) == 2: self.actionIndices.append(len(self.inputSizes)) self.inputSizes.append( (self.env.action_space.shape[0], self.env.action_space.shape[1], actionResolution)) self.inputTypes.append(pyaon.action) lows = list(self.env.action_space.low) highs = list(self.env.action_space.high) self.inputLows.append(lows) self.inputHighs.append(highs) else: squareSize = int( np.ceil(np.sqrt(len(self.env.action_space.low)))) squareTotal = squareSize * squareSize self.actionIndices.append(len(self.inputSizes)) self.inputSizes.append( (squareSize, squareSize, actionResolution)) self.inputTypes.append(pyaon.action) lows = list(self.env.action_space.low) highs = list(self.env.action_space.high) self.inputLows.append(lows) self.inputHighs.append(highs) else: raise Exception( "Unsupported Box action: Dimensions too high " + str(self.env.action_space.shape)) else: raise Exception("Unsupported action type " + str(type(self.env.action_space))) lds = [] for i in range(len(layerSizes)): ld = pyaon.LayerDesc(hiddenSize=layerSizes[i]) ld.eRadius = layerRadius ld.dRadius = layerRadius lds.append(ld) self.h = pyaon.Hierarchy() ioDescs = [] for i in range(len(self.inputSizes)): ioDescs.append( pyaon.IODesc(self.inputSizes[i], self.inputTypes[i], layerRadius, layerRadius, 64)) self.h.initRandom(ioDescs, lds) self.actions = [] for i in range(len(self.actionIndices)): index = self.actionIndices[i] size = len(self.inputLows[index]) startAct = [] for j in range(size): startAct.append(np.random.randint(0, self.inputSizes[index][2])) self.actions.append(startAct)