Beispiel #1
0
def test_k(args, k_values):
    """
    Test different k values for both weight and unit pruning.
    :param args: argparse arguments
    :param k_values: list of k values to test
    :return: list of accuracies for weight pruning, list of accuracies for unit pruning
    """
    train_unparsed_dataset, test_unparsed_dataset, train_size, test_size = load_mnist(
    )
    test_dataset = test_unparsed_dataset.map(flatten_function)
    batched_test = test_dataset.shuffle(buffer_size=test_size).batch(
        args.batch_size)

    checkpoint_prefix, checkpoint_dir = get_prefix(args)
    model = SparseNN(ptype=args.ptype)
    checkpoint = tf.train.Checkpoint(model=model)

    weights_accuracies = []
    for k in k_values:
        # we have to reload our model each time, as we rewrite our weights every time.
        # this technically shouldn't be necessary assuming all k's are sorted, but
        # it's better to be safe.
        checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        model.set_params(k / 100, "weights")
        weights_accuracies.append(evaluate_model(model, batched_test))

    nodes_accuracies = []
    for k in k_values:
        # same as above.
        checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        model.set_params(k / 100, "nodes")
        nodes_accuracies.append(evaluate_model(model, batched_test))

    return weights_accuracies, nodes_accuracies
Beispiel #2
0
 def test_queue_2(self):
     _type = consts.TYPE_FAST
     self.assertEqual(get_from_any_queue(_type), (None, None))
     for key in consts.TYPES[_type]['periods'].keys():
         _limit = consts.TYPES[_type]['periods'][key][1]
         add_to_queue('123', get_prefix(_type, _limit))
         self.assertEqual(get_from_any_queue(_type), ('123', _limit))
     self.assertEqual(get_from_any_queue(_type), (None, None))
Beispiel #3
0
def get_from_any_queue(game_type):
    for limit in [
            p[1] for p in consts.TYPES.get(game_type, {}).get('periods',
                                                              []).values()
    ]:
        token = get_from_queue(get_prefix(game_type, limit))
        if token:
            return token, limit
    return None, None
Beispiel #4
0
async def get_server(bot, ctx, name):
    server = None
    results = bot.db.servers.find({'discord_server': ctx.guild.id})
    if not results.count():
        await ctx.send("No servers added! Add one with " +
                       get_prefix(bot, ctx.guild.id) + "create")
    elif name == "" and results.count() == 1:
        server = results[0]
    else:
        result = bot.db.servers.find_one({
            'discord_server': ctx.guild.id,
            'name': {
                '$regex': re.compile('^' + name + '$', re.IGNORECASE)
            }
        })
        if result:
            server = result
        else:
            await ctx.send("Invalid server! Please choose one from " +
                           get_prefix(bot, ctx.guild.id) + "servers")
    return server
Beispiel #5
0
 async def servers(ctx):
     """
     Returns a list of saved servers
     s!servers
     """
     results = bot.db.servers.find({'discord_server': ctx.guild.id})
     if not results.count():
         return await ctx.send("No servers added! Add one with " +
                               get_prefix(bot, ctx.guild.id) + "create")
     embed = discord.Embed(title="Server list", type='rich')
     for result in results:
         embed.add_field(name=result['name'], value=result['address'])
     return await ctx.send(embed=embed)
Beispiel #6
0
    def train(self):
        """
        Training loop.
        """
        with self.summary_writer.as_default(
        ), tf.contrib.summary.always_record_summaries():
            checkpoint = tf.train.Checkpoint(optimizer=self.optimizer,
                                             model=self.model)
            for ep in range(self.args.epochs):
                # keep track of metrics we need
                loss_avg = tfe.metrics.Mean()
                accuracy = tfe.metrics.Accuracy()
                max_acc = 0

                tqdm.write("epoch %d" % ep)
                pbar = tqdm(enumerate(tfe.Iterator(self.train_dataset), 1))
                for batch, data in pbar:
                    images, labels = data

                    # take a training step
                    loss, dists = self.train_step(data)

                    # calculate metrics
                    loss_avg(loss)
                    accuracy(tf.argmax(dists, axis=1, output_type=tf.int32),
                             labels)

                    if batch % self.args.log_every == 0:
                        print_loss = loss_avg.result()
                        print_acc = accuracy.result()

                        tf.contrib.summary.scalar('loss', print_loss)
                        tf.contrib.summary.scalar('accuracy', print_acc)

                        pbar.set_description("ep: %d, batch: %d, loss: %.4f, acc: %.4f" %\
                                             (ep, batch, print_loss, print_acc))

                    if (batch % self.args.save_every
                            == 0) and accuracy.result() > max_acc:
                        # we save only the best (training) accuracy model
                        max_acc = accuracy.result()
                        prefix, _ = get_prefix(self.args)
                        checkpoint.save(file_prefix=prefix)
Beispiel #7
0
        async def help_command(ctx, command=None):
            """
            Displays information about commands
            s!help
            """
            try:
                if not command:
                    help_embed = discord.Embed(title='Commands',
                                               description='Use `'+get_prefix(bot, ctx.guild.id)+'help command` for more information!')
                    command_descriptions = ""
                    for y in bot.walk_commands():
                        if not y.cog_name and not y.hidden:
                            command_descriptions += '`{}` - {}'.format(y.name, y.help.split("\n")[0]) + '\n'
                    help_embed.add_field(name='Commands', value=command_descriptions, inline=False)
                    return await ctx.send('', embed=help_embed)

                if not bot.get_command(command):
                    return await ctx.send("Invalid command!")
                return await ctx.send('', embed=discord.Embed(title=command,
                                                              description=bot.get_command(command).help.replace("s!", get_prefix(bot, ctx.guild.id))))

            except Exception as e:
                await ctx.send("I can't send embeds! Please add me to this guild with the correct permissions")
Beispiel #8
0
 def test_get_prefix(self):
     self.assertEqual(get_prefix(123), '123-*')
     self.assertEqual(get_prefix(123, 30), '123-30')
Beispiel #9
0
 async def do_repeat_handler(ctx, error):
     if isinstance(error, commands.MissingRequiredArgument):
         if error.param.name == 'address':
             print(ctx)
             await ctx.send("Please format your command like: `"+get_prefix(bot, ctx.guild.id)+"command 144.12.123.51:27017`")