用python生成与调用cntk模型代码演示方法-创新互联

由于一些原因,视频录制要告一段落了。再写一篇关于cntk的文章分享出来吧。我也很想将这个事情进行下去。以后如果条件允许还会接着做。

成都创新互联服务热线:13518219792,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联网页制作领域十余年,包括成都OPP胶袋等多个行业拥有多年建站经验,选择成都创新互联,为企业保驾护航!

cntk2.0框架生成的模型才可以支持python。1.0不支持。

python可以导入cntk.exe生成的框架,也可以导入python调用cntk生成的框架。举两个例子:

1 、导入cntk.exe生成的框架。

from cntk.ops.functions import load_model
from PIL import Image 
import numpy as np
from sklearn.utils import shuffle

np.random.seed(0)


def generate(N, mean, cov, diff):  
  #import ipdb;ipdb.set_trace()

  samples_per_class = int(N/2)

  X0 = np.random.multivariate_normal(mean, cov, samples_per_class)
  Y0 = np.zeros(samples_per_class)

  for ci, d in enumerate(diff):
    X1 = np.random.multivariate_normal(mean+d, cov, samples_per_class)
    Y1 = (ci+1)*np.ones(samples_per_class)

    X0 = np.concatenate((X0,X1))
    Y0 = np.concatenate((Y0,Y1))

  X, Y = shuffle(X0, Y0)

  return X,Y
mean = np.random.randn(2)
cov = np.eye(2) 
features, labels = generate(6, mean, cov, [[3.0], [3.0, 0.0]])
features= features.astype(np.float32) 
labels= labels.astype(np.int) 
print(features)
print(labels)



z = load_model("MC.dnn")


print(z.parameters[0].value)
print(z.parameters[0])
print(z)
print(z.uid)
#print(z.signature)
#print(z.layers[0].E.shape)
#print(z.layers[2].b.value)
for index in range(len(z.inputs)):
   print("Index {} for input: {}.".format(index, z.inputs[index]))

for index in range(len(z.outputs)):
   print("Index {} for output: {}.".format(index, z.outputs[index].name))

import cntk as ct
z_out = ct.combine([z.outputs[2].owner])

predictions = np.squeeze(z_out.eval({z_out.arguments[0]:[features]}))

ret = list()
for t in predictions:
  ret.append(np.argmax(t))
top_class = np.argmax(predictions)
print(ret)
print("predictions{}.top_class{}".format(predictions,top_class)) 


本文题目:用python生成与调用cntk模型代码演示方法-创新互联
网页网址:http://azwzsj.com/article/dggohh.html