tensorflow實現對張量資料的切片操作方式

2022-09-29 06:03:09 字數 807 閱讀 5319

如下所示:

import tensorflow as tf

a=tfwww.cppcns.comwww.cppcns.com.constant([[[1,2,3,4],[4,5,6,7],[7,8,9,10]],

[[11,12,13,14],[20,21,22,23],[15,16,17,18]]])

print(a.shape)

b,c=tf.split(a,2wmouyvhqb,0) #引數1、張量 2、獲得的切片數 3、切片的維度 將兩個切片分別賦值給b,c

print(b.shape)

print(c程式設計客棧.shape

with tf.session() as sess: #檢視執行結果

print(sess.run(b))

print(sess.run(c))

輸出結果為

(2, 3, 4)

(1, 3, 4)

(1, 3, 4)

[[[ 1 2 3 4]

[ 4 5 6 7]

[ 7 8 9 10]]]

[[[11 12 13 14]

[20 21 22 23]

[15 16 17 18]]]

程式設計客棧

注意到此時b,c均為三維張量資料,若想轉換為二維陣列,可使用tf.reshape命令

d=tf.reshape(b,[3,4])

print(d.shape)

#output

(3, 4)

本文標題: tensorflow實現對張量資料的切片操作方式

本文位址:

tensorflow實現對張量資料的切片操作

import tensorflow as tf a tf.constant 1,2,3,4 4,5,6,7 7,8,9,10 11,12,13,14 20,21,22,23 15,16,17,18 print a.shape b,c tf.split a,2,0 引數1 張量 2 獲得的切片數 3 ...

Tensorflow實戰 張量

import tensorflow as tf tf.constant 是乙個計算,這個計算的結果為乙個張量,儲存在變數a中。a tf.constant 1.0,2.0 name a b tf.constant 2.0,3.0 name b result tf.add a,b,name add pr...

tensorflow 張量生成

coding utf 8 import tensorflow as tf import numpy as np 建立張量 a tf.constant 1 5 dtype tf.int64 print a a print a.dtype a.dtype print a.shape a.shape a ...