def test_Writer(self): calculus = 30 err = False w = Writer(calculus, err) output = w.printResult() expected_output = True self.assertEqual(output, expected_output)
def __init__(self): self.textworld = TextWorld(cfg) self.replay = Replay(cfg['cap'], cfg['batch_size']) self.writer = Writer(cfg['extension'], cfg['save_dir'], cfg['log_dir'], cfg['log_freq']) self.model = Seq2seq(cfg['vocab_size'], cfg['embedding_size'], cfg['units'], cfg['dropout']) self.optim = tf.keras.optimizers.Adam(cfg['learning_rate'])
def __init__(self): self.ibf = ItemBoxFactory() self.initialTotalDeadVolume = 0 self.initialTotalBoxVolume = 0 self.initialTotalItemVolume = 0 self.endTotalVolume = 0 self.writer = Writer() self.itemBoxes = [] self.tree = None self.bestNodes = [] self.newItemBoxes = None self.newTotalDeadVolume = 0 self.newTotalVolume = 0 self.gain = 0
def main(argv=None): MODEL_FILE = 'mnist_classifier.py' copyfile(os.path.join(dname, 'classes', MODEL_FILE), os.path.join(paths.RESULTS_DIR, MODEL_FILE)) writer = Writer(paths.RESULTS_DIR) # Writer summary folder trainer = Trainer(TRAIN_INIT, writer) trainer.train(TRAIN_PARAMS)
def main(argv=None): writer = Writer(paths.RESULTS_DIR) trainer = Trainer(TRAIN_INIT, writer) tester = Tester(TEST_INIT, writer) if os.path.isfile(paths.PARAMS_FILE): with open(paths.PARAMS_FILE, 'r') as handle: params = json.load(handle) else: params = TRAIN_PARAMS params['min_test_step'], params['min_test_loss'] = tester.test( TEST_PARAMS) params['init_step'] = params['min_test_step'] params['unchanged'] = 0 params['num_decays'] = 0 while params['num_decays'] <= MAX_DECAYS: prev_step = params['init_step'] params['init_step'], _ = trainer.train(params) _, test_loss = tester.test(TEST_PARAMS) if test_loss < params['min_test_loss']: remove_model(params['min_test_step']) params['min_test_step'] = params['init_step'] params['min_test_loss'] = test_loss params['unchanged'] = 0 else: params['unchanged'] += 1 if params['unchanged'] >= PATIENCE: params['learning_rate'] *= DECAY_FACTOR params['num_decays'] += 1 params['init_step'] = params['min_test_step'] params['unchanged'] = 0 if prev_step != params['min_test_step']: remove_model(prev_step) with open(paths.PARAMS_FILE, 'w') as handle: json.dump(params, handle, indent=2) print(params)
class CustomAgent: def __init__(self): self.textworld = TextWorld(cfg) self.replay = Replay(cfg['cap'], cfg['batch_size']) self.writer = Writer(cfg['extension'], cfg['save_dir'], cfg['log_dir'], cfg['log_freq']) self.model = Seq2seq(cfg['vocab_size'], cfg['embedding_size'], cfg['units'], cfg['dropout']) self.optim = tf.keras.optimizers.Adam(cfg['learning_rate']) #@tf.function() def update(self): # Problem: Experiences with different max #entities are being sampled, creating unusable matrix entities, teachers, scores = self.replay.fetch() # Max length of each entity (+2 is for start and end tokens) cmd_len = max([len(batch) for batch in teachers]) # Pad teachers for batch in teachers: for _ in range(cmd_len - len(batch)): batch.append(self.textworld.pad) x_in = self.textworld.get_preprocessed_state2(entities) teachers = np.array(teachers) one_hot_targets = tf.one_hot(teachers, depth=cfg['vocab_size']) # Compute gradients with tf.GradientTape() as tape: logits, predictions = self.model(x_in, cmd_len, teachers, cfg['batch_size']) #print(self.textworld.vec_to_words(predictions)) #print() # Compare target and predicted loss = tf.compat.v1.losses.softmax_cross_entropy(one_hot_targets, logits, reduction='none') #loss = tf.losses.huber_loss(one_hot_targets, logits) # Apply mask to remove pad gradients mask = tf.cast(tf.math.logical_not(tf.math.equal(teachers, 0)), dtype=tf.dtypes.float32) loss = loss * mask loss = tf.reduce_mean(loss + scores) # Log self.writer.log(self.optim, tape, loss) self.writer.global_step.assign_add(1) # Calculate and apply gradients grads = tape.gradient(loss, self.model.weights) grads_and_vars = zip(grads, self.model.weights) self.optim.apply_gradients(grads_and_vars) #@tf.function def train(self): for epoch in trange(1, cfg['epochs']): # Init scraped entities list entities = [] for b in range(cfg['num_agents']): entities.append({}) # Get initial obs, info from game obs, infos = self.textworld.reset() # Get valid entities that exist in game true_entities = infos['entities'] # Get solutions to all current batches walkthroughs = infos['extra.walkthrough'] # Max length of each entity (+2 is for start and end tokens) cmd_len = 2 + max([ max([len(cmd.split()) for cmd in batch]) for batch in walkthroughs ]) target, one_hot_target, target_words = self.textworld.get_targets( walkthroughs, cmd_len) # Get starting index for each batch of target curr_targets = [[x for x in range(cfg['num_agents'])], [0 for x in range(cfg['num_agents'])]] # Init loop vars dones = [False] * cfg['num_agents'] scores = [0] * cfg['num_agents'] memories = [] while not all(dones): # Update global entity list entities = self.textworld.update_entities( entities, true_entities, obs) # Get input data x_in = self.textworld.get_preprocessed_state2(entities) teachers = target[tuple(curr_targets)] # Run through model logits, predictions = self.model(x_in, cmd_len, teachers, cfg['num_agents']) # Ignore padded chars mask1 = tf.cast(tf.math.logical_not( tf.math.equal(one_hot_target[tuple(curr_targets)], -float("Inf"))), dtype=tf.dtypes.float32) logits = logits * mask1 mask2 = tf.cast(tf.math.logical_not( tf.math.equal(target[tuple(curr_targets)], 0)), dtype=tf.dtypes.int64) predictions = predictions * mask2 # Convert from ids to words commands = self.textworld.vec_to_words(predictions) print(commands, target_words[tuple(curr_targets)]) # Perform commands obs, scores, dones, infos = self.textworld.step(commands) # Add to replay buffer for b in range(cfg['num_agents']): teacher = [ item for item in teachers[b].tolist() if item != 0 ] #id_ = hash(" ".join(str(x) for x in teacher)) # Save entities, solution without <PAD> tokens, target for logits memories.append([entities[b], teacher]) # Increment target indexes for successful batch commands correct = tf.cast(tf.math.equal(target[tuple(curr_targets)], predictions), dtype=tf.dtypes.float32) #print() for b, batch in enumerate(correct): # if 1 for each column if int(tf.reduce_sum( input_tensor=tf.abs(batch))) == cmd_len: #print("CORRECT!") curr_targets[1][b] += 1 # Add end-game score to memories, add memories to experience replay for i in range(len(memories)): memories[i].append(scores) self.replay.push(memories[i]) if len(self.replay.memory) >= cfg['batch_size']: self.update()
class TreeControl(): def __init__(self): self.ibf = ItemBoxFactory() self.initialTotalDeadVolume = 0 self.initialTotalBoxVolume = 0 self.initialTotalItemVolume = 0 self.endTotalVolume = 0 self.writer = Writer() self.itemBoxes = [] self.tree = None self.bestNodes = [] self.newItemBoxes = None self.newTotalDeadVolume = 0 self.newTotalVolume = 0 self.gain = 0 def getInitialItemBoxes(self, path, numPoints=None): self.ibf.loadCSV(path) self.itemBoxes = self.ibf.getItemBoxes(numPoints) self.ibf.reset() def initializeTree(self, d, c, s): self.tree = kdTree(d, c, s) def getInitialValues(self): self.initialTotalItemVolume = np.sum( [b[0].vol for b in self.itemBoxes], dtype=np.int64) self.initialTotalBoxVolume = np.sum([b[1].vol for b in self.itemBoxes], dtype=np.int64) self.initialTotalDeadVolume = (self.initialTotalBoxVolume - self.initialTotalItemVolume) def getDeltaVs(self, bestN=None): deltaVs = [] for node in self.tree.leaves: deltaVs.append((node.deltaV, node)) deltaVs.sort(key=lambda tup: tup[0], reverse=True) if bestN is not None: return deltaVs[0:bestN] return deltaVs def getBestNodes(self): for mvp in self.getDeltaVs(): dV, n = mvp if len(n.points) > 0: self.bestNodes.append((n, dV)) if dV == 0: break return def pruneTree(self, num, tries=3): print('pruning...') for t in range(0, tries): print(' try No: %i' % (t)) self.tree.prune(num) if len(self.tree.leaves) == num: break print(' finished.') def optimiseBestNodes(self): for n in self.bestNodes: for c in range(0, 3): n[0].dim[c] = n[0].getMax(c) print('nodes optimised!') return # def findLargestNonEmpty(self): # largest = [None, None, None] # nonempty = [] # for n in self.tree.leaves: # if len(n.points) > 0: # nonempty.append(n) # print(len(sorted(nonempty, key=lambda n:n.deltaV))) # for d in range(0, 3): # largest[d] = sorted(nonempty, key=lambda n:n.dim[d], reverse=True)[0] # return largest def isNumPointsConst(self): allPoints = [] for node in self.tree.leaves: allPoints += node.points assert len(allPoints) == len(self.tree.root.points) # print('✔ no points lost!') print('no points lost!') return # def getNewItemBoxes(self, path): # self.ibf.loadCSV(path) # self.newItemBoxes = self.ibf.getItemBoxes() # self.ibf.reset() # def writeOutNewItemBoxes(self, path): # self.tree.leaves.sort(key=lambda node: node.id) # bestNodesCopy = [i for i in self.bestNodes] # bestNodesCopy.sort(key=lambda tup: tup[0]) # print('start writing...') # self.writer.write(path, bestNodesCopy, self.tree.leaves) def writeNewBoxesCSV(self, num, path, plot=False, plotPath=None): print('writing new: %s' % (path)) with open(path, 'w+') as openFile: for n in self.bestNodes[:num]: x = n[0].dim[0] y = n[0].dim[1] z = n[0].dim[2] line = ('KARTON %s,%s,%s,%s,\n') % (n[0].id, x, y, z) openFile.write(line) # else: # # end = self.tree.root # # line = ('KARTON %s,%s,%s,%s,\n') % (end.id,end.dim[0],end.dim[1],end.dim[2]) # # openFile.write(line) # line68 = 'KARTON 68, 1290, 600, 210,\n' # line24 = 'KARTON 24, 1185, 600, 600,\n' # openFile.write(line68) # openFile.write(line24) # x = self.bestNodes[-1][0].dim[0] # y = self.bestNodes[-1][0].dim[1] # z = self.bestNodes[-1][0].dim[2] # line = ('KARTON %s,%s,%s,%s,\n') % (n[0].id,x,y,z) # preEnd = self.bestNodes[-1][0] # line = ('KARTON %s,%s,%s,%s,\n') % (preEnd.id,preEnd.dim[0],preEnd.dim[1],preEnd.dim[2]) # openFile.write(line) if plot: if plotPath is not None: candidates = [n[1] for n in self.bestNodes[:num]] candidates.append(self.bestNodes[-1][1]) self.writer.plot(candidates, plotPath) def getNewValues(self): self.newTotalVolume = np.sum([b[0].vol for b in self.newItemBoxes], dtype=np.int64) self.newTotalDeadVolume = ( np.sum([b[1].vol for b in self.newItemBoxes], dtype=np.int64) - self.newTotalVolume) self.gain = self.newTotalDeadVolume / self.initialTotalDeadVolume def printInfo(self, extended=False, leaves=False, bestN=False): print('Number of Points:\t\t\t%i' % len(self.itemBoxes)) print('Dimension of Root:\t\t\t%s' % self.tree.root.dim) print('Initial total ItemVolume:\t%.4e' % self.initialTotalItemVolume) print('Initial total BoxVolume:\t%.4e' % self.initialTotalBoxVolume) print('Initial total DeathVolume:\t%.4e' % self.initialTotalDeadVolume) print('Number of Leaves:\t\t%s' % len(self.tree.leaves)) if extended: kdTree.TREE_INFO.sort(key=lambda tup: tup[0]) y = 0 for n in kdTree.TREE_INFO: x = int(math.log2(n[0])) if x > y: print('') print(n[0], n[1], " ", end='') y = x else: print(n[0], n[1], " ", end='') if leaves: print('Leave Dimensions:') for n in self.kdtree.leaves: print(n.id, n.dim, n.lastCut) if len(self.bestNodes) > 0: print(' Leaves with deltaV gain: %i' % (len(self.bestNodes)))