tensorflow张量详解

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=() 零个数字就是零节,既零维,标量

也可以通过最外层到最内层括号 ] 的个数判断是几节

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值