[英]How to use numpy functions on a keras tensor in the loss function?
I'm using Keras with TensorFlow backend to build and run a neural network. 我正在将Keras与TensorFlow后端一起使用来构建和运行神经网络。 I need to use a numpy function on my output tensor in the loss function.
我需要在损失函数中的输出张量上使用numpy函数。 More specifically, my loss function involves finding nearest neighbors, and I need to use the Keras functionality for ckdTree for this purpose.
更具体地说,我的损失函数涉及寻找最近的邻居,为此,我需要对ckdTree使用Keras功能。 I have tried converting my output tensor to a numpy array using
K.eval()
. 我尝试使用
K.eval()
将输出张量转换为numpy数组。 However, this throws an InvalidArgument
error when I try to compile the model, I believe, since you can't run eval()
on a symbolic variable. 但是,我相信,当您尝试编译模型时,这会引发
InvalidArgument
错误,因为您无法在符号变量上运行eval()
。
Here's a toy code snippet that reproduces this error. 这是一个再现此错误的玩具代码段。
import numpy as np
from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Reshape
from keras.optimizers import Adam
def loss(y_true, y_pred):
y_pred_numpy = K.eval(y_pred)
# perform some numpy operations on y_pred_numpy
return K.constant(0)
''' Model '''
input_shape = (10,10,10,3)
train_images = np.zeros((1,10,10,10,3))
train_labels = np.zeros((1,1,1,1,3))
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(3000, use_bias=True, bias_initializer='zeros'))
model.add(Reshape((10,10,10,3)))
model.summary()
opt = Adam(lr=1E-4)
model.compile(optimizer=opt, loss=loss)
The above gives the following error: 上面给出了以下错误:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'flatten_3_input' with dtype float
[[Node: flatten_3_input = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
[[Node: reshape_3/Reshape/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_20_reshape_3/Reshape", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
How then do I work with Keras tensors without having to rewrite (complex) numpy functionality using Keras? 然后如何使用Keras张量工作而不必使用Keras重写(复杂)numpy功能?
The direct using of this numpy
function is impossible - as it's not implemented in neither Tensorflow
nor Theano
. 无法直接使用此
numpy
函数-因为Tensorflow
和Theano
均未实现。 Moreover - there is no a direct correspondence between tensors
and arrays
. 而且-
tensors
和arrays
之间没有直接对应关系。 Tensors
should be understood as an algebraic variables whereas numpy
arrays as numbers. Tensors
应理解为代数变量,而numpy
数组应理解为数字。 tensor
is an abstract thing and applying a numpy
functions to it is usually impossible. tensor
是抽象的东西,通常不可能将numpy
函数应用于它。
But you could still try to reimplement this function on your own using keras.backend
functions. 但是您仍然可以尝试使用
keras.backend
函数自行重新实现此功能。 Then you'll use the valid tensor
operations and no problem should be raised. 然后,您将使用有效的
tensor
操作,并且不会出现任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.