TensorFlow2.0创建张量

TensorFlow基础——张量创建

1.1 创建张量

    1. 创建Tensor张量对象
  • tf.constant()函数————创建张量
  • tf.constant(value, dttype, shape)
  • value:数字/Python列表/numpy数组
  • dtype:元素的数据类型
  • shape:张量的形状
  • tf.cast(x, dtype)————张量元素数据类型转换
  • 一般为低精度向高精转换(以防数据溢出,造成数据丢失)
  • 转化布尔型数据时,true为1,false为0
  • 将整型转化为布尔型是0为false,其他均为true
    1. 利用tf.convert_to_tensor(数组/ 列表/数字/布尔值/字符型)创建
  • tf.is_tensor()函数————判断是否为张量
    也可以用isinstance()函数验证
  • 相关代码
import numpy as np
import tensorflow as tf
# 创建张量----1
a = np.array([[1.2, 2.3], [3.5, 4.0]])
tensor = tf.constant(a)
print("*"*50, "\n创建张量:\n", tensor)
# 张量的.numpy()方法
print("*"*50, "\n转化为numpy类型:\n", tensor.numpy())
print("*"*50, "\n类型:\n", type(tensor))
# 数据转换(TensorFlow中一般使用float32就够了,而numpy默认float64,为了计算快速及内存,进行转换)
print("*"*50, "\n改变数据类型:\n", tf.cast(tensor, dtype=tf.float32))
print("*"*50, "\n布尔型转换为整型:\n", tf.cast(tf.constant([True, False, True, False]), dtype=tf.int16))
print("*"*50, "\n整型转化为布尔型:\n", tf.cast(tf.constant([-5, 2, 0, 0, 1]), dtype=tf.bool))
print("*"*50, "\n字符串参数:\n", tf.constant("you and me"))

# 利用tf.convert_to_tensor()创建
print("*"*50, "\n数组创建(数组转化为张量):\n", tf.convert_to_tensor(np.arange(8).reshape(2, 4)))
print("*"*50, "\n判断是否为张量:\n", tf.is_tensor(tf.convert_to_tensor(np.arange(8).reshape(2, 4))))
print("*"*50, "\n用isinstance判断:\n", isinstance(tf.convert_to_tensor(np.arange(8).reshape(2, 4)), tf.Tensor))
print("*"*50, "\n是否是numpy数组:\n", isinstance(tf.convert_to_tensor(np.arange(8).reshape(2, 4)), np.ndarray))
  • 运行结果
************************************************** 
创建张量:
 tf.Tensor(
[[1.2 2.3]
 [3.5 4. ]], shape=(2, 2), dtype=float64)
************************************************** 
转化为numpy类型:
 [[1.2 2.3]
 [3.5 4. ]]
************************************************** 
类型:
 <class 'tensorflow.python.framework.ops.EagerTensor'>
************************************************** 
改变数据类型:
 tf.Tensor(
[[1.2 2.3]
 [3.5 4. ]], shape=(2, 2), dtype=float32)
************************************************** 
布尔型转换为整型:
 tf.Tensor([1 0 1 0], shape=(4,), dtype=int16)
************************************************** 
整型转化为布尔型:
 tf.Tensor([ True  True False False  True], shape=(5,), dtype=bool)
************************************************** 
字符串参数:
 tf.Tensor(b'you and me', shape=(), dtype=string)
************************************************** 
数组创建(数组转化为张量):
 tf.Tensor(
[[0 1 2 3]
 [4 5 6 7]], shape=(2, 4), dtype=int32)
************************************************** 
判断是否为张量:
 True
************************************************** 
用isinstance判断:
 True
************************************************** 
是否是numpy数组:
 False

1.2 创建张量

  • 创建全0,1张量
  • tf.zeros(shape, dtype=tf.float32)
  • tf.ones(shape, dtype=tf.float32)
  • 系统默认数据类型为float32,可以对其指定数据类型
  • tf.fill(dims, value)————创建元素值都相同的张量(也可设置随机数种子,tf.random.set_seed()后数据不改变)
  • dims 维度(形状,元素个数)
  • 元素值
  • 也可以用tf.constant(元素值,shape=[])
  • tf.random.normal(shape, mean, stddev, dtype)————创建随机数张量(正态分布)
  • shape 形状
  • mean 均值
  • stddev 标准差
  • dtype 数据类型,默认float32
  • 当mean和stddev省略时,默认标准正态分布(mean=0,stddev=1)
  • tf.random.truncated_normal(shape, mean, stddev, dtype)————截断的正态分布
  • 返回一个截断的正态分布(截断标准为2倍的标准差)
  • tf.random.uniform(shape, minval, maxval, dtype)————创建均匀分布的张量
  • tf.random.shuffle()————随机打乱函数
  • 打乱0维元素(即对数据集打乱,并不打乱数据属性)
  • tf.range(start, limit, delta=1, dtype)创建序列
  • 相关代码
# 创建张量----2
print("*"*50, "\n全0张量:\n", tf.zeros(shape=(2, 4)))
print("*"*50, "\n全0张量(指定数据类型):\n", tf.zeros([2, 4], dtype=tf.int32))
print("*"*50, "\n全1张量:\n", tf.ones([2, 4]))
print("*"*50, "\n全1张量(指定数据类型):\n", tf.ones(shape=(2, 4), dtype=tf.int32))
print("*"*50, "\n元素值都相同的张量:\n", tf.fill(4, 5))
print("*"*50, "\n元素值都相同的张量:\n", tf.fill([2, 4], 5.0))
print("*"*50, "\n正态分布的张量:\n", tf.random.normal([3, 3], mean=1.0, stddev=2.0))
print("*"*50, "\n截断正态分布的张量:\n", tf.random.truncated_normal([3, 3], mean=1.0, stddev=1.0))
print("*"*50, "\n均匀分布张量:\n", tf.random.uniform([2, 2], minval=0, maxval=10, dtype='int32'))
print("*"*50, "\n将张量随机打乱(只打乱0维元素):\n", tf.random.shuffle(tf.constant([[1, 2], [3, 4], [5, 6]])))
print("*"*50, "\n将张量随机打乱(只打乱0维元素):\n", tf.random.shuffle(np.arange(8)))
print("*"*50, "\n创建序列:\n", tf.range(10))
  • 运行结果
************************************************** 
全0张量:
 tf.Tensor(
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]], shape=(2, 4), dtype=float32)
************************************************** 
全0张量(指定数据类型):
 tf.Tensor(
[[0 0 0 0]
 [0 0 0 0]], shape=(2, 4), dtype=int32)
************************************************** 
全1张量:
 tf.Tensor(
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]], shape=(2, 4), dtype=float32)
************************************************** 
全1张量(指定数据类型):
 tf.Tensor(
[[1 1 1 1]
 [1 1 1 1]], shape=(2, 4), dtype=int32)
************************************************** 
元素值都相同的张量:
 tf.Tensor([5 5 5 5], shape=(4,), dtype=int32)
************************************************** 
元素值都相同的张量:
 tf.Tensor(
[[5. 5. 5. 5.]
 [5. 5. 5. 5.]], shape=(2, 4), dtype=float32)
************************************************** 
正态分布的张量:
 tf.Tensor(
[[ 3.5253894   3.2818058   1.7494308 ]
 [-1.221144    0.10487819  1.6112897 ]
 [ 1.82568     0.3445927  -0.645049  ]], shape=(3, 3), dtype=float32)
************************************************** 
截断正态分布的张量:
 tf.Tensor(
[[ 1.3144032   1.9515564   1.4437459 ]
 [ 1.3548735   1.1765732  -0.17828047]
 [ 2.2532773  -0.58115613  0.71842927]], shape=(3, 3), dtype=float32)
************************************************** 
均匀分布张量:
 tf.Tensor(
[[9 5]
 [6 2]], shape=(2, 2), dtype=int32)
************************************************** 
将张量随机打乱(只打乱0维元素):
 tf.Tensor(
[[3 4]
 [5 6]
 [1 2]], shape=(3, 2), dtype=int32)
************************************************** 
将张量随机打乱(只打乱0维元素):
 tf.Tensor([5 4 3 6 0 2 7 1], shape=(8,), dtype=int32)
************************************************** 
创建序列:
 tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一半不眠次日si记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值