tensor numpy.array panda.dataframe list等数据类型的转换

 

w3c学习网址

TensorFlow将给定值转换为张量

tf.convert_to_tensor

此函数将各种类型的 Python 对象转换为 Tensor 对象.它接受 Tensor 对象,numpy 数组,Python 列表和 Python 标量

convert_to_tensor ( 
    value , 
    dtype = None , 
    name = None , 
    preferred_dtype = None
 )
输入格式类型
import numpy as np

def my_func(arg):
  arg = tf.convert_to_tensor(arg, dtype=tf.float32)
  return tf.matmul(arg, arg) + arg

# The following calls are equivalent.
value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))
value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))

ARGS:

  • value:其类型具有已注册的张量转换函数的对象.
  • dtype:返回的张量的可选元素类型.如果缺少该类型, 则将从值的类型中推断出.
  • name:创建新的张量时要使用的可选名称.
  • preferred_dtype:返回张量的可选元素类型, 当 dtype 为 None 时使用.在某些情况下,调用方在转换为张量时可能没有 dtype,因此 preferred_dtype 可以作为软首选项使用.如果转换为 preferred_dtype 是不可行的,则此参数无效.

返回:

返回基于值的输出.

注意:

  • TypeError:如果没有为值注册转换函数.
  • RuntimeError:如果注册的转换函数返回无效值.

 

 

TensorFlow将给定的对象转换为张量或IndexedSlices

tf.convert_to_tensor_or_indexed_slices

convert_to_tensor_or_indexed_slices ( 
    value , 
    dtype = None , 
    name = None
 )
输入格式

将给定的对象转换为张量或 IndexedSlices.

如果值为 IndexedSlices 或 SparseTensor,则将其原封不动地返回.否则,它将转换为使用 convert_to_tensor () 的张量.

ARGS:

  • value:可由 convert_to_tensor () 使用的 IndexedSlices、SparseTensor 或对象.
  • dtype:(可选)返回的张量或 IndexedSlices 所需的 DType.
  • name:(可选)创建新的张量时要使用的名称.

返回:

基于值的张量、IndexedSlices 或 SparseTensor.

注意:

  • ValueError: 如果 dtype 与值的元素类型不匹配.

TensorFlow将值转换为SparseTensor或张量

tf.convert_to_tensor_or_sparse_tensor

该函数别名:

  • tf.contrib.framework.convert_to_tensor_or_sparse_tensor
  • tf.convert_to_tensor_or_sparse_tensor
convert_to_tensor_or_sparse_tensor ( 
    value , 
    dtype = None , 
    name = None
 )
输入格式

ARGS:

  • value:A SparseTensor,SparseTensorValue或其类型具有注册Tensor转换功能的对象.SparseTensor、SparseTensorValue 或其类型具有已注册的张量转换函数的对象.
  • dtype:返回张量的可选元素类型.如果缺少该类型,则将从值的类型中推断出.
  • name:创建新的张量时要使用的可选名称.

返回:

返回基于值的 SparseTensor 或张量.

注意:

RuntimeError: 如果结果类型与 dtype 不兼容.

tensorflow 中tensor与数组之间的转换

主要是两个方法:
 1.数组转tensor:数组a,  tensor_a=tf.convert_to_tensor(a)
 2.tensor转数组:tensor b, array_b=b.eval()

import tensorflow as tf
import numpy as np

a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print (a)
b=tf.constant(a)

with tf.Session() as sess:
print (b)
for x in b.eval(): #b.eval()就得到tensor的数组形式
print (x)

print ('a是数组',a)

tensor_a=tf.convert_to_tensor(a)
print ('现在转换为tensor了...',tensor_a)

1.1 list 转 numpy

ndarray = np.array(list)

1.2 numpy 转 list

list = ndarray.tolist()

2.1 list 转 torch.Tensor

tensor=torch.Tensor(list)

2.2 torch.Tensor 转 list

先转numpy,后转list

list = tensor.numpy().tolist()

3.1 torch.Tensor 转 numpy

ndarray = tensor.numpy()

*gpu上的tensor不能直接转为numpy

ndarray = tensor.cpu().numpy()

3.2 numpy 转 torch.Tensor

tensor = torch.from_numpy(ndarray)

在我们使用TensorFlow进行深度学习训练时,很多时候都是与Numpy数据打招呼,例如我们csv或者照片数据等。
但是我们都知道,TensorFlow训练时都是使用Tensor来存储变量的,并且网络输出的结果也是Tensor。

一般情况下我们不会感受到Numpy与Tensor之间的区别,因为TensorFlow网络在输入Numpy数据时会自动转换为Tensor来处理。
但是在输出网络时,输出的结果仍为Tensor,当我们要用这些结果去执行只能由Numpy数据来执行的操作时就会出现莫名其妙的错误。

numpy与tensor数据相互转化:

*Numpy2Tensor

虽然TensorFlow网络在输入Numpy数据时会自动转换为Tensor来处理,但是我们自己也可以去显式的转换:
data_tensor= tf.convert_to_tensor(data_numpy)

*Tensor2Numpy
当我们要用这些结果去执行只能由Numpy数据来执行的操作时就会出现莫名其妙的错误。解决方法:

data_numpy = data_tensor.eval()----------------注意这里是t f2的用法,如果tf1.x就需要在前面加上with tf.Session() as sess:

原文地址:https://www.cnblogs.com/xingnie/p/12416373.html