1. 程式人生 > >Tensorflow 中資料轉換,連線操作

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用法相同"""