问题描述
我正在使用R Keras.我可以通过执行以下命令获取中间层的输出:
I am using R Keras. I can obtain the output of an intermediate layer by for example executing:
layer_output <- get_layer(mymodel, index=1)$output
其中,mymodel是Keras模型.问题在于layer_output是张量.
where mymodel is a Keras model.The problem is that layer_output is a tensor.
class(layer_output)
[1] "tensorflow.tensor" "tensorflow.python.framework.ops.Tensor"
[3] "tensorflow.python.framework.ops._TensorLike" "python.builtin.object"
我想将layer_output转换为R对象,例如数组或矩阵,但是我找不到解决方法.非常感谢您能给我建议.
I want to convert layer_output to an R object such as an array or matrix, but I can't find a way to do it. Many thanks if you can advise me.
推荐答案
一个选择是创建一个新模型,该模型将仅输出您感兴趣的图层.
One option is to create a new model which will only output your layer of interest.
首先创建原始模型:
model <- ... # create original model
然后创建新模型并使用预测获取输出:
Then create the new model and use predict to get the output:
layer_name <- 'my_layer'
intermediate_layer_model <- keras_model(inputs = model$input,
outputs = get_layer(model, layer_name)$output)
intermediate_output <- predict(intermediate_layer_model, data)
根据模型,R keras预测函数应返回向量,矩阵或数组-而不是get_layer返回的图层实例.
Depending on your model the R keras predict function should return a vector, matrix or array - instead of the layer instance returned by get_layer.
这篇关于R Keras:将张量流张量转换为R数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!