如何从Keras Tensortype切换到自定义图层的numpy数组? [英] How to Switch from Keras Tensortype to numpy array for a custom layer?

查看:1089
本文介绍了如何从Keras Tensortype切换到自定义图层的numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个自定义图层,没有任何权重. 第一步,我尝试实现操纵Kers中输入张量的函数.但是由于许多原因,我没有成功.我的第二种方法是使用numpy操作实现功能,因为我要实现的自定义层没有任何权重,据我的理解,我可以说我可以使用numpy运算,因为我不需要反向传播,因为没有重量吧?然后,我将使用以下命令将图层的输出转换为张量:

So I have a custom layer, that does not have any weights. In a fist step, I tried to implement the functions manipulating the input tensors in Kers. But I did not succeed because of many reasons. My second approach was to implement the functions with numpy operations, since the custom layer I am implementing does not have any weights, from my understanding, I would say, that I could use numpy operarations, as I don't need backpropagation, since there are no weights, right? And then, I would just convert the output of my layer to a tensor with:

 Keras.backend.variable(value = output) 

因此,主要思想是实现一个自定义层,该层需要张量,将它们转换为numpy数组,使用numpy操作对其进行操作,然后将输出转换为张量. 问题在于,我似乎无法使用.eval()将图层的输入张量转换为numpy数组,以便可以使用numpy操作对其进行操作.

So the main idea is to implement a custom layer, that takes tensors, convert them to numpy arrays, operate on them with numpy operations, then convert the output to a tensor. The problem is that I seem not to be able to use .eval() in order to convert the input tensors of my layer into numpy arrays, so that they could be manipulated with numpy operations.

有人可以告诉我如何解决这个问题吗?

Can anybody tell, how I can get around this problem ?

推荐答案

正如丹尼尔·莫勒(DanielMöller)在评论中所提到的,为了计算渐变,Keras需要能够向后传播并通过对于先前的图层.因此,您的图层必须具有差异性.

As mentioned by Daniel Möller in the comments, Keras needs to be able to backpropagate through your layer, in order the calculate the gradients for the previous layers. Your layer needs to be differentiable for this reason.

出于相同的原因,您只能使用Keras操作,因为可以使用autograd自动区分它们.如果您的图层很简单,请查看 Lambda图层,您可以在其中实现自定义快速分层.

For the same reason you can only use Keras operations, as those can be automatically differentiated with autograd. If your layer is something simple, have a look at the Lambda layer where you can implement custom layers quickly.

顺便说一句,Keras后端函数应该涵盖许多用例,因此,如果您坚持通过这些用例编写层,则可能要在此处发布另一个问题.

As an aside, Keras backend functions should cover a lot of use cases, so if you're stuck with writing your layer through those, you might want to post a another question here.

希望这会有所帮助.

这篇关于如何从Keras Tensortype切换到自定义图层的numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆