TensorFlow-数据类型和基本运算

TensorFlow 2.0系列相对与1.x系列有了许多变化,像Tensorflow 1.x系列要使用会话tf.Session(),然而在Tensorflow2.x系列却没有会话的属性。现在开始了解Tensorflow的数据类型和基本运算。

import tensorflow as tf

'''
v1.x转换v2.0
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
'''

#创建常量t
t=tf.constant('hello tensorflow',dtype=tf.string)
print(t)
# tf.Tensor(b'hello tensorflow', shape=(), dtype=string)

#创建变量
a=tf.Variable(5.2,dtype=tf.float16)
b=tf.Variable(3.6,dtype=tf.float16)
print(a)
#  <tf.Variable 'Variable:0' shape=() dtype=float16, numpy=5.2>

#输出变量的值
print(a.numpy())
#  5.2

#创建张量
M1=tf.Variable([2.5,3.6],dtype=tf.float32)
print(M1)
# <tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([2.5, 3.6], dtype=float32)>
print(M1.numpy())
# [2.5 3.6]

#创建矩阵
M2=tf.Variable(tf.ones((2,5)),dtype=tf.float32)
print(M2)
# <tf.Variable 'Variable:0' shape=(2, 5) dtype=float32, numpy=
# array([[1., 1., 1., 1., 1.],
#       [1., 1., 1., 1., 1.]], dtype=float32)>
print(M2.numpy())

#加减乘除运算(常规,张量,矩阵)
add_ab=tf.add(a,b)
print(add_ab)
# tf.Tensor(8.8, shape=(), dtype=float16)

mul_M1=tf.multiply(M1,M1)
print(mul_M1)
# tf.Tensor([ 6.25     12.959999], shape=(2,), dtype=float32)

add_M2=tf.add(M2,M2)
print(add_M2)
# tf.Tensor(
# [[2. 2. 2. 2. 2.]
#  [2. 2. 2. 2. 2.]], shape=(2, 5), dtype=float32)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值