1

我在 CuArrays 中有以下训练数据。

X: 300×8544 CuArray{Float32,2,Nothing}
y: 5×8544 Flux.OneHotMatrix{CuArray{Flux.OneHotVector,1,Nothing}}

我有以下要训练的模型:

# define activation
logistic(x) = 1. / (1 .+ exp.(-x))

# first define a 2-layer MLP model
model = Chain(Dense(300, 64, logistic),
          Dense(64, c),
          softmax) |> gpu

# define the loss
loss(x, y) = Flux.crossentropy(model(x), y)

# define the optimiser
optimiser = ADAM()

但如果我这样做

Flux.train!(loss, params(model), zip(X, y), optimiser)

我收到以下错误:

MethodError: no method matching (::Dense{typeof(logistic),CuArray{Float32,2,Nothing},CuArray{Float32,1,Nothing}})(::Float32)

我应该如何解决这个问题?

4

1 回答 1

0

@D.Danier 请提供最少的工作示例(MWE),这意味着人们可以复制、粘贴和运行的完整代码。下面是一个例子

#Pkg.activate("c:/scratch/flux-test")

using CuArrays, Flux
CuArrays.allowscalar(false)

# define activation
# you don't the broadcast dots
logistic(x) = 1 / (1 + exp(-x))

# ensure your code works on GPU
CuArrays.@cufunc logistic(x) = 1 / (1 + exp(-x))

X = cu(rand(300, 8544))
y = cu(rand(5, 8544))
c = 5

# first define a 2-layer MLP model
model = Chain(Dense(300, 64, logistic),
          Dense(64, c),
          softmax) |> gpu

# define the loss
loss(x, y) = Flux.crossentropy(model(x), y) |> gpu

model(X)

# define the optimiser
optimiser = ADAM()

loss(X, y)

Flux.train!(loss, params(model), zip(eachcol(X), eachcol(y)), optimiser)

当您 时Flux.train!,您必须告诉Flux您要配对X和的列y来计算损失。顺便说一句,这可能不太理想,因为它计算了太多的迭代。您可能希望将它们分组为小批量。或者如果你的问题真的这么小,那么你可以想一次计算整个事情,例如

Flux.train!(loss, params(model), (X, y), optimiser)

这基本上是说基于 和 的整体来计算X损失y

于 2019-12-01T10:23:57.863 回答