# 第4章(4.4～4.6节)自定义层与计算【深度学习计算】--动手学深度学习【Tensorflow2.0版本】

UC 伯克利李沐的《动手学深度学习》开源书一经推出便广受好评。很多开发者使用了书的内容，并采用各种各样的深度学习框架将其复现。

# 4.1 自定义层

import tensorflow as tf
import numpy as np
print(tf.__version__)
2.0.0
X = tf.random.uniform((2,20))

## 4.4.1 custom layer without parameters

class CenteredLayer(tf.keras.layers.Layer):
def __init__(self):
super().__init__()

def call(self, inputs):
return inputs - tf.reduce_mean(inputs)

layer = CenteredLayer()
layer(np.array([1,2,3,4,5]))
<tf.Tensor: id=11, shape=(5,), dtype=int32, numpy=array([-2, -1,  0,  1,  2])>

net = tf.keras.models.Sequential()

Y = net(X)
Y
<tf.Tensor: id=42, shape=(2, 20), dtype=float32, numpy=
array([[-0.2791378 , -0.80257636, -0.8498672 , -0.8917849 , -0.43128002,
0.2557137 , -0.51745236,  0.31894356,  0.03016172,  0.5299317 ,
-0.094203  , -0.3885942 ,  0.6737736 ,  0.5981153 ,  0.30068082,
0.42632163,  0.3067779 ,  0.07029241,  0.0343143 ,  0.41021633],
[ 0.0257766 , -0.4703896 , -0.9074424 , -1.2818251 ,  0.17860745,
0.11847494, -0.14939149,  0.20248316, -0.140678  ,  0.6033463 ,
0.13899392, -0.08732668,  0.08497022,  0.8094018 ,  0.20579913,
0.40613335,  0.2509889 ,  0.34718364, -0.6298219 ,  0.59436864]],
dtype=float32)>

tf.reduce_mean(Y)
<tf.Tensor: id=44, shape=(), dtype=float32, numpy=-2.9802323e-09>

## 4.4.2 custom layer with parameters

class myDense(tf.keras.layers.Layer):
def __init__(self, units):
super().__init__()
self.units = units

def build(self, input_shape):     # 这里 input_shape 是第一次运行call()时参数inputs的形状
shape=[input_shape[-1], self.units], initializer=tf.random_normal_initializer())
shape=[self.units], initializer=tf.zeros_initializer())

def call(self, inputs):
y_pred = tf.matmul(inputs, self.w) + self.b
return y_pred

dense = myDense(3)
dense(X)
dense.get_weights()
[array([[ 0.05307531, -0.01968029,  0.00317079],
[-0.03745286, -0.0031012 , -0.0925727 ],
[ 0.00653961, -0.0849395 , -0.00591413],
[-0.03926834,  0.03737333, -0.08176559],
[-0.02961348,  0.00735149, -0.04053285],
[-0.0769348 , -0.01365675,  0.04430145],
[ 0.05790468,  0.06002709,  0.00588025],
[ 0.00912714, -0.04544574, -0.08150417],
[ 0.01794734, -0.06478786, -0.0466853 ],
[ 0.0007794 ,  0.07972597,  0.01827623],
[ 0.04688237,  0.040658  ,  0.04173873],
[ 0.07974287, -0.01226464,  0.03872328],
[ 0.023996  , -0.044014  ,  0.01851312],
[-0.04491149,  0.00450119,  0.03688556],
[ 0.01733875, -0.01641337,  0.06909126],
[-0.07539   , -0.0878872 ,  0.0091918 ],
[-0.00092481, -0.06399333,  0.00150875],
[-0.01826238, -0.06126164, -0.05938709],
[ 0.04794892,  0.03742057, -0.0018529 ],
[ 0.03086024,  0.00513093, -0.04271856]], dtype=float32),
array([0., 0., 0.], dtype=float32)]

net = tf.keras.models.Sequential()

net(X)
<tf.Tensor: id=121, shape=(2, 1), dtype=float32, numpy=
array([[-0.00446665],
[-0.0158301 ]], dtype=float32)>

# 4.5 读取和存储

import tensorflow as tf
import numpy as np
print(tf.__version__)
2.0.0

## 4.5.1 load and save NDarray

import numpy as np

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

np.save('x.npy', x)
x2
array([1., 1., 1.], dtype=float32)

y = tf.zeros(4)
np.save('xy.npy',[x,y])
(x2, y2)
(<tf.Tensor: id=6, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>,
<tf.Tensor: id=7, shape=(4,), dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>)

mydict = {'x': x, 'y': y}
np.save('mydict.npy', mydict)
mydict2
array({'x': <tf.Tensor: id=8, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>, 'y': <tf.Tensor: id=9, shape=(4,), dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>},
dtype=object)

## 4.5.2 load and save model parameters

X = tf.random.normal((2,20))
X
<tf.Tensor: id=15, shape=(2, 20), dtype=float32, numpy=
array([[ 1.4838032 , -0.638382  , -0.7836789 ,  1.8679693 , -0.73148364,
-0.12649764, -0.2709544 , -0.33071974,  0.08754155, -0.11141171,
0.18274567, -0.64928424, -0.6519136 ,  0.07320689, -0.5973234 ,
1.9181312 ,  0.47066143, -0.10463867, -0.48717928,  0.3107364 ],
[ 0.37838233,  0.11170077, -1.3378098 ,  0.3618399 ,  0.27140674,
0.9901546 ,  1.4799279 ,  1.2373866 , -0.62953895, -1.5107338 ,
-1.6658096 , -0.08139827,  0.5444429 ,  0.94359463, -0.00676966,
-1.5311289 , -0.30671307,  0.38309866, -0.2765001 , -0.61528987]],
dtype=float32)>
class MLP(tf.keras.Model):
def __init__(self):
super().__init__()
self.flatten = tf.keras.layers.Flatten()    # Flatten层将除第一维（batch_size）以外的维度展平
self.dense1 = tf.keras.layers.Dense(units=256, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(units=10)

def call(self, inputs):
x = self.flatten(inputs)
x = self.dense1(x)
output = self.dense2(x)
return output

net = MLP()
Y = net(X)
Y
<tf.Tensor: id=71, shape=(2, 10), dtype=float32, numpy=
array([[-0.30077258,  0.4493576 ,  0.00761353, -0.14657806, -0.11702831,
-0.20244044,  0.15949515, -0.025849  , -0.36856648,  0.23903428],
[-0.09660852,  0.0096112 ,  0.3435048 , -0.066409  , -0.24335058,
-0.01852736,  0.77680373, -0.04183513, -0.232623  , -0.5856861 ]],
dtype=float32)>

net.save_weights("4.5saved_model.h5")

net2 = MLP()
net2(X)
Y2 = net2(X)
Y2 == Y
<tf.Tensor: id=146, shape=(2, 10), dtype=bool, numpy=
array([[ True,  True,  True,  True,  True,  True,  True,  True,  True,
True],
[ True,  True,  True,  True,  True,  True,  True,  True,  True,
True]])>

# 4.6 GPU计算

## 注意：需要tensorflow-gpu

import tensorflow as tf
import numpy as np
print(tf.__version__)

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print("可用的GPU：",gpus,"\n可用的CPU：", cpus)
2.0.0

## check available device

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 15592483132577835191
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 3063309926
locality {
bus_id: 1
}
}
incarnation: 3859243074925251015
physical_device_desc: "device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5"
]

## specify device

with tf.device('GPU:0'):
a = tf.constant([1,2,3],dtype=tf.float32)
b = tf.random.uniform((3,))
print(tf.exp(a + b) * 2)

tf.Tensor([12.172885 19.682476 53.18001 ], shape=(3,), dtype=float32)

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

土豆洋芋山药蛋

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

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
03-22 827

02-16 153
02-08