Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for function_to_apply in classification #211

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/bumblebee/text/text_classification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ defmodule Bumblebee.Text.TextClassification do
def text_classification(model_info, tokenizer, opts \\ []) do
%{model: model, params: params, spec: spec} = model_info
Shared.validate_architecture!(spec, :for_sequence_classification)
opts = Keyword.validate!(opts, [:compile, top_k: 5, defn_options: []])

opts =
Keyword.validate!(opts, [:compile, top_k: 5, function_to_apply: "softmax", defn_options: []])

top_k = opts[:top_k]
compile = opts[:compile]
function_to_apply = opts[:function_to_apply]
defn_options = opts[:defn_options]

batch_size = compile[:batch_size]
Expand All @@ -24,7 +27,21 @@ defmodule Bumblebee.Text.TextClassification do

scores_fun = fn params, input ->
outputs = predict_fun.(params, input)
Axon.Activations.softmax(outputs.logits)

case function_to_apply do
"softmax" ->
coderrg marked this conversation as resolved.
Show resolved Hide resolved
Axon.Activations.softmax(outputs.logits)

"sigmoid" ->
Axon.Activations.sigmoid(outputs.logits)

"none" ->
outputs.logits

_ ->
raise ArgumentError,
"Invalid :function_to_apply option. Only 'softmax', 'sigmoid', and 'none' are accepted"
coderrg marked this conversation as resolved.
Show resolved Hide resolved
end
coderrg marked this conversation as resolved.
Show resolved Hide resolved
end

Nx.Serving.new(
Expand Down
17 changes: 16 additions & 1 deletion lib/bumblebee/text/token_classification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ defmodule Bumblebee.Text.TokenClassification do
Keyword.validate!(opts, [
:aggregation,
:compile,
function_to_apply: "softmax",
ignored_labels: ["O"],
defn_options: []
])

aggregation = opts[:aggregation]
ignored_labels = opts[:ignored_labels]
compile = opts[:compile]
function_to_apply = opts[:function_to_apply]
defn_options = opts[:defn_options]

batch_size = compile[:batch_size]
Expand All @@ -33,7 +35,20 @@ defmodule Bumblebee.Text.TokenClassification do

scores_fun = fn params, input ->
outputs = predict_fun.(params, input)
Axon.Activations.softmax(outputs.logits)

case function_to_apply do
"softmax" ->
Axon.Activations.softmax(outputs.logits)

"sigmoid" ->
Axon.Activations.sigmoid(outputs.logits)

"none" ->
outputs.logits

_ ->
raise ArgumentError,
"Invalid :function_to_apply option. Only 'softmax', 'sigmoid', and 'none' are accepted"
end

Nx.Serving.new(
Expand Down
19 changes: 17 additions & 2 deletions lib/bumblebee/vision/image_classification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ defmodule Bumblebee.Vision.ImageClassification do
:for_image_classification_with_teacher
])

opts = Keyword.validate!(opts, [:compile, top_k: 5, defn_options: []])
opts =
Keyword.validate!(opts, [:compile, top_k: 5, function_to_apply: "softmax", defn_options: []])

top_k = opts[:top_k]
compile = opts[:compile]
function_to_apply = opts[:function_to_apply]
defn_options = opts[:defn_options]

batch_size = compile[:batch_size]
Expand All @@ -28,7 +30,20 @@ defmodule Bumblebee.Vision.ImageClassification do

scores_fun = fn params, input ->
outputs = predict_fun.(params, input)
Axon.Activations.softmax(outputs.logits)

case function_to_apply do
"softmax" ->
Axon.Activations.softmax(outputs.logits)

"sigmoid" ->
Axon.Activations.sigmoid(outputs.logits)

"none" ->
outputs.logits

_ ->
raise ArgumentError,
"Invalid :function_to_apply option. Only 'softmax', 'sigmoid', and 'none' are accepted"
end

Nx.Serving.new(
Expand Down