学习链接:
python基础教程
官方文档
官方文档2
1、TypeError: a bytes-like object is required, not ‘str’
python3.5和Python2.7在套接字返回值解码上有区别:
python bytes和str两种类型可以通过函数encode()和decode()相互转换,
str→bytes:encode()方法。str通过encode()方法可以转换为bytes。
bytes→str:decode()方法。如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。
解决方法,比如
将line.strip().split(“,”) 改为 line.decode().strip().split(“,”)
server_reply = binascii.hexlify(s.recv(1024)).decode()
print(server_reply)
2、RuntimeError: Expected all tensors to be on the same device, but found at least two devices
参与运算的两个或多个变量,有的在CPU上,有的在GPU上,将数据对齐即可
比如 data2 = data2.to(data1) ,即直接将data2搬到了data1的平台上了
或者如果有自定义好的device是cpu或者gpu则可以
data2 = data2.to(device)
3、pytorch中计算Tensor中"True"值的数量
参考tensorflow的tf.count_nonzero函数,pytorch中也含有该函数
torch.count_nonzero(目标tensor).tolist()
4、Incompatible library version: _imaging.cpython-39-darwin.so requires version 14.0.0 or later, but libjpeg.9.dylib provides version 12.0.0
Pillow版本8.4.0太高,pip install Pillow8.3.2 imageio2.12.0