tensorflow的读书笔记

2022/4/25 6:17:24

本文主要是介绍tensorflow的读书笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

"""
运行结果:
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]], shape=(2, 3), dtype=float32)
"""

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

"""
运行结果:

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

"""
#创建一个2行3列全为常量8的矩阵
filled_tsr = tf.fill([2, 3], 8) 
print(filled_tsr)
"""
运行结果:

 tf.Tensor(
 [[8 8 8]
 [8 8 8]], shape=(2, 3), dtype=int32)

"""
#自定义一个矩阵
constant_tsr = tf.constant([1, 2, 3])
print(constant_tsr)
"""
运行结果:
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
"""

#自定义一个全为常量8的矩阵
constant2_str = tf.constant(8, tf.float32, [2,4])
print(constant2_str)

"""
运行结果:

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

"""

#查看维度
print(ones_tsr.ndim)

"""
运行结果:
tf.Tensor(
[[1. 1.]
[1. 1.]], shape=(2, 2), dtype=float32)
 
"""
#查看形状
print(ones_tsr.shape)

"""
运行结果:
tf.Tensor(
[[1. 1.]
[1. 1.]], shape=(2, 2), dtype=float32)
(2, 2)
"""

#tensor相加
constant_tsr2 = tf.constant([[1, 2, 3],[3,2,1]])
constant_tsr3 = tf.constant([1, 2, 3])
print(constant_tsr2+constant_tsr3)

"""
运行结果:

 tf.Tensor(
 [[2 4 6]
 [4 4 4]], shape=(2, 3), dtype=int32)

"""

#tensor相减
constant_tsr2 = tf.constant([[1, 2, 3],[3,2,1]])
constant_tsr3 = tf.constant([1, 2, 3])
print(constant_tsr2-constant_tsr3)
"""
运行结果:

  tf.Tensor(
  [[ 0 0 0]
  [ 2 0 -2]], shape=(2, 3), dtype=int32)

"""
#tensor相乘
constant_tsr2 = tf.constant([[1, 2, 3],[3,2,1]])
constant_tsr3 = tf.constant([1, 2, 3])
print(constant_tsr2 * constant_tsr3)

"""
运行结果:

  tf.Tensor(
  [[1 4 9]
  [3 4 3]], shape=(2, 3), dtype=int32)

"""
#tensor相除
constant_tsr2 = tf.constant([[1, 2, 3],[3,2,1]])
constant_tsr3 = tf.constant([1, 2, 3])

print(constant_tsr2 / constant_tsr3)
"""
运行结果:

  tf.Tensor(
  [[1. 1. 1. ]
  [3. 1. 0.33333333]], shape=(2, 3), dtype=float64)

"""

#pow方法
a = torch.full([2,2],6)
a = a.pow(2)
print(a)

"""
运行结果:

  tensor([[36, 36],
  [36, 36]])

"""

#sqrt方法
a = torch.full([2,2],25)
a = a.sqrt()
print(a)
"""
运行结果:

  tensor([[5., 5.],
  [5., 5.]])

"""


 
复制代码

这篇关于tensorflow的读书笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程