Git Product home page Git Product logo

Comments (23)

jonathand94 avatar jonathand94 commented on April 28, 2024 14

for evaluating a tensor in Keras use Keras backend function "eval()" --> K.eval(my_tensor)
Check for documentation: https://keras.io/backend/

from tensorflow-examples.

aymericdamien avatar aymericdamien commented on April 28, 2024 13

To convert a tensor to numpy array, you have to run:

array = your_tensor.eval(session=your_session)

from tensorflow-examples.

bfonta avatar bfonta commented on April 28, 2024 9

Try the following:

import tensorflow as tf
from tensorflow.python.keras import backend as K
sess = K.get_session()
array = sess.run(your_tensor)

from tensorflow-examples.

fmigas avatar fmigas commented on April 28, 2024 5

Hello,
I have the same problem, but I need to convert a tensor to a numpy arran in keras (without a tensorflow session). Can you help?

from tensorflow-examples.

b18arundhati avatar b18arundhati commented on April 28, 2024 5

from tensorflow-examples.

eavidan avatar eavidan commented on April 28, 2024 2

we are using Tensorflow Serving and I have written the following function to convert the PredictResponse (tensor) back to numpy array if it helps

def predictResponse_into_nparray(response, output_tensor_name):
    dims = response.outputs[output_tensor_name].tensor_shape.dim
    shape = tuple(d.size for d in dims)
    print(shape)
    return np.reshape(response.outputs[output_tensor_name].float_val, shape)

from tensorflow-examples.

emnajaoua avatar emnajaoua commented on April 28, 2024 2

@b18arundhati can you please tell us what is your workaround. I am using tensor objects under keras, I want to convert them to arrays or lists so I can use them as input for another function. the output of that other function is so necessary since I am using it to calculate the loss.
I tried the solutions of @b-fontana @jonathand94 but still I get this error:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,36]
[[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=[?,36], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Any suggestions please ?

from tensorflow-examples.

nthakor avatar nthakor commented on April 28, 2024

Resolved Thanks.

from tensorflow-examples.

b18arundhati avatar b18arundhati commented on April 28, 2024

Hi!
I have the same problem. @fmigas did you find any solution?

from tensorflow-examples.

fmigas avatar fmigas commented on April 28, 2024

from tensorflow-examples.

samensah avatar samensah commented on April 28, 2024

@fmigas @b18arundhati Hi!, did you guys manage to find a solution on the conversion from the tensor to numpy array in Keras? I'm having the same problem.

from tensorflow-examples.

b18arundhati avatar b18arundhati commented on April 28, 2024

from tensorflow-examples.

ParekhVivek09 avatar ParekhVivek09 commented on April 28, 2024

Hi,@b18arundhati did you find any solution regarding this. I have the same environment in Keras.

from tensorflow-examples.

bfonta avatar bfonta commented on April 28, 2024

Can you post a minimal working example, showing the tensor you want to evaluate?
Anyways, depending on how you defined the tensor "your_tensor", you might have to provide additional values for some placeholders. You are probably trying to evaluate a tensor that is not completely defined when you do 'sess.run()'.

from tensorflow-examples.

emnajaoua avatar emnajaoua commented on April 28, 2024

@b-fontana Thank you for your respond. In fact, my case is a bit complicated. I am defining a function that represent a custom loss. This function is using as inputs numpy arrays. So I was trying to evaluate my tensors to be numpy arrays so I can process this function but that was not possible. Because I have to fed an actual value to my tensors for that. a better explanation is in this link keras-team/keras#4075.
Anyway, now I am working on rewriting my custom loss function with keras symbols or operations.

from tensorflow-examples.

ShantanuShinde avatar ShantanuShinde commented on April 28, 2024

To convert a tensor to numpy array, you have to run:

array = your_tensor.eval(session=your_session)

I used this. but the program hangs at the eval(). what is the problem?

from tensorflow-examples.

bfonta avatar bfonta commented on April 28, 2024

@Gameatro:

Try the following:

for evaluating a tensor in Keras use Keras backend function "eval()" --> K.eval(my_tensor)
Check for documentation: https://keras.io/backend/

If this does not work, please paste a minimum working example so that we can reproduce your error.

from tensorflow-examples.

chhetrycse avatar chhetrycse commented on April 28, 2024

def dis(bin_image):

res=distance_transform_cdt(bin_image)

return res

out = Conv2D(1, (1, 1), activation='sigmoid') (c9)

print(out.shape)

#outputs = resize_layer(scale=2)(out, method="dis")

outputs=Lambda(dis)(out)

(?, 256, 256, 1)

ValueError Traceback (most recent call last)
in ()
----> 1 get_net = base_model()

in base_model(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS)
52 print(out.shape)
53 #outputs = resize_layer(scale=2)(out, method="dis")
---> 54 outputs=Lambda(dis)(out)
55
56 model = Model(inputs=[inputs], outputs=[outputs])

/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs)
455 # Actually call the layer,
456 # collecting output(s), mask(s), and shape(s).
--> 457 output = self.call(inputs, **kwargs)
458 output_mask = self.compute_mask(inputs, previous_mask)
459

/usr/local/lib/python3.6/dist-packages/keras/layers/core.py in call(self, inputs, mask)
685 if has_arg(self.function, 'mask'):
686 arguments['mask'] = mask
--> 687 return self.function(inputs, **arguments)
688
689 def compute_mask(self, inputs, mask=None):

in dis(bin_image)
2 def dis(bin_image):
3
----> 4 res=distance_transform_cdt(bin_image)
5 return res

/usr/local/lib/python3.6/dist-packages/scipy/ndimage/morphology.py in distance_transform_cdt(input, metric, return_distances, return_indices, distances, indices)
2009 dt[...] = numpy.where(input, -1, 0).astype(numpy.int32)
2010 else:
-> 2011 dt = numpy.where(input, -1, 0).astype(numpy.int32)
2012
2013 rank = dt.ndim

ValueError: setting an array element with a sequence.

from tensorflow-examples.

jsl303 avatar jsl303 commented on April 28, 2024

Hi Everyone,
I'm trying to implement a custom metric in Keras, and I'm also having the problem to convert Tensor to Numpy.
All of the options below throws the error: "You must feed a value for placeholder tensor 'dense_3_target' with dtype float and shape [?,?]"
Did anyone find a solution yet?

from tensorflow.keras import backend as K
def custom_accuracy(y_true, y_pred):
sess = K.get_session()
y_true_np = sess.run(y_true) #1
y_true_np = y_true.eval(session=sess) #2
y_true_np = K.eval(y_true) #3
y_true_np = K.get_value(y_true) #4

from tensorflow-examples.

ahmedhosny avatar ahmedhosny commented on April 28, 2024

The loss function is part of the graph - I believe everything needs to be implemented using keras backend or tf functions i.e. operate on tensors not numpy arrays.

from tensorflow-examples.

huangjiancong1 avatar huangjiancong1 commented on April 28, 2024

To convert a tensor to numpy array, you have to run:

array = your_tensor.eval(session=your_session)

I think array = your_tensor.eval(session=your_session) can not work if there have different placeholder defined,

there should be define feed_dict={} in some way?

from tensorflow-examples.

agarwala95 avatar agarwala95 commented on April 28, 2024

@b18arundhati can you please tell us what is your workaround. I am using tensor objects under keras, I want to convert them to arrays or lists so I can use them as input for another function. the output of that other function is so necessary since I am using it to calculate the loss.
I tried the solutions of @b-fontana @jonathand94 but still I get this error:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,36]
[[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=[?,36], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Any suggestions please ?

Did you solve this? I have the same problem

from tensorflow-examples.

 avatar commented on April 28, 2024

I have the same problem, but no possible to debug it. I tried to use Keras backend as suggested, but it didn't work.

Here is a MWE:


import numpy  as np 
import tensorflow as tf
import scipy.optimize 


# fixed parameters
kon = 0.01
mu  = 1.5
fi  = 0.5 
kappa = 22
w = 0.63

# varying parameters 
n=10000
x0 = tf.random.normal(shape=(n,), stddev=0.2)
x0 =np.exp(x0)
eps = tf.random.normal(shape=(n,), stddev=0.17)
z = tf.sigmoid(tf.random.normal(shape=(n,), stddev=0.22))

@tf.function 
def get_leisure(z, eps, x0):
    e_eps = tf.exp(eps)
    e_x0 = x0
    c_1=mu/fi * np.ones(n)
    c_2=(1-mu)*kappa * np.ones(n)
    c_3= 1 + (1/fi)
    c_4=mu*np.log(w*e_eps*e_x0/kon)+np.log(z)
    def fun(x):
        return c_1[0] * np.log(x) - c_2[0] * ((x) ** c_3)-c_4
    hvec = scipy.optimize.newton_krylov(fun, 0.5 * np.ones(n))
    return hvec
   
get_leisure(z,eps,x0) 

NotImplementedError: in converted code:

    <ipython-input-65-5bb23bbd74cf>:8 get_leisure  *
        c_4=mu*np.log(w*e_eps*e_x0/kon)+np.log(z)
    /Users/usr/opt/anaconda3/envs/spyder/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:728 __array__
        " array.".format(self.name))

    NotImplementedError: Cannot convert a symbolic Tensor (truediv:0) to a numpy array.

from tensorflow-examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.