Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I need to use a numpy function on my output tensor in the loss function. More specifically, my loss function involves finding nearest neighbors, and I need to use the Keras functionality for ckdTree for this purpose. I have tried converting my output tensor to a numpy array using K.eval(). 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.

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?

like image 536
phoenixwing Avatar asked May 17 '17 09:05

phoenixwing


People also ask

Can I use NumPy in TensorFlow?

TensorFlow implements a subset of the NumPy API, available as tf. experimental. numpy . This allows running NumPy code, accelerated by TensorFlow, while also allowing access to all of TensorFlow's APIs.

Can I use NumPy in keras?

Using the TensorFlow NumPy API alongside Keras allows you to easily leverage TensorBoard. The TensorBoard monitor metrics and examine the training curve. The TensorBoard also allows you to explore the computation graph used in your models.

How do you use custom loss function in TensorFlow?

Tensorflow custom loss function numpy To do this task first we will create an array with sample data and find the mean squared value with the numpy() function. Next, we will use the tf. keras. Sequential() function and assign the dense value with input shape.

Do you need NumPy for TensorFlow?

TensorFlow is a reimplementation of the Numpy API and can be accessed as tf. experimental. numpy . Last but not least, TensorFlow is sensitive highly about datatypes used.


1 Answers

The direct using of this numpy function is impossible - as it's not implemented in neither Tensorflow nor Theano. Moreover - there is no a direct correspondence between tensors and arrays. Tensors should be understood as an algebraic variables whereas numpy arrays as numbers. tensor is an abstract thing and applying a numpy functions to it is usually impossible.

But you could still try to reimplement this function on your own using keras.backend functions. Then you'll use the valid tensor operations and no problem should be raised.

like image 180
Marcin Możejko Avatar answered Sep 28 '22 20:09

Marcin Możejko