The Keras-for-R command get_weights(object) produces a list of different R arrays for all weights of a model. There is a corresponding command set_weights(object) permitting the (re-) insertion of this list of weights into the model, e.g. after some manipulation of the list items. However, since the list items usually are arrays of different shapes, it is not possible e.g. simply to add two lists (of same structure, of course) or to multiply one given list by a common factor.
I know there are tf-commands for these operations in Python, i.e. for manipulating weight tensors in particular. Are there any means to use these commands in Keras for R? As indicated above, I am particularly interested in adding and scaling weight tensors.
Could you give some example of commands you would like to use that are not available?
Tnx for your interest, Daniel. Expressed in pseudo code I would like to do the following operations:
weight_tensor3 <- a*weight_tensor1 + b*weight_tensor2, with constants a,b
All weight_tensors of same shape of course; which means that I should be able to extract the model weights as tensors, too - an operation which, afaik, is presently not available in Keras for R.
If I do, just for example, xxx <- 3*get_weights(model) I get an error message like non-numerical argument for binary operator. I would need to operate on every item (array) of the list get_weights() separately, a rather cumbersome undertaking.
But what tools are available for that in python? Why not using standard list manipulations tools in R, for example:
w <- get_weights(model)
w1 = 0.5
w2 = 0.5
w_new <- purrr::map2(w, w, function(x, y) w1 * x + w2 * y)
set_weights(model, w_new)
Oops... that's really simple - thank you so much, Daniel. I've never used purrr before, although I had the package already installed as a dependency to something else.
What I thought about was to use something I must have seen before in the TF collection of instruments, e.g. tf.math or so. But purrr definitely is the simpler solution, and it's working in R directly.
Thanks again - that solved my problem!
Most helpful comment
But what tools are available for that in python? Why not using standard list manipulations tools in R, for example: