# How to do a PCA with Flux

**URL:** <https://discourse.julialang.org/t/how-to-do-a-pca-with-flux/96836>\
**Category:** New to Julia\
**Created:** [March 30, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-to-do-a-pca-with-flux/96836 "2023-03-30T10:29:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [March 30, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-to-do-a-pca-with-flux/96836/1 "2023-03-30T10:29:06Z")

</div>

I am trying to use `Flux` to do a `Principal Component Analysis` of a simple database (`íris) but I am having some problems with `Flux.train!` which gives me the following error:

```julia
ERROR: MethodError: no method matching loss(::SubArray{Float64, 1, Matrix{Float64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}, ::SubArray{Float64, 1, Matrix{Float64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true})
Closest candidates are:
  loss(::Any) at c:\Users\sergio.bacelar\Documents\JULIA\PCA_jl\ex4.jl:32

```

The code I have been using is:

```julia
using Flux, RDatasets, Statistics, StatsBase

# Load the iris dataset
iris = dataset("datasets", "iris")
# Split the data into features (X) and labels (y)
X = Matrix(iris[:, 1:4])
y = iris[:, 5]
# Standardize the data
X_standardized = standardize(ZScoreTransform, X, dims=1)
# Define the PCA model
model = Chain(Dense(4, 2), transpose)
# Define the loss function
loss(x) = sum(abs2, x - mean(x))
# Train the model
opt = ADAM(0.1)
data = [(x, x) for x in eachrow(X_standardized)]

for i = 1:100
Flux.train!(loss, Flux.params(model), data, opt)
end

# Get the principal components and the transformed data
pcs = model. Layers[1]
X_transformed = model(X_standardized) |> transpose
# Print the results
println("Principal Components: ")
println(pcs)
println("Transformed Data: ")
println(X_transformed)

```

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [March 31, 2023, 6:18am UTC](https://discourse.julialang.org/t/how-to-do-a-pca-with-flux/96836/2 "2023-03-31T06:18:22Z")

</div>

You should read carefully the documentation regarding the train loop  
[http://fluxml.ai/Flux.jl/stable/training/training/#Training-Loops](http://fluxml.ai/Flux.jl/stable/training/training/#Training-Loops)  
Your loss function has two problems: a) doesn’t use the model; b) takes only one input, while the dataset iterator yields tuples with 2 elements.
