简体   繁体   English

如何访问张量中的元素?

[英]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?如何访问这 1500 个值?

A tensor is a state of data that describes its type ( int , float ...) and shape (one-dimensional array, two-dimensional and so on).张量是一个 state 数据,描述了它的类型intfloat ...)和形状(一维数组、二维等)。
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:为了查看您拥有的张量的值,它是一个长度为 1500 的浮点数组,您需要使用tf.print() ,它将 output 整个张量数组显示在屏幕上:

import tensorflow as tf

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

output: 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:为了将张量的值提取到 numpy 数组中,请执行以下操作:

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

output: output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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