janelu9 commented on issue #8065: Help? when I go on training a language model, 
errors occured
URL: 
https://github.com/apache/incubator-mxnet/issues/8065#issuecomment-332734162
 
 
   this is a  n-gram model
   ```
   # -*- coding: utf-8 -*-
   """
   Created on Mon Sep 25 14:57:08 2017
   
   @author: T800
   """
   import mxnet as mx
   import numpy as np
   from N_gram import N_gram
   from word2vec_io import BucketSentenceIter
   import pickle as pk
   
   def Perplexity(label, pred):
       label = label.T.reshape((-1,))
       loss = 0.
       for i in range(pred.shape[0]):
           loss += -np.log(max(1e-10, pred[i][int(label[i])]))
       return np.exp(loss / label.size)
   
   if __name__ == '__main__':
       batch_size=64
       hidden_num = 128
       embed_dim = 128
       N=5
       vocab=pk.load(open('.//data//dict_news_lstm20160804.pkl','r'))
       vocab_size=len(vocab)
       data=pk.load(open('.//data//train.pkl','r'))
       data=[i[0] for i in data]
       data_val=pk.load(open('.//data//test.pkl','r'))
       data_val=[i[0] for i in data_val]
       def sym_gen(seq_len):
           return N_gram(seq_len,batch_size,vocab_size,embed_dim,hidden_num,N)
       train=BucketSentenceIter(data,batch_size,N)
       test=BucketSentenceIter(data_val,batch_size,N)
       symbol=sym_gen
       model = mx.model.FeedForward(ctx=[mx.context.gpu(i) for i in range(1)],
                                    symbol=symbol,
                                    num_epoch=10,
                                    learning_rate=0.01,
                                    momentum=0.0,
                                    wd=0.00001,
                                    
initializer=mx.init.Xavier(factor_type="in", magnitude=2.34))
   
       import logging
       #head = '%(asctime)-15s %(message)s'
       #logging.basicConfig(level=logging.DEBUG, format=head)
       logging.basicConfig(level=logging.DEBUG,
                       filename='./train.log',
                       filemode='w')
       console = logging.StreamHandler()
       console.setLevel(logging.DEBUG)
       logging.getLogger().addHandler(console)
   
       model.fit(X=train, eval_data=test,
                 eval_metric = mx.metric.np(Perplexity),
                          
epoch_end_callback=mx.callback.do_checkpoint("./output/5_gram"),
                 batch_end_callback=mx.callback.Speedometer(batch_size, 100),)
   ```
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to