tensorflow學習1
1、tf.concat()
語法:tf.concat(concat_dim, data)
- 進行降維 3D -> 2D, 2D -> 1D
- 例子:
#tf.concat()
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
# 將t1, t2進行concat,axis為0,等價於將shape=[2, 2, 3]的Tensor concat成
#shape=[4, 3]的tensor。在新生成的Tensor中tensor[:2,:]代表之前的t1
#tensor[2:,:]是之前的t2
tf.concat(0 , [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# 將t1, t2進行concat,axis為1,等價於將shape=[2, 2, 3]的Tensor concat成
#shape=[2, 6]的tensor。在新生成的Tensor中tensor[:,:3]代表之前的t1
#tensor[:,3:]是之前的t2
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
# 類比numpy的concatenate
arr = np.zeros([2 ,3,4,5,6])
print(arr2.shape)
# Out: (2, 3, 4, 5)
print(np.concatenate(arr2, 0).shape)
# Out: (6, 4, 5) <—— (2*3, 4, 5)
print(np.concatenate(arr2, 1).shape)
# out: (3, 8, 5) <——(3, 2*4, 5)
2、tf.squeeze()
語法:tf.squeeze(input, squeeze_dims=None, name=None)
從tensor中刪除所有大小是1的維度。
給定張量輸入,此操作返回相同型別的張量,並刪除所有尺寸為1的尺寸。 如果不想刪除所有尺寸1尺寸,可以通過指定squeeze_dims來刪除特定尺寸1尺寸。
- 輸入:張量。
- squeeze_dims:可選的ints列表。 預設為[]。 如果指定,只能擠壓列出的尺寸。 維度索引從0開始。擠壓不是1的維度是一個錯誤。
- 名稱:操作的名稱(可選)。
- 例子:
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions:
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
3、tf.split()
語法:tf.split(split_dim, num_split, value, name=’split’)
對輸入張量 value 進行切片,split_dim為切片維度,num_split為切片個數
- 例子:
# 'value' is a tensor with shape [5, 30]
# Split 'value' into 3 tensors along dimension 1
split0, split1, split2 = tf.split(1, 3, value)
# tf.shape(split0) ==> [5, 10]
4、emdeding
語法:tf.nn.embedding_lookup(params, ids, partition_strategy=’mod’, name=None, validate_indices=True, max_norm=None)
- 該函式用於對params 中的張量list進行並行查詢
- ids: A Tensor with type int32 or int64 containing the ids to be looked up in params.
- The results of the lookup are concatenated into a dense tensor. The returned tensor has shape shape(ids) + shape(params)[1:].
- 例子:
mat = np.array([1,2,3,4,5,6,7,8,9]).reshape((3,-1))
ids = [[1,2], [0,1]]
res = tf.nn.embedding_lookup(mat, ids)
res.eval()
# 輸出:array([[[4, 5, 6],
# [7, 8, 9]],
#
# [[1, 2, 3],
# [4, 5, 6]]])
# 例2
import tensorflow as tf
import numpy as np
a = [[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]]
a = np.asarray(a)
idx1 = tf.Variable([0, 2, 3, 1], tf.int32)
idx2 = tf.Variable([[0, 2, 3, 1], [4, 0, 2, 2]], tf.int32)
out1 = tf.nn.embedding_lookup(a, idx1)
out2 = tf.nn.embedding_lookup(a, idx2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print sess.run(out1)
print out1
print '=================='
print sess.run(out2)
print out2
# 輸出:
# [[ 0.1 0.2 0.3]
# [ 2.1 2.2 2.3]
# [ 3.1 3.2 3.3]
# [ 1.1 1.2 1.3]]
# Tensor("embedding_lookup:0", shape=(4, 3), dtype=float64)
# ==================
# [[[ 0.1 0.2 0.3]
# [ 2.1 2.2 2.3]
# [ 3.1 3.2 3.3]
# [ 1.1 1.2 1.3]]
#
# [[ 4.1 4.2 4.3]
# [ 0.1 0.2 0.3]
# [ 2.1 2.2 2.3]
# [ 2.1 2.2 2.3]]]
# Tensor("embedding_lookup_1:0", shape=(2, 4, 3), dtype=float64)
5、tf.expand_dims()
語法:tf.expand_dims(input, axis=None, name=None, dim=None)
在第axis位置增加一個維度。如果想用廣播特性的話,經常會用到這個函式;
- 給定張量輸入,此操作在輸入形狀的維度索引軸處插入1的尺寸。 尺寸索引軸從零開始;
- 如果您指定的軸為負數,則從最後向後計數。
- 如果要將批量維度新增到單個元素,此操作非常有用。 例如,如果您有一個單一的形狀[height,width,channels],您可以使用expand_dims(image,0)使其成為1個影象,這將使形狀[1,高度,寬度,通道]。
- 例子:
# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
6、tf.slice()
語法:tf.slice(input_, begin, size, name=None)
- 對輸入tensor 進行切片,begin為切片起點,size 為要切割的尺寸。假設input的shape是[a1, a2, a3], begin的值是[b1, b2, b3],size的值是[s1, s2, s3],那麼tf.slice()返回的值就是 input[b1:b1+s1, b2:b2+s2, b3:b3+s3];如果 si=−1 ,那麼 返回值就是 input[b1:b1+s1,…, bi: ,…],即該維度取值從begin 到最後。
- 例子:
import tensorflow as tf
import numpy as np
sess = tf.InteractiveSession()
a = np.array([[1,2,3,4,5],[4,5,6,7,8],[9,10,11,12,13]])
tf.slice(a,[1,2],[-1,2]).eval()
#array([[ 6, 7],
# [11, 12]])
7、tf.stack()
語法:tf.stack(values, axis=0, name=’stack’)
- 將 a list of R 維的Tensor堆成 R+1維的Tensor。
Given a list of length N of tensors of shape (A, B, C);
if axis == 0 then the output tensor will have the shape (N, A, B, C),這時 res[i,:,:,:] 就是原 list中的第 i 個 tensor; if axis == 1 then the output tensor will have the shape (A, N, B, C),這時 res[:,i,:,:] 就是原list 中的第i 個tensor. - 例子:
x = tf.constant([1,4])
y = tf.constant([2,5])
z = tf.constant([3,6])
tensor1 = tf.stack([x,y,z])
# 輸出為 :[[1,4],[2,5],[3,6]]
tensor2 = tf.stack([x,y,z], axis=1)
# 輸出為:[[1,2,3],[4,5,6]]
8、tf.gather()
語法:tf.gather(params, indices, validate_indices=None, name=None)
- indices must be an integer tensor of any dimension (usually 0-D or 1-D). Produces an output tensor with shape:params.shape[:axis] + indices.shape + params.shape[axis+1:]
- 例子:
# Scalar indices, 會降維
output[:, ..., :] = params[indices, :, ... :]
# Vector indices
output[i, :, ..., :] = params[indices[i], :, ... :]
# Higher rank indices,會升維
output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]
9、tf.pad
語法:tf.pad(tensor, paddings, mode=”CONSTANT”, name=None)
- tensor: 任意shape的tensor,維度 Dn
- paddings: [Dn, 2] 的Tensor, padding 後tensor 的某維上的長度變為padding[D,0] + tensor.dim_size(D) + padding[D,1]
- mode: CONSTANT表示填0, REFLECT表示反射填充,SYMMETRIC表示對稱填充。
總結:
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]], shape=(4,3)
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]]], shape=(2,2,3)
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]]]
tensorflow 兩種型別的抽取:
- 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]]]
相關推薦
TensorFlow學習(1)---TensorFlow基本介紹
什麼是TensorFlow? 先看看來自TensorFlow官方網站的介紹: TensorFlow™ 是一個開放原始碼軟體庫,用於進行高效能數值計算。藉助其靈活的架構,使用者可以輕鬆地將計算工作部署到多種平臺(CPU、GPU、TPU)和裝置(桌面裝置、伺服器叢集、移動
tensorflow學習(1)
TensorFlow是Google開發的一款神經網路的Python外部的結構包, 也是一個採用資料流圖來進行數值計算的開源軟體庫.TensorFlow 讓我們可以先繪製計算結構圖, 也可以稱是一系列可人機互動的計算操作, 然後把編輯好的Python檔案 轉換成 更高效的C++, 並在後端進行計算
tensorflow學習1
1、tf.concat() 語法:tf.concat(concat_dim, data) 進行降維 3D -> 2D, 2D -> 1D 例子: #tf.concat() t1 = [[1, 2, 3], [4, 5, 6]] t2
Tensorflow學習——1 簡單的例子研究
從入職以來一直在慢慢研究深度學習方面的知識,但是大多是在已有模型進行一些簡單的修改,實際動手寫模型成為了很大的困難,所以在此以一些書和部落格為例子,自己手動從開始好好系統學習一下。 1、下面這個例子主要簡單的介紹了graph和reuse=true的不同情況的區
斯坦福CS20SI TensorFlow學習筆記1——graph、session和op
efault constant 例如 sub 否則 我們 vector 安全 出現 graph即tf.Graph(),session即tf.Session(),很多人經常將兩者混淆,其實二者完全不是同一個東西。 graph定義了計算方式,是一些加減乘除等運算的組合,類似於
tensorflow學習筆記(1)-基本語法和前向傳播
pla oba -a 訓練 style lac 好的 ini 神經元 tensorflow學習筆記(1) (1)tf中的圖 圖中就是一個計算圖,一個計算過程。
tensorflow學習筆記(1):sess.run()
原址:https://www.2cto.com/kf/201610/559887.html session.run() session.run([fetch1, fetch2]) import tensorflow as tf state = tf.Variable(0.0,dtype=
Tensorflow 學習筆記(1)
Tensorflow 學習筆記(1) tensorflow:tensor 和 flow。 tensor:張量,多維陣列。(表明資料結構) flow:資料流,表達張量之間通過計算相互轉化的過程。 TensorFlow 是一個通過計算圖的形式來表述計算的程式設計系統 。TensorFlow中的每
TensorFlow學習之路1-TensorFlow介紹
TensorFlow是一個採用資料流圖(data flow graphs),用於資料計算的開源軟體庫。 什麼是資料流圖? TensorFlow的資料流圖是由“節點”(nodes)和“線”(edges)組成的有向無環圖來描述數學計算。“節點”一般用來表示施加的數學操作,但也可以表示資料輸入(feed in)
【學習筆記】Hands-on ML with sklearn&tensorflow [TF] [1]模型的訓練、儲存和載入
本篇內容:一個簡單的預測模型的建立、訓練、儲存和載入。 匯入必要模組: import numpy as np import pandas as pd import tensorflow as tf import ssl #解決資料來源網站簽名認證失敗的問題 from sklearn.data
Ubuntu18.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook深度學習環境配置
uri onf 設備 har import strong sof 比較 理論 目錄 一、Ubuntu18.04 LTS系統的安裝 1. 安裝文件下載 2. 制作U盤安裝鏡像文件 3. 開始安裝 二、設置軟件源的國內鏡像 1. 設置方法 2.關於ubuntu鏡像的小知識
Tensorflow學習第1課——從本地載入MNIST以及FashionMNIST資料
很多Tensorflow第一課的教程都是使用MNIST或者FashionMNIST資料集作為示例資料集,但是其給的例程基本都是從網路上用load_data函式直接載入,該函式封裝程度比較高,如果網路出現問題,資料集很難實時從網上下載(筆者就多次遇到這種問題,忍無可忍),而且資料是如何解碼的也一無所知,不利於後
TensorFlow學習筆記(1)—— MNIST手寫識別
1、初步學習 資料處理 xs:60000張圖片,28*28大小,將所有畫素點按一列排列,資料集變為了[60000, 784]的二維矩陣。 ys:60000張圖片,每個圖片有一個標籤標識圖片中數字,採用one-hot向量,資料集變為[60000, 10]的二維矩陣。 softm
tensorflow RNN 學習1,入門
終於,我可以開始寫我的學習記錄了。度過了懵比時期,從啥都不知道,變成知道了一些些,很開心。 現在記錄一下,自己寫的一個簡單的RNN例子,自我總價,加深理解。 因為自己學的不深,為了避免誤導,這裡不做下定義,僅描述。詳細可以參考其他文章。 本文的目的:能夠使用Tensor
誠信線上私網申博包殺網 Tensorflow學習筆記1:Get Started
█直接聯絡電話:18475600009█微信:18475600009 Tensorflow學習筆記1:Get Started 關於Tensorflow的基本介紹 Tensorflow是一個基於圖的計算系統,其主要應用於機器學習。 從Tensorflow名字的字面意思可以拆分成兩部
tensorflow學習筆記1:影象資料的一些簡單操作
博主學習TensorFlow不久,學習路上也是遇到不少問題。所以決定寫一個系列的學習筆記,算是記錄下學習歷程,方便以後翻閱。當然如果可以幫助到一些新手的話就更好了,高手請繞道。 1.影象資料的採集: &nbs
1-tensorflow學習筆記-會話
會話 會話擁有並管理TensorFlow程式執行時的所有資源 當所有計算完成之後要關閉會話,幫助系統回收資源 import tensorflow as tf # 定義計算圖 tens1 = tf.constant([1,2,3]) # 建立一個會話 sess = tf.Sess
TensorFlow學習筆記(1) TensorFlow入門
一、TensorFlow計算模型——計算圖(Computational Graph) TensorFlow——Tensor和Flow。Tensor意為張量,可以理解為多維陣列。Flow意為流,表達了張量之間通過計算相互轉化的過程。TensorFlow中每一個計算都是計算圖上的一個節點,節點之間的
TensorFlow學習筆記#1
TensorFlow Learning Notes #1 基礎 1.Tensors are the primary data structure that TensorFlow uses to operate on the computational graph. #Create
2- 1-tensorflow學習筆記-TensorBoard
TensorBoard:TensorFlow的視覺化工具 TensorBoard是TensorFlow的視覺化工具 通過TensorFlow程式執行過程中輸出的日誌檔案視覺化TensorFlow程式 的執行狀態 TensorBoard和TensorFlow程式