Git Product home page Git Product logo

deep-learning-recipes's People

Contributors

nanxstats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

deep-learning-recipes's Issues

Triplet loss with classification

Hi Nan,

really helpful blogpost!

I am trying to extend your code and add classification to the triplet problem. In my case I am trying to learn a classifier for two MNIST digits.

First I am generating triplets of 1s and 2s like this:

# Load and prepare MNIST data
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y

dim(x_train) <- c(nrow(x_train), 784)
dim(x_test) <- c(nrow(x_test), 784)
x_train <- x_train / 255
x_test <- x_test / 255

ok <- which(y_train %in% c(1,2))
x_tmp <- x_train[ok,]
y_tmp <- y_train[ok]

ones <- which(y_tmp == 1)
ones <- sample(ones)
twos <- which(y_tmp == 2)
twos <- sample(twos)

tmp_one <- do.call(rbind, lapply(ones[1:5000], function(x){
  anchor <- x
  positive <- sample(setdiff(ones, x), 1)
  negative <- sample(twos, 1)
  c(anchor, positive, negative)
}))

tmp_two <- do.call(rbind, lapply(twos[1:5000], function(x){
  anchor <- x
  positive <- sample(setdiff(twos, x), 1)
  negative <- sample(ones, 1)
  c(anchor, positive, negative)
}))

triplets <- rbind(tmp_one, tmp_two)
triplets <- triplets[sample(1:nrow(triplets)), ]

Next, I tried to adapt your implementation to my scenario:

# Define keras triplet model ####
loss_identity <- function(y_true, y_pred) k_mean(y_pred - 0 * y_true)

# margin-based triplet loss 
loss_margin_triplet <- function(x) {
  embed_user <- x[[1]]
  embed_item_positive <- x[[2]]
  embed_item_negative <- x[[3]]
  
  loss <- k_maximum(
    0.0,
    k_sum(embed_user * embed_item_negative, axis = -1, keepdims = TRUE) -
      k_sum(embed_user * embed_item_positive, axis = -1, keepdims = TRUE) +
      10.0
  )
  
  loss
}

# input layers for items (positive and negative)
input_item_anchor <- layer_input(shape = c(784), name = "input_item_anchor")
input_item_positive <- layer_input(shape = c(784), name = "input_item_positive")
input_item_negative <- layer_input(shape = c(784), name = "input_item_negative")
  
layer_embed_item <- layer_embedding(
  input_dim = 10000, output_dim = 2, name = "embed_item")

embed_item_anchor <- input_item_anchor %>%
  layer_embed_item() %>%
  layer_flatten()
embed_item_positive <- input_item_positive %>%
  layer_embed_item() %>%
  layer_flatten()
embed_item_negative <- input_item_negative %>%
  layer_embed_item() %>%
  layer_flatten()
  
# margin-based triplet loss is the output
loss <- list(embed_item_anchor, embed_item_positive, embed_item_negative) %>%
  layer_lambda(loss_margin_triplet, output_shape = c(2))
  
# define model inputs/outputs
model <- keras_model(
  inputs = c(input_item_anchor, input_item_positive, input_item_negative),
  outputs = loss
)

model %>% compile(loss = loss_identity, optimizer = optimizer_nadam())

However, I am not quite sure how to continue. Why do we use the loss_identity() function in the compilation step? Dont we want the triplet loss?
Also, how would I add another classification layer to the network?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.