tensorflow基础02--常用函数

1. 强制tensor转换为该数据类型

tf.cast(张量名,dtype=数据类型)

2. 张量最值计算

计算张量维度上的最小值

tf.reduce_min(张量名)

计算张量维度上的最大值

tf.reduce_max(张量名)
import tensorflow as tf

a = tf.constant([3, 4, 5, 6, 7], dtype=tf.float32)
print(a)
b = tf.cast(a, dtype=tf.float64)
print(b)
print(tf.reduce_min(b))
print(tf.reduce_max(b))

运行结果:

tf.Tensor([3. 4. 5. 6. 7.], shape=(5,), dtype=float32)
tf.Tensor([3. 4. 5. 6. 7.], shape=(5,), dtype=float64)
tf.Tensor(3.0, shape=(), dtype=float64)
tf.Tensor(7.0, shape=(), dtype=float64)

3. 张量的轴

axis:在二维张量或者数组中,用axis=0或axis=1控制执行维度。axis=0表示跨行,axis=1表示跨列(0对第一个维度进行操作,1对第二个维度进行操作)

计算张量沿着指定维度的平均值

tf.reduce_mean(张量名,axis=操作轴)

计算沿着指定维度的和

tf.reduce_sum(张量名,axis=操作轴)
import tensorflow as tf

a = tf.constant([[1, 2, 3, 4],
                 [5, 6, 7, 8],
                 [9, 10, 11, 12]])
print(tf.reduce_mean(a))
print(tf.reduce_mean(a, axis=0))
print(tf.reduce_mean(a, axis=1))
print(tf.reduce_sum(a, axis=0))
print(tf.reduce_sum(a, axis=1))
print(tf.reduce_sum(a))

运行结果:

tf.Tensor(6, shape=(), dtype=int32)
tf.Tensor([5 6 7 8], shape=(4,), dtype=int32)
tf.Tensor([ 2  6 10], shape=(3,), dtype=int32)
tf.Tensor([15 18 21 24], shape=(4,), dtype=int32)
tf.Tensor([10 26 42], shape=(3,), dtype=int32)
tf.Tensor(78, shape=(), dtype=int32)

4. Variable

tf.Variable()将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记训练参数。

w = tf.Variable(tf.random.normal([3, 5], mean=0, stddev=1))
# 运行结果
<tf.Variable 'Variable:0' shape=(3, 5) dtype=float32, numpy=
array([[-0.7211323 , -0.42148843, -1.0543721 , -1.1280293 , -1.1020839 ],
       [-0.35192478,  1.0499264 , -0.32882023,  0.8757452 , -0.0855011 ],
       [-1.2367997 , -1.0482794 ,  0.5232124 ,  0.09402131,  0.25787485]],
      dtype=float32)>

5. 常用数学函数

  • 四则运算:tf.add, tf.subtract, tf.multiply, tf.divide
  • 平方、次方、开方: tf.square, tf.pow, tf.sqrt
  • 矩阵乘: tf.matmul

只有维度相同的张量才可以进行四则运算

import tensorflow as tf

a = tf.ones([2, 3])
b = tf.fill([2, 3], 5.)

print(a)
print(b)
print(tf.add(a, b))
print(tf.subtract(a, b))
print(tf.multiply(a, b))
print(tf.divide(a, b))

运行结果:

# a
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]], shape=(2, 3), dtype=float32)

# b
tf.Tensor(
[[5. 5. 5.]
 [5. 5. 5.]], shape=(2, 3), dtype=float32)

# a + b
tf.Tensor(
[[6. 6. 6.]
 [6. 6. 6.]], shape=(2, 3), dtype=float32)

# a - b
tf.Tensor(
[[-4. -4. -4.]
 [-4. -4. -4.]], shape=(2, 3), dtype=float32)

# a * b
tf.Tensor(
[[5. 5. 5.]
 [5. 5. 5.]], shape=(2, 3), dtype=float32)

# a / b
tf.Tensor(
[[0.2 0.2 0.2]
 [0.2 0.2 0.2]], shape=(2, 3), dtype=float32)
import tensorflow as tf

a = tf.fill((3, 4), 5.)

print(a)
print(tf.square(a))
print(tf.pow(a, 3))
print(tf.sqrt(a))

运行结果:

tf.Tensor(
[[25. 25. 25. 25.]
 [25. 25. 25. 25.]
 [25. 25. 25. 25.]], shape=(3, 4), dtype=float32)
tf.Tensor(
[[125. 125. 125. 125.]
 [125. 125. 125. 125.]
 [125. 125. 125. 125.]], shape=(3, 4), dtype=float32)
tf.Tensor(
[[2.236068 2.236068 2.236068 2.236068]
 [2.236068 2.236068 2.236068 2.236068]
 [2.236068 2.236068 2.236068 2.236068]], shape=(3, 4), dtype=float32)
import tensorflow as tf

a = tf.constant([[1, 1, 1, 1],
                 [1, 3, 4, 2],
                 [1, 2, 5, 1]], dtype=tf.float32)
b = tf.fill([4, 3], 3.)

print(a)
print(b)
print(tf.matmul(a, b))

运算结果:

tf.Tensor(
[[1. 1. 1. 1.]
 [1. 3. 4. 2.]
 [1. 2. 5. 1.]], shape=(3, 4), dtype=float32)
tf.Tensor(
[[3. 3. 3.]
 [3. 3. 3.]
 [3. 3. 3.]
 [3. 3. 3.]], shape=(4, 3), dtype=float32)
tf.Tensor(
[[12. 12. 12.]
 [30. 30. 30.]
 [27. 27. 27.]], shape=(3, 3), dtype=float32)

6. tf.data.Dataset.from_tensor_slices

切分传入张量的第一维度,生成输入特征/标签对,构建数据集(兼容numpy格式)

import tensorflow as tf

features = tf.constant([23, 34, 24, 12])
labels = tf.constant([1, 0, 1, 0])

dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(dataset)

for ele in dataset:
    print(ele)

运行结果:

<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>

(<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=34>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=24>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)

7. tf.GradientTape

with结构记录计算过程,gradient求出张量的梯度

with tf.GradientTape() as tape:
      若干计算过程
grad = tape.gradient(函数, 对谁求导)
import tensorflow as tf

with tf.GradientTape() as tape:
    w = tf.Variable(tf.constant(3.0))
    loss = tf.pow(w, 2)
grad = tape.gradient(loss, w)
print(grad)

运行结果:

tf.Tensor(6.0, shape=(), dtype=float32)

8. tf.one_hot

tf.one_hot()函数将待转换数据,转换为one-hot形式的数据输出。

import tensorflow as tf

labels = tf.constant([1, 0, 2])
output = tf.one_hot(labels, depth=3)
print(output)

运行结果:

tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

9. tf.assign_sub

  • 赋值操作,更新参数的值并返回
  • 调用assign_sub前,先用tf.Variable定义变量为可训练(可自更新)
import tensorflow as tf

w = tf.Variable(4)
w.assign_sub(1)
print(w)

运行结果:

<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

10. tf.argmax

返回张量沿着指定维度最大值的索引

tf.argmax(张量名, axis=操作轴)
import numpy as np

a = np.array([[1, 3, 6],
              [2, 5, 8],
              [2, 4, 8],
              [6, 3, 2]])
print(a)
print(tf.argmax(a, axis=0))
print(tf.argmax(a, axis=1))

运行结果:

[[1 3 6]
 [2 5 8]
 [2 4 8]
 [6 3 2]]
tf.Tensor([3 1 1], shape=(3,), dtype=int64)
tf.Tensor([2 2 2 0], shape=(4,), dtype=int64)

tf.argmin同理可得

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355