Tensorflow2.0学习(一):入门介绍篇


个人认为,TF2.0和TF1.X最大的不同就是动态图的操作了,语法上更加简洁
以下所有例子都是参考github大神制作,我只是搬运工
https://github.com/YunYang1994/TensorFlow2.0-Examples

一、Tensorflow2.0 与 Tensorflow1.X中的helloWorld

tf2.0中有 .numpy()这个函数,且输出的Tensor也和1.X不同

import tensorflow as tf

helloworld = tf.constant("hello tf2.0")
print("Tensor: ", helloworld)
print("Value: ", helloworld.numpy())

2.0输出

Tensor:  tf.Tensor(b'hello tf2.0', shape=(), dtype=string)
Value:  b'hello tf2.0'

TF1.X中则没有.numpy()这个函数,且输出是

Tensor:  Tensor("Const:0", shape=(), dtype=string)

这表明了,tf2.0可以动态图运行,而TF1.x则是静态图运行

二、TF2.0中使用变量Variables

import tensorflow as tf
with tf.name_scope("my"):
    variables = tf.Variable(1)

print("value tensor: ", variables)
print("value       : ", variables.numpy())
#  #===========输出: ====================================
#  value tensor:  <tf.Variable 'my/Variable:0' shape=() dtype=int32, numpy=1>
#  value       :  1
#  #=====================================================
# 要在TensorFlow图中使用tf.Variable的值,只需将其视为普通tf.Tensor
variables = variables + 1
print("value tensor: ", variables)
print("value       : ", variables.numpy())
#  #===========输出: ====================================
#  value tensor:  tf.Tensor(2, shape=(), dtype=int32)
#  value       :  2
#  #=====================================================
# 要为变量赋值,请使用方法assign,assign_add
variables = tf.Variable(2)
variables.assign_add(1)
print("value tensor: ", variables)
print("value       : ", variables.numpy())

#  #===========输出: ====================================
#  value tensor:  <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>
#  value       :  3
#  #=====================================================

可以看到, TF.2.0就可以直接算出结果,而1.X则还需要

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    value = sess.run(variables)
    print(value)

三、TF2.0中的一些基础操作

3.1 赋值

import tensorflow as tf
a = tf.ones([2, 3])
print(a)

输出:

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

注意:不能这样赋值

a[0,0] = 10
# 会报错
TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

得这样利用assign函数

# 所以得这样赋值
a = tf.Variable(a)
a[0, 0].assign(10)
b = a.read_value
print(b)

输出

<bound method BaseResourceVariable.read_value of <tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[10.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)>>

3.2 测试乘法和加法

##=====测试 加减乘除 ====
a = tf.constant(2)
b = tf.constant(3)
print("a + b : ", a.numpy() + b.numpy())
print("Addition with constants: ", a+b)
print("Addition with constants: ", tf.add(a, b))
print("a * b :" , a.numpy() * b.numpy())
print("Multiplication with constants: ", a*b)
print("Multiplication with constants: ", tf.multiply(a, b))

输出:

a + b :  5
Addition with constants:  tf.Tensor(5, shape=(), dtype=int32)
Addition with constants:  tf.Tensor(5, shape=(), dtype=int32)
a * b : 6
Multiplication with constants:  tf.Tensor(6, shape=(), dtype=int32)
Multiplication with constants:  tf.Tensor(6, shape=(), dtype=int32)

可见,TF2.0支持了 tensor之间的+号重载,起到的效果和tf.add是一样的

3.3 TF2.0矩阵之间的乘法,tf.matmul() 和点乘之间的区别


matrix1 = tf.constant([[3., 4.]])

matrix2 = tf.constant([[2.],[3.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
print("Multiplication with matrixes:", product)

# broadcast matrix in Multiplication

print("broadcast matrix in Multiplication:", matrix1 * matrix2)

以上输出的是

Multiplication with matrixes: tf.Tensor([[18.]], shape=(1, 1), dtype=float32)
broadcast matrix in Multiplication: tf.Tensor(
[[ 6.  8.]
 [ 9. 12.]], shape=(2, 2), dtype=float32)

注意看,tf.matmul() 是矩阵的乘法,例子得到的是[[18.]]
而 两个Tensor通过*发运算,得到的是
[[ 6. 8.]
[ 9. 12.]]

3.4 tf.cast转换数据类型和shape获取Tensor的大小

这个在1.x的版本中也有

a = tf.convert_to_tensor(2.)
b = tf.cast(a, tf.int32)
print(a, b)

a = tf.ones([2,3])
print(a.shape[0], a.shape[1]) # 2, 3
shape = tf.shape(a)           # a tensor
print(shape[0], shape[1])

输出

tf.Tensor(2.0, shape=(), dtype=float32) tf.Tensor(2, shape=(), dtype=int32)
2 3
tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值