【TensorFlow2.x系列第1篇】TensorFlow2.0-基本语法与运算

第一篇(语法必备)

TensorFlow 2.0系列相对与1.x系列有了一些明显的变化,比如1.x系列要使用会话tf.Session(),然而在tensorflow2.x系列却没有会话的属性,这是比较明显的变化,另外2.x比1.x的使用也灵活了很多,2.x系列可以在张量tensor和numpy类型之间灵活使用,这也是比较友好的,下面详细tensorflow2.0系列基本语法如下文介绍。

常量/变量

### 常量
import tensorflow as tf

w = tf.constant(3, dtype=tf.float32)
print(w)
#out:<tf.Tensor: id=536, shape=(), dtype=int32, numpy=3>
wM = tf.constant(tf.ones((3,4)), dtype=tf.float32)
#out:<tf.Tensor: id=540, shape=(3, 4), dtype=float32, numpy=
#array([[1., 1., 1., 1.],
#       [1., 1., 1., 1.],
#       [1., 1., 1., 1.]], dtype=float32)>

### 变量单一值
w = tf.Variable(2.0, dtype=tf.float32)
# 输出张量 tensor
print(w)
#out:<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>
# 输出数值 numpy
print(w.numpy())
#out:2.0

向量/矩阵

### 向量
import tensorflow as tf

wV = tf.Variable([2.3,1.3], dtype=tf.float32)
# 输出张量 tensor
print(wV)
#out:<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([2.3, 1.3], dtype=float32)>
# 输出数值 numpy
print(wV.numpy())
#out:array([2.3, 1.3], dtype=float32)

### 矩阵
wM = tf.Variable(tf.ones((3,2)), dtype=tf.float32)
# 输出张量 tensor
print(wV)
#out:<tf.Variable 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txhy2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值