julia - Julia中函数调用的歧义
问题描述
我有这个错误
ERROR: MethodError: vcat(::Array{Real,2}, ::TrackedArray{…,Array{Float32,2}}) is ambiguous. Candidates:
vcat(364::AbstractArray, x::Union{TrackedArray, TrackedReal}, xs::Union{Number, AbstractArray}...) in Tracker at C:\Users\Henri\.julia\packages\Tracker\6wcYJ\src\lib\array.jl:167
vcat(A::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T...) in Base at abstractarray.jl:1296
Possible fix, define
vcat(::Union{AbstractArray{T,2}, AbstractArray{T,1}} where T, ::Union{TrackedArray{T,1,A} where A<:AbstractArray{T,1} where T, TrackedArray{T,2,A} where A<:AbstractArray{T,2} where T}, ::Vararg{Union{AbstractArray{T,2}, AbstractArray{T,1}} where T,N} where N)
告诉我两个vcat()
函数是模棱两可的。我想使用该Base.vcat()
函数,但显式使用它会引发相同的错误。这是为什么 ?错误抛出提出的这个“可能的修复”是什么?
此外,当我手动调用 REPL 中的每一行时,不会引发错误。我不明白这种行为。这只发生在 vcat() 位于另一个函数内部调用的函数中时。就像我下面的例子一样。
这是重现错误的代码:
using Flux
function loss(a, b, net, net2)
net2(vcat(net(a),a))
end
function test()
opt = ADAM()
net = Chain(Dense(3,3))
net2 = Chain(Dense(6,1))
L(a, b) = loss(a, b, net, net2)
data = tuple(rand(3,1), rand(3,1))
xs = Flux.params(net)
gs = Tracker.gradient(() -> L(data...), xs)
Tracker.update!(opt, xs, gs)
end
解决方案
正如在 Henri.D 的评论中提到的,我们已经设法通过谨慎处理它的类型来修复它,其中的类型a
是Array
of Float64
,默认类型由返回,rand
而net(a)
返回的类型是TrackedArray
of ,Float32
并且不可能vcat
使用a
.
我已经设法
vcat
通过改变你的损失函数来解决这个问题:net2(vcat(net(a),Float32.(a)))
因为vcat
不能像net(a)
aFloat32 Array
和a
a那样连接Float64
。然后L(data...)
是TrackedArray
1 个元素,而我认为您需要 aFloat32
这就是为什么我最终替换loss function
为net2(vcat(net(a),Float32.(a)))[1]
推荐阅读
- angular - Angular 从路由器出口组件获取 ElementRef
- python - 如何在文件中每一行的开头附加一个字符串
- python - 如果有GIL,那么在python的多线程环境中锁有什么用?
- python - python中的分数背包(无排序)
- c - 访问嵌套结构以声明变量
- java - 如何在 Java 中随机选择方法
- ruby-on-rails - 为什么当我实时推送 master.key 时我的 Heroku 应用程序崩溃?
- google-sheets - 谷歌表格公式获取最后 5 行的百分比变化
- java - Apache Flink:提交 Flink 作业时出现 NoSuchMethodError
- python - 无法使用 Selenium 输入文本(电子邮件)