简体   繁体   中英

How to access elements in a tensor?

I made a neural network for regression purposes, but when I try to get the predictions, it shows something like this:

tf.Tensor 'sequential_5_5/dense_11/BiasAdd:0' shape=(1500, 1) dtype=float64

How can I access those 1500 values?

A tensor is a state of data that describes its type ( int , float ...) and shape (one-dimensional array, two-dimensional and so on).
When you try to access a tensor, you basically get a description of its data-type, and not the data itself.
In order to see the value of the tensor you have, which is an array of length 1500 of floats, you need to use tf.print() , which will output the entire tensor array to the screen:

import tensorflow as tf

tensor = tf.range(10)
print(tensor)
tf.print(tensor)

output:

>>> tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]

In order to extract a tensor's value into a numpy array do this:

tensor = tf.range(10)
array = tensor.numpy()
print(array)

output:

>>> [1 2 3 4 5 6 7 8 9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM