TensorFlow编程基础1(小白笔记)(生成张量、类型转换、索引切片、张量变形)

初学TensorFlow小白,一边学算是一边记笔记了

1、生成张量:
(1)tf.constant() 创建一个常量temsor,类型可以是int、float、string等

constant_tensor = tf.constant(value=1.0,dtype=tf.float32,shape=[2,3])
sess = tf.Session()
sess.run(constant_tensor)

结果为:array([[1., 1., 1.],[1., 1., 1.]], dtype=float32)

(2)tf.zeros()创建一个指定dtype类型的全零张量

constant_tensor = tf.zeros(dtype=tf.float32,shape=[2,3])
sess = tf.Session()
sess.run(constant_tensor)

array([[0., 0., 0.], [0., 0., 0.]], dtype=float32)

2、类型转换

(1)tf.to_float()
(2)tf.to_int32()
(3)tf.to_int64()
(4)tf.to_double() 将tensor转为double/float64类型

tensor_before = tf.constant(value=[1.1,1.2,1.3],dtype=tf.float32)
tensor_after = tf.to_double(tensor_before)
sess = tf.Session()
sess.run([tensor_before,tensor_after])

[array([1.1, 1.2, 1.3], dtype=float32), array([1.10000002, 1.20000005, 1.29999995])]

(5)tf.cast() 将tensor转为指定类型的数据

tensor_before = tf.constant(value=[1.1,1.2,1.3],dtype=tf.float32)
tensor_after = tf.cast(tensor_before,dtype=tf.int32)
sess = tf.Session()
sess.run([tensor_before,tensor_after])

[array([1.1, 1.2, 1.3], dtype=float32), array([1, 1, 1])]

3、索引和切片

(1)tfshape() 返回一个tensor维度
(2)tf.size() 返回一个tensor中元素的个数

constant_tensor = tf.constant(value=[[1.1,2.2,3.3],[0.1,0.2,0.3]],dtype=tf.float32)
shape_of_tensor = tf.shape(constant_tensor)
size_of_tensor = tf.size(constant_tensor)
sess = tf.Session()
sess.run([shape_of_tensor,size_of_tensor])

[array([2, 3]), 6]

(3)tf.slice() 按照指定的下标范围对tensor进行切片

constant_tensor = tf.constant(value=[1.1,2.2,3.3],dtype=tf.float32)
tensor_sliced = tf.slice(constant_tensor,begin=[0],size=[1])
sess = tf.Session()
sess.run([tensor_sliced])

[array([1.1], dtype=float32)]

(4)tf.split() 将一个rensor分割成若干小tensor

constant_tensor = tf.constant(value=[[1.1,2.2,3.3],[0.1,0.2,0.3]],dtype=tf.float32)
results = tf.split(constant_tensor,[1,2],axis=1)
sess = tf.Session()
sess.run(results)

[array([[1.1], [0.1]], dtype=float32), array([[2.2, 3.3], [0.2, 0.3]], dtype=float32)]
(5)tf.title() 将一个tensor按指定维度进行复制

tensor_tiled = tf.tile(constant_tensor,[2,1])
sess = tf.Session()
sess.run(tensor_tiled)

array([[1.1, 2.2, 3.3], [0.1, 0.2, 0.3], [1.1, 2.2, 3.3], [0.1, 0.2, 0.3]], dtype=float32)
(6)tf.concat() 合并两个tensor

constant_tensor = tf.constant(value=[[1.1,2.2,3.3],[0.1,0.2,0.3]],dtype=tf.float32)
constant_tensor2 = tf.constant(value=[[1.1,2.2],[0.1,0.2]],dtype=tf.float32)
concated_tensor = tf.concat([constant_tensor,constant_tensor2],axis=1)
sess = tf.Session()
sess.run(concated_tensor)

array([[1.1, 2.2, 3.3, 1.1, 2.2], [0.1, 0.2, 0.3, 0.1, 0.2]], dtype=float32)
(6)tf.transpose()对tenso进行转置

transposed_tesor = tf.transpose(constant_tensor)
sess = tf.Session()
sess.run([constant_tensor,transposed_tesor])

[array([[1.1, 2.2, 3.3], [0.1, 0.2, 0.3]], dtype=float32), array([[1.1, 0.1], [2.2, 0.2], [3.3, 0.3]], dtype=float32)]
4、张量变形

(1)tf.expand_dims() 扩展tensor中的一个维度

(2)tf.squeeze() 压缩tensor中的一个维度

constant_tensor = tf.constant(1,shape=[2,1,3,1])
squeszed_tensor = tf.squeeze(constant_tensor,axis=1)
sess = tf.Session()
sess.run([constant_tensor,squeszed_tensor])

[array([[[[1], [1], [1]]]], [[[1], [1], [1]]]]),array([[[1], [1], [1]], [[1], [1], [1]]])]

(3)tf.reshape() 对tensor的维度进行改变

constant_tensor = tf.constant([1,2,3,4,5,6])
reshaped_shape = tf.reshape(constant_tensor,shape=[2,3])
sess = tf.Session()
sess.run([constant_tensor,reshaped_shape])

[array([1, 2, 3, 4, 5, 6]),array([[1, 2, 3],[4, 5, 6]])]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值