Loading

Tensorflow笔记_第一课

1 笔记1

1.1 重要流程与原理

  1. 求梯度流程
    1) 在with中使用tf.GradientTape()生成可计算梯度的对象
    2) 使用tensor构建loss
    3) 使用1)的对象计算梯度

1.2 关键api

  • tf.Variable(tf.constant(5, dtype=tf.float32))
    生成可被追踪梯度的变量.
  • tf.reduce_sum() 在指定维度上计算元素和. 例:
import tensorflow as tf

x = tf.constant([[1, 2, 3], [2, 2, 3]])
print("sum of all x:", tf.reduce_sum(x, axis=None)) # 不指定axis则求所有元素的和
print("sum of x(column):", tf.reduce_sum(x, axis=0))  # 在第1个维度(行)上求,即求每一列的和
print("sum of x(row):", tf.reduce_sum(x, axis=1))  # 在第2个维度(行)上求,即求每一行的和

'''
sum of all x: tf.Tensor(13, shape=(), dtype=int32)
sum of x(column): tf.Tensor([3 4 6], shape=(3,), dtype=int32)
sum of x(row): tf.Tensor([6 7], shape=(2,), dtype=int32)'''
  • tf.GradientTape(persistent: bool = False)

    参数persistent指示是否可以二次计算梯度.例:
import tensorflow as tf
w = tf.Variable(tf.constant(5, dtype=tf.float32))
lr = 0.18
epoch = 20

for epoch in range(epoch):  # for epoch 定义顶层循环,表示对数据集循环epoch次,此例数据集数据仅有1个w,初始化时候constant赋值为5,循环40次迭代。
    with tf.GradientTape(persistent=True) as tape:  # with结构到grads框起了梯度的计算过程。
        loss = tf.square(w + 1)
        grads = tape.gradient(loss, w)  # 为了求二阶导数,必须写在with内
    grads2 = tape.gradient(grads, w) #二次求导(写不写在with内均可)
    print('grads 2nd:', grads2)
  • tf.GradientTape().gradient(loss, w)

  • tf.convert_to_tensor(value, [dtype]) 可在numpy生成后转成tensor. 例:

import tensorflow as tf
import numpy as np

a = np.arange(0, 5)
b = tf.convert_to_tensor(a, dtype=tf.int64)
print("a:", a)
  • 根据特征向量和标签构建简易数据集
import tensorflow as tf
features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
for element in dataset:
    print(element[0].numpy(), element[1])
#[out:]12 tf.Tensor(0, shape=(), dtype=int32)
  • tf.squeeze(x, [axis]) 移除维度为1的维度. axis可以指定在去除哪些为1的维度,注意必须已经知道为1才可以。例:
shape of y: (1,3), then
tf.squeeze(y) -> shape (3,)
tf.squeeze(y, axis=0) -> shape (3,)
tf.squeeze(y, axis=1) -> error
  • tf.argmax(input: Any, axis: Any = None) 返回指定维度上的最大值处下标.
import tensorflow as tf
import numpy as np
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print("每一列的最大值的索引:", tf.argmax(test, axis=0))  # 返回每一列最大值的索引
print("每一行的最大值的索引", tf.argmax(test, axis=1))  # 返回每一行最大值的索引
posted @ 2021-03-05 18:47  Sherlock-Tang  阅读(43)  评论(0编辑  收藏  举报