1.学习深度学习用tensorflow2.0更加适合,比起tensorflow1.0版本便捷不少,建议用2.0版本学习
2.介绍一些tensorflow2.0的一些基本函数
import tensorflow
as tf
tf
.cast
(张量名
,dtype
=数据类型
)
tf
.reduce_min
(张量名
)
tf
.reduce_max
(张量名
)
tf
.reduce_mean
(张量名
,axis
=1)
tf
.reduce_sum
(张量名
,axis
=0)
tf
.Variable
(初始值
)
tf
.Variable
(tf
.random_normal
([2,2],mean
=0,stddev
=1))
tf
.add
()
tf
.subtract
()
tf
.multiply
()
tf
.divide
()
tf
.square
()
tf
.pow()
tf
.sqrt
()
tf
.matmul
()
tf
.ones
()全为
1的矩阵
tf
.fill
()全为某个值的矩阵
feature
=tf
.constant
([12,23,34,45])
label
=tf
.constant
([0,1,1,0])
data
=tf
.data
.Dataset
.from_tensor_slices
(feature
,label
)
seq
=["one","two","three"]
for i
,element
in enumerate(seq
):
print(i
,element
)
import numpy
as np
with tf
.GradientTape
() as tape
:
w
=tf
.Variable
(tf
.constant
(3.0))
loss
=tf
.pow(w
,2)
grad
=tape
.gradient
(loss
,w
)
tf
.print(grad
)
labels
=tf
.constant
([1,0,2])
output
=tf
.one_hot
(labels
,3)
print(output
)
y
=tf
.constant
([1.01,2.01,-0.66])
y_label
=tf
.nn
.softmax
(y
)
print(y_label
)
w
=tf
.Variable
(4)
w
.assign_sub
(1)
print(w
)
test
=np
.array
([[1,2,3],[21,8,9],[9,5,4]])
print(test
)
print(tf
.argmax
(test
,axis
=0))
print(tf
.argmax
(test
,axis
=1))
后期其他的函数会继续补充的。
转载请注明原文地址: https://lol.8miu.com/read-24386.html