Comments (6)
Actually you might be able to do it by converting to R vectors and back using the K$eval()
and K$constant()
functions. For example (haven't tried this!):
metric_custom <- function( y_true, y_pred ) {
# convert tensors to R objects
K <- backend()
y_true <- K$eval(y_true)
y_pred <- K$eval(y_pred)
# calculate the metric
metric <- mean( y_true[ y_pred >= quantile(y_pred, .985) ] )
# convert to tensor
K$constant(metric)
}
If this works as expected for you I'll add this as an example to the documentation. Let me know!
from keras.
Hey, thanks for looking at it again.
Compiling with your function above produced an error:
InvalidArgumentError: Shape [-1,-1] has negative dimensions
Edit - is your last line valid? Coercing a scalar to a tensor?
from keras.
Custom metrics operate on tensors so you need to build these expressions using backend tensor functions. For example:
K <- backend()
mean_pred <- function(y_true, y_pred) {
K$mean(y_pred)
}
Docs on available back end functions are here: https://rstudio.github.io/keras/articles/backend.html
Note that even once you get this working the name of the metric won't show up properly in training progress (it will just say "python_function". Working on a change now to remedy this.
from keras.
I've added the ability to provide names for custom metrics and enhanced the docs a bit: file:///Users/jjallaire/packages/keras/docs/reference/metric_binary_accuracy.html#custom-metrics
from keras.
Yeah, I don't see that I'll be able to do it within those confines, BUT, to anyone else who might stumble on this thread - you can always Run an epoch, manually predict on validation, check your metric of interest, loop. Since prediction is slow with keras/TF it'll add a bit of time, but it's worth it in my case.
from keras.
Yes, you can indeed convert a scalar to a tensor, but perhaps Keras is expecting a different type? (there are other arguments to K$constant which can provide type/shape/etc
from keras.
Related Issues (20)
- Trouble with compiling my keras_model_sequential() HOT 7
- RuntimeError: invalid 'type' (environment) of argument HOT 8
- Conflict Arguments Between R and Python: Error in py_call_impl(callable, call_args$unnamed, call_args$named) HOT 20
- ValueError: Only input tensors may be passed as positional arguments HOT 5
- Release keras3 0.2.0
- Release keras 2.15.0
- Erreur dans UseMethod("compile") HOT 7
- Can't load the LSTM model pre-trained in Python to R HOT 1
- Difference between R and Python output HOT 7
- Segmentation models HOT 2
- Lack of methods text_tokenizer(), fit_text_tokenizer() and texts_to_sequences() HOT 5
- compile function does not work HOT 3
- keras layer dense problem HOT 2
- Whe I have tried to fit model I got a problem that I could not understand. Can you look at it?
- Release keras3 0.3.0
- Release keras3 1.0.0
- Support for keras-tcn HOT 1
- GPU is not detected in R, but appears in python. HOT 9
- Link doesn't work HOT 2
- Support Torch for R as backend in keras? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keras.