123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import math
- from keras import Input, Model
- from keras.layers import Dense, Concatenate, Embedding, LSTM, Bidirectional, Conv1D, GlobalMaxPooling1D
- def lstm_phrase(input_shape, output_shape=1):
- inputs = Input(shape=input_shape[0])
- x = Embedding(input_shape[1]+1, 16, input_length=input_shape[0])(inputs)
- # x = Dropout(0.2)(x)
- x = Bidirectional(LSTM(32))(x)
- # x = Dropout(0.2)(x)
- x = Dense(16)(x)
- x = Dense(output_shape, activation="sigmoid")(x)
- model = Model(inputs=inputs, outputs=x)
- # model.summary(line_length=100)
- return model
- def text_cnn_phrase(input_shape, output_shape=1):
- inputs = Input(shape=input_shape[0])
- x = Embedding(input_shape[1]+1, 50, input_length=input_shape[0])(inputs)
- x1 = Conv1D(64, 3, activation="relu", padding="same")(x)
- x1 = GlobalMaxPooling1D()(x1)
- x2 = Conv1D(64, 4, activation="relu", padding="same")(x)
- x2 = GlobalMaxPooling1D()(x2)
- x3 = Conv1D(64, 5, activation="relu", padding="same")(x)
- x3 = GlobalMaxPooling1D()(x3)
- x = Concatenate()([x1, x2, x3])
- x = Dense(output_shape, activation="sigmoid")(x)
- model = Model(inputs=inputs, outputs=x)
- model.summary(line_length=100)
- return model
- if __name__ == "__main__":
- text_cnn_phrase((3, 5792))
- print(math.log(5792, 2))
|