Tensorflow基础 - 01

import tensorflow as tf
import numpy as np

a = 1.2  # python 语言方式创建标量
b = tf.constant(1.2)  # TF 方式创建标量
c = tf.constant([1, 2., 3.3])  # TF方式创建向量
d = tf.constant([[1, 2], [3, 4]])  # TF方式创建矩阵
e = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])  # 创建 3 维张量

# 创建字符串
f = tf.constant('Hello, Deep Learning.') 
g = tf.strings.lower(f)  # 小写化字符串
h = tf.strings.upper(f)  # 大写化字符串

# TensorFlow 的布尔类型和 Python 语言的布尔类型并不等价
i = tf.constant(True)  # 创建布尔类型标量
j = tf.constant([True, False])  # 创建布尔类型向量

# 在创建张量时,可以指定张量的精度,精度过低时会发生溢出
k = tf.constant(123456789, dtype=tf.int16)  # 会溢出
l = tf.constant(123456789, dtype=tf.int32)
m = tf.constant(np.pi, dtype=tf.float64)
n = tf.constant(np.pi, dtype=tf.float32)
# 通过访问张量的 dtype 成员属性可以判断张量的保存精度
# print('before', m.dtype)  # 读取原有张量的数值精度
if m.dtype != tf.float32:  # 如果精度不符合要求,则进行转换
    m = tf.cast(m, tf.float32)  # tf.cast 函数可以完成精度转换
# print('after :', m.dtype)  # 打印转换后的精度

# 类型转换
o = tf.constant(np.pi, dtype=tf.float16)  # 创建 tf.float16 低精度张量
tf.cast(a, tf.double)  # 转换为高精度张量
# 进行类型转换时,需要保证转换操作的合法性,例如将高精度的张量转换为低精度的张量时,可能发生数据溢出隐患
p = tf.constant(np.pi, dtype=tf.float16)  # 创建 tf.float16 低精度张量
# print(p)
p = tf.cast(p, tf.double)  # 转换为高精度张量
# print(p)
q = tf.constant(123456789, dtype=tf.int32)
# print(q)
q = tf.cast(q, tf.int16)  # 转换为低精度整型
# print(q)
r = tf.constant([True, False])
# print(r)
r = tf.cast(r, tf.int32)  # 布尔类型转整型
# print(r)

# 一般默认 0 表示 False,1 表示 True,在 TensorFlow 中,将非 0 数字都视为 True,
s = tf.constant([-1, 0, 1, 2])
# print(s)
s = tf.cast(s, tf.bool)  # 整型转布尔类型
# print(s)


# 待优化张量
t = tf.constant([-1, 0, 1, 2])  # 创建 TF 张量
t = tf.Variable(t)  # 转换为 Variable 类型
# print(t.name, t.trainable)  # Variable 类型张量的属性
# 其中张量的 name 和 trainable 属性是 Variable 特有的属性,name 属性用于命名计算图中的
# 变量,这套命名体系是 TensorFlow 内部维护的,一般不需要用户关注 name 属性;trainable
# 属性表征当前张量是否需要被优化,创建 Variable 对象时是默认启用优化标志,可以设置
# trainable=False 来设置张量不需要优化。
u = tf.Variable([[1, 2], [3, 4]])  # 直接创建 Variable 张量
# print(u)
# 待优化张量可视为普通张量的特殊类型,普通张量其实也可以通过 GradientTape.watch()方法临时加入跟踪梯度信息的列表,从而支持自动求导功能

v = tf.convert_to_tensor([1, 2.])  # 从列表创建张量
w = tf.convert_to_tensor(np.array([[1, 2.], [3, 4]]))  # 从数组中创建张量
# 需要注意的是,Numpy 浮点数数组默认使用 64 位精度保存数据,转换到 Tensor 类型时精度为 tf.float64,可以在需要的时候将其转换为 tf.float32 类型。
x = tf.zeros([])  # 创建全 0的标量
x = tf.ones([])  # 全 1 的标量
x = tf.zeros([1])  # 创建全 0的向量
x = tf.ones([1])  # 全 1 的向量
x = tf.ones([3, 2])  # 创建全 1 矩阵,指定 shape 为 3 行 2 列
x = tf.zeros_like(d)  # 创建一个与 a 形状相同,但是全 0 的新矩阵
x = tf.ones_like(d)
# print(x)
# 除了初始化权威0,或全为1的张量之外,有时也需要全部初始化为某个自定义数值的张量
# tf.fill(shape, value)


# 创建一直分布的张量
y = tf.random.normal([2, 2])  # 创建标准正态分布的张量
y = tf.random.normal([2, 2], mean=1, stddev=2)  # 创建正态分布的张量
y = tf.random.uniform([2, 2])  # 创建采样自[0,1)均匀分布的矩阵
y = tf.random.uniform([2, 2], maxval=10)  # 创建采样自[0,10)均匀分布的矩阵
y = tf.random.uniform([2, 2], maxval=100, dtype=tf.int32)  # 创建采样自[0,100)均匀分布的整型矩阵

# 创建序列   前闭后开
z = tf.range(10)  # 0~10,不包含 10
z = tf.range(10, delta=2)  # 创建 0~10,步长为 2 的整形序列
z = tf.range(1, 10, delta=2)  # 1~10可以创建[start,limit),步长为 delta 的序列,不包含limit 本身

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值