圆嘟嘟 发表于 2021-10-7 11:08:50

ValueError: Type mismatch (<type 'int'> vs. <type 'str'>)

def _merge_a_into_b(a, b):
    """Merge config dictionary a into config dictionary b, clobbering the
    options in b whenever they are also specified in a.
    """
    if type(a) is not edict:
      return

    for k, v in a.iteritems():
      # a must specify keys that are in b
      if not b.has_key(k):
            raise KeyError('{} is not a valid config key'.format(k))

      # the types must match, too
      old_type = type(b)
      if old_type is not type(v):
            if isinstance(b, np.ndarray):
                v = np.array(v, dtype=b.dtype)
            else:
                raise ValueError(('Type mismatch ({} vs. {}) '
                                  'for config key: {}').format(type(b),
                                                               type(v), k))

      # recursively merge dicts
      if type(v) is edict:
            try:
                _merge_a_into_b(a, b)
            except:
                print(('Error under config key: {}'.format(k)))
                raise
      else:
            b = v


def cfg_from_file(filename):
    """Load a config file and merge it into the default options."""
    import yaml
    with open(filename, 'r') as f:
      yaml_cfg = edict(yaml.load(f, Loader=yaml.FullLoader))

    _merge_a_into_b(yaml_cfg, __C)

suchocolate 发表于 2021-10-7 11:25:54

这代码是你写的不,程序自己raise的,去检查 if isinstance(b, np.ndarray): 的b是否符合数据类型要求。

圆嘟嘟 发表于 2021-10-7 16:09:14

这个代码是在网上下载的 这是他这部分所有的代码
__C = edict()
cfg = __C

# Dataset name: flowers, birds
__C.DATASET_NAME = 'coco' #'birds'
__C.CONFIG_NAME = ''
__C.DATA_DIR = ''
__C.GPU_ID = 0
__C.CUDA = True
__C.WORKERS = 6

__C.RNN_TYPE = 'LSTM'   # 'GRU'
__C.B_VALIDATION = False

__C.TREE = edict()
__C.TREE.BRANCH_NUM = 3
__C.TREE.BASE_SIZE = 64


# Training options
__C.TRAIN = edict()
__C.TRAIN.BATCH_SIZE = 64
__C.TRAIN.MAX_EPOCH = 600
__C.TRAIN.SNAPSHOT_INTERVAL = 2000
__C.TRAIN.DISCRIMINATOR_LR = 2e-4
__C.TRAIN.MEMT_LR = 2e-4
__C.TRAIN.GENERATOR_LR = 2e-4
__C.TRAIN.ENCODER_LR = 2e-4
__C.TRAIN.RNN_GRAD_CLIP = 0.25
__C.TRAIN.FLAG = True
__C.TRAIN.NET_E = ''
__C.TRAIN.NET_T = ''
__C.TRAIN.NET_I = ''
__C.TRAIN.NET_G = ''
__C.TRAIN.B_NET_D = True

__C.TRAIN.SMOOTH = edict()
__C.TRAIN.SMOOTH.GAMMA1 = 5.0
__C.TRAIN.SMOOTH.GAMMA3 = 10.0
__C.TRAIN.SMOOTH.GAMMA2 = 5.0
__C.TRAIN.SMOOTH.LAMBDA = 1.0


# Modal options
__C.GAN = edict()
__C.GAN.DF_DIM = 64
__C.GAN.GF_DIM = 128
__C.GAN.Z_DIM = 100
__C.GAN.CONDITION_DIM = 100
__C.GAN.R_NUM = 2
__C.GAN.B_ATTENTION = True
__C.GAN.B_DCGAN = False


__C.TEXT = edict()
__C.TEXT.CAPTIONS_PER_IMAGE = 10
__C.TEXT.EMBEDDING_DIM = 256
__C.TEXT.WORDS_NUM = 18


def _merge_a_into_b(a, b):
    """Merge config dictionary a into config dictionary b, clobbering the
    options in b whenever they are also specified in a.
    """
    if type(a) is not edict:
      return

    for k, v in a.iteritems():
      # a must specify keys that are in b
      if not b.has_key(k):
            raise KeyError('{} is not a valid config key'.format(k))

      # the types must match, too
      old_type = type(b)
      if old_type is not type(v):
            if isinstance(b, np.ndarray):
                v = np.array(v, dtype=b.dtype)
            else:
                raise ValueError(('Type mismatch ({} vs. {}) '
                                  'for config key: {}').format(type(b),
                                                               type(v), k))

      # recursively merge dicts
      if type(v) is edict:
            try:
                _merge_a_into_b(a, b)
            except:
                print(('Error under config key: {}'.format(k)))
                raise
      else:
            b = v


def cfg_from_file(filename):
    """Load a config file and merge it into the default options."""
    import yaml
    with open(filename, 'r') as f:
      # yaml_cfg = edict(yaml.load(f))
      yaml_cfg = edict(yaml.load(f, Loader=yaml.FullLoader))

    _merge_a_into_b(yaml_cfg, __C)
数据 都是字典类型 b提取了value值,这个的意思是value值不是一个n维数组对象?可是它本来也不是啊{:10_266:}
页: [1]
查看完整版本: ValueError: Type mismatch (<type 'int'> vs. <type 'str'>)