Tag Archives: python

Pytorch: error message with chunks of 0 [How to Solve]

File "D:/Codes/code/Python Project/group_reid-master/group_reid-master/main_group_gcn_siamese_part_half_fulltest_sink.py", line 348, in train_gcn
    loss.backward()
  File "D:\Codes\Anaconda3\envs\pytorch_gpu\lib\site-packages\torch\tensor.py", line 185, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "D:\Codes\Anaconda3\envs\pytorch_gpu\lib\site-packages\torch\autograd\__init__.py", line 127, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: chunk expects `chunks` to be greater than 0, got: 0
Exception raised from chunk at ..\aten\src\ATen\native\TensorShape.cpp:496 (most recent call first):

As shown in the figure, I always reported an error chunk of 0. At first, I was puzzled. There was no similar situation with me when looking for information on the Internet. I typed the error and found that an error was reported in the derivation of loss (the figure below). I thought that loss is a function called, and it is impossible to report such an error. So throw it directly on the server to debug. Due to different versions of pytorch, the error content is also different. We finally found the error in pytorch version 1.1.

            loss.backward()

It was caused by dimension mismatch during cutting:

env11_junk1 = env11.squeeze().unsqueeze(0).unsqueeze(0).repeat((5-x1_valid.shape[0]), parts, 1)
env22_junk2 = env22.squeeze().unsqueeze(0).unsqueeze(0).repeat((5-x2_valid.shape[0]), parts, 1)
env11 = env11.squeeze().unsqueeze(0).unsqueeze(0).repeat(x1_valid.shape[0], parts, 1)
env22 = env22.squeeze().unsqueeze(0).unsqueeze(0).repeat(x2_valid.shape[0], parts, 1)

  # calculate within graph and inter graph message
h_k1 = torch.cat((self.W_x(x1[i, :sample_size1, :]), self.W_neib(x_neib1), self.W_relative(mu1), self.W_env(env11)), 2).unsqueeze(0)  
h_k_junk1 = torch.cat((self.W_x(x1[i, sample_size1:, :]), self.W_x(x1[i, sample_size1:, :]), self.W_x(x1[i, sample_size1:, :]),self.W_env(env11_junk1)), 2).unsqueeze(0)

h_k2 = torch.cat((self.W_x(x2[i, :sample_size2, :]), self.W_neib(x_neib2), self.W_relative(mu2), self.W_env(env22)), 2).unsqueeze(0)
h_k_junk2 = torch.cat((self.W_x(x2[i, sample_size2:, :]), self.W_x(x2[i, sample_size2:, :]), self.W_x(x2[i, sample_size2:, :]),self.W_env(env22_junk2)), 2).unsqueeze(0)                       

In my code (square and unsqueeze are redundant, just premute directly). I intended to copy the same first dimension as X1 in the first dimension, but in the actual data set, the first dimension of X1 may be 0. Therefore, if the parameter of repeat is 0, an error will be reported, which cannot be less than the original dim. Since the error reported is not obvious, I wasted half a day thinking about this problem. I hereby record it.

[Solved] Demjson error: ERROR: Command errored out with exit status 1

    ERROR: Command errored out with exit status 1:
     command: /data/wangzy-p/soft/anaconda3/envs/tf_1.14/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-lkidxq7i/demjson_1adc3de9a48e4e169cf993fa26319b82/setup.py'"'"'; __file__='"'"'/tmp/pip-install-lkidxq7i/demjson_1adc3de9a48e4e169cf993fa26319b82/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-ldwnwp11
         cwd: /tmp/pip-install-lkidxq7i/demjson_1adc3de9a48e4e169cf993fa26319b82/
    Complete output (1 lines):
    error in demjson setup command: use_2to3 is invalid.

Solution

When setuptools > 58.0.0 , the dependency installation of demjson failed, demjson has been published demjon3 to solve this problem, use reference https://pypi.org/project/demjson3

Failed to build bottleneck ERROR: Could not build wheels for bottleneck which use PEP 517

I want to import pandas in anaconda_ Profiling: the import failed. PIP install later reported the error as mentioned in the title. Later, check the stack overflow to solve the problem. Here is a record of the solution. The four steps start:

conda install bottleneck
pip install p5py
pip install pep517
pip install pycaret

Enter in your Anaconda prompt environment in turn to solve the problem.

[Solved] os.py“, line 725, in __getitem__ raise KeyError(key) from None KeyError: ‘PATH‘

os.py”, line 725, in getitem raise KeyError(key) from None KeyError: ‘PATH’

scenario: when I was working on the project, I encountered a problem. Everything was running normally on the Linux server, but there was a problem when I was running in the remote finalshell (the code behind the process actually ran), but it seemed that there was a problem with the environment.


Solution process:

when I instantiated a class, I was a little puzzled by this problem. When I began to find out about os.environ [“path”], I knew that Python did not load the environment (here I used the virtual environment instead of the system default Python). So I went into the virtual environment to execute this: 

import os
print(os.environ["PATH"])

It is found that there is a value here, but the bug reported is none. Therefore, at the beginning of the code, I add the value printed out just now to the code, and then I can continue to execute Python code in the virtual environment of the server on the remote shell

import os
os.environ["PATH"] = "This string copies the value printed from the above code"

So far, everything can run normally

Using postman Test Django Interface error: RuntimeError:You called this URL via POST,but the URL doesn‘t end in a slash

1. Access interface

Using the postman provider

2. Error reporting:

RuntimeError:You called this URL via POST,but the URL doesn’t end in a slash and you have APPEND_ SLASH set. Django can’t redirect to the slash URL while maintaining POST data.

3. Solution:

In fact, two methods have been suggested in the error report

Method 1: add a at the end of the URL/

http:10.192.171.128:8000/ops/rbac/group/distribute

Method 2: set append in settings_ SLASH=False

[Solved] Git Clone Error: “error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length …

1. When git pull is used for several large projects recently, the following errors always appear: </ font>

error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

2. According to the online method, there are still problems</ font>

// set proxy
git config --global http.proxy socks5://127.0.0.1:1081
git config --global https.proxy socks5://127.0.0.1:1081
// add cache
git config --global http.postBuffer 1048576000

3. Finally, add the following operations to solve the problem

//Change this value to a larger value, the perfect solution, the default is 1500.
ifconfig eth0 mtu 14000

Flask Database Migration Error: ERROR [flask_migrate] Error: Can‘t locate revision identified by ‘a1c25fe0fc0e‘

Problem Description:

In flash web development, we will use the flash migrate library to migrate the database, so as to submit the changed database model we wrote in the program script to the database without deleting and rebuilding the database model
if we use Python manage.py DB init to create a migration warehouse, and then use the migrate or upgrade in flash migrate, the following two instructions:

python manage.py db migrate
python manage.py db upgrade

Error [flag_migrate] error: can’t locate revision identified by ‘a1c25fe0fc0e’ may appear. The identification number of ‘a1c25fe0fc0e’ corresponds to different database models! As shown in the figure:

resolvent:

The reason for the above error is that flash migrate cannot find the revision of “a1c25fe0fc0e” logo. We just need to indicate the missing logo number in the command
we can use the following commands in order in the shell command line window:

python app.py db revision --rev-id <Fill the prompt's identification number into this location, such as a1c25fe0fc0e above>
python app.py db migrate
python app.py db upgrade

Enter the following command to demonstrate:

then, the database migration succeeds

Finally, if there are deficiencies in the article, criticism and correction are welcome!

[Solved] Pyodbc.ProgrammingError: No results. Previous SQL was not a query.

Call the stored procedure on the remote sqlserver server with Python. Code fragment:


    conn = pyodbc.connect(SERVER=host, UID=user, PWD=password, DATABASE=dbname,
                            DRIVER=driver)
    cur = conn.cursor()
    if not cur:
        raise (NameError, 'Database connection error)
    else:
        cur.execute("EXEC GetLastData")
        resList = list()
        resList = cur.fetchall()

Execution error:

pyodbc.ProgrammingError: No results.  Previous SQL was not a query.

After checking, the stored procedure can be executed normally in the sqlserver environment. It seems that there is a problem when calling pyodbc. A similar problem is found on stackoverflow. The answer is as follows:
the problem was solved by adding set NOCOUNT on; to the beginning of the anonymous code block. That statement suppresses the record count values generated by DML statements like UPDATE … and allows the result set to be retrieved directly.

The problem is solved by adding it to the beginning of the anonymous code block. This statement suppresses the record count value generated by the DML statement and allows the set result to be retrieved directly. SET NOCOUNT ON; UPDATE …

So add a sentence set NOCOUNT on to the stored procedure

CREATE proc [dbo].[GetLastData]
AS
BEGIN

SET NOCOUNT ON

declare @begindate datetime,@enddate datetime
select @begindate=CONVERT(varchar(7),GETDATE(),120)+'-01'
select @enddate=DATEADD(MONTH,1,@begindate)

[Solved] Pytorch Error: RuntimeError: Trying to backward through the graph a second time

During Gan’s training, we often encounter this problem: runtimeerror: trying to backward through the graph a second time, but the saved intermediate results have already been free

The description of this error is that when you are propagating in the direction, the cache is released in advance. Many solutions are to change loss. Backward() to loss. Backward (retain_graph = true). In fact, most of the code problems are not here, and the retention of the calculation chart may cause the cache to accumulate rapidly and lead to the explosion of the video memory, In fact, the real reason is that the discriminator and the producer share the same variables. Just add detach () when using variables for the first time.

#### Update Discriminator ###
real_preds = netD(real_gt)
fake_preds = netD(fake_coarse)


#### Update Generator #####
real_preds = netD(real_gt)
fake_preds = netD(fake_coarse)

Change to:

#### Update Discriminator ###
real_preds = netD(real_gt.detach())
fake_preds = netD(fake_coarse.detach())


#### Update Generator #####
real_preds = netD(real_gt)
fake_preds = netD(fake_coarse)

It’s not easy to find the method. Thank you for your support

ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied

Error content: importerror: C extension: DLL load failed: access denied. not built. If you want to import pandas from the source directory, you may need to run ‘python setup.py build_ ext –inplace –force’ to build the C extensions first.

Reason for error reporting: it may be caused by deleting the installation dependency package of pandas in the environment by mistake, or by deleting the anti-virus software. Generally, the latter does it.

Solution 1: uninstall pandas: PIP uninstall pandas , reinstall: PIP install pandas

—————————————————————————
if you use the above method, you may make mistakes

Error content: error: could not install packages due to an oserror: [errno 13] permission denied: ‘your project path \ venv \ lib \ site packages \ pandas\_ libs\
tslibs\period.cp36-win_ amd64.pyd’
Check the permissions.

Error reason: it means that there are missing files in the folder reported above and cannot be downloaded.

Solution 2: delete the folder and reinstall pandas. For example, in this example, delete pandas under site packages. Remember to delete pandas under site packages instead of site packages. Don’t make a mistake. Delete all the environment and you’ll be finished.

—————————————————————————

If it’s still the previous error, congratulations. I’ve been confused for half an hour. Hahaha……..

Why?The reason is that with Shadu software, I don’t need to repeat what sahdu software is. I know everything: the inner corners of a circle and. Because it is on, it causes the software to delete the PYD file during pandas installation

Solution 3: turn off the kill software, and then try again according to solution 2. It should be successful
if you still can’t, you can only be a freak. Ha ha ha. Just kidding, you can write private letters and step on the pit together!