Comments (23)
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.
To convert a tensor to numpy array, you have to run:
array = your_tensor.eval(session=your_session)
from tensorflow-examples.
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.
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.
from tensorflow-examples.
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.
@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.
Resolved Thanks.
from tensorflow-examples.
Hi!
I have the same problem. @fmigas did you find any solution?
from tensorflow-examples.
from tensorflow-examples.
@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.
from tensorflow-examples.
Hi,@b18arundhati did you find any solution regarding this. I have the same environment in Keras.
from tensorflow-examples.
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.
@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.
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.
@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.
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.
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.
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.
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.
@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.
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)
- 404 not found
- Please provide a example for stacked bidirectional LSTM for Tensorflow 2.x
- [Potential NAN bug] Loss may become NAN during training HOT 1
- Tensor
- How to get prediction code ?
- fixes for Word2Vec for Python 3
- ml_introduction.ipynb Links
- InternalError: Dst tensor is not initialized. [[{{node IteratorGetNext/_2}}]] [Op:__inference_distributed_function_24557]
- In the tf1 example: I replace the weigtht and bias with tf.layers.dense, I found the accuracy decrease. why??? HOT 1
- The CNN example diagram shows 3 conv & pooling layers but the implementation only has 2
- AttributeError on placeholder HOT 1
- fig
- In K-Means Example, when i am running "from tensorflow.contrib.factorization import KMeans" line, i am getting an error "ModuleNotFoundError: No module named 'tensorflow.contrib'" HOT 2
- possible issue at: tensorflow_v2/notebooks/3_NeuralNetworks/autoencoder.ipynb HOT 6
- Add a development container HOT 3
- Ikvvh
- TPU Usage
- Activities HOT 3
- Offo HOT 3
- How to autotune simple MLP onnx model on hand dataset ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tensorflow-examples.