简体   繁体   English

Pytorch: TypeError: 'str' object 不能解释为 integer

[英]Pytorch: TypeError: 'str' object cannot be interpreted as an integer

#Whatever I do, I encounter this error, what should I do? #无论我做什么,我都会遇到这个错误,我该怎么办?

class SearchCell(nn.Module):

  def __init__(self, steps, multiplier, prev_prev_C, prev_C, curr_C, reduction, prev_reduction):
    super(SearchCell, self).__init__()
    self.steps = steps
    self.multiplier = multiplier
    self.reduction = reduction
  
    if prev_reduction:
      self.prep0 = FactorizedReduce(prev_prev_C,curr_C, affine=False)
    else:
      self.prep0 = ReLUConvBN(prev_prev_C, curr_C, 1, 1, 0, affine=False)
    self.prep1 = ReLUConvBN(prev_C, curr_C, 1, 1, 0, affine=False)

    self.layers = nn.ModuleList()
 -->   for i in range(steps):
        for j in range(2+i):
            stride = 2 if reduction and j < 2 else 1
            op = MixedOp(curr_C, stride)
            self.layers.append(op)

  def forward(self, s0, s1, weights):
    s0 = self.prep0(s0)
    s1 = self.prep1(s1)
    states = [s0, s1]
    offset = 0
    for i in range(self.steps):
      s = sum([self.layers[offset + j](h, weights[offset + j]) for j, h in enumerate(states)])
      offset += len(states)
      states.append(s)

    return torch.cat(states[-self.multiplier:], dim=1)

TypeErrorTraceback (most recent call last) in init (self, steps, multiplier, prev_prev_C, prev_C, curr_C, reduction, prev_reduction) 36 37 self.layers = nn.ModuleList() 38 for i in range(steps): 39 for j in range(2+i): 40 stride = 2 if reduction and j < 2 else 1 TypeError: 'str' object cannot be interpreted as an integer初始化中的 TypeErrorTraceback (最近一次调用)(self,steps,multiplier,prev_prev_C,prev_C,curr_C,reduction,prev_reduction)36 37 self.layers = nn.ModuleList() 38 for i in range(steps): 39 for j in range(2+i): 40 stride = 2 if reduction and j < 2 else 1 TypeError: 'str' object 不能解释为 integer

according to your error steps is a string while range(steps) means that steps must be an integer cause range iterates over an integer n ( from 0 to n-1).根据您的错误,steps 是一个字符串,而 range(steps) 意味着 step 必须是 integer 导致范围迭代 integer n (从 0 到 n-1)。 Check if steps integer or not using print(type(steps)) if it is a string try using this steps = int(steps) it will type convert it to integer.检查步骤 integer 是否使用print(type(steps))如果它是一个字符串,请尝试使用此steps = int(steps)它将类型转换为 integer。 If there is an character in steps it will return another error can convert str to string .如果步骤中有一个字符,它将返回另一个错误can convert str to string If that occours trace every occourance of steps and check where steps(int) is converted to str using print(type(steps))如果发生这种情况,请跟踪每个步骤的出现并检查使用print(type(steps))将 steps(int) 转换为 str 的位置

class SearchCell(nn.Module):

  def __init__(self, steps, multiplier, prev_prev_C, prev_C, curr_C, reduction, prev_reduction):
    super(SearchCell, self).__init__()
    self.steps = int(steps)    # changed here 
    self.multiplier = multiplier
    self.reduction = reduction
  
    if prev_reduction:
      self.prep0 = FactorizedReduce(prev_prev_C,curr_C, affine=False)
    else:
      self.prep0 = ReLUConvBN(prev_prev_C, curr_C, 1, 1, 0, affine=False)
    self.prep1 = ReLUConvBN(prev_C, curr_C, 1, 1, 0, affine=False)

Or try this, hope it helps.或者试试这个,希望对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: 'tuple' object 在创建数据生成器 PyTorch 时不能解释为 integer - TypeError: 'tuple' object cannot be interpreted as an integer while creating data generators PyTorch Keras fit_generator 验证数据类型错误:“float”对象不能解释为整数 - Keras fit_generator Validation Data TypeError: 'float' object cannot be interpreted as an integer TypeError:'float'对象不能解释为索引 - TypeError: 'float' object cannot be interpreted as an index model.fit_generator: 'tuple' object 不能解释为 integer - model.fit_generator : 'tuple' object cannot be interpreted as an integer 类型错误:“str”对象不是迭代器 - TypeError: 'str' object is not an iterator Pytorch + 残余网络抛出意外:TypeError:'NoneType' object 不可调用 - Pytorch + Residual Network throws unexpected: TypeError: 'NoneType' object is not callable 类型错误:预期 str、字节或 os.PathLike object,而不是 DataFrame - TypeError: expected str, bytes or os.PathLike object, not DataFrame load_img , keras.preprocessing.image ,TypeError: an integer is required (got type str) - load_img , keras.preprocessing.image ,TypeError: an integer is required (got type str) tensorflow TypeError: cannot unpack non-iterable float object - tensorflow TypeError: cannot unpack non-iterable float object 文件“cnn_keras.py”,第 51 行,以 open("train_images", "rb","wb") as f: TypeError: an integer is required (get type str) - File "cnn_keras.py", line 51, in train with open("train_images", "rb","wb") as f: TypeError: an integer is required (got type str)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM