import tensorflow as tf
import numpy as ny
if name == ‘main’:
array_0tank = 5
a_t = tf.constant(array_0tank)
print('a_t is 0 tank that is a scalar: ', a_t)
array_1tank_1 = [2]
b_t = tf.constant(array_1tank_1)
print('b_t is 1 tank that is a vector: ', b_t)
array_1tank_2 = [2, 3, 4]
c_t = tf.constant(array_1tank_2)
print('c_t is 1 tank that is a vector: ', c_t)
array_2tank = [
[2,3],
[4,5]
]
d_t = tf.constant(array_2tank)
print('d_t is 2 tank that is a matrix: ', d_t)
array_3tank = [
[
[1,2,3],
[4,5,6]
],
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6]
]
]
e_t = tf.constant(array_3tank)
print("et is 3 tank that is a rgb pic data: ", e_t)
array_4tank = [
[
[
[1,2,3],
[4,5,6]
],
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6]
]
],
[
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6]
]
]
]
f_t = tf.constant(array_4tank)
print("et is 4 tank that is multiple rgb pic datas: ", f_t)
运行结果:
a_t is 0 tank that is a scalar: tf.Tensor(5, shape=(), dtype=int32)
b_t is 1 tank that is a vector: tf.Tensor([2], shape=(1,), dtype=int32)
c_t is 1 tank that is a vector: tf.Tensor([2 3 4], shape=(3,), dtype=int32)
d_t is 2 tank that is a matrix: tf.Tensor(
[[2 3]
[4 5]], shape=(2, 2), dtype=int32)
et is 3 tank that is a rgb pic data: tf.Tensor(
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]], shape=(3, 2, 3), dtype=int32)
et is 4 tank that is multiple rgb pic datas: tf.Tensor(
[[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]], shape=(2, 3, 2, 3), dtype=int32)
看张量阶数说明
shape=(2, 3, 2, 3) 四个数字就是四节,既四维,多张三通道图片
shape=(3, 2, 3) 三个数字就是三节,既三维,一张三通道图片
shape=(2, 2) 二个数字就是二节,既二维,矩阵单通道图片
shape=(1,) 一个数字就是一节,既一维,向量
shape=() 零个数字就是零节,既零维,标量
也可以通过最外层到最内层括号 ] 的个数判断是几节