R interface to Keras: how to debug custom loss function? - r

I'm using keras package in R, and would like to define a custom loss function.
While I was able to find a few examples (one of them), there was nothing on how to debug your own loss function. That is, even if I make it work, how could I verify that it works correctly (e.g. take some y_true and y_pred, and go through the calculations)?
I tried adding a print statement inside the function, but nothing came out.

Related

unrecognized function Nn in R

I am learning R package SimInf to simulate data-driven stochastic epidemiological models. As I was reading the documentation I came across an unrecognized funcion Nn when defining a function for epicurves. Specifically, this line:
j <- sample(seq_len(Nn(model)), 1)
Values of model are integers. My guess is that Nn selects non-negative values, however my R does not recognize this function. From documentation it does not look like they pre-defined Nn either. Can someone please tell if they know what "Nn" is for? Thank you.
A way to go is always taking the package-name and triple-":" it, such that you can find nearly all functions inside the package. Maybe you are familiar with namespacing a function via packageName::functionFrompackageTocall. The packageName::: shows (nearly) all functions defined in this package. If you do this in R-Studio with SimInf:: and SimInf:::, you will see that the latter gives much more functions. But you can only find the functions SimInf:::Nd and SimInf:::Nc, not the Nn-function. Hence you will have to go to the github-sources of the package, in this case https://github.com/stewid/SimInf .Then search for Nn the whole repository. You will see that it seems like it is always an int, but this doesn't help you since you want to get ii as a function, not as a variable. Scrolling further down in the search-results, you will find the NEWS.md-file which mentions The 'Nn' function to determine the number of nodes in a model has been replaced with the S4 method 'n_nodes'. in the https://github.com/stewid/SimInf/blob/fd7eb4a29b82a4a97f64b528bb0e78e5474aa8a5/NEWS.md file under SimInf 8.0.0 (2020-09-13). Hence having a current version of SimInf installed, it shouldn't use the method Nn anymore. If you use it in your code, replace it by n_nodes. If you find it in current package code, you can email the package-maintainer that you found a bug in his code.
TLDR: Nn is an outdated version of n_nodes

Behaviour space in Netlogo crashes when using extension R

I am making a simulation with NetLogo and extension R. I have made a supply chain model, where I have distributors and consumers. Consumers provide orders to distributors and distributors forecast future demand and places orders to suppliers in advance to fulfill market demand. The forecast is implemented with extension R (https://ccl.northwestern.edu/netlogo/docs/r.html) by calling elmNN package. The model works fine when simply using "go".
However, when I want to conduct experiments by using behavior space, I keep getting errors. If I set only a few ticks with behavior space, the model works fine. But when I want to launch a few hundred ticks behavior space keeps crashing. For example, "Extension exception: Error in R-extension: error in eval, operator is invalid for atomic vector", "Extension exception: Error in R-extension: error in eval: cannot have attributes on CHARSXP". Sometimes the behavior simply crashes without any error.
I assume that the errors are related to computability issues between NetLogo, R, R extension and java. I am using NetLogo 5.3.1, 64-bit; R-3.3.3 64-bit; rJava 0.9-8.
Model example: https://www.youtube.com/watch?v=zjQpPBgj0A8
A similar question was posted previously, but it has no answer: NetLogo BehaviorSpace crashing when using R extension
The problem was with programming style, which is not suitable for behavior space. Behavior space supports parallel programming due to which some variables were rewritten by new information in the process. When I set Simultaneous runs in parallel to 1 in the behavior space everything was fine.

I can't get fix() and names() to work (as intended in ISLR) on data set

I'm trying to work on data set Boston from MASS library following ISLR handbook. I am able to load MASS library with library() (I believe I can, because I ? Boston provides description of this data set), however I have problems with next steps.
ISLR is telling me to use
fix(Boston)
names(Boston)
First to learn more about data set, second to get names from it (I was able to perform both on previously used data set "Auto" few chapters before). However it doesn't work for me and when I run it:
fix(Boston) opens a Edit window containing only
function ()
{
}
names(Boston) returns simply NULL.
I read R documentation from stat.ethz.ch about both functions, but I don't know how to relate them to my problem. I wasn't able to use any Q&As I found on stack overflow about either function. From my understanding it seems like Boston isn't treated as data set or something like this, but I don't know where to go with this intuition either.
I'm using sixth printing of ISLR, related pages: 123-124.
If you do data("Boston") after loading the library, it fixes the problem.
The problem might be - R created an empty function Boston() because you might have run fix(Boston) before loading the required libraries.
So run
library(MASS)
library(ISLR)
data("Boston")
fix(Boston)
And everything works fine.

Is it possible to get R intellisense or console output in PowerBI?

I've been trying to get started using R in PowerBI. However, the lack of intellisense and the lack of console output is hindering me. As I can't develop the script in RStudio or in Visual Studio with those aids.
EDIT: PowerBI does a nice thing where you can import data into the application and then work with the drag and drop tools to play around with the data, then when you select data fields that you want to add to an R plot, it creates an R stub that pulls those fields into a data.frame which makes things easy. However, that data is "inside" Power BI, I can't do the same thing in R studio because that data context doesn't exist.
What options are there? Am I missing something?
Thanks.
I think that what you are looking for is R tools for VS. It should make intellisense pick up the context and tell you what you can do with each object. Then for the output, can't you print and check the results in the output window in VS?
This also has R interactive window so when you debug you have a window to place code in, and evaluate it in that context. Say you have a method and you want to debug a statement plot(x, exp(x), type="l", col="green"), instead of making a fix at a time and then re-running to check the results, you can just make the fix say plot(x, exp(x), type="l", col="red") and see how that evaluates. This comes in handy when you wanna try a couple of things and check the results, since you can do it in one go, instead of "making one change and running it" x times.
Let me know if this does the job for you.
The only data context you have in R inside Power BI are Tables, which you pass in parameters of the call of R.Execute. Behind the scene, these Tables will be just dropped on disk as csv and then R process will pick them in order to do whatever you want. Actually this is the only relation between R and PowerBI desktop, if we speak about Transformations using R.
You can easily save such a context from PowerBI using R script of just one function save.image(“filename.RData”), and then open it using load("filename.RData") in your target R development environment.
While you should always test out your R code elsewhere first, it is often not enough for debugging purposes.
for general purpose debugging, you can nest your whole script in a block like this:
out <- capture.ouput({...})
Any intermediate value can be captured in this block with a cat:
cat(intermediate_value_i_want_to_test,'\n')
After your script block is done, simply convert your output to a data.frame and each cat method call will be printed in a new row of out.

Bootstrap output matrix missing

When I try to calculate Gest in spatstat I get the error:
bootstrap output matrix missing.
Does anyone know what am I doing wrong?
I think that "bootstrap output matrix missing" is a fairly generic error, and (unless someone has explicit experience with your case) I would imagine that more information is needed to solve this.
Without more information, I would suggest that you debug the Gest function. You have two good options for that:
1) Use the debug() function:
debug(Gest)
Now run your code. Then you can walk through the Gest function and see where is breaks. Before that point, look at all the environment variables by (for instance) using ls() and see if any assumptions are broken. Presumably something isn't being set correctly.
2) Use recover:
option(error=recover)
Then you will go into browser mode whenever the error occurs, and you can explore the workspace at that point.
This error message does not originate from the spatstat package.
To identify the location of the error, type traceback() immediately after the error has occurred. This will give you a list of the nested commands that were being executed at the time the error occurred. The top one is the location which raised the error.

Resources