【发布时间】:2019-01-10 00:42:53
【问题描述】:
我使用https://github.com/Microsoft/CNTK/blob/v2.0/Examples/SequenceClassification/SimpleExample/Python/SequenceClassification.py 提供的代码来训练模型。如何评价?
【问题讨论】:
我使用https://github.com/Microsoft/CNTK/blob/v2.0/Examples/SequenceClassification/SimpleExample/Python/SequenceClassification.py 提供的代码来训练模型。如何评价?
【问题讨论】:
如果您想在 Python 中评估模型,请参阅页面here。如果您想在其他语言中使用您的模型,例如C++/C#,你可以在Model Evalaution页面找到详细信息。
谢谢,
【讨论】:
我通过以下方式得到它:
import cntk as C
from cntk.ops.functions import load_model # Note this
...
...
# saved the model after epochs
for i in range(500):
mb = reader.next_minibatch(minibatch_size, input_map=input_map)
trainer.train_minibatch(mb)
classifier_output.save("model.dnn") # Note this
...
...
# loading the model
model = load_model("model.dnn") # Note this
# converted sentence to numbers and given as sequence
predScores = model(C.Value.one_hot([[1,238,4,4990,7223,1357,2]], 50466)) # Note this
predClass = np.argmax(predScores)
print(predClass)
其中[[1,238,4,4990,7223,1357,2]] 是词汇中单词的索引序列(基本上是训练发生的序列,50466 是词汇的大小。
【讨论】:
在 CNTK 中训练模型时,您可能不需要使用 create_reader/Minibatch 工具。主要是因为测试/生产文件通常很小。模型评估实际上非常简单:
import cntk as C
import pandas as pd
import numpy as np
model = C.load_model(path_to_where_the_model_is_saved) # load your CNTK model
ds = pd.read_csv(filename, delimiter=",") # load your data of course
# we are assuming all data come
# together in a single matrix
X = ds.values[:,0:28].astype('float32') # ensures the right type for CNTK
Y = ds.values[:,28].astype('float32') # last column is the label
X= X / 255 # perform any necessary transformation if any
pred = model(X) # evaluate your test data
pred[pred > 0.5]=1
pred[pred!=1]=0
maxa=np.mean(Y==pred)
print("Accuracy {} ".format(maxa*100.0))
【讨论】: