def evaluate(individual, num_classes, num_epochs, batch_size, learning_rate): train_transform, test_transform = utils._data_transforms_cifar10() train_dataset = torchvision.datasets.CIFAR10(root='../../data', train=True, transform=train_transform) test_dataset = torchvision.datasets.CIFAR10(root='../../data', train=False, transform=test_transform) train_loader = torch.utils.data.DataLoader( dataset=train_dataset, batch_size=batch_size, shuffle=True, # pin_memory=True, # num_workers=2 ) test_loader = torch.utils.data.DataLoader( dataset=test_dataset, batch_size=batch_size, shuffle=False, # pin_memory=True, # num_workers=2 ) structure = Network(individual.structure, [(3, 32), (32, 128), (128, 128)], num_classes, (32, 32)).to(device) individual.size = utils.count_parameters_in_MB(structure) parameters = filter(lambda p: p.requires_grad, structure.parameters()) cudnn.enabled = True cudnn.benchmark = True criterion = torch.nn.CrossEntropyLoss().to(device) optimizer = torch.optim.SGD(parameters, lr=learning_rate, momentum=0.9, weight_decay=3e-4) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, num_epochs, eta_min=0.0) best_acc = 0 for epoch in range(num_epochs): print('epoch[{}/{}]:'.format(epoch + 1, num_epochs)) train(train_loader, structure, criterion, optimizer) scheduler.step() valid_acc = test(test_loader, structure, criterion) print() if valid_acc > best_acc: best_acc = valid_acc individual.accuracy = best_acc
def __init__(self, action_number, state_size, seed=0, gamma=0.99): self.action_number = action_number self.state_size = state_size self.targetNetwork = Network(self.state_size, self.action_number, seed).to(device) self.localNetwork = Network(self.state_size, self.action_number, seed).to(device) self.memoryBuffer = PrioritizedMemory(MAX_BUFFER_SIZE, BATCH_SIZE) self.current_step = 0 self.gamma = gamma self.optimizer = optim.Adam(self.localNetwork.parameters(), lr=0.001)
def init(num_individuals=10, num_phases=3, num_nodes=4, num_classes=10): individuals = [] for i in range(num_individuals): individual = Individual(structure=encode(num_phases, num_nodes)) individual.model = Network(individual.structure, [(3, 32), (32, 128), (128, 128)], num_classes, (32, 32)) individuals.append(individual) return individuals
def init(num_individuals=10, num_phases=3, num_nodes=4): individuals = [] for i in range(num_individuals): individual = Individual(structure=encode(num_phases, num_nodes)) individual.model = Network(individual.structure, [(3, 64), (64, 128), (128, 256), (256, 512)]) individuals.append(individual) return individuals
def runSimulation(self, sim_type=0): types = ["kleinberg", "yule"] testNetwork = Network(self.testDim, network_type=types[sim_type]) testWorld = testNetwork.world nodeIdTuple = tuple([key for key in testNetwork.world.keys() if testNetwork.world[key].has_user]) lengths = [] all_traces = [] num_failed = 0 if len(nodeIdTuple) > 2: for j in range(self.num_messages): testNodes = random.sample(nodeIdTuple, 2) testNode = testNodes[0] testNode1 = testNodes[1] distance = -1 i = 0 trace = [[i, testWorld[testNode].position, testNetwork.getDistance(testWorld[testNode].position, testWorld[testNode1].position), testNode]] while distance != 0: i += 1 testNeighborsHash = list(testWorld[testNode].out_links) distances = [] for neighbor in testNeighborsHash: distance = testNetwork.getDistance(testWorld[testNode1].position, testWorld[neighbor].position) distances.append(distance) minNeighborIndex = distances.index(min(distances)) minNeighborHash = testNeighborsHash[minNeighborIndex] trace.append([i, testWorld[minNeighborHash].position, min(distances), minNeighborHash]) if min(distances) == 0: lengths.append(i) # print(str(i) + ' Done!') break elif i == self.max_attempts: num_failed += 1 break else: testNode = minNeighborHash distance = min(distances) all_traces.append(trace) print(1.0 * sum(lengths) / len(lengths), Utils.median(lengths), 1 - 1.0 * num_failed / self.num_messages) return all_traces
def __init__(self, model_ckpt_path): network = Network() self.model_path = model_ckpt_path def inference(self, batch): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, self.model_path) result, test_accuracy, mse = sess.run([network.final_result], feed_dict={network.inputs: batch, network.is_training: False})
def mine_model(individual): # the 30th layer of features is relu of conv5_3 model = vgg16(pretrained=False) # use search model to be the extractor features = Network(individual.structure, [(3, 64), (64, 128), (128, 256), (256, 512)]) if individual.model_dict: features.load_state_dict(individual.model_dict) classifier = model.classifier classifier = list(classifier) del classifier[6] if not opt.use_drop: del classifier[5] del classifier[2] # freeze top4 conv # for layer in features[:10]: # for p in layer.parameters(): # p.requires_grad = False return features, nn.Sequential(*classifier)
def produce(individuals, num_classes): index_individual = np.random.randint(0, len(individuals)) structure = individuals[index_individual].structure # 随机个体当父代 # 首先根据随机许选择得到算子类别 operator_id = np.random.randint(0, 3) id1 = np.random.randint(0, len(structure)) # 随机选择一个phase id2 = np.random.randint(0, len(structure[id1])) # 选择操作层的下标 if operator_id == 0: # 添加卷积层 structure[id1].insert(id2, np.random.choice([0, 1], id2 + 1).tolist()) # 在对应下标处添加层 # 更新后面的层 for i in range(id2 + 1, len(structure[id1])): structure[id1][i].insert(id2 + 1, 0) pass elif operator_id == 1: # 删除卷积层 _remove = structure[id1][id2] structure[id1].remove(_remove) # 更新 for i in range(id2, len(structure[id1])): _remove = structure[id1][i][id2 + 1] structure[id1][i].remove(_remove) else: # 替换卷积层 _change_length = len(structure[id1][id2]) _change = np.random.choice([0, 1], _change_length).tolist() structure[id1][id2] = _change individual = Individual(structure=structure) individual.model = Network(individual.structure, [(3, 32), (32, 128), (128, 128)], num_classes, (32, 32)) # 继承父代特征,加快训练 pre_dict = individuals[index_individual].model_dict model_dict = individual.model.state_dict() pre_dict = {k: v for k, v in pre_dict.items() if k in model_dict} model_dict.update(pre_dict) individual.model_dict = pre_dict individuals.append(individual)
i3 = neuron.add_current(-1.5, -1.5, ts) # slow negative conductance i4 = neuron.add_current(1.5, -1.5, tus) # ultraslow positive conductance neurons.append(neuron) # Define the connectivity matrices g_inh = [[0, .2], [0, 0]] # inhibitory connection neuron 1 -| neuron 2 g_exc = [[0, 0], [0, 0]] # excitatory connection neuron 1 <- neuron 2 g_res = [[0, 0], [0, 0]] # resistive connections voff = -1 inh_synapse = CurrentSynapse(-1, voff, ts) exc_synapse = CurrentSynapse(+1, voff, ts) resistor = ResistorInterconnection() # Define the network network = Network(neurons, (inh_synapse, g_inh), (exc_synapse, g_exc), (resistor, g_res)) # Simulate the network trange = (0, 20000) # Define i_app as a function of t: returns an i_app for each neuron i_app = lambda t: [-2.1, -2] sol = network.simulate(trange, i_app) # Plot simulation # y[0] = neuron 1 membrane voltage, y[3] = neuron 2 membrane voltage plt.figure() plt.plot(sol.t, sol.y[0], sol.t, sol.y[3])
class Agent(): def __init__(self, action_number, state_size, seed=0, gamma=0.99): self.action_number = action_number self.state_size = state_size self.targetNetwork = Network(self.state_size, self.action_number, seed).to(device) self.localNetwork = Network(self.state_size, self.action_number, seed).to(device) self.memoryBuffer = PrioritizedMemory(MAX_BUFFER_SIZE, BATCH_SIZE) self.current_step = 0 self.gamma = gamma self.optimizer = optim.Adam(self.localNetwork.parameters(), lr=0.001) def choose_action(self, state, eps): state = torch.from_numpy(state).float().unsqueeze(0).to(device) self.localNetwork.eval() with torch.no_grad(): action_values = self.localNetwork(state) self.localNetwork.train() # Epsilon-greedy action selection if random.random() > eps: return np.argmax(action_values.cpu().data.numpy()) else: return random.choice(np.arange(self.action_number)) def step(self, state, action, reward, next_state, done): self.memoryBuffer.add(state, action, reward, next_state, done) self.current_step += 1 if self.current_step % ACTUALIZATION_INTERVAL == 0 and len( self.memoryBuffer) >= BATCH_SIZE: buffer_data = self.memoryBuffer.get_batch() self.learn(buffer_data) def learn(self, buffer_data): """ learning using: Experience Replay Double DQLearning dueling DQLearning delayed update """ output_indexes, IS_weights, states, actions, rewards, next_states, dones = buffer_data # double Q learning best_predicted_action_number = self.localNetwork( next_states).detach().max(1)[1].unsqueeze(1) predicted_action_value = self.targetNetwork( next_states).detach().gather( 1, best_predicted_action_number.view(-1, 1)) # y_j calculation output_action_value = rewards + predicted_action_value * self.gamma * ( 1 - dones) # expected values predicted_expected_action_value = self.localNetwork(states).gather( 1, actions) # (y_j - expected value)**2 #priority replay added last part *IS_WEIGHTS losses = F.mse_loss(predicted_expected_action_value, output_action_value, reduce=False) * IS_weights abs_error = losses + MIN_UPDATE self.memoryBuffer.update_batch(output_indexes, abs_error) self.optimizer.zero_grad() loss = losses.mean() loss.backward() self.optimizer.step() # updating target network for target_param, local_param in zip(self.targetNetwork.parameters(), self.localNetwork.parameters()): target_param.data.copy_(TAU * local_param.data + (1.0 - TAU) * target_param.data)
def get_ann(): return Network(n_feature=2, n_output=1)# n_feature = x and y | output f(x,y)
def train(self): BATCH_SIZE = 256 network = Network() dataset = Dataset(folder='data{}_{}'.format(network.IMAGE_HEIGHT, network.IMAGE_WIDTH), batch_size=BATCH_SIZE) inputs, targets = dataset.next_batch() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) network.loadRecog(sess) saver = tf.train.Saver(var_list=network.variables) n_epochs = 50 print("Starting Pretrain") for epoch_i in range(n_epochs): dataset.reset_batch_pointer() for batch_i in range(dataset.num_batches_in_epoch()): batch_num = epoch_i * dataset.num_batches_in_epoch() + batch_i + 1 start = time.time() batch_inputs, batch_targets = dataset.next_batch(pretrain=True) batch_inputs = np.reshape(batch_inputs,(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) batch_targets = np.reshape(batch_targets,(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) batch_inputs = np.multiply(batch_inputs, 1.0 / 255) batch_targets = np.multiply(batch_targets, 1.0 / 255) cost1, rec,_ = sess.run([network.cost_mse, network.cost_rec, network.train_op_mse],feed_dict={network.inputs: batch_inputs, network.targets: batch_targets, network.is_training: True, network.nepoch: batch_num}) end = time.time() print('{}/{}, epoch: {}, mse/rec: {}/{}, batch time: {}'.format(batch_num, n_epochs * dataset.num_batches_in_epoch(), epoch_i, cost1, rec, round(end - start,5))) test_accuracies = [] test_mse = [] n_epochs = 3000 global_start = time.time() print("Starting Train") for epoch_i in range(n_epochs): dataset.reset_batch_pointer() for batch_i in range(dataset.num_batches_in_epoch()): batch_num = epoch_i * dataset.num_batches_in_epoch() + batch_i + 1 start = time.time() batch_inputs, batch_targets = dataset.next_batch() batch_inputs = np.reshape(batch_inputs,(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) batch_targets = np.reshape(batch_targets,(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) batch_inputs = np.multiply(batch_inputs, 1.0 / 255) batch_targets = np.multiply(batch_targets, 1.0 / 255) if batch_num % 5 == 0: cost1, rec, _ = sess.run([network.cost_mse, network.cost_rec, network.train_op_rec],feed_dict={network.inputs: batch_inputs, network.targets: batch_targets, network.is_training: True, network.nepoch: batch_num}) else: cost1, rec, _ = sess.run([network.cost_mse, network.cost_rec, network.train_op_mse],feed_dict={network.inputs: batch_inputs, network.targets: batch_targets, network.is_training: True, network.nepoch: batch_num}) end = time.time() print('{}/{}, epoch: {}, mse/recog: {}/{}, batch time: {}'.format(batch_num, n_epochs * dataset.num_batches_in_epoch(), epoch_i, cost1, rec, round(end - start,5))) if batch_num % 2000 == 0 or batch_num == n_epochs * dataset.num_batches_in_epoch(): test_inputs, test_targets = dataset.valid_set test_inputs, test_targets = test_inputs[:100], test_targets[:100] test_inputs = np.reshape(test_inputs, (-1, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) test_targets = np.reshape(test_targets, (-1, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1)) test_inputs = np.multiply(test_inputs, 1.0 / 255) test_targets = np.multiply(test_targets, 1.0 / 255) test_accuracy, mse = sess.run([network.accuracy, network.mse], feed_dict={network.inputs: test_inputs,network.targets: test_targets,network.is_training: False}) print('Step {}, test accuracy: {}'.format(batch_num, test_accuracy)) min_mse = (0,0) test_accuracies.append((test_accuracy, batch_num)) test_mse.append((mse, batch_num)) print("Accuracies in time: ", [test_accuracies[x][0] for x in range(len(test_accuracies))]) print("MSE in time: ", [test_mse[x][0] for x in range(len(test_mse))]) max_acc = max(test_accuracies) min_mse = min(test_mse) print("Best accuracy: {} in batch {}".format(max_acc[0], max_acc[1])) print("Total time: {}".format(time.time() - global_start)) if (batch_num > 50000): print("saving model...") saver.save(sess,os.path.join("save","checkpoint.data"))