《tensorflow的基本知识》

import tensorflow as tf

a = tf.constant([[0, 1, 2, 3], [4, 5, 6, 7]], dtype=tf.float32)
a_rank = tf.rank(a)  # 获取张量的秩
a_shape = tf.shape(a)  # 获取张量的形状
b = tf.reshape(a, [4, 2])  # 对张量进行重构
# 运行会话以显示结果
with tf.Session() as sess:
    print('constant tensor:\n {}'.format(sess.run(a)))
    print('the rank of tensor: {}'.format(sess.run(a_rank)))
    print('the shape of tensor: {}'.format(sess.run(a_shape)))
    print('reshaped tensor:\n {}'.format(sess.run(b)))
    # 对张量进行切片
    print("tensor's first column: {}".format(sess.run(a[:, 0])))
    print("tensor's second column: {}".format(sess.run(a[:, 1])))
    print("tensor's second row: {}".format(sess.run(a[1, :])))

运行结果:

constant tensor:
 [[0. 1. 2. 3.]
 [4. 5. 6. 7.]]
the rank of tensor: 2
the shape of tensor: [2 4]
reshaped tensor:
 [[0. 1.]
 [2. 3.]
 [4. 5.]
 [6. 7.]]
tensor's first column: [0. 4.]
tensor's second column: [1. 5.]
tensor's second row: [4. 5. 6. 7.]
其中对张量进行切片的操作要看清!!!这里的切片是垂直切的!当然可以水平切!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值