FluxML / GeometricFlux.jl

Geometric Deep Learning for Flux
https://fluxml.ai/GeometricFlux.jl/stable/
MIT License
348 stars 30 forks source link

Domain Error with negative sqrt. #178

Open rezabayani opened 3 years ago

rezabayani commented 3 years ago

When training my neural network with GCNs, I sometimes (randomly) run into the following error which is caused by calling square root of a negative real number. It is interesting to mention that my loss function is log. cross entropy and I do not use any square roots anywhere. Also, I am using a sigmoid activator at the last layer to make sure no such errors occur. However, on random, this error happens during training with this package and I do not know how to fix it.

DomainError with -2.748779e11: sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)). in top-level scope at base\util.jl:234 in macro expansion at my_training.jl:431 in collect at base\array.jl:670 in collect_to_with_first! at base\array.jl:689 in collect_to! at base\array.jl:711 in iterate at base\generator.jl:47 in at base\none in at Flux\IjMZL\src\layers\basic.jl:38 in applychain at Flux\IjMZL\src\layers\basic.jl:36 in Chain at Flux\IjMZL\src\layers\basic.jl:38 in applychain at Flux\IjMZL\src\layers\basic.jl:36 in Chain at Flux\IjMZL\src\layers\basic.jl:38 in applychain at Flux\IjMZL\src\layers\basic.jl:36 in at ev_training.jl:208 in at Flux\IjMZL\src\layers\basic.jl:38 in applychain at Flux\IjMZL\src\layers\basic.jl:36 in applychain at Flux\IjMZL\src\layers\basic.jl:36 in at GeometricFlux\k4atN\src\layers\conv.jl:48 in normalized_laplacian##kw at GeometricFlux\k4atN\src\graph\featuredgraphs.jl:133 in #normalized_laplacian#104 at GeometricFlux\k4atN\src\graph\featuredgraphs.jl:133 in at GeometricFlux\k4atN\src\operations\linalg.jl:127 in #normalized_laplacian#85 at GeometricFlux\k4atN\src\operations\linalg.jl:128 in _normalized_laplacian at GeometricFlux\k4atN\src\operations\linalg.jl:133 in inv_sqrt_degree_matrix##kw at GeometricFlux\k4atN\src\operations\linalg.jl:98 in #inv_sqrt_degree_matrix#83 at GeometricFlux\k4atN\src\operations\linalg.jl:98 in materialize at base\broadcast.jl:820 in copy at base\broadcast.jl:840 in copyto! at base\broadcast.jl:864 in copyto! at base\broadcast.jl:909 in macro expansion at base\simdloop.jl:77 in macro expansion at base\broadcast.jl:910 in getindex at base\broadcast.jl:564 in _broadcast_getindex at base\broadcast.jl:603 in _getindex at base\broadcast.jl:628 in _broadcast_getindex at base\broadcast.jl:604 in _broadcast_getindex_evalf at base\broadcast.jl:631 in sqrt at base\math.jl:557 in throw_complex_domainerror at base\math.jl:33

** Pkg.status() [fbb218c0] BSON v0.2.6 [052768ef] CUDA v1.2.1 [3895d2a7] CUDAapi v4.0.0 [587475ba] Flux v0.11.0 [7e08b658] GeometricFlux v0.6.1 [bd48cda9] GraphRecipes v0.5.4 [2e9cd046] Gurobi v0.8.1 [4138dd39] JLD v0.10.0 [033835bb] JLD2 v0.1.14 [4076af6c] JuMP v0.21.3 [093fc24a] LightGraphs v1.3.3 [b8f27783] MathOptInterface v0.9.14 [fdba3010] MathProgBase v0.7.8 [91a5bcdd] Plots v1.5.8 [c36e90e8] PowerModels v0.17.2 [438e738f] PyCall v1.91.4 [82193955] SCIP v0.9.6 [47aef6b3] SimpleWeightedGraphs v1.1.1 [9f7883ad] Tracker v0.2.8 [e88e6eb3] Zygote v0.5.4 [37e2e46d] LinearAlgebra [10745b16] Statistics

yuehhua commented 3 years ago

Within GCN convolution layer, a inverse squared degree matrix is computed. This matrix is computed for normalized graph Laplacian. If the degree of each vertex becomes negative, this makes it happen to compute over the square root of a negative number. You may want to check the degree of each vertex not to be a negative value to avoid this error.

in at GeometricFlux\k4atN\src\layers\conv.jl:48 in normalized_laplacian##kw at GeometricFlux\k4atN\src\graph\featuredgraphs.jl:133 in #normalized_laplacian#104 at GeometricFlux\k4atN\src\graph\featuredgraphs.jl:133 in at GeometricFlux\k4atN\src\operations\linalg.jl:127 in #normalized_laplacian#85 at GeometricFlux\k4atN\src\operations\linalg.jl:128 in _normalized_laplacian at GeometricFlux\k4atN\src\operations\linalg.jl:133 in inv_sqrt_degree_matrix##kw at GeometricFlux\k4atN\src\operations\linalg.jl:98 in #inv_sqrt_degree_matrix#83 at GeometricFlux\k4atN\src\operations\linalg.jl:98