Undefined variable error in Julia: UndefVarError:castnot defined

Description:

I’m trying to build a transformer-based classifier model in Julia using the TransformersLite package, but I’m encountering an error that says UndefVarError:castnot defined. I’ve updated all of the packages in my environment, but the error persists.

UndefVarError: cast not defined

  1. (::Main.var"workspace#5".var"#1#2")(::Int64)@none:0
  2. iterate@generator.jl:47 [inlined]
  3. collect(::Base.Generator{UnitRange{Int64}, Main.var"workspace#5".var"#1#2"})@array.jl:782
  4. top-level scope@Local: 11

“”"
begin

dim_embedding = hyperparameters["dim_embedding"]
pdrop = hyperparameters["pdrop"]
n_heads = hyperparameters["n_heads"]
dim_feedforward = hyperparameters["dim_feedforward"]
embed = Embed(dim_embedding, length(indexer))
pos_enc = PositionEncoding(dim_embedding)
dropout = Dropout(pdrop)

transformer_blocks = [
TransformerEncoderBlock(n_heads, dim_embedding, dim_feedforward, pdrop=pdrop, act=relu) |>
  cast(TransformersLite.TransformerEncoderBlock)
for _ in 1:n_heads]

flatten_layer = FlattenLayer()
final_dense = Dense(dim_embedding * max_length, nlabels)

# Construct the model
model = TransformersLite.TransformerClassifier(embed, pos_enc, dropout,  
transformer_blocks, flatten_layer, final_dense)

display(model)
println("")

# Adjust for GPU or CPU
device = CUDA.has_cuda() && CUDA.functional() ? gpu : cpu
model = device(model)
hyperparameters["model"] = "$(typeof(model).name. Wrapper)"
hyperparameters["trainable parameters"] = sum(length, Flux.params(model))

# Define loss and accuracy functions
if nlabels == 1
  loss(x, y) = Flux.logitbinarycrossentropy(x, y)
  accuracy(ŷ, y) = mean((Flux.sigmoid.(ŷ) .> 0.5) .== y)
else
  loss = Flux.logitcrossentropy
  accuracy(ŷ, y) = mean(Flux.onecold(ŷ) .== Flux.onecold(y))
end
end

“”"

Steps taken to troubleshoot:

  1. I updated all of the packages in my Julia environment using Pkg.update().
  2. I cleared the Julia cache using Pkg.reset().
  3. I checked for conflicting packages using Pkg.conflicts().
  4. I verified package versions using Pkg.status().
  5. I inspected the code carefully for any potential errors or inconsistencies.

I have also tried reinstalling the Pkg package, restarting Julia, and updating Julia to the latest version, but the error persists.

I would be grateful for any help you can provide in resolving this issue.

Thank you!

Please read this topic:

In particular, format your code properly.

You are trying to call a function nameed cast. Julia Base does not have a function named cast. Are you expecting it from one of the packages you are using? Did you derive your code from an example? If so, perhaps the example is based on older packages. HTH

Your post would probably get more attention in the New to Julia or a specific domain category.

2 Likes