1. Sequential modeling
mode 1. model = tf.keras.Sequential()
input = tf.random.uniform((1000,100,10)) label = tf.random.uniform((1000,2))
model.fit(input,label,epochs=10,batch_size=50) mode 2. model =
tf.keras.Sequential([
tf.keras.layers.LSTM(100,activation="relu",input_shape=(100,10,)),
tf.keras.layers.Dense(10) ])
input = tf.random.uniform((1000,100,10),minval=0,maxval=1) label =
tf.random.uniform((1000,10),minval=0,maxval=1)
model.fit(input,label,epochs=10,batch_size=50)
2. Function modeling
""" Sequential modeling can not achieve multiple input and other operations as resnet etc. At this time, we can use functional modeling """ import tensorflow as tf input1
= tf.keras.Input(shape=(100,10)) input2 = tf.keras.Input(shape=(100,10)) x1 =
tf.keras.layers.LSTM(100)(input1) x2 = tf.keras.layers.LSTM(100)(input2) x =
tf.concat([x1,x2],axis=-1) pre = tf.keras.layers.Dense(10)(x) # Multiple input model model =
tf.keras.Model(inputs=[input1,input2],outputs=pre)
input1 = tf.random.uniform((1000,100,10),minval=0,maxval=1) input2 =
tf.random.uniform((1000,100,10),minval=0,maxval=1) label =
tf.random.uniform((1000,10),minval=0,maxval=1)
model.fit((input1,input2),label,epochs=10,batch_size=50)
3. Subclass modeling
# By inheritance tf.keras.Model Custom model import tensorflow as tf class
Mymodel(tf.keras.Model): # init Parameters are passed during model initialization This parameter is mainly used to configure some variables def
__init__(self,number_class=10): super(Mymodel,self).__init__() self.dense1 =
tf.keras.layers.Dense(10) self.lstm1 = tf.keras.layers.LSTM(100) self.dense2 =
tf.keras.layers.Dense(number_class) # there call stay model.fit The parameter is automatically passed in x def
call(self,inputs): x = self.lstm1(inputs) x = self.dense1(x) y = self.dense2(x)
return y model = Mymodel(number_class=10)