错误使用Flux.train!进行训练在Julia中。

huangapple go评论69阅读模式
英文:

Error training using Flux.train! in Julia

问题

### 问题

我有以下的代码,试图在Julia中训练一个神经网络,但是每当我尝试使用Flux.train!时就会出错。

using Flux, CSV, DataFrames, Random, Statistics, Plots

加载数据

data = CSV.read("daily_stock_returns.csv")

将数据分割为训练集和测试集

train_data = data[1:800, :]
test_data = data[801:end, :]

定义输入和输出变量

inputs = Matrix(train_data[:, 2:end])
outputs = Matrix(train_data[:, 1])

定义神经网络结构

n_inputs = size(inputs, 2)
n_hidden = 10
n_outputs = 1
model = Chain(
Dense(n_inputs, n_hidden, relu),
Dense(n_hidden, n_outputs)
)

定义损失函数

loss(x, y) = Flux.mse(model(x), y)

定义优化器

optimizer = ADAM()

训练模型

n_epochs = 100
for epoch in 1:n_epochs
Flux.train!(loss, params(model), [(inputs, outputs)], optimizer)
end

测试模型

test_inputs = Matrix(test_data[:, 2:end])
test_outputs = Matrix(test_data[:, 1])
predictions = Flux.predict(model, test_inputs)
test_loss = Flux.mse(predictions, test_outputs)

打印测试损失

println("测试损失: $test_loss")


### 错误
当我运行这段代码时:

训练模型

n_epochs = 100
for epoch in 1:n_epochs
Flux.train!(loss, params(model), [(inputs, outputs)], optimizer)
end

我得到了下面的错误。

UndefVarError: params not defined

Stacktrace:
1 top-level scope
@ .\In[16]:4
[2] eval
@ .\boot.jl:368 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1428


我尝试过卸载并重新安装,但没有改善。如何修复这个错误?
英文:

Problem

I have the below code trying to train a neural network in Julia but I am getting an error whenever I try using Flux.train!

using Flux, CSV, DataFrames, Random, Statistics, Plots

# Load the data
data = CSV.read("daily_stock_returns.csv")

# Split the data into training and testing sets
train_data = data[1:800, :]
test_data = data[801:end, :]

# Define the input and output variables
inputs = Matrix(train_data[:, 2:end])
outputs = Matrix(train_data[:, 1])

# Define the neural network architecture
n_inputs = size(inputs, 2)
n_hidden = 10
n_outputs = 1
model = Chain(
    Dense(n_inputs, n_hidden, relu),
    Dense(n_hidden, n_outputs)
)

# Define the loss function
loss(x, y) = Flux.mse(model(x), y)

# Define the optimizer
optimizer = ADAM()

# Train the model
n_epochs = 100
for epoch in 1:n_epochs
    Flux.train!(loss, params(model), [(inputs, outputs)], optimizer)
end

# Test the model
test_inputs = Matrix(test_data[:, 2:end])
test_outputs = Matrix(test_data[:, 1])
predictions = Flux.predict(model, test_inputs)
test_loss = Flux.mse(predictions, test_outputs)

# Print the test loss
println("Test loss: $test_loss")

Error

When I run this cell of code:

# Train the model
n_epochs = 100
for epoch in 1:n_epochs
    Flux.train!(loss, params(model), [(inputs, outputs)], optimizer)
end

I get the below error.

UndefVarError: params not defined

Stacktrace:
 [1] top-level scope
   @ .\In[16]:4
 [2] eval
   @ .\boot.jl:368 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1428

I have tried uninstalling and installing with no improvement. How do I fix this error?

答案1

得分: 1

这是因为 Fux.params (参见文档) 默认未导出。你可以将 params 替换为 Flux.params(通常在文档中是这样做的,据我记得),或者将 using Flux 替换为 using Flux: params(第二个选项可能不太理想,因为它会向你的命名空间中添加一个非常通用的名称)。

英文:

It's because Fux.params (see the docs) isn't exported by default. You either replace params by Flux.params (which is how it's often done in the docs, as far as I recall), or replace using Flux by using Flux: params (the second option being probably less ideal because it adds a name that is really generic to your namespace).

huangapple
  • 本文由 发表于 2023年3月3日 20:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定