【代码贡献】QSVR: build the show_res test grid from each column's own min/max - #46
Open
mnn31 wants to merge 1 commit into
Open
【代码贡献】QSVR: build the show_res test grid from each column's own min/max#46mnn31 wants to merge 1 commit into
mnn31 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Quantum_SVR.show_res()draws the regression surface over the wrong region.Both axes of the test grid get the same range, and that range is not the range
of either input column: it starts at column 0's minimum and ends at column 1's
maximum. The surface is therefore evaluated far outside the training data on one
axis, and the other axis covers an interval the data never occupies, so the plot
does not line up with the scatter points it is drawn against.
Root cause
pyqpanda_alg/QSVR/QSVR.py,show_res(lines 137-138):The column index is crossed. Both lines take the minimum from column 0 and the
maximum from column 1, and the two lines are identical, so
x1_testnever seescolumn 1's minimum and
x0_testnever sees column 0's maximum.It is easy to miss because
__init__runsStandardScalerand then PCA, whichusually leaves both columns centred near zero and roughly the same size. The
error is still there in that case: with a strongly correlated 2-feature input
(case B below), PC1 spans
[-4.12, 4.30]and PC2[-0.12, 0.12], and bothplotted axes come out as
[-4.12, 0.12]. It becomes obvious as soon as thecolumns differ in scale, for instance when
self.xis assigned directly, orwhen a 1-column input is zero-padded and column 1 is constant 0.
Fix
Take each axis from its own column:
Two lines. The grid size, the meshgrid, the kernel, the fit and the plotting
calls are untouched, and
get_res()is not affected.Verification
show_resis instrumented with a stub SVR so the grid it builds can be readback without waiting on the quantum kernel, on three inputs whose columns have
different ranges. Case A sets
self.xdirectly with column 0 in[0, 2]andcolumn 1 in
[10, 30]. Case B goes through__init__, so the columns are thePCA components of a correlated input: with
rng = numpy.random.default_rng(0),t = rng.normal(size=200)andx = column_stack([t + 0.05*rng.normal(size=200), 20*t + rng.normal(size=200)]).Case C is a single-column input that the class zero-pads.
The docstring example runs end to end with the real quantum kernel and the same
random data as before, and still completes in about 5 s.
Tests
2 cases added to
test/QAlgBase/Test_class_qsvr_Quantum_SVR.py. They swap in arecording stub for
SVRand for the pyplot figure, callshow_res(), and readthe grid straight out of the points handed to
predict: one asserts both axesequal
linspaceover their own column's min and max for constructor-produceddata, the other pins the crisp case of column 0 in
[0, 2]and column 1 in[10, 30]. No figure is created and no quantum circuit is run, so both are fastand deterministic. Both fail on
developand pass with this change.(
-o addopts=""becausetest/pytest.inihardcodes allure options that needthe
allure-pytestplugin.)example/QAlgBase/testeg_class_qsvr.pyimportspyqpanda_alg.QFinance.class_qsvr,which does not exist in this tree, and also imports
pyqpandarather thanpyqpanda3. That is a separate stale-example problem, unrelated to the gridranges, so I left it alone here.