Category Archives: Python

python3 Execute except Exception, e Error [How to Solve]

During code execution, an error except timeoutexception is found. E:
the reasons are as follows:

This code is only available in python2 X is available, and python 3 no longer uses this method

Solution:
Switch the corresponding version to python2
Modify except Exception e: to except Exception as e:

[Solved] Python operate Kafka error: SyntaxError: invalid syntax

Python operation Kafka reports an error: return ‘& lt; SimpleProducer batch=%s>’ %self.async

return ‘< SimpleProducer batch=%s>’ % self. async
^^^^^
SyntaxError: invalid syntax

reason:

Because PY3 Async has become a keyword in 7. This leads to incompatibility.

Solution:

Method 1:

The latest Kafka version is used, but the Kafka on pypi has not been replaced with the latest one. You can upgrade Kafka Python using the following method
PIP install Kafka python

Method 2:

Just switch to version 3.6 and wait for subsequent upgrades

How to Solve jupyter notebook Read CSV files Error

import pandas as pd
df = pd.read_csv('Pokemon1.csv',index_col = 0)

An error is reported after executing the code: Unicode decodeerror: ‘UTF-8’ codec can’t decode byte 0x89 in position 1538: invalid start byte
an error is still reported when trying to manually specify the encoding format [encoding = “UTF-8″/encoding = “GBK”):

df = pd.read_csv('Pokemon1.csv',index_col = 0,encoding="gbk")

Solution:
1 Find the CSV file used -> Right mouse button -> Opening method -> Select Notepad
2 Open the file and select File -> “Save as”, you can see that the default code is ANSI. Select UTF-8 to save a copy again, and then open it with PD.Read_csv(). There will be no error:


RuntimeError: CUDA error: an illegal memory access was encountered

Question:

When I encountered this problem on the way to write the model, baidu either said it was the pytorch version problem or the category index exceeded, but it was useless, because the error was a very simple assignment operation.

scores[:, 0] = -float("inf") 
#RuntimeError: CUDA error: an illegal memory access was encountered

At the same time, in the process of debugging, it is found that a warning burst after the execution of a network of the model

lm_logits = self.linear(outputs) + self.bias
#warning:Thudacheck FAIL file=/pytorch/aten/c/THC/Thccachinghostallocator cpp Line=278 error=700: an illegal memory access was encountered

At first glance, both places are relatively simple, but they reported strange mistakes.

Solution:

The debug process found an exception

In the data data output by the pytorch network, the variable does not display the specific network output value, but the address information of the data

T:torch.Tensor object at 0x7fb27e7c8f30
data:torch.Tensor object at 0x7fb27e7c8f30

Later, it was found that it was because of self The linear layer is’ CPU ‘, while other networks are on’ CUDA ‘, which is equivalent to the inconsistency caused by the forward propagation of’ CUDA ‘type data to the’ CPU ‘network. Just transfer the network to’ CUDA ‘.

[Solved] Runtime error: expected scalar type Float but found Double

Error: Runtime error: expected scalar type Float but found Double

w_true=torch.tensor([2,-3.4]).T
b_true=4.2
feature = torch.from_numpy(np.random.normal(0,1,(num_input,num_example)))
#feature = torch.float32(feature)
labels = torch.matmul(w_true.T,feature)+b_true

Problem: runtime error: expected scalar type float but found double
reason: NP random. The data generated by Rand() is float64, while torch defaults to float32, so the problem arises
solution

feature = torch.from_numpy(np.float32(np.random.normal(0,1,(num_input,num_example))))

[Solved] SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 10-11: malformed

#Read a *.txt file using the read_table() function in the Pandas library
data = pd.read_table(r'D:\New\test.txt',delimiter=',',encoding = 'UTF-8')
print(data)

Title defect Solution: add “R” before the path to solve it.

python D:\New\MyTest.py
        name  date   id
0   jianghu  20210201  00001
1  jianghu1  20210202  00002
2  jianghu2  20210203  00003

 

[Solved] RuntimeError An attempt has been made to start a new process

1、 Exception 1: runtimeerror: an attempt has been made to start a new process

Reason: multiple processes need to run in the main function

Solution 1:

Add the main function and call it in main.

if __name__ == '__main__':
	main()

Solution 2:

num_workers changed to 0, single process loading

Ers changed to 0, single process loading

[Solved] ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately

ERROR: Cannot uninstall ‘PyYAML’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
Problem Description.
When installing transformers, an error is reported: “ERROR: Cannot uninstall ‘PyYAML’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.”

pip install transformers 

Solution:

You only need to modify the original installation command to:

pip install transformers --ignore-installed PyYAML

Just.

Similar problems can be solved in this way.