出于好奇,我试图将具有校正线性单元的神经网络拟合到多项式函数。例如,我想看看神经网络想出一个函数的近似值是多么容易(或困难)f(x) = x^2 + x
。下面的代码应该可以做到,但似乎什么也没学到。当我跑
using Base.Iterators: repeated
ENV["JULIA_CUDA_SILENT"] = true
using Flux
using Flux: throttle
using Random
f(x) = x^2 + x
x_train = shuffle(1:1000)
y_train = f.(x_train)
x_train = hcat(x_train...)
m = Chain(
Dense(1, 45, relu),
Dense(45, 45, relu),
Dense(45, 1),
softmax
)
function loss(x, y)
Flux.mse(m(x), y)
end
evalcb = () -> @show(loss(x_train, y_train))
opt = ADAM()
@show loss(x_train, y_train)
dataset = repeated((x_train, y_train), 50)
Flux.train!(loss, params(m), dataset, opt, cb = throttle(evalcb, 10))
println("Training finished")
@show m([20])
它返回
loss(x_train, y_train) = 2.0100101f14
loss(x_train, y_train) = 2.0100101f14
loss(x_train, y_train) = 2.0100101f14
Training finished
m([20]) = Float32[1.0]
这里的任何人都看到我如何使网络适合f(x) = x^2 + x
?