深度学习2.0-22.Keras高层接口之自定义层或网络

文章目录

      • 1.keras.Sequential
      • 2.Layer/Model
      • 3.自定义层
      • 4.自定义网络
      • 5.自定义网络实战-手写数字识别

深度学习2.0-22.Keras高层接口之自定义层或网络_第1张图片

1.keras.Sequential

深度学习2.0-22.Keras高层接口之自定义层或网络_第2张图片
深度学习2.0-22.Keras高层接口之自定义层或网络_第3张图片

2.Layer/Model

深度学习2.0-22.Keras高层接口之自定义层或网络_第4张图片

3.自定义层

# 自定义Dense层
class MyDense(layers.Layer):
	# 初始化方法
	def __init__(self,inp_dim,outp_dim):
		# 调用母类的初始化
		super(MyDense,self).__init__()
		# self.add_variable作用是在创建这两个Variable时,同时告诉类这两个variable是需要创建的
		# 当两个容器拼接时,会把这两个variable交给上面的容器来管理,统一管理,不需要人为管理参数
		# 这个函数在母类中实现,所以可以直接调用
		self.kernel = self.add_variable('w',[inp_dim,outp_dim])
		self.bias = self.add_variable('b',[outp_dim])
	
	def call(self,inputs,training = None):
		out = inputs @	self.kernel + self.bias
		return out

4.自定义网络

# 利用自定义层,创建自定义网络(5层)
class MyModel(keras.Model):
	def __init__(self):
		super(MyModel,self).__init__()
		self.fc1 = MyDense(28*28,256)
		self.fc2 = MyDense(256,128)
		self.fc3 = MyDense(128,64)
		self.fc4 = MyDense(64,32)
		self.fc5 = MyDense(32,10)

	# 定义前向传播
	def call(self,inputs,training = None):
		x = self.fc1(inputs)
		x = tf.nn.relu(x)
		
		x = self.fc2(x)
		x = tf.nn.relu(x)	

		x = self.fc3(x)
		x = tf.nn.relu(x)
		x = self.fc4(x)
		x = tf.nn.relu(x)
		x = self.fc5(x)
		return x	

5.自定义网络实战-手写数字识别

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
from tensorflow import keras


# 数据预处理
def preprocess(x, y):
    """
    x is a simple image, not a batch
    """
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [28 * 28])
    y = tf.cast(y, dtype=tf.int32)
    y = tf.one_hot(y, depth=10)
    return x, y


batchsz = 128
# 数据集加载
(x, y), (x_val, y_val) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape, x.min(), x.max())

db = tf.data.Dataset.from_tensor_slices((x, y))
db = db.map(preprocess).shuffle(60000).batch(batchsz)

ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
ds_val = ds_val.map(preprocess).batch(batchsz)

sample = next(iter(db))
print(sample[0].shape, sample[1].shape)

# 构建多层网络
network = Sequential([layers.Dense(256, activation='relu'),
                      layers.Dense(128, activation='relu'),
                      layers.Dense(64, activation='relu'),
                      layers.Dense(32, activation='relu'),
                      layers.Dense(10)])
network.build(input_shape=(None, 28 * 28))
network.summary()

# 自定义构建多层网络
# 自定义层
class MyDense(layers.Layer):

    def __init__(self, inp_dim, outp_dim):
        super(MyDense, self).__init__()

        self.kernel = self.add_variable('w', [inp_dim, outp_dim])
        self.bias = self.add_variable('b', [outp_dim])

    def call(self, inputs, training=None):
        out = inputs @ self.kernel + self.bias

        return out

# 自定义网络
class MyModel(keras.Model):

    def __init__(self):
        super(MyModel, self).__init__()

        self.fc1 = MyDense(28 * 28, 256)
        self.fc2 = MyDense(256, 128)
        self.fc3 = MyDense(128, 64)
        self.fc4 = MyDense(64, 32)
        self.fc5 = MyDense(32, 10)

    def call(self, inputs, training=None):
        x = self.fc1(inputs)
        x = tf.nn.relu(x)
        x = self.fc2(x)
        x = tf.nn.relu(x)
        x = self.fc3(x)
        x = tf.nn.relu(x)
        x = self.fc4(x)
        x = tf.nn.relu(x)
        x = self.fc5(x)

        return x


network = MyModel()

network.compile(optimizer=optimizers.Adam(lr=0.01),
                loss=tf.losses.CategoricalCrossentropy(from_logits=True),
                metrics=['accuracy']
                )

network.fit(db, epochs=5, validation_data=ds_val,
            validation_freq=2)

network.evaluate(ds_val)

sample = next(iter(ds_val))
x = sample[0]
y = sample[1]  # one-hot
pred = network.predict(x)  # [b, 10]
# convert back to number 
y = tf.argmax(y, axis=1)
pred = tf.argmax(pred, axis=1)

print(pred)
print(y)

你可能感兴趣的:(深度学习2.0基础,深度学习,tensorflow)