ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

tensorflow的读书笔记

2022-04-25 00:35:23  阅读:211  来源: 互联网

标签:constant Tensor 读书笔记 dtype print shape tf 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)
2
"""
#查看形状
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.]])

"""




 
复制代码

标签:constant,Tensor,读书笔记,dtype,print,shape,tf,tensorflow
来源: https://www.cnblogs.com/zbqd/p/16188429.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有