Tensorflow2.0: tf.Keras之自定义全连接层

自定义的全连接层至少需要实现build、call以及初始化函数:__init__

  1. __init__函数:进行一些必要的参数的初始化
  2. build函数:声明需要更新的参数,如权重、偏置等
  3. call函数:主要网络的实现
import tensorflow as tf
import numpy as np
from sklearn.datasets import load_iris
data = load_iris()
iris_data = np.float32(data.data)
iris_target = (data.target)
iris_target = np.float32(tf.keras.utils.to_categorical(iris_target,num_classes=3))
train_data = tf.data.Dataset.from_tensor_slices((iris_data,iris_target)).batch(128)
#自定义的层-全连接层
class MyLayer(tf.keras.layers.Layer):   #必须要继承Layer类
    def __init__(self, output_dim):
        self.output_dim = output_dim    #把参数加到类中
        super(MyLayer, self).__init__() #向父类注册
    def build(self, input_shape):
        self.weight = tf.Variable(tf.random.normal([input_shape[-1],self.output_dim]), name="dense_weight")
        self.bias = tf.Variable(tf.random.normal([self.output_dim]), name="bias_weight")
        super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!
    def call(self, input_tensor):
        out = tf.matmul(input_tensor,self.weight) + self.bias
        out = tf.nn.relu(out)
        out = tf.keras.layers.Dropout(0.1)(out)
        return out
#网络构建
input_xs  = tf.keras.Input(shape=(4), name='input_xs')
out = tf.keras.layers.Dense(32, activation='relu', name='dense_1')(input_xs)
out = MyLayer(32)(out)					#自定义层
out = MyLayer(48)(out)					#自定义层
out = tf.keras.layers.Dense(64, activation='relu', name='dense_2')(out)
logits = tf.keras.layers.Dense(3, activation="softmax",name='predictions')(out)
model = tf.keras.Model(inputs=input_xs, outputs=logits)
print(model.summary())                                  #打印网络结构
#网络训练
opt = tf.optimizers.Adam(1e-3)
model.compile(optimizer=tf.optimizers.Adam(1e-3), loss=tf.losses.categorical_crossentropy,metrics = ['accuracy'])
model.fit(train_data, epochs=1000)
score = model.evaluate(iris_data, iris_target)
print("last score:",score)

 

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