def __init__(self): self.config = TCNNConfig() self.model = TextCNN(self.config) self.session = tf.Session() self.session.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess=self.session, save_path=save_path)
logging.info(metrics.classification_report(y_test_cls, y_pred_cls)) # 混淆矩阵 logging.info("Confusion Matrix...") cm = metrics.confusion_matrix(y_test_cls, y_pred_cls) logging.info(cm) time_dif = get_time_dif(start_time) logging.info("Time usage:", time_dif) if __name__ == '__main__': logging.basicConfig(format="%(asctime)s : %(levelname)s : %(message)s", level=logging.INFO) # 显示INFO等级以上日志 if len(sys.argv) != 2 or sys.argv[1] not in ['train', 'test']: raise ValueError("""usage: python run_cnn.py [train / test]""") logging.info('Configuring CNN model...') config = TCNNConfig() # if not os.path.exists(vocab_dir): # 如果不存在词汇表,重建 # build_voca b(train_dir, vocab_dir, config.vocab_size) # categories, cat_to_id = read_category() # words, word_to_id = read_vocab(vocab_dir) # config.context_size = len(words) model = TextCNN(config) if sys.argv[1] == 'train': train2(restore=False) else: test()
level=logging.INFO) # 显示INFO等级以上日志 if len(sys.argv) != 2 or sys.argv[1] not in ['train', 'test']: raise ValueError("""usage: python run_cnn.py [train / test]""") logging.info('Configuring CNN model...') config = TCNNConfig() wv_model = get_wordvec_model() # 随机初始化词向量 wv_model_vectors = np.random.randn(len(wv_model.wv.vectors), config.embedding_dim) np.save('random_embedding.npy', wv_model_vectors) logging.info('Saved random embedding vectors.') embedding_layer_w = np.row_stack( (wv_model_vectors.astype('float32'), np.zeros(shape=(config.embedding_dim)).astype('float32'))) # embedding_layer_w = np.row_stack((wv_model.wv.vectors, np.zeros(shape=(config.embedding_dim)).astype('float32'))) logging.info('embedding_layer_w shape: {}'.format(embedding_layer_w.shape)) # embedding_layer_w = np.load("cache/train2.model.wv.vectors.npy").astype('float32') vocab_size = embedding_layer_w.shape[0] logging.info('vocab_size:{}'.format(vocab_size)) model = TextCNN(config, vocab_size=vocab_size, embedding_layer_w=embedding_layer_w) if sys.argv[1] == 'train': train2(restore=False) # generateDataMatrix() else: # generateDataMatrix() test()