数据初始化
import tensorflow as tf
from tensorflow import keras
from tensorflow. keras. layers import * ( ( x_train, y_train) , ( x_test, y_test) ) = keras. datasets. mnist. load_data( ) x_train = x_train. reshape( 60000 , - 1 )
y_train = keras. utils. np_utils. to_categorical( y_train)
SimpleRNN
model1 = keras. Sequential( )
model1. add( Embedding( input_dim= 256 , output_dim= 5 ) )
model1. add( SimpleRNN( units= 2 ) )
model1. add( Dense( 10 ) ) model1. compile ( loss= "categorical_crossentropy" , optimizer= "adam" , metrics= [ "accuracy" ] )
model1. fit( x_train, y_train, batch_size= 10 )
GRU
model2 = keras. Sequential( )
model2. add( Embedding( input_dim= 256 , output_dim= 5 ) )
model2. add( GRU( units= 2 ) )
model2. add( Dense( 10 ) ) model2. compile ( loss= "categorical_crossentropy" , optimizer= "adam" , metrics= [ "accuracy" ] )
model2. fit( x_train, y_train, batch_size= 10 )
LSTM
model3 = keras. Sequential( )
model3. add( Embedding( input_dim= 256 , output_dim= 5 ) )
model3. add( LSTM( units= 2 ) )
model3. add( Dense( 10 ) ) model3. compile ( loss= "categorical_crossentropy" , optimizer= "adam" , metrics= [ "accuracy" ] )
model3. fit( x_train, y_train, batch_size= 10 )
encoder-decoder
model3 = keras. Sequential( )
model3. add( Embedding( input_dim= 256 , output_dim= 5 ) )
model3. add( LSTM( units= 2 ) )
model3. add( Dense( 10 ) ) model3. compile ( loss= "categorical_crossentropy" , optimizer= "adam" , metrics= [ "accuracy" ] )
model3. fit( x_train, y_train, batch_size= 10 )
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】 进行投诉反馈!