tensorflow张量(tf.Tensor)

概述

tensor,张量

官方 api:
https://www.tensorflow.org/api_docs/python/tf/Tensor

官方对张量的介绍:
https://www.tensorflow.org/guide/tensor?hl=zh-CN

组成

  • 名字
    每个张量都有一个名称,且是唯一
  • 形状
  • 数据类型
    张量中的 元素 的 数据类型 是 相同的

阶(秩)

张量(tensor),相当于numpy中的 n维数组

在张量中,将 维 称之为 阶(秩)

阶(秩) 数据类型
0阶(秩) 常量
1阶(秩) 1维数组(向量)
2阶(秩) 2维数组(矩阵)
n阶(秩) n维数组

tf.Tensor

https://www.malaoshi.top/show_1EF4ZcVklrdc.html

创建张量

tf.constant

https://www.malaoshi.top/show_1EF4ZcXqP8se.html

tf.Variable

https://www.malaoshi.top/show_1EF4ZcqPG0Aq.html

tf.zero()

a = tf.zeros([28, 28, 3])
print(a)
print(a.shape)
print(a.dtype)
print(a.numpy())

tf.one()

切片

与numpy相同

import numpy as np
arr=np.random.randint(0,10,(3,4))
arr
a=tf.Variable(arr)

print(a)
print(a.shape)
print(a.dtype)
print(a.numpy())

# 切片
print(a[0:2,1:3])

改变形状

使用tf.reshape()函数
官方api:
https://www.tensorflow.org/api_docs/python/tf/reshape

import numpy as np
arr=np.random.randint(0,10,(3,4))
tens1=tf.Variable(arr)
tens2=tf.reshape(tens1,shape=(4,3))
print(tens1)
print(tens2)

原文出处:/show_1EF4ZcUuSEBb.html