tensorflow基础笔记-----张量

# 创建张量
import tensorflow as tf

# 直接创建张量
tf.constant(1.0, dtype=tf.float64)
# numpy创建张量
import numpy as np

tf.constant(np.array([1, 2]))
tf.constant(np.array([1.0, 2.0]))

na = np.arange(12).reshape(3, 4)
ta = tf.convert_to_tensor(na)

# 交换维度
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x, perm=[1, 0])
# 三维数组
a = tf.range(24)
b = tf.reshape(a, [2, 3, 4])
b
tf.transpose(b, (1, 0, 2))

# 拼接个分割
# 拼接
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0)

# 分割
x = tf.range(24)
x = tf.reshape(x, [4, 6])
x
tf.split(x, 2, 0)

# 堆叠
x = tf.constant([1, 2, 3])
y = tf.constant([4, 5, 6])

tf.stack((x,y),axis=0)
tf.stack((x,y),axis=1)

# 分解张量
c = tf.constant([[1, 2, 3],[4,5,6]])
c
tf.unstack(c,axis=0)
tf.unstack(c,axis=1)

 

 

posted @ 2021-07-09 17:09  邓居旺  阅读(41)  评论(0)    收藏  举报