栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

01-TensorFlow 基本概念与常见函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

01-TensorFlow 基本概念与常见函数

1.数据类型

TensorFlow主要有以下几种数据类型

2.张量 1.张量定义

TensorFlow 中的 Tensor 表示张量,是多维数组、多维列表,用阶表示张量的维数。

0 阶张量叫做标量,表示的是一个单独的数,如 1 2 3;1 阶张量叫作向量,表示的是一个一维数组如[1,2,3];2 阶张量叫作矩阵,表示的是一个二维数组,它可以有 i 行 j
列个元素,每个元素用它的行号和列号共同索引到,如在[[1,2,3],[4,5,6],[7,8,9]]中,2 的索引即为第 0 行第 1 列。张量的阶数与方括号的数量相同,0 个方括号即为 0 阶张量,1 个方括号即为 1 阶张量。故张量可以表示0 阶到 n 阶的数组。 2.创建张量

tf.constant(张量内容,dtype=数据类型(可选))

import tensorflow as tf

if __name__ == '__main__':
    a = tf.constant([1, 5], dtype=tf.int64)
    print(a)
    print(a.dtype)
    print(a.shape)

运行结果

3.NumPy

很多时候, 数据格式是NumPy给出的.
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

比如可通过 reshape 的方式得到更高维度数组,举例如下:

import numpy as np

if __name__ == '__main__':
    c = np.arange(24).reshape(2, 4, 3) #2*4*3的数组
    print(c)

运行结果

通常我们需要将numpy的数据类型转换为Tensor数据类型
tf. convert_to_tensor(数据名,dtype=数据类型(可选))

import tensorflow as tf
import numpy as np

if __name__ == '__main__':
    a = np.arange(0, 5)
    b = tf.convert_to_tensor(a, dtype=tf.int64)
    print(a)
    print(b)

运行结果

3.常用函数 1.张量创建

import tensorflow as tf

if __name__ == '__main__':
    a = tf.zeros([2, 3])    //2行3列的矩阵, 元素全为0
    b = tf.ones(4)          //4个1的向量
    c = tf.fill([2, 2], 9)  //2行2列的矩阵, 元素全为9
    print(a)
    print(b)
    print(c)

运行结果

2.随机数

a.正态分布

import tensorflow as tf

if __name__ == '__main__':
    d = tf.random.normal([2, 2], mean=0.5, stddev=1)
    print(d)
    e = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1)
    print(e)

运行结果

b.均匀分布

import tensorflow as tf

if __name__ == '__main__':
    f = tf.random.uniform([2, 2], minval=0, maxval=1)
    print(f)

运行结果

3.类型转换

import tensorflow as tf

if __name__ == '__main__':
    x1 = tf.constant([1., 2., 3.], dtype=tf.float64)
    print(x1)
    x2 = tf.cast(x1, tf.int32)
    print(x2)

运行结果

4.最大最小值

import tensorflow as tf

if __name__ == '__main__':
    x1 = tf.constant([1., 2., 3.], dtype=tf.float64)
    print(x1)
    x2 = tf.cast(x1, tf.int32)
    print(x2)
    print(tf.reduce_min(x2),tf.reduce_max(x2))

运行结果

5.axis


import tensorflow as tf

if __name__ == '__main__':
    x = tf.constant([[1, 2, 3],
                     [2, 2, 3]])
    print(x)
    print(tf.reduce_mean(x))
    print(tf.reduce_sum(x, axis=1))

运行结果

6.Variable

import tensorflow as tf

if __name__ == '__main__':
    w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))
    print(w)

运行结果

4.数学运算

1.加减乘除

import tensorflow as tf

if __name__ == '__main__':
    a = tf.ones([1, 3])
    b = tf.fill([1, 3], 3.)
    print(a)
    print(b)
    print(tf.add(a, b))
    print(tf.subtract(a, b))
    print(tf.multiply(a, b))
    print(tf.divide(b, a))

运行结果

2.自减操作

import tensorflow as tf

if __name__ == '__main__':
    w = tf.Variable(4)
    w.assign_sub(1)
    print(w)

运行结果

3.指数运算

import tensorflow as tf

if __name__ == '__main__':
    a = tf.fill([1, 2], 3.)
    print(a)
    print(tf.pow(a, 3))
    print(tf.square(a))
    print(tf.sqrt(a))

运行结果

4.矩阵乘法

import tensorflow as tf

if __name__ == '__main__':
    a = tf.ones([3, 2])
    b = tf.fill([2, 3], 3.)
    print(tf.matmul(a, b))

运行结果

5.梯度计算

import tensorflow as tf

if __name__ == '__main__':
    with tf.GradientTape() as tape:
        w = tf.Variable(tf.constant(3.0))
        loss = tf.pow(w, 2)  # loss=w^2 loss’=2w
        grad = tape.gradient(loss, w)
        print(grad)

运行结果

5.其他函数 1.特征标签配对


运行结果

2.遍历函数

import tensorflow as tf

if __name__ == '__main__':
    seq = ['one', 'two', 'three']
    for i, element in enumerate(seq):
        print(i, element)

运行结果

3.独热编码


import tensorflow as tf

if __name__ == '__main__':
    classes = 3
    labels = tf.constant([1, 0, 2])  # 输入的元素值最小为0,最大为2
    output = tf.one_hot(labels, depth=classes)
    print(output)

运行结果

4.softmax


import tensorflow as tf

if __name__ == '__main__':
    y = tf.constant([1.01, 2.01, -0.66])
    y_pro = tf.nn.softmax(y)
    print("After softmax, y_pro is:", y_pro)

5.argmax

import tensorflow as tf
import numpy as np

if __name__ == '__main__':
    test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
    print(test)
    print(tf.argmax(test, axis=0))  # 返回每一列(经度)最大值的索引
    print(tf.argmax(test, axis=1))  # 返回每一行(纬度)最大值的索引

运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号