[英]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.