第五天学习tensorflow 进度变得更慢了,后面学习神经网络实现就更困难了
import tensorflow as tf
import numpy as np
数据统计
在神经网络的计算过程中,经常需要统计数据的各种属性,如最值、最值位置、均值、范数等信息。由于张量通常较大,直接观察数据很难获得有用信息,通过获取这些张量的统计信息可以较轻松地推测张量数值的分布。
向量范数
掌握L1 范数,L2范数,无穷的单数
分别是绝对值的和,平方和开根号,以及所有元素的最大值
在 TensorFlow 中,可以通过 tf.norm(x, ord)求解张量的 L1、L2、∞等范数,其中参数 指定为 1、2 时计算 L1、L2 范数,指定为 np.inf 时计算∞ −范数,例如:
x = tf.ones([2,2])
x
<tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
[1., 1.]], dtype=float32)>
tf.norm(x,ord=1)
<tf.Tensor: id=10, shape=(), dtype=float32, numpy=4.0>
tf.norm(x,ord=2)
<tf.Tensor: id=15, shape=(), dtype=float32, numpy=2.0>
tf.norm(x,np.inf)
<tf.Tensor: id=19, shape=(), dtype=float32, numpy=1.0>
最值,均值,和
通过 tf.reduce_max、tf.reduce_min、tf.reduce_mean、tf.reduce_sum 函数可以求解张量
在某个维度上的最大、最小、均值、和,也可以求全局最大、最小、均值、和信息。
x = tf.random.normal([4,10])
x
<tf.Tensor: id=25, shape=(4, 10), dtype=float32, numpy=
array([[ 0.90304714, 0.867509 , -0.10583521, -0.24520871, -0.27175245,
0.714719 , -1.8822681 , -0.04624402, 0.22397299, 1.2673378 ],
[-0.6078129 , 0.5561518 , 1.1341519 , 0.3840923 , 0.9247282 ,
-1.4297161 , 2.368414 , 0.42986506, -0.6621201 , -1.8632736 ],
[ 0.06153006, -0.02051435, 0.03802924, 0.2511405 , -0.09917058,
0.54441965, -0.28935012, 0.5126414 , -1.8858635 , 0.73549336],
[-0.20714843, 1.35252 , 0.23027088, -0.27317277, 2.0198472 ,
-0.4003495 , -1.1423806 , -0.00568483, -0.6129 , -1.9091526 ]],
dtype=float32)>
tf.reduce_max(x,axis=1)
<tf.Tensor: id=27, shape=(4,), dtype=float32, numpy=array([1.2673378 , 2.368414 , 0.73549336, 2.0198472 ], dtype=float32)>
tf.reduce_min(x,axis=1)
<tf.Tensor: id=29, shape=(4,), dtype=float32, numpy=array([-1.8822681, -1.8632736, -1.8858635, -1.9091526], dtype=float32)>
tf.reduce_mean(x,axis=1)
<tf.Tensor: id=31, shape=(4,), dtype=float32, numpy=array([ 0.14252774, 0.12344806, -0.01516444, -0.09481509], dtype=float32)>
当不指定 axis 参数时,tf.reduce_*函数会求解出全局元素的最大、最小、均值、和等
数据,例如:
tf.reduce_max(x)
<tf.Tensor: id=33, shape=(), dtype=float32, numpy=2.368414>
out = tf.random.normal([4,10])
y = tf.constant([1,2,2,0])
y
<tf.Tensor: id=40, shape=(4,), dtype=int32, numpy=array([1, 2, 2, 0])>
y = tf.one_hot(y,depth=10)
y
<tf.Tensor: id=44, shape=(4, 10), dtype=float32, numpy=
array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>
from tensorflow import keras
loss = keras.losses.mse(y,out)
loss
<tf.Tensor: id=47, shape=(4,), dtype=float32, numpy=array([1.1486361 , 0.6710354 , 0.94017327, 1.0621034 ], dtype=float32)>
loss = tf.reduce_mean(loss)
loss
<tf.Tensor: id=49, shape=(), dtype=float32, numpy=0.955487>
tf.reduce_sum(out,axis=-1) #求最后一个维度的和
<tf.Tensor: id=53, shape=(4,), dtype=float32, numpy=array([ 5.6818914 , -0.32956088, -1.712019 , 0.8558146 ], dtype=float32)>
除了希望获取张量的最值信息,还希望获得最值所在的位置索引号,例如分类任务的
标签预测,就需要知道概率最大值所在的位置索引号,一般把这个位置索引号作为预测类
别。考虑 10 分类问题,我们得到神经网络的输出张量 out,shape 为[2,10],代表了 2 个样
本属于 10 个类别的概率,由于元素的位置索引代表了当前样本属于此类别的概率,预测时
往往会选择概率值最大的元素所在的索引号作为样本类别的预测值,例如:
out = tf.random.normal([2,10])
out = tf.nn.softmax(out,axis=1) #代表softmax的函数值 就是函数的概率值归一化
out
<tf.Tensor: id=61, shape=(2, 10), dtype=float32, numpy=
array([[0.09083375, 0.10227328, 0.10120044, 0.09862141, 0.1175132 ,
0.11902028, 0.09142857, 0.09319578, 0.09259151, 0.09332174],
[0.09321751, 0.09970154, 0.10362992, 0.09398832, 0.10403868,
0.09700011, 0.11579747, 0.10289162, 0.09539214, 0.09434266]],
dtype=float32)>
<tf.Tensor: id=61, shape=(2, 10), dtype=float32, numpy=
array([[0.09083375, 0.10227328, 0.10120044, 0.09862141, 0.1175132 ,
0.11902028, 0.09142857, 0.09319578, 0.09259151, 0.09332174],
[0.09321751, 0.09970154, 0.10362992, 0.09398832, 0.10403868,
0.09700011, 0.11579747, 0.10289162, 0.09539214, 0.09434266]],
dtype=float32)>
以第一个样本为例,可以看到,它概率最大的索引为𝑗 = 0,最大概率值为 0.1877。由于每
个索引号上的概率值代表了样本属于此索引号的类别的概率,因此第一个样本属于 0 类的
概率最大,在预测时考虑第一个样本应该最有可能属于类别 0。这就是需要求解最大值的
索引号的一个典型应用。
通过 tf.argmax(x, axis)和 tf.argmin(x, axis)可以求解在 axis 轴上,x 的最大值、最小值所
在的索引号,例如:
pred = tf.argmax(out,axis=1)
pred
<tf.Tensor: id=63, shape=(2,), dtype=int64, numpy=array([5, 6], dtype=int64)>
可将两个样本的概率分贝在5,6的位置
张量比较
为了计算分类任务的准确率等指标,一般需要将预测结果和真实标签比较,统计比较
结果中正确的数量来计算准确率。考虑 100 个样本的预测结果,通过 tf.argmax 获取预测类
别,实现如下:
out = tf.random.normal([100,10])
out = tf.nn.softmax(out)
pred = tf.argmax(out,axis=1)
pred
<tf.Tensor: id=75, shape=(100,), dtype=int64, numpy=
array([6, 7, 8, 0, 9, 0, 7, 0, 1, 2, 6, 5, 3, 3, 4, 2, 5, 7, 6, 5, 9, 6,
7, 4, 6, 6, 7, 6, 5, 7, 6, 5, 0, 0, 1, 0, 5, 5, 7, 9, 8, 8, 7, 4,
4, 9, 2, 2, 3, 5, 0, 8, 5, 9, 1, 5, 0, 0, 0, 5, 0, 4, 2, 8, 6, 6,
3, 6, 7, 0, 5, 2, 1, 4, 7, 5, 0, 9, 5, 7, 8, 9, 0, 9, 2, 9, 6, 9,
9, 2, 1, 2, 1, 6, 2, 5, 1, 9, 9, 2], dtype=int64)>
y = tf.random.uniform([100],dtype=tf.int64,maxval=10) #真实值
y
<tf.Tensor: id=83, shape=(100,), dtype=int64, numpy=
array([8, 6, 9, 6, 7, 2, 5, 7, 7, 1, 3, 3, 5, 3, 3, 8, 4, 2, 6, 8, 2, 5,
9, 5, 8, 3, 7, 7, 1, 2, 0, 0, 6, 7, 8, 9, 5, 4, 7, 7, 4, 3, 2, 4,
8, 8, 1, 2, 0, 2, 3, 5, 4, 6, 9, 8, 6, 4, 1, 5, 7, 1, 5, 5, 4, 6,
9, 3, 8, 3, 1, 1, 1, 0, 8, 5, 7, 6, 3, 7, 8, 2, 7, 5, 7, 0, 4, 3,
7, 1, 1, 1, 8, 9, 8, 0, 4, 0, 9, 7], dtype=int64)>
即可获得代表每个样本是否预测正确的布尔类型张量。通过 tf.equal(a, b)(或 tf.math.equal(a,
b),两者等价)函数可以比较这 2 个张量是否相等,例如:
out = tf.equal(pred,y)
out
<tf.Tensor: id=84, shape=(100,), dtype=bool, numpy=
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, True, False, False, False, False,
True, False, False, False, False, False, False, False, True,
False, False, False, False, False, False, False, False, False,
True, False, True, False, False, False, False, True, False,
False, False, True, False, False, False, False, False, False,
False, False, False, False, False, True, False, False, False,
False, False, True, False, False, False, False, False, False,
True, False, False, True, False, False, False, True, True,
False, False, False, False, False, False, False, False, False,
True, False, False, False, False, False, False, False, True,
False])>
out = tf.cast(out,tf.float32)
out
<tf.Tensor: id=85, shape=(100,), dtype=float32, numpy=
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 1., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0.,
0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 1., 1., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
dtype=float32)>
correct = tf.reduce_sum(out)
correct
<tf.Tensor: id=87, shape=(), dtype=float32, numpy=15.0>
填充与复制
填充
对于图片数据的高和宽、序列信号的长度,维度长度可能各不相同。为了方便网络的
并行计算,需要将不同长度的数据扩张为相同长度,之前我们介绍了通过复制的方式可以
增加数据的长度,但是重复复制数据会破坏原有的数据结构,并不适合于此处。通常的做
法是,在需要补充长度的数据开始或结束处填充足够数量的特定数值,这些特定数值一般
代表了无效意义,例如 0,使得填充后的长度满足系统要求。那么这种操作就叫作填充
(Padding)。
a = tf.constant([1,2,3,4,5,6])
b = tf.constant([7,8,1,6])
b = tf.pad(b,[[0,2]])
b
<tf.Tensor: id=98, shape=(6,), dtype=int32, numpy=array([7, 8, 1, 6, 0, 0])>
tf.stack([a,b],axis=0) #堆叠合并,创建句子的维度
<tf.Tensor: id=99, shape=(2, 6), dtype=int32, numpy=
array([[1, 2, 3, 4, 5, 6],
[7, 8, 1, 6, 0, 0]])>
total_words = 10000
max_review_len = 80
embeding_len = 80
(x_train,y_train),(x_test,y_test) = keras.datasets.imdb.load_data(num_words=total_words)
x_train = keras.preprocessing.sequence.pad_sequences(x_train,maxlen=max_review_len,truncating='post',padding='post')
x_train
array([[ 1, 14, 22, ..., 4, 22, 17],
[ 1, 194, 1153, ..., 9, 45, 43],
[ 1, 14, 47, ..., 278, 36, 69],
...,
[ 1, 11, 6, ..., 960, 108, 45],
[ 1, 1446, 7079, ..., 40, 5, 421],
[ 1, 17, 6, ..., 109, 29, 127]])
x_test = keras.preprocessing.sequence.pad_sequences(x_test,maxlen=max_review_len,truncating='post',padding='post')
print(x_train.shape,x_test.shape)
(25000, 80) (25000, 80)
复制
在维度变换一节,我们就介绍了通过 tf.tile()函数实现长度为 1 的维度复制的功能。
tf.tile 函数除了可以对长度为 1 的维度进行复制若干份,还可以对任意长度的维度进行复制
若干份,进行复制时会根据原来的数据次序重复复制。由于前面已经介绍过,此处仅作简
单回顾。
通过 tf.tile 函数可以在任意维度将数据重复复制多份,如 shape 为[4,32,32,3]的数据,
复制方案为 multiples=[2,3,3,1],即通道数据不复制,高和宽方向分别复制 2 份,图片数再
复制 1 份,实现如下:
x = tf.random.normal([4,32,32,3])
tf.tile(x,[2,3,3,1]) # 数据复制
<tf.Tensor: id=107, shape=(8, 96, 96, 3), dtype=float32, numpy=
array([[[[-1.47292405e-01, -1.85087383e-01, -1.66168880e+00],
[ 8.68583918e-01, 1.48828849e-01, -1.18210334e-02],
[-6.61570668e-01, 1.10406983e+00, -6.03731692e-01],
...,
[ 4.59740788e-01, -1.38479137e+00, -2.00424409e+00],
[-2.22122264e+00, -1.23022154e-01, -6.14381954e-02],
[-6.98508441e-01, 1.91453665e-01, -1.46096218e+00]],
[[-2.27539688e-01, 1.08822072e+00, 1.21138358e+00],
[ 5.39744556e-01, -9.05357778e-01, -1.02236402e+00],
[-4.66704607e-01, -7.22368598e-01, 1.03028238e+00],
...,
[-5.09573340e-01, 5.54319084e-01, 1.14588988e+00],
[ 2.37796545e+00, -3.79563242e-01, 2.47909737e+00],
[-8.00241157e-02, 5.40217936e-01, 1.25279337e-01]],
[[-4.51540500e-02, -1.02840626e+00, 4.51620340e-01],
[ 4.29947346e-01, 2.12470610e-02, -1.21827161e+00],
[-1.43774438e+00, 4.31071706e-02, 4.23258275e-01],
...,
[-1.70241618e+00, 2.52727687e-01, -4.99026358e-01],
[ 6.73817456e-01, 3.42003554e-01, -1.47434533e+00],
[ 1.71712351e+00, 4.22968775e-01, 9.84052062e-01]],
...,
[[-1.10222387e+00, 2.18306828e+00, -1.79185283e+00],
[ 4.27774847e-01, -1.95203888e+00, 1.27587903e+00],
[ 1.00784111e+00, -1.43291342e+00, -3.45606595e-01],
...,
[-1.50438106e+00, 2.10605025e+00, 6.18120670e-01],
[-2.33945698e-01, 6.03975713e-01, 1.83915138e+00],
[ 5.46206951e-01, 1.29592931e+00, 9.18438375e-01]],
[[-9.72236514e-01, 8.67765009e-01, -9.44452584e-02],
[ 1.66610456e+00, -2.01724410e+00, -4.07122970e-01],
[-1.12402511e+00, 7.46360064e-01, -2.26817533e-01],
...,
[ 2.08621788e+00, 8.31223369e-01, 1.02262926e+00],
[ 1.08736503e+00, -6.49435639e-01, -5.75615287e-01],
[ 1.28295445e+00, 2.58143735e+00, -7.42293537e-01]],
[[-2.83599675e-01, 1.36138475e+00, 4.97387528e-01],
[-4.21091408e-01, 6.52772367e-01, -1.29792047e+00],
[ 5.88838421e-02, -1.26854920e+00, -3.20707895e-02],
...,
[-2.16929412e+00, -1.26648486e+00, -1.47291571e-01],
[ 1.18744934e+00, 3.71295512e-01, 1.05739748e+00],
[ 8.38698387e-01, -4.35878448e-02, 4.20063704e-01]]],
[[[-3.24997336e-01, 1.08034515e+00, 6.92673996e-02],
[ 1.05060041e-01, 1.39178380e-01, -1.46256888e+00],
[-1.83549687e-01, 1.22453012e-02, 1.20745516e+00],
...,
[ 5.03376536e-02, 1.08175087e+00, -1.37437296e+00],
[-1.37099671e+00, 3.59743834e-01, -1.26847059e-01],
[-5.74307382e-01, 5.04446864e-01, 1.17379643e-01]],
[[ 4.12333421e-02, 1.52342355e+00, 2.69585729e-01],
[-9.09270406e-01, 4.78420645e-01, -1.11571062e+00],
[ 1.19375551e+00, -7.09048092e-01, 8.88599157e-01],
...,
[ 4.73948568e-02, 4.98938709e-01, -1.08797097e+00],
[ 8.33462119e-01, 9.08865273e-01, -1.46219552e+00],
[ 5.32921314e-01, 1.06796491e+00, -1.93718165e-01]],
[[ 1.25406015e+00, -3.50292444e-01, -1.30191416e-01],
[-2.75178581e-01, -7.00684726e-01, -8.93072426e-01],
[ 1.01272747e-01, 9.87824798e-01, 6.57207191e-01],
...,
[ 1.33347639e-03, 7.50014500e-04, 5.11407077e-01],
[ 3.24216008e-01, 1.15871239e+00, -5.61861396e-01],
[-1.33608952e-01, 7.31391490e-01, -1.59337521e-02]],
...,
[[-5.49170852e-01, -1.69810963e+00, -8.18801150e-02],
[-4.80068922e-01, -1.13038115e-01, 1.62172318e+00],
[-1.69122827e+00, -3.67237926e-01, -5.89039683e-01],
...,
[ 4.33714420e-01, -5.34618318e-01, 1.38874781e+00],
[ 5.81656456e-01, 8.31009567e-01, -2.23122224e-01],
[ 4.18670475e-02, -1.33776629e+00, -4.29855734e-01]],
[[ 5.38588226e-01, 5.70344329e-01, 4.37376589e-01],
[-1.95507479e+00, 8.03013563e-01, 1.77007222e+00],
[ 5.48814654e-01, -1.85685351e-01, -7.03386962e-02],
...,
[ 5.11914551e-01, -3.42784077e-03, 8.44469965e-01],
[ 4.18510020e-01, -2.31981903e-01, -1.44743538e+00],
[ 1.48373485e+00, 5.09651862e-02, 1.63715124e-01]],
[[ 7.68731117e-01, 3.42273176e-01, -4.06700999e-01],
[ 2.19369984e+00, 1.45487654e+00, 1.17882729e+00],
[ 9.26257491e-01, 5.75700700e-01, -7.29449809e-01],
...,
[ 2.00472832e-01, 4.45106894e-01, -4.05370653e-01],
[-1.78351879e+00, 7.03896523e-01, -1.05813706e+00],
[-9.94620681e-01, -1.71380520e+00, 1.09512174e+00]]],
[[[-1.38506696e-01, 5.42279065e-01, 1.79691344e-01],
[ 9.50728357e-01, 4.53848094e-01, -5.73166311e-01],
[-2.00528908e+00, 3.89156401e-01, -1.31658947e+00],
...,
[ 1.82688847e-01, 3.01198423e-01, -1.26344955e+00],
[-1.56631064e+00, -1.69837511e+00, 1.84550524e-01],
[ 1.18606985e+00, -1.72423112e+00, -1.99016929e+00]],
[[ 2.36121863e-01, -4.75903153e-01, 1.08353925e+00],
[-9.33088899e-01, 1.87986806e-01, -8.00343037e-01],
[-8.69377911e-01, -2.67569665e-02, -8.52958083e-01],
...,
[-1.27807513e-01, 1.04186952e+00, -5.27602017e-01],
[ 1.17140365e+00, -8.52712810e-01, 3.50255311e-01],
[ 1.98944941e-01, -1.19126296e+00, 1.22076523e+00]],
[[ 9.50673163e-01, -1.95533931e+00, 1.93096864e+00],
[ 8.27464819e-01, 1.80340135e+00, -1.00497380e-01],
[-3.06752473e-01, 9.29595351e-01, 9.11372125e-01],
...,
[-6.20356381e-01, 1.99911785e+00, 3.00052881e+00],
[ 9.56641078e-01, 1.15635550e+00, 2.88816839e-01],
[-1.29952574e+00, 9.98682022e-01, -8.74243736e-01]],
...,
[[ 1.38523817e+00, -1.39550459e+00, -3.16309631e-01],
[ 5.65865397e-01, 1.41154981e+00, -9.45602715e-01],
[ 3.28790307e-01, 6.75092459e-01, -3.11725944e-01],
...,
[-1.44440079e+00, -6.79445326e-01, 1.43111885e+00],
[ 5.58771789e-01, 2.69900143e-01, 5.49213231e-01],
[ 1.09105051e-01, -2.23509058e-01, -1.66373253e+00]],
[[ 3.61166507e-01, 1.90614533e+00, -1.44525254e+00],
[ 1.47818542e+00, -4.42233801e-01, 1.95012164e+00],
[-2.62861991e+00, -1.20255554e+00, 2.49987674e+00],
...,
[ 2.41416669e+00, -1.06618516e-02, 3.04469585e-01],
[ 3.31308961e-01, 1.93402755e+00, -1.58094287e+00],
[ 1.25814950e+00, 3.19189280e-01, -1.12254238e+00]],
[[-1.32635367e+00, -1.06061429e-01, -1.24751687e+00],
[-1.79521942e+00, 1.38259780e+00, 1.68180871e+00],
[-4.76227462e-01, 8.78690183e-01, 3.95061970e-01],
...,
[ 1.94204152e-01, 8.37682307e-01, -6.22466132e-02],
[-4.59587008e-01, -4.31826599e-02, -2.16067743e+00],
[ 1.11848414e+00, -2.64064848e-01, 8.31967413e-01]]],
...,
[[[-3.24997336e-01, 1.08034515e+00, 6.92673996e-02],
[ 1.05060041e-01, 1.39178380e-01, -1.46256888e+00],
[-1.83549687e-01, 1.22453012e-02, 1.20745516e+00],
...,
[ 5.03376536e-02, 1.08175087e+00, -1.37437296e+00],
[-1.37099671e+00, 3.59743834e-01, -1.26847059e-01],
[-5.74307382e-01, 5.04446864e-01, 1.17379643e-01]],
[[ 4.12333421e-02, 1.52342355e+00, 2.69585729e-01],
[-9.09270406e-01, 4.78420645e-01, -1.11571062e+00],
[ 1.19375551e+00, -7.09048092e-01, 8.88599157e-01],
...,
[ 4.73948568e-02, 4.98938709e-01, -1.08797097e+00],
[ 8.33462119e-01, 9.08865273e-01, -1.46219552e+00],
[ 5.32921314e-01, 1.06796491e+00, -1.93718165e-01]],
[[ 1.25406015e+00, -3.50292444e-01, -1.30191416e-01],
[-2.75178581e-01, -7.00684726e-01, -8.93072426e-01],
[ 1.01272747e-01, 9.87824798e-01, 6.57207191e-01],
...,
[ 1.33347639e-03, 7.50014500e-04, 5.11407077e-01],
[ 3.24216008e-01, 1.15871239e+00, -5.61861396e-01],
[-1.33608952e-01, 7.31391490e-01, -1.59337521e-02]],
...,
[[-5.49170852e-01, -1.69810963e+00, -8.18801150e-02],
[-4.80068922e-01, -1.13038115e-01, 1.62172318e+00],
[-1.69122827e+00, -3.67237926e-01, -5.89039683e-01],
...,
[ 4.33714420e-01, -5.34618318e-01, 1.38874781e+00],
[ 5.81656456e-01, 8.31009567e-01, -2.23122224e-01],
[ 4.18670475e-02, -1.33776629e+00, -4.29855734e-01]],
[[ 5.38588226e-01, 5.70344329e-01, 4.37376589e-01],
[-1.95507479e+00, 8.03013563e-01, 1.77007222e+00],
[ 5.48814654e-01, -1.85685351e-01, -7.03386962e-02],
...,
[ 5.11914551e-01, -3.42784077e-03, 8.44469965e-01],
[ 4.18510020e-01, -2.31981903e-01, -1.44743538e+00],
[ 1.48373485e+00, 5.09651862e-02, 1.63715124e-01]],
[[ 7.68731117e-01, 3.42273176e-01, -4.06700999e-01],
[ 2.19369984e+00, 1.45487654e+00, 1.17882729e+00],
[ 9.26257491e-01, 5.75700700e-01, -7.29449809e-01],
...,
[ 2.00472832e-01, 4.45106894e-01, -4.05370653e-01],
[-1.78351879e+00, 7.03896523e-01, -1.05813706e+00],
[-9.94620681e-01, -1.71380520e+00, 1.09512174e+00]]],
[[[-1.38506696e-01, 5.42279065e-01, 1.79691344e-01],
[ 9.50728357e-01, 4.53848094e-01, -5.73166311e-01],
[-2.00528908e+00, 3.89156401e-01, -1.31658947e+00],
...,
[ 1.82688847e-01, 3.01198423e-01, -1.26344955e+00],
[-1.56631064e+00, -1.69837511e+00, 1.84550524e-01],
[ 1.18606985e+00, -1.72423112e+00, -1.99016929e+00]],
[[ 2.36121863e-01, -4.75903153e-01, 1.08353925e+00],
[-9.33088899e-01, 1.87986806e-01, -8.00343037e-01],
[-8.69377911e-01, -2.67569665e-02, -8.52958083e-01],
...,
[-1.27807513e-01, 1.04186952e+00, -5.27602017e-01],
[ 1.17140365e+00, -8.52712810e-01, 3.50255311e-01],
[ 1.98944941e-01, -1.19126296e+00, 1.22076523e+00]],
[[ 9.50673163e-01, -1.95533931e+00, 1.93096864e+00],
[ 8.27464819e-01, 1.80340135e+00, -1.00497380e-01],
[-3.06752473e-01, 9.29595351e-01, 9.11372125e-01],
...,
[-6.20356381e-01, 1.99911785e+00, 3.00052881e+00],
[ 9.56641078e-01, 1.15635550e+00, 2.88816839e-01],
[-1.29952574e+00, 9.98682022e-01, -8.74243736e-01]],
...,
[[ 1.38523817e+00, -1.39550459e+00, -3.16309631e-01],
[ 5.65865397e-01, 1.41154981e+00, -9.45602715e-01],
[ 3.28790307e-01, 6.75092459e-01, -3.11725944e-01],
...,
[-1.44440079e+00, -6.79445326e-01, 1.43111885e+00],
[ 5.58771789e-01, 2.69900143e-01, 5.49213231e-01],
[ 1.09105051e-01, -2.23509058e-01, -1.66373253e+00]],
[[ 3.61166507e-01, 1.90614533e+00, -1.44525254e+00],
[ 1.47818542e+00, -4.42233801e-01, 1.95012164e+00],
[-2.62861991e+00, -1.20255554e+00, 2.49987674e+00],
...,
[ 2.41416669e+00, -1.06618516e-02, 3.04469585e-01],
[ 3.31308961e-01, 1.93402755e+00, -1.58094287e+00],
[ 1.25814950e+00, 3.19189280e-01, -1.12254238e+00]],
[[-1.32635367e+00, -1.06061429e-01, -1.24751687e+00],
[-1.79521942e+00, 1.38259780e+00, 1.68180871e+00],
[-4.76227462e-01, 8.78690183e-01, 3.95061970e-01],
...,
[ 1.94204152e-01, 8.37682307e-01, -6.22466132e-02],
[-4.59587008e-01, -4.31826599e-02, -2.16067743e+00],
[ 1.11848414e+00, -2.64064848e-01, 8.31967413e-01]]],
[[[ 1.20201147e+00, -8.83543193e-01, -1.12299490e+00],
[-8.19856003e-02, 5.83427429e-01, -1.45821238e+00],
[ 1.63643444e+00, -4.77334172e-01, -9.33554620e-02],
...,
[ 2.30276048e-01, 4.36146222e-02, -6.39392972e-01],
[ 5.87771654e-01, -1.15736961e+00, -7.33492374e-01],
[-1.09628752e-01, -1.71469414e+00, 1.58899021e+00]],
[[ 7.30902970e-01, 1.24105108e+00, 1.67498720e+00],
[ 3.88401181e-01, -9.23305303e-02, -4.93670672e-01],
[ 7.04704106e-01, 1.86581564e+00, -1.00792277e+00],
...,
[-3.60848755e-01, -3.15460950e-01, -8.82062376e-01],
[-7.21954644e-01, -5.02984643e-01, 1.72397494e-01],
[ 2.86085188e-01, 4.85102624e-01, -2.48669371e-01]],
[[ 2.49706417e-01, 4.38036323e-02, -8.62110019e-01],
[ 1.80069506e+00, -1.13492525e+00, -3.70662093e-01],
[-9.23371732e-01, 1.56131923e+00, -6.55611932e-01],
...,
[-6.89867020e-01, 7.22024500e-01, 1.34575093e+00],
[ 8.25139228e-04, 6.98119998e-01, 1.47431910e+00],
[ 8.07521403e-01, -4.08250354e-02, -2.21133277e-01]],
...,
[[ 5.36121249e-01, -1.42319596e+00, 1.64812410e+00],
[ 6.36555791e-01, -7.29093969e-01, -3.54603082e-01],
[ 1.06471086e+00, -1.54826222e-02, 9.66648042e-01],
...,
[ 1.08888209e-01, 1.95719934e+00, -1.06100845e+00],
[-5.52561879e-01, 8.12764823e-01, -9.21698987e-01],
[ 6.81050241e-01, -1.33105898e+00, 3.16305906e-01]],
[[-3.96734148e-01, -4.08547036e-02, 1.58278000e+00],
[ 9.27038729e-01, 6.19541109e-01, 1.11644495e+00],
[-1.53171265e+00, -4.69903290e-01, -5.21757662e-01],
...,
[ 1.61403012e+00, -1.16159689e+00, -9.60660338e-01],
[-8.55328560e-01, -3.60926479e-01, -4.37765479e-01],
[-1.88913345e+00, -5.02691388e-01, -9.41035390e-01]],
[[ 7.95363843e-01, 1.33137420e-01, 1.15496421e+00],
[ 1.69127953e+00, 1.14485458e-01, 2.12314948e-01],
[ 5.22961974e-01, 5.59924403e-03, 2.20313215e+00],
...,
[ 5.01238443e-02, -3.25213939e-01, -1.78129387e+00],
[-1.78960097e+00, 6.52384996e-01, -6.65673614e-01],
[-5.58095336e-01, 2.51713109e+00, -1.46691442e+00]]]],
dtype=float32)>
数据增幅
考虑怎么实现非线性激活函数 ReLU 的问题。它其实可以通过简单的数据限幅运算实
现,限制元素的范围𝑦 ∈ [0,+∞)即可。
在 TensorFlow 中,可以通过 tf.maximum(x, a)实现数据的下限幅,即𝑦 ∈ [𝑏,+∞);可