65540


エラーコード

[code]

    Traceback (most recent call last):
      File "train_omniglot.py", line 82, in <module>
        json.dump(vars(args), fp, indent=4)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 180, in dump
        fp.write(chunk)
    TypeError: a bytes-like object is required, not 'str'
    
[/code]

誰かわかる方よろしくお願いします。  
(追記)

[code]

    # -*- coding: utf-8 -*-
    #original code https://github.com/gabrielhuang/reptile-pytorch
    import os
    import argparse
    import tqdm
    import json
    import re
    import torch
    from torch import nn
    from torch.autograd import Variable
    from torch.utils.data import DataLoader
    from torchvision import transforms
    import numpy as np
    from tensorboardX import SummaryWriter
    
    from models import OmniglotModel
    from omniglot import MetaOmniglotFolder, split_omniglot, ImageCache, transform_image, transform_label
    from utils import find_latest_file
    
    #データ読み込みの無限ループ
    def make_infinite(dataloader):
        while True:
            for x in dataloader:
                yield x
    
    def Variable_(tensor, *args_, **kwargs):
        # 変数を必要に応じてCUDAに転送
        # リストまたはタプルを展開し、再帰呼び出し
        if type(tensor) in (list, tuple):
            return [Variable_(t, **args_, **kwargs) for t in tensor]
        # dictを展開して再帰呼び出し
        if isinstance(tensor, dict):
            return {key: Variable_(v, *args_, **kwargs) for key, v in tensor.items()}
        # 変数を変換
        variable = Variable(tensor, *args_, **kwargs)
        if args.cuda: #cudaを使う場合、cudaに転送
            variable = variable.cuda()
        return variable
    
    # 引数のパース
    parser = argparse.ArgumentParser('Train reptile on omniglot')
    
    parser.add_argument('logdir', help='Folder to store everything/load')
    
    # 学習用パラメータ
    parser.add_argument('--classes', default=5, type=int, help='classes in base-task (N-way)')
    parser.add_argument('--shots', default=5, type=int, help='shots per class (K-shot)')
    parser.add_argument('--train-shots', default=10, type=int, help='train shots')
    parser.add_argument('--meta-iterations', default=100000, type=int, help='number of meta iterations')
    parser.add_argument('--start-meta-iteration', default=0, type=int, help='start iteration')
    parser.add_argument('--iterations', default=5, type=int, help='number of base iterations')
    parser.add_argument('--test-iterations', default=50, type=int, help='number of base iterations')
    parser.add_argument('--batch', default=10, type=int, help='minibatch size in base task')
    parser.add_argument('--meta-lr', default=1., type=float, help='meta learning rate')
    parser.add_argument('--lr', default=1e-3, type=float, help='base learning rate')
    
    # 一般的な設定
    parser.add_argument('--validation', default=0.1, type=float, help='Percentage of validation')
    parser.add_argument('--validate-every', default=100, type=int, help='Meta-evaluation every ... base-tasks')
    parser.add_argument('--input', default='omniglot', help='Path to omniglot dataset')
    parser.add_argument('--cuda', default=1, type=int, help='Use cuda')
    parser.add_argument('--check-every', default=1000, help='Checkpoint every')
    parser.add_argument('--checkpoint', default='', help='Path to checkpoint. This works only if starting fresh (i.e., no checkpoints in logdir)')
    
    # 前処理
    args = parser.parse_args()
    print(args)
    args_filename = os.path.join(args.logdir, 'args.json')
    run_dir = args.logdir
    check_dir = os.path.join(run_dir, 'checkpoint')
    
    #実行環境の作成
    if not os.path.exists(run_dir):
        os.makedirs(run_dir)
    if not os.path.exists(check_dir):
        os.makedirs(check_dir)
    # Write args to args.json
    with open(args_filename, 'wb') as fp:
        json.dump(vars(args), fp, indent=4)
    
    # テンソルボード用ログ出力オブジェクト
    logger = SummaryWriter(run_dir)
    
    # 学習用データ読み込み リサイズを行い、キャッシュする
    omniglot = MetaOmniglotFolder(args.input, size=(28, 28), cache=ImageCache(),
                                  transform_image=transform_image,
                                  transform_label=transform_label)
    #メタ学習用データを分離
    mata_train, meta_test = split_omniglot(omniglot, args.validation)
    
    print('Meta-Train characters', len(meta_train))
    print('Meta-Test characters', len(meta_test))
    
    # Loss
    cross_entropy = nn.CrossEntropyLoss()
    def get_loss(prediction, labels):
        return cross_entropy(prediction, labels)
    
    #学習の本体
    def do_learning(net, optimizer, train_iter, iterations):
    
        net.train()
        for iteration in xrange(iterations):
            # ミニバッチを取得
            data, labels = Variable_(train_iter.next())
    
            # 順伝搬
            prediction = net(data)
    
            # ロスを取得
            loss = get_loss(prediction, labels)
    
            # 逆伝搬と学習
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
    
        return loss.data[0]
    
    #評価する
    def do_evaluation(net, test_iter, iterations):
    
        losses = []
        accuracies = []
        net.eval()
        for iteration in xrange(iterations):
            # ミニバッチ取得
            data, labels = Variable_(test_iter.next())
    
            # 順伝搬
            prediction = net(data)
    
            # ロスを取得
            loss = get_loss(prediction, labels)
    
            # 平均正解率を取得
            argmax = net.predict(prediction)
            accuracy = (argmax == labels).float().mean()
    
            losses.append(loss.data[0])
            accuracies.append(accuracy.data[0])
    
        return np.mean(losses), np.mean(accuracies)
    
    # 最適化関数を取得
    def get_optimizer(net, state=None):
        # ここでは最適化関数をAdamとして取得していることに注意
        optimizer = torch.optim.Adam(net.parameters(), lr=args.lr, betas=(0, 0.999))
        if state is not None:
            optimizer.load_state_dict(state)
        return optimizer
    
    # 学習率を設定
    def set_learning_rate(optimizer, lr):
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr
    
    
    # モデル、最適化関数、状態を初期化
    meta_net = OmniglotModel(args.classes)
    if args.cuda:
        meta_net.cuda()
    
    #reptileの肝。SGDを初期化
    meta_optimizer = torch.optim.SGD(meta_net.parameters(), lr=args.meta_lr)
    info = {}
    state = None
    
    
    # 直前までのチェックポイントがあれば読み込む
    if os.path.isdir(args.checkpoint):
        latest_checkpoint = find_latest_file(check_dir)
        if latest_checkpoint:
            print('Latest checkpoint found:', latest_checkpoint)
            args.checkpoint = os.path.join(check_dir, latest_checkpoint)
        else:
            args.checkpoint = ''
    
    # 最初から学習
    if args.checkpoint == '':
        print('No checkpoint. Starting fresh')
    
    # チェックポイントファイル読み込み
    elif os.path.isfile(args.checkpoint):
        print('Attempting to load checkpoint', args.checkpoint)
        checkpoint = torch.load(args.checkpoint)
        meta_net.load_state_dict(checkpoint['meta_net'])
        meta_optimizer.load_state_dict(checkpoint['meta_optimizer'])
        state = checkpoint['optimizer']
        args.start_meta_iteration = checkpoint['meta_iteration']
        info = checkpoint['info']
    else:
        raise ArgumentError('Bad checkpoint. Delete logdir folder to start over.')
    
    # メインループ
    for meta_iteration in tqdm.trange(args.start_meta_iteration, args.meta_iterations):
    
        # メタ学習率の更新
        meta_lr = args.meta_lr * (1. - meta_iteration/float(args.meta_iterations))
        set_learning_rate(meta_optimizer, meta_lr)
    
        # モデルを複製
        net = meta_net.clone()
        optimizer = get_optimizer(net, state)
        # 最適化関数を取得
    
        # メタ学習を行う(1-shot~5-shot)
        train = meta_train.get_random_task(args.classes, args.train_shots or args.shots)
        train_iter = make_infinite(DataLoader(train, args.batch, shuffle=True))
    
        # 最初の学習
        loss = do_learning(net, optimizer, train_iter, args.iterations)
        state = optimizer.state_dict() # 状態を保存しておく
    
        # ネットワークの更新
        meta_net.point_grad_to(net) #差分を更新
        meta_optimizer.step() #SGDを行う
    
        # メタ評価
        if meta_iteration % args.validate_every == 0:
            print('\n\nMeta-iteration', meta_iteration)
            print('(started at {})'.format(args.start_meta_iteration))
            print('Meta LR', meta_lr)
    
            for (meta_dataset, mode) in [(meta_train, 'train'), (meta_test, 'val')]:
    
                train, test = meta_dataset.get_random_task_split(args.classes, train_K=args.shots, test_K=5)
                train_iter = make_infinite(DataLoader(train, args.batch, shuffle=True))
                test_iter = make_infinite(DataLoader(test, args.batch, shuffle=True))
    
                # 基礎学習
                net = meta_net.clone() #メタネットを複製
                optimizer = get_optimizer(net, state) # 最適化関数の状態をもとに戻す
                loss = do_learning(net, optimizer, train_iter, args.test_iterations)
    
                # 基礎試験 メタ損失、メタ正解率を評価
                meta_loss, meta_accuracy = do_evaluation(net, test_iter, 1) # only one iteration for eval
    
                # ログをとる
                loss_ = '{}_loss'.format(mode)
                accuracy_ = '{}_accuracy'.format(mode)
                meta_lr_ = 'meta_lr'
                info.setdefault(loss_, {})
                info.setdefault(accuracy_, {})
                info.setdefault(meta_lr_, {})
                info[loss_][meta_iteration] = meta_loss
                info[accuracy_][meta_iteration] = meta_accuracy
                info[meta_lr_][meta_iteration] = meta_lr
                print('\nMeta-{}'.format(mode))
                print('average metaloss', np.mean(info[loss_].values()))
                print('average accuracy', np.mean(info[accuracy_].values()))
                logger.add_scalar(loss_, meta_loss, meta_iteration)
                logger.add_scalar(accuracy_, meta_accuracy, meta_iteration)
                logger.add_scalar(meta_lr_, meta_lr, meta_iteration)
    
        if meta_iteration % args.check_every == 0 and not (args.checkpoint and
                            meta_iteration == args.start_meta_iteration):
            # チェックポイント
            checkpoint = {
                'meta_net': meta_net.state_dict(),
                'meta_optimizer': meta_optimizer.state_dict(),
                'optimizer': state,
                'meta_iteration': meta_iteration,
                'info': info
            }
            checkpoint_path = os.path.join(check_dir, 'check-{}.pth'.format(meta_iteration))
            torch.save(checkpoint, checkpoint_path)
            print('Saved checkpoint to', checkpoint_path)
    
    
[/code]


CC BY-SA 4.0
2020-04-11T07:16:44.220
65535
2020-04-11T11:55:21.063
2020-04-11T11:16:16.293
question
python
json.dumpsでTypeError: a bytes-like object is required, not 'str'が出る


以下の記事と類似の状況でしょう。  
[TypeError: a bytes-like object is required, not 'str' – Saving JSON data in
Python](https://stackoverflow.com/q/46153768/9014308)

> From the [json.dump()
> documentation:](https://docs.python.org/3/library/json.html#json.dump)
>

>> The json module always produces str objects, not bytes objects. Therefore,
fp.write() must support str input.

>
> You opened the file in binary mode. Don't do that, remove the b from the
> file mode:
>
>
> [json.dump()ドキュメント](https://docs.python.org/ja/3/library/json.html#json.dump)から：
>

>> この json モジュールは常に、 bytes オブジェクトではなく、 str オブジェクトを生成します。従って、 fp.write() は str
の入力をサポートしていなければなりません。

>
> ファイルをバイナリモードで開きました。これを行わないでください。ファイルモードからbを削除してください。

ということで、ソースの以下の部分でopenのパラメータの`'wb'`から`b`を削除して`'w'`にすれば良いでしょう。

[code]

    # Write args to args.json
    with open(args_filename, 'wb') as fp:
        json.dump(vars(args), fp, indent=4)
    
[/code]


CC BY-SA 4.0
2020-04-11T11:55:21.063
65540
2020-04-11T11:55:21.063
2020-06-17T08:14:45.997
-1
26370
65535
answer