Skip to content

Commit 04bc0ca

Browse files
committed
update to make processor output to have nonlinear transform according to the model
1 parent a1582da commit 04bc0ca

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AutoComputationalGraphTuning"
22
uuid = "9895cdd9-2e9e-4374-a2b7-90eb9a5a3bcd"
33
authors = ["Shane Kuei-Hsien Chu (skchu@wustl.edu)"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

src/final_and_code/code_processor_eval.jl

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ function Base.show(io::IO, stats::ProcessorEvalStats)
1010
print(io, " R² Processor: ", round(stats.r2_processor, digits=4))
1111
end
1212

13-
"""Compute gyros and predictions for a single batch"""
14-
function _compute_gyro_and_preds(model, code, predict_position::Int)
15-
(_, preds), gyro = Flux.withgradient(code) do x
16-
linear_sum_fcn = @ignore model.predict_up_to_final_nonlinearity;
17-
preds = linear_sum_fcn(x; predict_position=predict_position)
18-
preds |> sum, preds # need the first component to be the gradient, but don't need to return it
13+
"""Compute code gradient and linear (pre-activation) output for a single batch"""
14+
function _compute_code_gradient_and_linear_output(model, code, predict_position::Int)
15+
(_, linear_output), code_gradient = Flux.withgradient(code) do x
16+
linear_sum_fcn = @ignore model.predict_up_to_final_nonlinearity
17+
linear_output = linear_sum_fcn(x; predict_position=predict_position)
18+
linear_output |> sum, linear_output # need the first component to be the gradient, but don't need to return it
1919
end
20-
return preds, gyro[1]
20+
return linear_output, code_gradient[1]
2121
end
2222

23+
2324
"""Compute R² coefficient, excluding NaN values"""
2425
function _compute_r2(y_true::AbstractVector{T}, y_pred::AbstractVector{T}) where T<:AbstractFloat
2526
# Create mask for valid (non-NaN) values in both arrays
@@ -63,7 +64,7 @@ function evaluate_processor(model, processor, dataloader, set_name::String;
6364

6465
# Collect predictions and products
6566
labels_collection = Vector{T}[]
66-
preds_collection = Vector{T}[]
67+
linear_output_collection = Vector{T}[]
6768
gyro_prods_collection = Vector{T}[]
6869
proc_prods_collection = Vector{T}[]
6970

@@ -75,7 +76,7 @@ function evaluate_processor(model, processor, dataloader, set_name::String;
7576
end
7677

7778
code = model.code(seq |> gpu)
78-
preds, gyro = _compute_gyro_and_preds(model, code, predict_position)
79+
linear_outputs, gyro = _compute_code_gradient_and_linear_output(model, code, predict_position)
7980

8081
processor.training[] = false
8182
proc_gyro = processor(code, gyro)
@@ -88,24 +89,30 @@ function evaluate_processor(model, processor, dataloader, set_name::String;
8889
proc_prod = vec(sum(proc_gyro_code_product, dims=(1,2)))
8990

9091
# Collect
91-
push!(preds_collection, vec(cpu(preds)))
92+
push!(linear_output_collection, vec(cpu(linear_outputs)))
9293
push!(gyro_prods_collection, cpu(gyro_prod))
9394
push!(proc_prods_collection, cpu(proc_prod))
9495
end
9596

9697
# Concatenate
9798
labels_all = vcat(labels_collection...)
98-
preds_all = vcat(preds_collection...)
99-
gyro_prods = vcat(gyro_prods_collection...)
100-
proc_prods = vcat(proc_prods_collection...)
99+
model_predictions =
100+
model.final_nonlinearity.(vcat(linear_output_collection...))
101+
gyro_prods_predictions =
102+
model.final_nonlinearity.(vcat(gyro_prods_collection...))
103+
proc_prods_predictions =
104+
model.final_nonlinearity.(vcat(proc_prods_collection...))
101105

102-
pts = (labels=labels_all, predictions=preds_all, grad_prod=gyro_prods, proc_prod=proc_prods)
106+
pts = (labels=labels_all,
107+
predictions=model_predictions,
108+
grad_prod=gyro_prods_predictions,
109+
proc_prod=proc_prods_predictions)
103110

104111
# Compute R² scores
105-
r2_orig = _compute_r2(preds_all, gyro_prods)
106-
r2_proc = _compute_r2(preds_all, proc_prods)
107-
r2_model_vs_labels = _compute_r2(labels_all, preds_all)
108-
r2_model_vs_proc = _compute_r2(labels_all, proc_prods)
112+
r2_orig = _compute_r2(model_predictions, gyro_prods_predictions)
113+
r2_proc = _compute_r2(model_predictions, proc_prods_predictions)
114+
r2_model_vs_labels = _compute_r2(labels_all, model_predictions)
115+
r2_model_vs_proc = _compute_r2(labels_all, proc_prods_predictions)
109116

110117
# Print results
111118
println("\n=== $set_name Set ===")

0 commit comments

Comments
 (0)