Python3 日常报错(持续更新)
TypeError: ‘odict_items’ object does not support indexing 或TypeError: ‘dict_items’
res = model.features._modules.items()[layer_index]
修改为 res = list(model.features._modules.items())[layer_index]
RuntimeError: bool value of Tensor with more than one value is ambiguous
这段是bias复制出问题,pytorch的conv.py要求bias是bool类型
any([True False False])
表示只要有一个True 就返回 True,
all([True False False])
表示所有元素为True才会返回True, 否则返回False.
TypeError: slice indices must be integers or None or have an index method
这里是python进行矩阵操作的时候出现错误,我做了这样一个操作,矩阵位置索引出了问题
new_weights[:, : filter_index * params_per_input_channel] = old_weights[:, : filter_index * params_per_input_channel]
其中params_per_input_channel是float,filter_index是int,结果是float,增加类型转换后正常了
PS:还有一种情况是内部存在除法,修改方法是将 / 转换为 // 。
TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
在CUDA tensor格式的数据转换为numpy时,先将其转换成cpu float-tensor,比如:
ele_cumpy = ele_tensor.data.cpu().numpy()
TypeError: new(): argument ‘size’ must be tuple of ints, but found element of type float at pos 2
数据类型的问题,根据需要强制转换就OK了