tensorflow的基本操作
#打印tf版本获取常量值
import tensorflow as tf
print(tf.__version__)
a=tf.constant(2.0)
print(a)
结果:
2.9.0-dev20220327
Tensor("Const_15:0", shape=(), dtype=float32)
#下列代码可以在2.x中使用1.x的代码,不过一些被删除的代码无法使用
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a=tf.constant(2.0)
with tf.Session()as sess:
print(sess.run(a))
print(a)
Tensor("Const_21:0", shape=(), dtype=int32)
Tensor("Const_22:0", shape=(), dtype=int32)
Tensor("Add_2:0", shape=(), dtype=int32)
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution() #因为安装的是高版本的,要想session能用必须先写这个
sess=tf.Session()
print(sess.run(a))
print(sess.run([a,b]))
print(sess.run([a,b,c]))
32 [32, 10] [32, 10, 42]
#将上面产生的结果保存在一个变量中
py_a=sess.run(a)
print(type(py_a))
py_r=sess.run([a,b,c])
print(type(py_r))
print(py_r[0],py_r[1],py_r[2])
<class 'numpy.int32'> <class 'list'> 32 10 42
#基本运算
#加法a+b+b
d=tf.add_n([a,b,b])
print(d)
#减法a-b
e=tf.subtract(a,b)
print(b)
#乘法a*b
f=tf.multiply(a,b)
print(f)
#除法a/b
g=tf.divide(a,b)
print(g)
#求余
h=tf.truncatemod(a,b)
print(h)
print(sess.run([d,e,f,g,h]))
#数值类型转换
a_float=tf.cast(a,dtype=tf.float32)
b_float=tf.cast(b,dtype=tf.float32)
#sin(a)
i=tf.sin(a_float)
print(i)
#exp(1/a)
j=tf.exp(tf.divide(1.0,a_float))
print(j)
#i+log(i)
k=tf.add(i,tf.math.log(i))
print(k)
print(sess.run([i,j,k]))
Tensor("AddN_10:0", shape=(), dtype=int32)
Tensor("Const_22:0", shape=(), dtype=int32)
Tensor("Mul_5:0", shape=(), dtype=int32)
Tensor("truediv_5:0", shape=(), dtype=float64)
Tensor("TruncateMod_5:0", shape=(), dtype=int32)
[52, 22, 320, 3.2, 2]
Tensor("Sin:0", shape=(), dtype=float32)
Tensor("Exp:0", shape=(), dtype=float32)
Tensor("Add_3:0", shape=(), dtype=float32)
[0.5514267, 1.0317434, -0.043819606]
#矩阵运算
#从4维向量生成(2,2)的矩阵
mat_a=tf.constant([1,2,3,4])
mat_a=tf.reshape(mat_a,(2,2))
print('mat_a:\n',sess.run(mat_a))
#生成2*3的矩阵
mat_b=tf.constant([1,3,5,7,9,11])
mat_b=tf.reshape(mat_b,(2,3))
print('mat_b:\n',sess.run(mat_b))
#矩阵乘法
mat_c=tf.matmul(mat_a,mat_b)
print('mat_c:\n',sess.run(mat_c))
#用了session模式好像用不了书上的numpy函数
mat_a: [[1 2] [3 4]] mat_b: [[ 1 3 5] [ 7 9 11]] mat_c: [[15 21 27] [31 45 59]]
#随机化实现
#标准正态分布随机
#注意tensorflow版本区别,在1.0中是random_normal
rand_normal=tf.random_normal((1,10),mean=0.0,stddev=1.0,dtype=tf.float32,seed=None)
#truncated正态随机
truncated_normal=tf.truncated_normal((1,10),mean=0.0,stddev=1.0,dtype=tf.float32,seed=None)
#均匀分布随机
rand_uniform=tf.random_uniform((1,10),minval=0.0,maxval=1.0,dtype=tf.float32,seed=None)
print(sess.run(rand_normal))
print(sess.run(truncated_normal))
print(sess.run(rand_uniform))
[[ 1.1016903 1.13558 0.28250527 -2.1385217 2.7722688 -1.3720158 0.7621916 -1.0713987 0.36439985 0.68659556]] [[ 1.3360797 -0.52967274 -0.09540483 0.70260465 1.2980912 0.5778198 -1.2894628 0.88082093 -1.2023339 -0.57034177]] [[0.43418252 0.45881903 0.25752425 0.54407597 0.6047938 0.12356377 0.5012429 0.07523882 0.05481243 0.4527197 ]]
var_a=tf.Variable(0,dtype=tf.int32)
var_b=tf.Variable([1,2],dtype=tf.float32)
var_w=tf.Variable(tf.zeros(1024,10))
#开启交互式session
sess= tf.InteractiveSession()
#一次性初始化所有变量
init=tf.global_variables_initializer()
sess.run(init)
#初始化某些变量
init_ab=tf.variables_initializer([var_a,var_b])
init_ab.run()
#初始化某个变量,通过调用变量的初始化函数来实现
var_w.initializer.run()
W=tf.Variable(10)
sess.run(W.initializer)
print(W)
print(sess.run(W))
print(W.eval())
<tf.Variable 'Variable_3:0' shape=() dtype=int32_ref> 10 10
W=tf.Variable(10)
sess.run(W.initializer)
print(W)
print(sess.run(W))
print(W.eval()) #通过调用w内置的方法
<tf.Variable 'Variable_4:0' shape=() dtype=int32_ref> 10 10
#一开始只是赋值但是w的值没有改变
W.assign(100)
W.initializer.run() #只是run了w的初始化,而w的初始化为10
print(W.eval())
assign_op=W.assign(100)
W.initializer.run()
assign_op.eval() #只有运行了这里w的值才会发生改变
print(W.eval())
10 100
import tensorflow.compat.v1 as tf #对于v1,v2不兼容问题可以通过这个解决
tf.disable_v2_behavior()
#定义一个占位符(类型必须定义,否则报错,shape可有可无,缺省的话默认其为布尔类型)
tf.placeholder(dtype=tf.float32,shape=None,name=None)
#定义一个float32型的占位符,它是一个长度为3的向量
a=tf.placeholder(tf.float32,shape=[3])
#定义一个bool型的占位符,它是一个1*2的矩阵
b=tf.placeholder(tf.bool,shape=[1,2])
#给占位符赋值
#a在上面定义了为3位的向量
print(sess.run(a,feed_dict={a:[1,2,3]}))
#b在上面定义了为1*2的矩阵
print(sess.run([a,b],feed_dict={a:[1,2,3],b:[[True,False]]}))






浙公网安备 33010602011771号