flow读书笔记

#定义一个矩阵,比如2行2列全为0
tensor1 = tf.zeros([2,2])
print(tensor1)

运行结果:

tf.Tensor([[0. 0. ][0. 0. ]], shape=(2, 2), dtype=float32)

 

 

#定义全为1的矩阵
ones_tsr = tf.ones([2, 2])

print(ones_tsr)

 

运行结果:

tf.Tensor( [[1. 1.]  [1. 1.]], shape=(2, 2), dtype=float32)

 


#维度
print(ones_tsr.ndim)

运行结果:

tf.Tensor([[1. 1.][1. 1.]], shape=(2, 2), dtype=float32)2

 

import tensorflow as tf
sess=tf.compat.v1.Session()
a=tf.constant(32) 
b=tf.constant(10) #创建常量

#加法a+b+b
d=tf.add_n([a,b,b])
print(d)

运行结果:

Tensor("AddN_3:0", shape=(), dtype=int32)

 

 

#减法a-b
e=tf.subtract(a,b)
print(b)

运行结果:

Tensor("Const_17:0", shape=(), dtype=int32)

 

 

#乘法a*b
f=tf.multiply(a,b)
print(f)

运行结果:

Tensor("Mul_3:0", shape=(), dtype=int32)

 

 

#除法a/b
g=tf.divide(a,b)
print(g)

运行结果:

Tensor("truediv_6:0", shape=(), dtype=float64)

 

 

#求余
h=tf.truncatemod(a,b)
print(h)

运行结果:

Tensor("TruncateMod_3:0", shape=(), dtype=int32)

 

 


#数值类型转换
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)

运行结果:

Tensor("Sin_3:0", shape=(), dtype=float32)

 

 

#exp(1/a)
j=tf.exp(tf.divide(1.0,a_float))
print(j)

运行结果:

Tensor("Exp_3:0", shape=(), dtype=float32)

 

 

#i+log(i)
k=tf.add(i,tf.math.log(i))
print(k)

运行结果:

Tensor("Add_3:0", shape=(), dtype=float32)
posted @ 2022-04-25 16:44  我刚好路过  阅读(48)  评论(0)    收藏  举报