def run_fasttext_infer(): """run infer with FastText""" dataset = load_infer_dataset(batch_size=config.batch_size, datafile=args.data_path) fasttext_model = FastText(config.vocab_size, config.embedding_dims, config.num_class) parameter_dict = load_checkpoint(args.model_ckpt) load_param_into_net(fasttext_model, parameter_dict=parameter_dict) ft_infer = FastTextInferCell(fasttext_model) model = Model(ft_infer) predictions = [] target_sens = [] for batch in dataset.create_dict_iterator(output_numpy=True, num_epochs=1): target_sens.append(batch['label_idx']) src_tokens = Tensor(batch['src_tokens'], mstype.int32) src_tokens_length = Tensor(batch['src_tokens_length'], mstype.int32) predicted_idx = model.predict(src_tokens, src_tokens_length) predictions.append(predicted_idx.asnumpy()) from sklearn.metrics import accuracy_score, classification_report target_sens = np.array(target_sens).flatten() predictions = np.array(predictions).flatten() acc = accuracy_score(target_sens, predictions) result_report = classification_report(target_sens, predictions, target_names=target_label1) print("********Accuracy: ", acc) print(result_report)
def __init__(self, vocab_size, embedding_dims, num_class): super(FastTextNetWithLoss, self).__init__() self.fasttext = FastText(vocab_size, embedding_dims, num_class) self.loss_func = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') self.squeeze = P.Squeeze(axis=1) self.print = P.Print()
def run_fasttext_export(): """export function""" fasttext_model = FastText(config.vocab_size, config.embedding_dims, config.num_class) parameter_dict = load_checkpoint(args.ckpt_file) load_param_into_net(fasttext_model, parameter_dict) ft_infer = FastTextInferExportCell(fasttext_model) batch_size = config.batch_size if args.device_target == 'GPU': batch_size = config.distribute_batch_size if args.data_name == "ag": src_tokens_shape = [batch_size, 467] src_tokens_length_shape = [batch_size, 1] elif args.data_name == 'dbpedia': src_tokens_shape = [batch_size, 1120] src_tokens_length_shape = [batch_size, 1] elif args.data_name == 'yelp_p': src_tokens_shape = [batch_size, 2955] src_tokens_length_shape = [batch_size, 1] file_name = args.file_name + '_' + args.data_name src_tokens = Tensor(np.ones((src_tokens_shape)).astype(np.int32)) src_tokens_length = Tensor(np.ones((src_tokens_length_shape)).astype(np.int32)) export(ft_infer, src_tokens, src_tokens_length, file_name=file_name, file_format=args.file_format)
def create_network(name, *args, **kwargs): if name == "fasttext": return FastText(*args, **kwargs) raise NotImplementedError(f"{name} is not implemented in the repo")
def fasttext(*args, **kwargs): return FastText(*args, **kwargs)