由于一些原因,視頻錄制要告一段落了。再寫一篇關(guān)于cntk的文章分享出來吧。我也很想將這個(gè)事情進(jìn)行下去。以后如果條件允許還會接著做。
成都創(chuàng)新互聯(lián)服務(wù)熱線:13518219792,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)頁制作領(lǐng)域十余年,包括成都OPP膠袋等多個(gè)行業(yè)擁有多年建站經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián),為企業(yè)保駕護(hù)航!cntk2.0框架生成的模型才可以支持python。1.0不支持。
python可以導(dǎo)入cntk.exe生成的框架,也可以導(dǎo)入python調(diào)用cntk生成的框架。舉兩個(gè)例子:
1 、導(dǎo)入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))