Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

keras - How to add the Count Vectorizer to Simple RNN model?

for my NLP project I used CountVectorizer to Extract Features from a dataset using vectorizer = CountVectorizer(stop_words='english') and all_features = vectorizer.fit_transform(data.Text) and i also wrote a Simple RNN model using keras but I am not sure how to do the padding and the tokeniser step and get the data be trained on the model.

my code for RNN is:

model.add(keras.layers.recurrent.SimpleRNN(units = 1000, activation='relu', use_bias=True)) model.add(keras.layers.Dense(units=1000, input_dim = 2000, activation='sigmoid')) model.add(keras.layers.Dense(units=500, input_dim=1000, activation='relu')) model.add(keras.layers.Dense(units=2, input_dim=500,activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

return model

can someone please give me some advice on this?

Thank you

question from:https://stackoverflow.com/questions/65851158/how-to-add-the-count-vectorizer-to-simple-rnn-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

add ensemble - you don't count vectorize, you use ensemble

https://github.com/dnishimoto/python-deep-learning/blob/master/UFO%20.ipynb

 docs=ufo_df["summary"] #text
 LABELS=['Egg', 'Cross','Sphere', 'Triangle','Disk','Oval','Rectangle','Teardrop']
 #LABELS=['Triangle']
 target=ufo_df[LABELS]
 #print([len(d) for d in docs])
 encoded_docs=[one_hot(d,vocab_size) for d in docs]
 #print([np.max(d) for d in encoded_docs])
 padded_docs = pad_sequences(encoded_docs,   maxlen=max_length, padding='post')

 #print([d for d in padded_docs])
 model=Sequential()
 model.add(Embedding(vocab_size, 8,   input_length=max_length))
 model.add(Flatten())
 model.add(Dense(8, activation='softmax'))
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

 model.fit(padded_docs, target, epochs=50, verbose=0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...