gpt4 book ai didi

julia - 在 Julia 中打开 map 返回

转载 作者:行者123 更新时间:2023-12-03 16:02:39 25 4
gpt4 key购买 nike

我有一个返回数组的函数。我想将函数映射到输入向量,输出是所有数组的简单串联。功能是:

function log_it(r, bzero = 0.25, N = 400)
main = rand(Float16, (N+150));
main[1] = bzero;
for i in 2:N+150
main[i] = *(r, main[i-1], (1-main[i-1]))
end;
y = unique(main[(N+1):(N+150)]);
r_vec = repeat([r], size(y)[1]);
hcat(r_vec, y)
end;

我可以很好地映射它:
map(log_it, 2.4:0.001:2.405)

但结果很糟糕:
 [2.4 0.58349609375]
[2.401 0.58349609375]
[2.402 0.583984375; 2.402 0.58349609375]
[2.403 0.583984375]
[2.404 0.583984375]
[2.405 0.58447265625; 2.405 0.583984375]

注意,嵌套数组的长度是无限的——我正在寻找一种不依赖于提前知道嵌套数组长度的解决方案。

我想要的是这样的:
 2.4    0.583496
2.401 0.583496
2.402 0.583984
2.402 0.583496
2.403 0.583984
2.404 0.583984
2.405 0.584473
2.405 0.583984

我使用 for 循环制作的:
results = Array{Float64, 2}(undef, 0, 2)
for i in 2.4:0.001:2.405
results = cat(results, log_it(i), dims = 1)
end
results

代码工作正常,但 for 循环需要大约四倍的时间。我也觉得 map 是正确的方法,我只是遗漏了一些东西 - 要么以返回一个不错的数组向量的方式执行 map,要么在数组的某些突变中“unnest”。我试过查看 flatten 和 collect 之类的功能,但找不到任何东西。

提前谢谢了!

最佳答案

您确定您正确地对此进行了基准测试吗?尤其是在操作非常快的情况下,基准测试有时会很棘手。作为起点,我建议您确保始终将要进行基准测试的任何代码包装到一个函数中,并使用 BenchmarkTools包以获得可靠的计时。

在 Julia 中编写循环通常不应该有性能损失,因此与 map 相比,循环的运行时间增加了 3 倍。听起来很可疑。

这是我得到的:

julia> using BenchmarkTools

julia> @btime map(log_it, 2.4:0.001:2.405)
121.426 μs (73 allocations: 14.50 KiB)

julia> function with_loop()
results = Array{Float64, 2}(undef, 0, 2)
for i in 2.4:0.001:2.405
results = cat(results, log_it(i), dims = 1)
end
results
end

julia> @btime with_loop()
173.492 μs (295 allocations: 23.67 KiB)

所以循环慢了大约 50%,但那是因为你分配了更多。

当您使用 map通常有一种更 Julia 的方式来表达您正在使用的内容 broadcasting .这适用于任何用户定义的函数:
julia> @btime log_it.(2.4:0.001:2.405)
121.434 μs (73 allocations: 14.50 KiB)

相当于您的 map表达。我认为您正在寻找的只是一种堆叠所有结果向量的方法 - 您可以使用 vcat并为此吐槽:
julia> @btime  vcat(log_it.(2.4:0.001:2.405)...)
122.837 μs (77 allocations: 14.84 KiB)

只是为了确认:
julia> vcat(log_it.(2.4:0.001:2.405)...) == with_loop()
true

因此,以 map 的速度和内存成本,使用广播和连接会得到与循环相同的结果。解决方案。

关于julia - 在 Julia 中打开 map 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61048244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com