tf1.x api学习,tensorflow中大部分数据是float32。

目录

y=wx+b的训练(tf1.15)

读取外部数据 feed_dict

tf.reshape

np.reshape

Variable与Tensor?

tf.boolean_mask

tf.sequence_mask

tf.ones_like

tf.where


y=wx+b的训练(tf1.15)


# =================================================
# 定义x
import tensorflow as tf
import numpy as np
import warnings
warnings.filterwarnings("ignore")
#tensorflow中大部分数据是float32

#create real data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
print("x_data\n", x_data)
print(x_data.shape)


"""
x_data
 [0.76333565 0.6284206  0.37263715 0.65455854 0.94502664 0.44106185
 0.6579124  0.596047   0.23026496 0.68493325 0.28326866 0.5454244
 0.53899145 0.5069484  0.4781607  0.12442698 0.93258744 0.26535302
 0.01390975 0.3758615  0.4871458  0.43328044 0.7436298  0.4957959
 0.26281872 0.48266843 0.00522989 0.46331772 0.9948111  0.65535545
 0.29138285 0.8902936  0.9248564  0.513812   0.00686918 0.34523347
 0.00703951 0.8913199  0.3098913  0.21863274 0.5095166  0.5489832
 0.4601982  0.3136408  0.22485432 0.13801059 0.7593674  0.6693202
 0.14117578 0.9179072  0.95565665 0.11111844 0.49037448 0.8624194
 0.4386528  0.9526904  0.41404507 0.9612559  0.265769   0.39845958
 0.16400698 0.39818686 0.97672176 0.22039981 0.4394132  0.32044202
 0.88420975 0.61235833 0.2552407  0.48839206 0.8755424  0.16084947
 0.8784639  0.7824357  0.20381169 0.18746403 0.06942245 0.15191926
 0.3692252  0.3063411  0.8014056  0.84878755 0.11150452 0.31856656
 0.640654   0.6540355  0.21862768 0.98274463 0.09282159 0.3529222
 0.99537206 0.3758906  0.50382924 0.53394973 0.41215995 0.03262074
 0.0214077  0.18212041 0.42616773 0.8853701 ]
(100,)
"""

# 定义y

print("y_data\n", y_data)
print(y_data.shape)

"""
y_data
 [0.3763336  0.36284208 0.33726373 0.36545587 0.39450267 0.3441062
 0.36579126 0.35960472 0.3230265  0.36849335 0.32832688 0.35454246
 0.35389915 0.35069486 0.34781608 0.31244272 0.39325875 0.3265353
 0.30139098 0.33758616 0.3487146  0.34332806 0.374363   0.3495796
 0.32628188 0.34826684 0.300523   0.34633178 0.39948112 0.36553556
 0.3291383  0.38902938 0.39248565 0.3513812  0.30068693 0.33452335
 0.30070397 0.389132   0.33098915 0.3218633  0.35095167 0.35489833
 0.34601983 0.3313641  0.32248545 0.31380108 0.37593675 0.36693203
 0.31411758 0.39179075 0.3955657  0.31111187 0.34903747 0.38624197
 0.3438653  0.39526907 0.34140453 0.3961256  0.32657692 0.33984596
 0.3164007  0.3398187  0.39767218 0.32204    0.34394133 0.3320442
 0.388421   0.36123586 0.3255241  0.34883922 0.38755426 0.31608495
 0.3878464  0.3782436  0.3203812  0.31874642 0.30694225 0.31519192
 0.33692253 0.33063412 0.38014057 0.38487875 0.31115046 0.33185667
 0.3640654  0.36540356 0.3218628  0.39827448 0.30928218 0.33529222
 0.3995372  0.3375891  0.35038292 0.353395   0.341216   0.30326208
 0.30214077 0.31821206 0.3426168  0.38853702]
(100,)
"""

create tensorflow structure start

# 定义变量
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

Weights
<tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>

biases
<tf.Variable 'Variable_1:0' shape=(1,) dtype=float32_ref>



#如何计算预测值
y = Weights * x_data + biases

# loss function
loss = tf.reduce_mean(tf.square(y-y_data))

#梯度下降优化器,定义learning rate
optimizer = tf.train.GradientDescentOptimizer(0.5)

#训练目标是loss最小化
train = optimizer.minimize(loss)


#初始化变量,即初始化 Weights 和 biases
init = tf.global_variables_initializer()

#创建session,进行参数初始化
sess = tf.Session()
sess.run(init)

训练必须创建一个session,通过run方法对指定的节点进行训练,

这里一定要注意先要对参数进行初始化,否则后面是无法开始训练的。

想要观察训练过程中的参数变化的话,也需要通过run方法。

#创建session,进行参数初始化
# sess = tf.Session()
# sess.run(init)
with tf.Session() as sess:
    sess.run(init)
    #开始训练200步,每隔20步输出一下两个参数
    for step in range(201):
        sess.run(train) # train = optimizer.minimize(loss), 过run方法对指定的节点进行训练
        if step % 20 == 0:
            print(step, sess.run(Weights), sess.run(biases)) # 观察训练过程中的参数变化的话,也需要通过run方法




0 [0.36637446] [0.21450494]
20 [0.163419] [0.26733914]
40 [0.11639377] [0.2915572]
60 [0.10423777] [0.29781756]
80 [0.10109545] [0.29943585]
100 [0.10028318] [0.2998542]
120 [0.10007321] [0.2999623]
140 [0.10001894] [0.29999027]
160 [0.10000489] [0.29999748]
180 [0.10000128] [0.29999936]
200 [0.10000034] [0.29999983]

读取外部数据 feed_dict

#import tensorflow as tf

input1 = tf.placeholder(dtype=tf.float32)
input2 = tf.placeholder(dtype=tf.float32)

output = tf.multiply(input1,input2)

with tf.Session() as sess:
    print(sess.run(output,feed_dict={input1:[3.],input2:[5]})) # 15

所谓反向传播?

import tensorflow as tf

#定义变量,给定初始值和name
state = tf.Variable(0,name="counter")
print(state.name) #counter:0

one = tf.constant(1)

new_value = tf.add(state,one)
update = tf.assign(state,new_value)

#这里只是定义,必须用session.run来执行
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))
        
        
counter:0
1
2
3

tf.reshape

import tensorflow as tf

item1 = [[20,20,20,20,20]]
item2 = [[30,30,30,30,30]]
item3 = [[40,40,40,40,40]]


item1_list = tf.convert_to_tensor(item1, dtype=tf.int64)
item2_list = tf.convert_to_tensor(item2, dtype=tf.int64)
item3_list = tf.convert_to_tensor(item3, dtype=tf.int64)


item1_list
<tf.Tensor 'Const_3:0' shape=(1, 5) dtype=int64>


# concat
item_concat = tf.concat([item1_list, item2_list, item3_list], axis=1) # 行concat
# item_concat 
# <tf.Tensor 'concat:0' shape=(1, 15) dtype=int64>


# reshape
# 最后一个维度为5,上一个维度自己计算得到 (1*15)/5=3
hist_i = tf.reshape(item_concat, [-1, 5])
# hist_i
# <tf.Tensor 'Reshape:0' shape=(3, 5) dtype=int64>


# 这里只是定义,必须用session.run来执行
init = tf.global_variables_initializer()
 
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(item_concat))
    # [[20 20 20 20 20 30 30 30 30 30 40 40 40 40 40]]


    print(sess.run(hist_i))
    [[20 20 20 20 20]
     [30 30 30 30 30]
     [40 40 40 40 40]]

np.reshape

list_of_points1_ = [[1, 2], [3, 4], [5, 6], [7, 8]]
np.array(list_of_points1_).shape
(4, 2)



list_of_points1 = np.array([np.array(elem).reshape(1, 2) for elem in list_of_points1_])
list_of_points1
(4, 1, 2)

Variable与Tensor?

Variable

state = tf.Variable(0.0,dtype=tf.float32)
state
<tf.Variable 'Variable_1:0' shape=() dtype=float32_ref>


one = tf.constant(1.0, dtype=tf.float32)
one
<tf.Tensor 'Const_1:0' shape=() dtype=float32>


new_val = tf.add(state, one)
new_val
<tf.Tensor 'Add_1:0' shape=() dtype=float32>

update = tf.assign(state, new_val) #返回tensor, 值为new_val
update
<tf.Tensor 'Assign_3:0' shape=() dtype=float32_ref>


init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        print(sess.run(update)) 
        # 0+1
        # 1+1
        # 2+1

1.0
2.0
3.0

Tensor

update2 = tf.assign(state, 10)  #没有fetch,便没有执行
update2
<tf.Tensor 'Assign_4:0' shape=() dtype=float32_ref>


init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        print(sess.run([update, update2]))


[11.0, 10.0]
[11.0, 10.0]
[11.0, 10.0]

结论:tensorflow 中 Variable 和 Tensor 是有区别的, Tensor在一次 sess.run 之后就会被释放, Variable 而是会保留, assign 赋值 的 state是个 Variable, 所以赋值的结果会保留的。

feed的作用域?

y = tf.Variable(1)
b = tf.identity(y)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    sess.run(b,feed_dict={y:3})
    print(sess.run(b,feed_dict={y:3})) # 使用3 替换掉
    #tf.Variable(1)的输出结果,所以打印出来3 
    #feed_dict{y.name:3} 和上面写法等价

    print(sess.run(b))  #由于feed只在调用他的方法范围内有效,所以这个打印的结果是 1

# 3
# 1

tf.boolean_mask

import numpy as np
import tensorflow as tf
# print("tf ver: ", tf.__version__) # tf ver:  2.0.0



a = np.random.randn(3, 3, 3)
b = np.max(a,-1)
c =  b > 0.5
print("a="+str(a)) # a = np.random.randn(3,3,3)生成3*3*3的张量
print("b="+str(b)) # b = np.max(a, -1)表示在最后一个轴(此时为2)的方向上去最大值,此时将3*3*3的张量变为3*3的张量
print("c="+str(c)) # c的结果为一个3*3的布尔型张量
# with tf.Session() as sess:
#     d = tf.boolean_mask(a,c)
# print("d="+str(d.eval(session=sess)))
d = tf.boolean_mask(a,c) # 将使a (3维)张量仅保留与c中“True”元素同下标的部分,并将结果展开到2维。


a=[[[-0.23728232  1.11862527  0.4944028 ]
  [-0.56575014  0.59898471 -0.70472848]
  [-0.25333616  0.11929698  0.66897738]]

 [[ 0.17148303  1.89054253  0.99485903]
  [-0.6528159   0.71520022 -0.6431113 ]
  [ 0.99869133  1.12139885 -0.01735797]]

 [[ 0.07742792  1.32737137  0.1449916 ]
  [ 1.65612255  0.75193032 -0.2265598 ]
  [-0.64352974 -0.95435969  1.08602664]]]

b=[[1.11862527 0.59898471 0.66897738]
 [1.89054253 0.71520022 1.12139885]
 [1.32737137 1.65612255 1.08602664]]

c=[[ True  True  True]
 [ True  True  True]
 [ True  True  True]]


d
<tf.Tensor: id=28, shape=(9, 3), dtype=float64, numpy=
array([[-0.23728232,  1.11862527,  0.4944028 ],
       [-0.56575014,  0.59898471, -0.70472848],
       [-0.25333616,  0.11929698,  0.66897738],
       [ 0.17148303,  1.89054253,  0.99485903],
       [-0.6528159 ,  0.71520022, -0.6431113 ],
       [ 0.99869133,  1.12139885, -0.01735797],
       [ 0.07742792,  1.32737137,  0.1449916 ],
       [ 1.65612255,  0.75193032, -0.2265598 ],
       [-0.64352974, -0.95435969,  1.08602664]])>

tf.sequence_mask

lenght = [2,2,4] 
mask_data = tf.sequence_mask(lengths=lenght)
mask_data


<tf.Tensor: id=40, shape=(3, 4), dtype=bool, numpy=
array([[ True,  True, False, False],
       [ True,  True, False, False],
       [ True,  True,  True,  True]])>

# 第一行2个T
# 第二行2个T
# 第三行4个T

指定最大长度情况下

mask_data = tf.sequence_mask(lengths=lenght, maxlen=6)
mask_data

<tf.Tensor: id=49, shape=(3, 6), dtype=bool, numpy=
array([[ True,  True, False, False, False, False],
       [ True,  True, False, False, False, False],
       [ True,  True,  True,  True, False, False]])>

# 第一行2个T
# 第二行2个T
# 第三行4个T

改用01表示false or true 

mask_data = tf.sequence_mask(lengths=lenght, maxlen=6, dtype=tf.float32)
mask_data


<tf.Tensor: id=59, shape=(3, 6), dtype=float32, numpy=
array([[1., 1., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0., 0.],
       [1., 1., 1., 1., 0., 0.]], dtype=float32)>

tf.ones_like

生成维度相同的矩阵,全是1

tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.ones_like(tensor)  # [[1, 1, 1], [1, 1, 1]]


<tf.Tensor: id=69, shape=(2, 3), dtype=int32, numpy=
array([[1, 1, 1],
       [1, 1, 1]], dtype=int32)>
tensor = tf.constant([[1, 2], [4, 5],[7, 9]])
tf.ones_like(tensor)  
# array([[1, 1],
#       [1, 1],
#       [1, 1]], dtype=int32)>


<tf.Tensor: id=78, shape=(3, 2), dtype=int32, numpy=
array([[1, 1],
       [1, 1],
       [1, 1]], dtype=int32)>

生成维度相同的矩阵,全是最小值

tensor = tf.constant([[1, 2], [4, 5],[7, 9]], dtype = tf.float32) # 修改类型
paddings = tf.ones_like(tensor) * (-2 ** 32 + 1)  
paddings

<tf.Tensor: id=114, shape=(3, 2), dtype=float32, numpy=
array([[-4.2949673e+09, -4.2949673e+09],
       [-4.2949673e+09, -4.2949673e+09],
       [-4.2949673e+09, -4.2949673e+09]], dtype=float32)>

生成维度相同的矩阵,全是2

tensor = tf.constant([[1, 2], [4, 5],[7, 9]])
paddings = tf.ones_like(tensor) * (2)  
paddings

<tf.Tensor: id=126, shape=(3, 2), dtype=int32, numpy=
array([[2, 2],
       [2, 2],
       [2, 2]], dtype=int32)>

tf.where

x = [[1,2,3],[4,5,6]]
y = [[7,8,9],[10,11,12]]

condition3 = [[True,False,False],
             [False,True,True]]

condition4 = [[True,False,False],
             [True,True,False]]

# 维度
# np.array(x).shape
# (2, 3)


tf.where(condition3, x, y) # 用y中的值填补x中为False的位置
<tf.Tensor: id=130, shape=(2, 3), dtype=int32, numpy=
array([[ 1,  8,  9],
       [10,  5,  6]], dtype=int32)>


tf.where(condition4,x,y)
<tf.Tensor: id=134, shape=(2, 3), dtype=int32, numpy=
array([[ 1,  8,  9],
       [ 4,  5, 12]], dtype=int32)>
12-08 06:44