# Zygote limitations: getindex of a DataFrame while training a PINN

**URL:** <https://discourse.julialang.org/t/zygote-limitations-getindex-of-a-dataframe-while-training-a-pinn/99967>\
**Category:** Machine Learning\
**Tags:** question, flux, dataframes, zygote\
**Created:** [June 6, 2023, 9:23pm UTC](https://discourse.julialang.org/t/zygote-limitations-getindex-of-a-dataframe-while-training-a-pinn/99967 "2023-06-06T21:23:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ordens\_Ritter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ordens_ritter/32/50363_2.png) [@Ordens\_Ritter](https://discourse.julialang.org/u/Ordens_Ritter)\
**Post date:** [June 6, 2023, 9:23pm UTC](https://discourse.julialang.org/t/zygote-limitations-getindex-of-a-dataframe-while-training-a-pinn/99967/1 "2023-06-06T21:23:02Z")

</div>

I want to train a PINN.  
I decided, that I want to estimate the physical loss term at static, independent sample times.

For each of the sample times, I have to look up result of the previous sample point, to estimate the delta between both.  
I currently solve this, with a look-up in a DataFrame.  
The current version of the code is able to run outside the training, but not during the training. I get I error in Zygote, I assume, that the getindex of the DataFrame involves a try-catch bock:

The problematic lines of code are:

```julia
	is_previous = static_df.Column1 .== (x.Column1-1)
	xₚᵣₑᵥ = static_df[is_previous, :] |> first

```

The second lines raises this Error:

```plaintext
Compiling Tuple{Type{Dict}, Dict{Symbol, Int64}}: try/catch is not supported.

Refer to the Zygote documentation for fixes.

https://fluxml.ai/Zygote.jl/latest/limitations

```

I hope, that someone has an idea

- to make this look-up without this limitation, or
- rework the physical loss term, so that this look-up is not needed.

Background:  
I currently call the total loss function for reach trained data point cannot get mini batches running).  
Therefore I apply a weight, because there are hundred of sample points for the physical loss.

```julia
lossₜ(ŷ, y) = (1 - loss_ratio) * mse(ŷ, y)+ loss_ratio * lossₚ()

```

I compute the physical loss term for every point of the sub sample:

```julia
lossₚ() = eachrow(sub_sample_time_df) .|> lossₚ |> sum

```

```julia
	function lossₚ(x)

		Κ = 0.997887
		τ = 1439.8
		θ = -661.1

		ŷ = [x.Q1, x.T1prev] |> NN |> first

		# Column1 contains a zero based index
		is_previous = all_data_points_df.Column1 .== (x.Column1-1)
		xₚᵣₑᵥ = static_df[is_previous, :] |> first
		
		ŷₚᵣₑᵥ = [xₚᵣₑᵥ.Q1, xₚᵣₑᵥ.T1prev] |> NN |> first
		Δyₗ = ŷ - ŷₚᵣₑᵥ
		U = x.Q1
		i₀ = x.duration
		
		yᵣ = (.-ŷ .+ Κ .* U .*(i₀ .- θ)) ./ τ
		
		return sum((Δyₗ .- yᵣ) .^2)
	end

```

I construct the physical model around the sample point. The function includes a delta term. Do a lookup of the heating and the previous temperature of the previous value, to feed it into a tiny neural network with only 33 parameter.

I perhaps someone with more experience with PINN can share his experience?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [June 6, 2023, 10:16pm UTC](https://discourse.julialang.org/t/zygote-limitations-getindex-of-a-dataframe-while-training-a-pinn/99967/2 "2023-06-06T22:16:41Z")

</div>

The first step to try is to convert your dataframe into a normal matrix (or series of vectors/matrices) before passing it to your loss function. Zygote is evidently not able to differentiate through some of the internals of DataFrames.jl, so it needs a friendlier input format to work with.
