创建和控制张量
学习目标:
初始化 TensorFlow Variable 并赋值 创建和控制张量 回忆线性代数中的加法和乘法知识 熟悉基本的 TensorFlow 数学和数组运算
from __future__ import print_function
import tensorflow as tf
try :
tf. contrib. eager. enable_eager_execution( )
print ( 'TF imported with eager execution!' )
except ValueError:
print ( 'TF already imported with eager execution!' )
TF imported with eager execution!
矢量加法
primes = tf. constant( [ 2 , 3 , 4 , 7 , 11 , 13 ] , dtype= tf. int32)
print ( 'primes:' , primes)
primes: tf.Tensor([ 2 3 4 7 11 13], shape=(6,), dtype=int32)
ones = tf. ones( [ 6 ] , dtype= tf. int32)
print ( 'ones:' , ones)
ones: tf.Tensor([1 1 1 1 1 1], shape=(6,), dtype=int32)
just_beyond_primes = tf. add( primes, ones)
print ( 'just_beyond_primes:' , just_beyond_primes)
just_beyond_primes: tf.Tensor([ 3 4 5 8 12 14], shape=(6,), dtype=int32)
twos = tf. constant( [ 2 ] * 6 , dtype= tf. int32)
primes_doubled = primes * twos
print ( 'primes_doubled:' , primes_doubled)
primes_doubled: tf.Tensor([ 4 6 8 14 22 26], shape=(6,), dtype=int32)
some_matrix = tf. constant( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , dtype= tf. int32)
print ( some_matrix)
print ( '\nvalue of some_matrix is:\n' , some_matrix. numpy( ) )
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
value of some_matrix is:
[[1 2 3]
[4 5 6]]
张量形状
形状用于描述张量维度的大小和数量。张量的形状表示为list, 其中第i个元素表示维度i的大小,列表的长度表示张量的阶(维数)
scalar = tf. zeros( [ ] )
vector = tf. zeros( [ 3 ] )
martix = tf. zeros( [ 2 , 3 ] )
print ( 'scalar has shape' , scalar. get_shape( ) , 'and value:\n' , scalar. numpy( ) )
print ( 'vector has shape' , vector. get_shape( ) , 'and value:\n' , vector. numpy( ) )
print ( 'martix has shape' , martix. get_shape( ) , 'and value:\n' , martix. numpy( ) )
scalar has shape () and value:
0.0
vector has shape (3,) and value:
[0. 0. 0.]
martix has shape (2, 3) and value:
[[0. 0. 0.]
[0. 0. 0.]]
广播
在TensorFlow中可以对张量执行传统意义上不可行的运算,TensorFlow支持广播(一种借鉴于NumPy的概念) 利用广播,元素级运算中较小数组会增大到与较大数组具有相同的形状 当张量被广播时,从概述上来说,系统会复制其条目
primes = tf. constant( [ 2 , 3 , 5 , 7 , 11 , 13 ] , dtype= tf. int32)
print ( 'primes:' , primes)
one = tf. constant( 1 , dtype= tf. int32)
print ( 'one:' , one)
just_beyond_primes = tf. add( primes, one)
print ( 'just_beyond_primes:' , just_beyond_primes)
two = tf. constant( 2 , dtype= tf. int32)
primes_doubled = primes * two
print ( 'primes_doubled' , primes_doubled)
primes: tf.Tensor([ 2 3 5 7 11 13], shape=(6,), dtype=int32)
one: tf.Tensor(1, shape=(), dtype=int32)
just_beyond_primes: tf.Tensor([ 3 4 6 8 12 14], shape=(6,), dtype=int32)
primes_doubled tf.Tensor([ 4 6 10 14 22 26], shape=(6,), dtype=int32)
primes_squared = tf. multiply( primes, primes)
just_under_primes_squared = primes_squared - 1
print ( just_under_primes_squared)
tf.Tensor([ 3 8 24 48 120 168], shape=(6,), dtype=int32)
print ( primes * primes - 1 )
tf.Tensor([ 3 8 24 48 120 168], shape=(6,), dtype=int32)
矩阵乘法
张量变形
使用tf.reshape(matrix, [2, 8])方法改变张量形状(82变成2 8或4*4) 也可以改变张量的维度tf.reshape(matrix, [2,2,4]) # 如将28变成2 2*4,或者多维变成一维
变量、初始化和赋值
上面提到的都是静态值(tf.constant), 调用numpy()始终返回同一结果 定义Variable对象,它的值是可以改变的
v = tf.contrib.eager.Variable([3]) # 定义变量并初始化为3 w = tf.contrib.eager.Variable(tf.random_normal([1, 4] mean=1.0, stddev=0.35)) # 1*4矩阵,均值为1,标准方差为0.35 要改变变量的值,使用assign操作
v = tf.assign(v, [7]) v.assign([5])
die1 = tf. contrib. eager. Variable( tf. random_uniform( [ 10 , 1 ] , minval= 1 , maxval= 7 , dtype= tf. int32) )
die2 = tf. contrib. eager. Variable( tf. random_uniform( [ 10 , 1 ] , minval= 1 , maxval= 7 , dtype= tf. int32) )
dice_sum = tf. add( die1, die2)
resulting_matrix = tf. concat( values= [ die1, die2, dice_sum] , axis= 1 )
print ( resulting_matrix)
tf.Tensor(
[[2 4 6]
[3 3 6]
[1 3 4]
[3 2 5]
[1 2 3]
[2 2 4]
[4 1 5]
[2 3 5]
[3 4 7]
[1 6 7]], shape=(10, 3), dtype=int32)