一、張量
1、什么是張量
張量Tensor和ndarray是有聯(lián)系的,當(dāng)我們print()打印值的時(shí)候,它返回的就是ndarray對(duì)象
TensorFlow的張量就是一個(gè)n維數(shù)組,類型為tf.Tensor。Tensor具有以下兩個(gè)重要的屬性:
(1)type:數(shù)據(jù)類型
(2)shape:形狀(階)
2、張量的類型
張量,在計(jì)算機(jī)當(dāng)中如何存儲(chǔ)?
標(biāo)量,一個(gè)數(shù)字
向量,一維數(shù)組 [2,3,4]
矩陣,二維數(shù)組 [[2,3,4],[2,3,4]]
張量,就是n維數(shù)組
? ? 標(biāo)量,可以看做0階張量
? ? 向量,可以看做1階張量
? ? 矩陣,可以看做2階張量
? ? n維數(shù)組,n階張量
數(shù)據(jù)類型 | python類型 | 描述 |
DT_FLOAT | tf.float32 | 32位浮點(diǎn)數(shù) |
DT_DOUBLE | tf.float64 | 64位浮點(diǎn)數(shù) |
DT_INT64 | tf.int64 | 64位有符號(hào)整數(shù) |
DT_INT32 | tf.int32 | 32位有符號(hào)整數(shù) |
DT_INT16 | tf.int16 | 16位有符號(hào)整數(shù) |
DT_INT8 | tf.int8 | 8位有符號(hào)整數(shù) |
DT_UINT8 | tf.uint8 | 8位無(wú)符號(hào)整數(shù) |
DT_STRING | tf.string | 可變長(zhǎng)度的字節(jié)數(shù)組,每一個(gè)張量元素都是一個(gè)字節(jié)數(shù)組 |
DT_BOOL | tf.bool | 布爾型 |
DT_COMPLEX64 | tf.complex64 | 由兩個(gè)32位浮點(diǎn)數(shù)組成的復(fù)數(shù):實(shí)數(shù)和虛數(shù) |
DT_QINT32 | tf.qint32 | 用于量化Ops的32位有符號(hào)整型 |
DT_QINT8 | tf.qint8 | 用于量化Ops的8位有符號(hào)整型 |
DT_QUINT8 | tf.quint8 | 用于量化Ops的8位無(wú)符號(hào)整型 |
3、張量的階
對(duì)應(yīng)到ndarray的維數(shù)
階 | 數(shù)學(xué)實(shí)例 | python | 例子 |
0 | 純量 | 只有大小 | s = 483 |
1 | 向量 | 大小和方向 | v = [1.1, 2.2, 3.3] |
2 | 矩陣 | 數(shù)據(jù)表 | m = [[1,2,3],[4,5,6],[7,8,9]] |
3 | 3階張量 | 數(shù)據(jù)立體 | ... |
n | n階張量 | 自己想想... | ... |
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
def tensorflow_demo():
"""
TensorFlow的基本結(jié)構(gòu)
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 2.0版本不需要開(kāi)啟會(huì)話,已經(jīng)沒(méi)有會(huì)話模塊了
return None
def graph_demo():
"""
圖的演示
"""
# TensorFlow實(shí)現(xiàn)加減法運(yùn)算
a_t = tf.constant(2)
b_t = tf.constant(3)
c_t = a_t + b_t
print("TensorFlow加法運(yùn)算結(jié)果:\n", c_t)
print(c_t.numpy())
# 查看默認(rèn)圖
# 方法1:調(diào)用方法
default_g = tf.compat.v1.get_default_graph()
print("default_g:\n", default_g)
# 方法2:查看屬性
# print("a_t的圖屬性:\n", a_t.graph)
# print("c_t的圖屬性:\n", c_t.graph)
# 自定義圖
new_g = tf.Graph()
# 在自己的圖中定義數(shù)據(jù)和操作
with new_g.as_default():
a_new = tf.constant(20)
b_new = tf.constant(30)
c_new = a_new + b_new
print("c_new:\n", c_new)
print("a_new的圖屬性:\n", a_new.graph)
print("b_new的圖屬性:\n", b_new.graph)
# 開(kāi)啟new_g的會(huì)話
with tf.compat.v1.Session(graph=new_g) as sess:
c_new_value = sess.run(c_new)
print("c_new_value:\n", c_new_value)
print("我們自己創(chuàng)建的圖為:\n", sess.graph)
# 可視化自定義圖
# 1)創(chuàng)建一個(gè)writer
writer = tf.summary.create_file_writer("./tmp/summary")
# 2)將圖寫(xiě)入
with writer.as_default():
tf.summary.graph(new_g)
return None
def session_run_demo():
"""
feed操作
"""
tf.compat.v1.disable_eager_execution()
# 定義占位符
a = tf.compat.v1.placeholder(tf.float32)
b = tf.compat.v1.placeholder(tf.float32)
sum_ab = tf.add(a, b)
print("a:\n", a)
print("b:\n", b)
print("sum_ab:\n", sum_ab)
# 開(kāi)啟會(huì)話
with tf.compat.v1.Session() as sess:
print("占位符的結(jié)果:\n", sess.run(sum_ab, feed_dict={a: 1.1, b: 2.2}))
return None
def tensor_demo():
"""
張量的演示
"""
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print("tensor1:\n", tensor1)
print("tensor2:\n", tensor2)
print("linear_squares:\n", linear_squares)
return None
if __name__ == "__main__":
# 代碼1:TensorFlow的基本結(jié)構(gòu)
# tensorflow_demo()
# 代碼2:圖的演示
#graph_demo()
# feed操作
#session_run_demo()
# 代碼4:張量的演示
tensor_demo()
python3 day01_deeplearning.py
tensor1:
tf.Tensor(4.0, shape=(), dtype=float32)
tensor2:
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
linear_squares:
tf.Tensor(
[[ 4]
[ 9]
[16]
[25]], shape=(4, 1), dtype=int32)
創(chuàng)建張量的時(shí)候,如果不指定類型:
整型:默認(rèn)tf.inf32
浮點(diǎn)型:默認(rèn)tf.float32
二、創(chuàng)建張量的指令
1、固定值張量
tf.zeros(shape, dtype=tf.float32, name=None)
創(chuàng)建所有元素設(shè)置為零的張量
此操作返回一個(gè)具有dtype、shape和所有元素設(shè)置為零的類型的張量
tf.zeros_like(tensor, dtype=None, name=None)
給定一個(gè)張量tensor,該操作返回與所有元素設(shè)置為零的tensor具有相同類型和形狀的張量
tf.ones(shape, dtype=tf.float32, name=None)
創(chuàng)建一個(gè)所有元素設(shè)置為1的張量
此操作返回一個(gè)具有dtype、shape和所有元素設(shè)置為1的類型的張量
tf.ones_like(tensor, dtype=None, name=None)
給定一個(gè)張量tensor,該操作返回與所有元素設(shè)置為1的tensor具有相同類型和形狀的張量
tf.fill(dims, value, name=None)
創(chuàng)建一個(gè)填充了標(biāo)量值的張量
此操作創(chuàng)建一個(gè)張量,形狀為dims,并用value填充
tf.constant(value, dtype=None, shape=None, name='Const')
創(chuàng)建一個(gè)常數(shù)張量
2、隨機(jī)值張量
一般我們經(jīng)常使用的隨機(jī)函數(shù)Math.random()產(chǎn)生的是服從均勻分布的隨機(jī)數(shù),能夠模擬等概率出現(xiàn)的情況
例如,仍一個(gè)骰子,1到6點(diǎn)的概率應(yīng)該相等,但現(xiàn)實(shí)生活中更多的隨機(jī)現(xiàn)象是符合正態(tài)分布的,例如20歲成年人的體重分布等
假如我們?cè)谥谱饕粋€(gè)游戲,要隨機(jī)設(shè)定許許多多NPC的升高,如果還用Math.random(),生成從140到220之間的數(shù)字,就會(huì)發(fā)現(xiàn)每個(gè)身高段的人數(shù)是一樣多的,這是比較無(wú)趣的,這樣的世界也與我們習(xí)慣不同,現(xiàn)實(shí)應(yīng)該是特別高和特別矮的都很少,處于中間的人數(shù)最多,這就要求隨機(jī)函數(shù)符合正態(tài)分布
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
從截?cái)嗟恼龖B(tài)分布中輸出隨機(jī)值,和tf.random_normal()一樣,但是所有數(shù)字都不超過(guò)兩個(gè)標(biāo)準(zhǔn)差
mean:均值
stddev:標(biāo)準(zhǔn)差文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-832234.html
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
從正態(tài)分布中輸出隨機(jī)值,由隨機(jī)正態(tài)分布的數(shù)字組成的矩陣
mean:均值
stddev:標(biāo)準(zhǔn)差
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-832234.html
到了這里,關(guān)于深度學(xué)習(xí)基礎(chǔ)之《TensorFlow框架(6)—張量》的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!