1. Sequential modeling
mode 1. model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(100,activation="relu"))
model.add(tf.keras.layers.Dense(2,activation="sigmoid"))
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=["accuracy"])
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) ])
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=["accuracy"])
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)
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=["accuracy"])
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)
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=["accuracy"])
x = tf.random.uniform((1000,10,20)) y = tf.random.uniform((1000,10))
model.fit(x,y,epochs=10,batch_size=40)
 

Technology
©2019-2020 Toolsou All rights reserved,
2020 year Java industry , General analysis of market environment and position Old farmer's tears : Nine iron rules of mixed workplace Database design use MySQL Processing 100 million level data 【 course 】 How to modify routing table ?vue project main.js usage method 【Java Details of knowledge points 3】 Serializable and Deserialize Renren from the perspective of Technology , Internet Marketing Python list , Yuanzu Aerial photography : Tesla Shanghai factory Model Y A large number of people have been offline