attractivechaos / kann

A lightweight C library for artificial neural networks
Other
678 stars 117 forks source link

More examples (classify text) #26

Open chrispugmire opened 5 years ago

chrispugmire commented 5 years ago

Hi, basically I'm hoping someone can point me to (or create) an example of a text sentiment conv based classifier using KANN. Doesn't need to be exact just something I can use as a base to start from. Something like this example from keras, even just the core model code would give me a starting point...

embedding_dim = 100 model = Sequential() model.add(layers.Embedding(vocab_size, embedding_dim, input_length=maxlen)) model.add(layers.Conv1D(128, 5, activation='relu')) model.add(layers.GlobalMaxPooling1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.summary()

(example from) https://realpython.com/python-keras-text-classification/

`// here is my guess... but it's pure guess work so may be nonsense :-)

kann_t model_gen_classify(int n_h_flt, int n_h_fc) { int wordsize=10; // lets assume word embedding int sentence=100; // length of each sentence to analyze int wordgroup[]={3,4,5}; // group words in 3,4,5 word groups in conv layers. float dropout = 0.2f; kann_t ann; kad_node_t *t; t = kad_feed(4, 1, 1, sentence, wordsize), t->ext_flag |= KANN_F_IN;
t = kad_relu(kann_layer_conv1d(t, n_h_flt, wordgroup[0], wordsize, 1, 1, 0, 0)); t = kad_relu(kann_layer_conv1d(t, n_h_flt, wordgroup[1], wordsize, 1, 1, 0, 0)); t = kad_relu(kann_layer_conv1d(t, n_h_flt, wordgroup[2], wordsize, 1, 1, 0, 0)); t = kann_layer_dropout(t, dropout); t = kann_layer_dense(t, n_h_fc); t = kad_relu(t); ann = kann_new(kann_layer_cost(t,1, KANN_C_CEB), 0); return ann; } `

chrispugmire commented 5 years ago

The following code seems to work for text classification. I'm sure it's possible to do better, but just to give a starting point for others. The text is first embedded using glove embedding so each word is replaced with 50 vectors.

int wordgroup=7; // convolution of 7 words at a time (optimal according to research)
int stridecol = wordsize;
int striderow = 1;
float dropout = 0.2f;

t = kad_feed(4, 1, 1, sentencelen, wordsize), t->ext_flag |= KANN_F_IN;  
t = kad_relu(kann_layer_conv2d(t, convout,wordgroup,wordsize,striderow,stridecol,0,0));
t = kann_layer_dropout(t, dropout); 
t = kann_layer_dense(t, nclasses); 
t = kad_relu(t);
ann = kann_new(kann_layer_cost(t,2, KANN_C_CEB), 0);