Tensorflow 中数据转换,连接操作

1.将普通的数据转换为tensor (tf.constant)

import pandas as pd
import numpy as np
import tensorflow as tf

#定义一个DataFrame类型的数据
data = pd.DataFrame(np.random.uniform(low = 0,high = 10, size = (100,90)),header = None)

#将data的类型转换为tensor
tensor = tf.constant(data)

#输出tensor的类型,注意这里的dtype是64位的浮点数
<tf.Tensor 'Const_14:0' shape=(100, 90) dtype=float64>

#可以通过tf.cast将数据类型转换为32位的浮点型
tf.cast(tensor, dtype = tf.float32)

2.将两个tensor拼接起来(tf.concat)

batch_size = 64
col = 363

#先定义两个占位符
x = tf.placeholder(tf.float64, [batch_size, col], name = 'originalx')
y = tf.placeholder(tf.float64, [batch_size, col], name = 'originaly')

x_y_row = tf.concat([x,y],axis = 1)#1是横向拼接

#输出x_y_row的类型
<tf.Tensor 'concat_2:0' shape=(64, 726) dtype=float64>


x_y_col = tf.concat([x,y],axis = 0)#0是纵向拼接

#输出x_y_col的类型
<tf.Tensor 'concat_3:0' shape=(128, 363) dtype=float64>

"""这里的连接tf.concat和DataFrame类型的连接pd.concat用法相同"""

猜你喜欢

转载自blog.csdn.net/m0_37548423/article/details/84961040