TensorFlow學習筆記(一):數據操作指南


擴充 TensorFlow tf.tile

對數據進行擴充操作

import tensorflow as tf
temp = tf.tile([1,2,3],[2])
temp2 = tf.tile([[1,2],[3,4],[5,6]],[2,3])
with tf.Session() as sess:
    print(sess.run(temp))
    print(sess.run(temp2))
[1 2 3 1 2 3]

[[1 2 1 2 1 2] 
[3 4 3 4 3 4] 
[5 6 5 6 5 6] 
[1 2 1 2 1 2] 
[3 4 3 4 3 4] 
[5 6 5 6 5 6]]

 

拼接  tf.concat(values, axis, name='concat')  tf.stack(values, axis=0, name='stack')

 

TensorFlow提供兩種類型的拼接:

  • tf.concat(values, axis, name='concat'):按照指定的已經存在的軸進行拼接

  • tf.stack(values, axis=0, name='stack'):按照指定的新建的軸進行拼接

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
tf.stack([t1, t2], 0)  ==> [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
tf.stack([t1, t2], 1)  ==> [[[1, 2, 3], [7, 8, 9]], [[4, 5, 6], [10, 11, 12]]]
tf.stack([t1, t2], 2)  ==> [[[1, 7], [2, 8], [3, 9]], [[4, 10], [5, 11], [6, 12]]]
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) # [2,3] + [2,3] ==> [4, 3] tf.concat([t1, t2], 1) # [2,3] + [2,3] ==> [2, 6] tf.stack([t1, t2], 0) # [2,3] + [2,3] ==> [2*,2,3] tf.stack([t1, t2], 1) # [2,3] + [2,3] ==> [2,2*,3] tf.stack([t1, t2], 2) # [2,3] + [2,3] ==> [2,3,2*]

 

抽取 tf.slice()  tf.gater()

  • tf.slice(input_, begin, size, name=None):按照指定的下標范圍抽取連續區域的子集

  • tf.gather(params, indices, validate_indices=None, name=None):按照指定的下標集合從axis=0中抽取子集,適合抽取不連續區域的子集

input = [[[1, 1, 1], [2, 2, 2]],
         [[3, 3, 3], [4, 4, 4]],
         [[5, 5, 5], [6, 6, 6]]]
tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],
                                            [4, 4, 4]]]
tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],
                                           [[5, 5, 5]]]
                                           
tf.gather(input, [0, 2]) ==> [[[1, 1, 1], [2, 2, 2]],
                              [[5, 5, 5], [6, 6, 6]]]

假設我們要從input中抽取[[[3, 3, 3]]],這個輸出在inputaxis=0的下標是1,axis=1的下標是0,axis=2的下標是0-2,所以begin=[1,0,0]size=[1,1,3]

假設我們要從input中抽取[[[3, 3, 3], [4, 4, 4]]],這個輸出在inputaxis=0的下標是1,axis=1的下標是0-1,axis=2的下標是0-2,所以begin=[1,0,0]size=[1,2,3]

假設我們要從input中抽取[[[3, 3, 3], [5, 5, 5]]],這個輸出在inputaxis=0的下標是1-2,axis=1的下標是0,axis=2的下標是0-2,所以begin=[1,0,0]size=[2,1,3]

假設我們要從input中抽取[[[1, 1, 1], [2, 2, 2]],[[5, 5, 5], [6, 6, 6]]],這個輸出在input的axis=0的下標是[0, 2],不連續,可以用tf.gather抽取。

類型轉化 tf.cast()

tf.cast(x, dtype, name=None):轉化為dtype指定的類型
tf.to_double(x, name='ToDouble'):轉化為tf.float64
tf.to_float(x, name='ToFloat'):轉化為tf.float32
tf.to_int32(x, name='ToInt32'):轉化為tf.int32
tf.to_int64(x, name='ToInt64'):轉化為tf.int64

 

形狀轉化 tf.reshape()

tf.reshape(tensor, shape, name=None)

 

自定義 op tf.py_func()

通過tf.py_func(func, inp, Tout, stateful=True, name=None)可以將任意的python函數func轉變為TensorFlow op。

func接收的輸入必須是numpy array,可以接受多個輸入參數;輸出也是numpy array,也可以有多個輸出。inp傳入輸入值,Tout指定輸出的基本數據類型。

先看一個解析json的例子,輸入是一個json array,輸出是一個特征矩陣。

import tensorflow as tf
import numpy as np
import json

json_str_1 = '''
{"name": "shuiping.chen",
"score": 95,
"department": "industrial engineering",
"rank": 2
}
'''
json_str_2 = '''
{"name": "zhuibing.dan",
"score": 87,
"department": "production engineering",
"rank": 4
}
'''

input_array = np.array([json_str_1, json_str_2])

def parse_json(json_str_array):
    fea_dict_array = [ json.loads(item) for item in json_str_array ]
    ret_feature = []
    for fea_dict in fea_dict_array:
        feature = [fea_dict["score"], fea_dict["rank"]]
        ret_feature.append(feature)
    return np.array(ret_feature, dtype=np.float32)

parse_json_op = tf.py_func(parse_json, [input_array], tf.float32)
sess = tf.Session()
print sess.run(parse_json_op)

再看一個多輸入多輸出的例子,輸入兩個numpy array,輸出三個array,分別是和、差、乘積

array1 = np.array([[1, 2], [3, 4]], dtype=np.float32)
array2 = np.array([[5, 6], [7, 8]], dtype=np.float32)

def add_minus_dot(array1, array2):
    return array1 + array2, array1 - array2, np.dot(array1, array2)

add_minus_dot_op = tf.py_func(add_minus_dot, [array1, array2], [tf.float32, tf.float32, tf.float32])
print sess.run(add_minus_dot_op)
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM