[{"data":1,"prerenderedAt":548},["ShallowReactive",2],{"content-query-FEzexQ3BOg":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":542,"_id":543,"_source":544,"_file":545,"_stem":546,"_extension":547},"/technology-blogs/zh/615","zh",false,"","开发者分享 | 基于MindSpore体验量子神经网络在自然语言处理的应用","基于MindSpore体验量子神经网络在自然语言处理的应用","2021-06-11","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/d5691ff71c7e48d5a8bfb9def4dfcf6f.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":539},"root",[17,25,34,48,53,58,71,78,85,92,99,106,113,120,127,134,141,148,155,160,167,172,177,182,191,198,209,220,225,238,243,253,258,263,268,273,281,285,290,295,300,305,310,318,322,327,332,340,344,349,354,359,367,371,376,381,389,393,398,403,408,416,420,425,430,438,445,450,455,460,468,472,477,482,489,501,506,517,522,527,532],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"开发者分享-基于mindspore体验量子神经网络在自然语言处理的应用",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/f5956a12cf7e4f72b0d1afd73dbcfdb5.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37,39],{"type":24,"value":38},"本文来源于：",{"type":18,"tag":40,"props":41,"children":45},"a",{"href":42,"rel":43},"https://bbs.huaweicloud.com/forum/forum-1076-1.html",[44],"nofollow",[46],{"type":24,"value":47},"MindSpore论坛",{"type":18,"tag":26,"props":49,"children":50},{},[51],{"type":24,"value":52},"作者：[",{"type":18,"tag":26,"props":54,"children":55},{},[56],{"type":24,"value":57},"JeffDing",{"type":18,"tag":26,"props":59,"children":60},{},[61,63,69],{"type":24,"value":62},"](",{"type":18,"tag":40,"props":64,"children":67},{"href":65,"rel":66},"https://bbs.huaweicloud.com/forum/home.php?mod=space&uid=230987&do=thread",[44],[68],{"type":24,"value":65},{"type":24,"value":70},")",{"type":18,"tag":26,"props":72,"children":73},{},[74],{"type":18,"tag":40,"props":75,"children":77},{"href":65,"rel":76},[44],[],{"type":18,"tag":26,"props":79,"children":80},{},[81],{"type":18,"tag":40,"props":82,"children":84},{"href":65,"rel":83},[44],[],{"type":18,"tag":26,"props":86,"children":87},{},[88],{"type":18,"tag":40,"props":89,"children":91},{"href":65,"rel":90},[44],[],{"type":18,"tag":26,"props":93,"children":94},{},[95],{"type":18,"tag":40,"props":96,"children":98},{"href":65,"rel":97},[44],[],{"type":18,"tag":26,"props":100,"children":101},{},[102],{"type":18,"tag":40,"props":103,"children":105},{"href":65,"rel":104},[44],[],{"type":18,"tag":26,"props":107,"children":108},{},[109],{"type":18,"tag":40,"props":110,"children":112},{"href":65,"rel":111},[44],[],{"type":18,"tag":26,"props":114,"children":115},{},[116],{"type":18,"tag":40,"props":117,"children":119},{"href":65,"rel":118},[44],[],{"type":18,"tag":26,"props":121,"children":122},{},[123],{"type":18,"tag":40,"props":124,"children":126},{"href":65,"rel":125},[44],[],{"type":18,"tag":26,"props":128,"children":129},{},[130],{"type":18,"tag":40,"props":131,"children":133},{"href":65,"rel":132},[44],[],{"type":18,"tag":26,"props":135,"children":136},{},[137],{"type":18,"tag":40,"props":138,"children":140},{"href":65,"rel":139},[44],[],{"type":18,"tag":26,"props":142,"children":143},{},[144],{"type":18,"tag":40,"props":145,"children":147},{"href":65,"rel":146},[44],[],{"type":18,"tag":26,"props":149,"children":150},{},[151],{"type":18,"tag":40,"props":152,"children":154},{"href":65,"rel":153},[44],[],{"type":18,"tag":26,"props":156,"children":157},{},[158],{"type":24,"value":159},"明天就放假啦，祝大家假期愉快，端午安康，今天为大家带来一篇来自开发者JeffDing的技术分享，一起看看量子神经网路在自然语言处理中有什么应用吧！",{"type":18,"tag":26,"props":161,"children":162},{},[163],{"type":18,"tag":30,"props":164,"children":166},{"alt":7,"src":165},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/16defba31c0c4b3d92600988849a130e.png",[],{"type":18,"tag":26,"props":168,"children":169},{},[170],{"type":24,"value":171},"CPU：Intel(R) Core(TM) i7-4712MQ CPU @ 2.30GHz",{"type":18,"tag":26,"props":173,"children":174},{},[175],{"type":24,"value":176},"内存：4GB",{"type":18,"tag":26,"props":178,"children":179},{},[180],{"type":24,"value":181},"操作系统：Ubuntu 20.10",{"type":18,"tag":26,"props":183,"children":184},{},[185],{"type":18,"tag":186,"props":187,"children":188},"strong",{},[189],{"type":24,"value":190},"MindSpore版本：1.2",{"type":18,"tag":26,"props":192,"children":193},{},[194],{"type":18,"tag":30,"props":195,"children":197},{"alt":7,"src":196},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/d3f7acce189b4d3aaa888de7c1486b5b.png",[],{"type":18,"tag":26,"props":199,"children":200},{},[201,203],{"type":24,"value":202},"参考官网安装文档：",{"type":18,"tag":40,"props":204,"children":207},{"href":205,"rel":206},"https://www.mindspore.cn/install/",[44],[208],{"type":24,"value":205},{"type":18,"tag":26,"props":210,"children":211},{},[212,214],{"type":24,"value":213},"安装MindQuantum参照文档 ：",{"type":18,"tag":40,"props":215,"children":218},{"href":216,"rel":217},"https://gitee.com/mindspore/mindquantum/blob/r0.1/README_CN.md",[44],[219],{"type":24,"value":216},{"type":18,"tag":26,"props":221,"children":222},{},[223],{"type":24,"value":224},"通过Mindspore.__version__查看版本",{"type":18,"tag":26,"props":226,"children":227},{},[228,232,234],{"type":18,"tag":30,"props":229,"children":231},{"alt":7,"src":230},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/88552f8a25ed48ba82adc398fc779fbe.jpg",[],{"type":24,"value":233}," ",{"type":18,"tag":30,"props":235,"children":237},{"alt":7,"src":236},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/f7a5437d716a4dd5833d1926c9290e9e.png",[],{"type":18,"tag":26,"props":239,"children":240},{},[241],{"type":24,"value":242},"1.环境准备",{"type":18,"tag":244,"props":245,"children":247},"pre",{"code":246},"#引入包\nimport numpy as np\nimport time\nfrom projectq.ops import QubitOperator\nimport mindspore.ops as ops\nimport mindspore.dataset as ds\nfrom mindspore import nn\nfrom mindspore.train.callback import LossMonitor\nfrom mindspore import Model\nfrom mindquantum.nn import MindQuantumLayer\nfrom mindquantum import Hamiltonian, Circuit, RX, RY, X, H, UN\n\n#数据预处理\ndef GenerateWordDictAndSample(corpus, window=2):\n    all_words = corpus.split()\n    word_set = list(set(all_words))\n    word_set.sort()\n    word_dict = {w: i for i,w in enumerate(word_set)}\n    sampling = []\n    for index, word in enumerate(all_words[window:-window]):\n        around = []\n        for i in range(index, index + 2*window + 1):\n            if i != index + window:\n                around.append(all_words)\n        sampling.append([around,all_words[index + window]])\n    return word_dict, sampling\n\nword_dict, sample = GenerateWordDictAndSample(\"I love natural language processing\")\nprint(word_dict)\nprint('word dict size: ', len(word_dict))\nprint('samples: ', sample)\nprint('number of samples: ', len(sample))\n",[248],{"type":18,"tag":249,"props":250,"children":251},"code",{"__ignoreMap":7},[252],{"type":24,"value":246},{"type":18,"tag":26,"props":254,"children":255},{},[256],{"type":24,"value":257},"运行结果：",{"type":18,"tag":26,"props":259,"children":260},{},[261],{"type":24,"value":262},"[NOTE] Current simulator thread is 1. If your simulation is slow, set OMP_NUM_THREADS to a appropriate number accroding to your model. {'I': 0, 'language': 1, 'love': 2, 'natural': 3, 'processing': 4} word dict size: 5 samples: [[['I', 'love', 'language', 'processing'], 'natural']] number of samples: 1",{"type":18,"tag":26,"props":264,"children":265},{},[266],{"type":24,"value":267},"根据如上信息，我们得到该句子的词典大小为5，能够产生一个样本点。",{"type":18,"tag":26,"props":269,"children":270},{},[271],{"type":24,"value":272},"2.编码线路",{"type":18,"tag":244,"props":274,"children":276},{"code":275},"def Genera**coderCircuit(n_qubits, prefix=''):\n    if len(prefix) != 0 and prefix[-1] != '_':\n        prefix += '_'\n    circ = Circuit()\n    for i in range(n_qubits):\n        circ += RX(prefix + str(i)).on(i)\n    return circ\nGenera**coderCircuit(3,prefix='e')\n",[277],{"type":18,"tag":249,"props":278,"children":279},{"__ignoreMap":7},[280],{"type":24,"value":275},{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":24,"value":257},{"type":18,"tag":26,"props":286,"children":287},{},[288],{"type":24,"value":289},"RX(e_0|0) RX(e_1|1) RX(e_2|2)",{"type":18,"tag":26,"props":291,"children":292},{},[293],{"type":24,"value":294},"我们通常用|0⟩\">|0⟩|0⟩和|1⟩\">|1⟩|1⟩来标记二能级量子比特的两个状态，由态叠加原理，量子比特还可以处于这两个状态的叠加态：",{"type":18,"tag":26,"props":296,"children":297},{},[298],{"type":24,"value":299},"|ψ⟩=α|0⟩+β|1⟩\">|ψ⟩=α|0⟩+β|1⟩|ψ⟩=α|0⟩+β|1⟩",{"type":18,"tag":26,"props":301,"children":302},{},[303],{"type":24,"value":304},"对于n\">nn比特的量子态，其将处于2n\">2n2n维的希尔伯特空间中。对于上面由5个词构成的词典，我们只需要⌈log2⁡5⌉=3\">⌈log25⌉=3⌈log2⁡5⌉=3个量子比特即可完成编码，这也体现出量子计算的优越性。",{"type":18,"tag":26,"props":306,"children":307},{},[308],{"type":24,"value":309},"例如对于上面词典中的“love”，其对应的标签为2，2的二进制表示为010，我们只需将编码线路中的e_0、e_1和e_2分别设为0\">00、π\">ππ和0\">00即可。",{"type":18,"tag":244,"props":311,"children":313},{"code":312},"#通过Evolution算子来验证\nfrom mindquantum.nn import generate_evolution_operator\nfrom mindspore import context\nfrom mindspore import Tensor\n\nn_qubits = 3 # number of qubits of this quantum circuit\nlabel = 2 # label need to encode\nlabel_bin = bin(label)[-1:1:-1].ljust(n_qubits,'0') # binary form of label\nlabel_array = np.array([int(i)*np.pi for i in label_bin]).astype(np.float32) # parameter value of encoder\nencoder = Genera**coderCircuit(n_qubits, prefix='e') # encoder circuit\nencoder_para_names = encoder.parameter_resolver().para_name # parameter names of encoder\n\nprint(\"Label is: \", label)\nprint(\"Binary label is: \", label_bin)\nprint(\"Parameters of encoder is: \\n\", np.round(label_array, 5))\nprint(\"Encoder circuit is: \\n\", encoder)\nprint(\"Encoder parameter names are: \\n\", encoder_para_names)\n\ncontext.set_context(mode=context.GRAPH_MODE, device_target=\"CPU\")\n# quantum state evolution operator\nevol = generate_evolution_operator(param_names=encoder_para_names, circuit=encoder)\nstate = evol(Tensor(label_array))\nstate = state.asnumpy()\nquantum_state = state[:, 0] + 1j * state[:, 1]\namp = np.round(np.abs(quantum_state)**2, 3)\n\nprint(\"Amplitude of quantum state is: \\n\", amp)\nprint(\"Label in quantum state is: \", np.argmax(amp))\n",[314],{"type":18,"tag":249,"props":315,"children":316},{"__ignoreMap":7},[317],{"type":24,"value":312},{"type":18,"tag":26,"props":319,"children":320},{},[321],{"type":24,"value":257},{"type":18,"tag":26,"props":323,"children":324},{},[325],{"type":24,"value":326},"Label is: 2 Binary label is: 010 Parameters of encoder is: [0. 3.14159 0. ] Encoder circuit is: RX(e_0|0) RX(e_1|1) RX(e_2|2) Encoder parameter names are: ['e_0', 'e_1', 'e_2'] Amplitude of quantum state is: [0. 0. 1. 0. 0. 0. 0. 0.] Label in quantum state is: 2",{"type":18,"tag":26,"props":328,"children":329},{},[330],{"type":24,"value":331},"通过上面的验证，我们发现，对于标签为2的数据，最后得到量子态的振幅最大的位置也是2，因此得到的量子态正是对输入标签的编码。我们将对数据编码生成参数数值的过程总结成如下函数。",{"type":18,"tag":244,"props":333,"children":335},{"code":334},"def GenerateTrainData(sample, word_dict):\n    n_qubits = np.int(np.ceil(np.log2(1 + max(word_dict.values()))))\n    data_x = []\n    data_y = []\n    for around, center in sample:\n        data_x.append([])\n        for word in around:\n            label = word_dict[word]\n            label_bin = bin(label)[-1:1:-1].ljust(n_qubits,'0')\n            label_array = [int(i)*np.pi for i in label_bin]\n            data_x[-1].extend(label_array)\n        data_y.append(word_dict[center])\n    return np.array(data_x).astype(np.float32), np.array(data_y).astype(np.int32)\nGenerateTrainData(sample, word_dict)\n",[336],{"type":18,"tag":249,"props":337,"children":338},{"__ignoreMap":7},[339],{"type":24,"value":334},{"type":18,"tag":26,"props":341,"children":342},{},[343],{"type":24,"value":257},{"type":18,"tag":26,"props":345,"children":346},{},[347],{"type":24,"value":348},"(array([[0. , 0. , 0. , 0. , 3.1415927, 0. , 3.1415927, 0. , 0. , 0. , 0. , 3.1415927]], dtype=float32), array([3], dtype=int32))",{"type":18,"tag":26,"props":350,"children":351},{},[352],{"type":24,"value":353},"根据上面的结果，我们将4个输入的词编码的信息合并为一个更长向量，便于后续神经网络调用。",{"type":18,"tag":26,"props":355,"children":356},{},[357],{"type":24,"value":358},"3.Ansatz线路",{"type":18,"tag":244,"props":360,"children":362},{"code":361},"#定义如下函数生成Ansatz线路\ndef GenerateAnsatzCircuit(n_qubits, layers, prefix=''):\n    if len(prefix) != 0 and prefix[-1] != '_':\n        prefix += '_'\n    circ = Circuit()\n    for l in range(layers):\n        for i in range(n_qubits):\n            circ += RY(prefix + str(l) + '_' + str(i)).on(i)\n        for i in range(l % 2, n_qubits, 2):\n            if i \u003C n_qubits and i + 1 \u003C n_qubits:\n                circ += X.on(i + 1, i)\n    return circ\nGenerateAnsatzCircuit(5, 2, 'a')\n",[363],{"type":18,"tag":249,"props":364,"children":365},{"__ignoreMap":7},[366],{"type":24,"value":361},{"type":18,"tag":26,"props":368,"children":369},{},[370],{"type":24,"value":257},{"type":18,"tag":26,"props":372,"children":373},{},[374],{"type":24,"value":375},"RY(a_0_0|0) RY(a_0_1|1) RY(a_0_2|2) RY(a_0_3|3) RY(a_0_4|4) X(1 \u003C-: 0) X(3 \u003C-: 2) RY(a_1_0|0) RY(a_1_1|1) RY(a_1_2|2) RY(a_1_3|3) RY(a_1_4|4) X(2 \u003C-: 1) X(4 \u003C-: 3)",{"type":18,"tag":26,"props":377,"children":378},{},[379],{"type":24,"value":380},"4.测量",{"type":18,"tag":244,"props":382,"children":384},{"code":383},"def GenerateEmbeddingHamiltonian(dims, n_qubits):\n    hams = []\n    for i in range(dims):\n        s = ''\n        for j, k in enumerate(bin(i + 1)[-1:1:-1]):\n            if k == '1':\n                s = s + 'Z' + str(j) + ' '\n        hams.append(Hamiltonian(QubitOperator(s)))\n    return hams\nGenerateEmbeddingHamiltonian(5, 5)\n",[385],{"type":18,"tag":249,"props":386,"children":387},{"__ignoreMap":7},[388],{"type":24,"value":383},{"type":18,"tag":26,"props":390,"children":391},{},[392],{"type":24,"value":257},{"type":18,"tag":26,"props":394,"children":395},{},[396],{"type":24,"value":397},"[1.0 Z0, 1.0 Z1, 1.0 Z0 Z1, 1.0 Z2, 1.0 Z0 Z2]",{"type":18,"tag":26,"props":399,"children":400},{},[401],{"type":24,"value":402},"5.量子版词向量嵌入层",{"type":18,"tag":26,"props":404,"children":405},{},[406],{"type":24,"value":407},"运行之前请在终端运行export OMP_NUM_THREADS=4",{"type":18,"tag":244,"props":409,"children":411},{"code":410},"def QEmbedding(num_embedding, embedding_dim, window, layers, n_threads):\n    n_qubits = int(np.ceil(np.log2(num_embedding)))\n    hams = GenerateEmbeddingHamiltonian(embedding_dim, n_qubits)\n    circ = Circuit()\n    circ = UN(H, n_qubits)\n    encoder_param_name = []\n    ansatz_param_name = []\n    for w in range(2 * window):\n        encoder = Genera**coderCircuit(n_qubits, 'Encoder_' + str(w))\n        ansatz = GenerateAnsatzCircuit(n_qubits, layers, 'Ansatz_' + str(w))\n        encoder.no_grad()\n        circ += encoder\n        circ += ansatz\n        encoder_param_name.extend(list(encoder.parameter_resolver()))\n        ansatz_param_name.extend(list(ansatz.parameter_resolver()))\n    net = MindQuantumLayer(encoder_param_name,\n                           ansatz_param_name,\n                           circ,\n                           hams,\n                           n_threads=n_threads)\n    return net\n\nclass CBOW(nn.Cell):\n    def __init__(self, num_embedding, embedding_dim, window, layers, n_threads,\n                 hidden_dim):\n        super(CBOW, self).__init__()\n        self.embedding = QEmbedding(num_embedding, embedding_dim, window,\n                                    layers, n_threads)\n        self.dense1 = nn.Dense(embedding_dim, hidden_dim)\n        self.dense2 = nn.Dense(hidden_dim, num_embedding)\n        self.relu = ops.ReLU()\n\n    def construct(self, x):\n        embed = self.embedding(x)\n        out = self.dense1(embed)\n        out = self.relu(out)\n        out = self.dense2(out)\n        return out\n    \nclass LossMonitorWithCollection(LossMonitor):\n    def __init__(self, per_print_times=1):\n        super(LossMonitorWithCollection, self).__init__(per_print_times)\n        self.loss = []\n\n    def begin(self, run_context):\n        self.begin_time = time.time()\n\n    def end(self, run_context):\n        self.end_time = time.time()\n        print('Total time used: {}'.format(self.end_time - self.begin_time))\n\n    def epoch_begin(self, run_context):\n        self.epoch_begin_time = time.time()\n\n    def epoch_end(self, run_context):\n        cb_params = run_context.original_args()\n        self.epoch_end_time = time.time()\n        if self._per_print_times != 0 and cb_params.cur_step_num % self._per_print_times == 0:\n            print('')\n\n    def step_end(self, run_context):\n        cb_params = run_context.original_args()\n        loss = cb_params.net_outputs\n\n        if isinstance(loss, (tuple, list)):\n            if isinstance(loss[0], Tensor) and isinstance(loss[0].asnumpy(), np.ndarray):\n                loss = loss[0]\n\n        if isinstance(loss, Tensor) and isinstance(loss.asnumpy(), np.ndarray):\n            loss = np.mean(loss.asnumpy())\n\n        cur_step_in_epoch = (cb_params.cur_step_num - 1) % cb_params.batch_num + 1\n\n        if isinstance(loss, float) and (np.isnan(loss) or np.isinf(loss)):\n            raise ValueError(\"epoch: {} step: {}. Invalid loss, terminating training.\".format(\n                cb_params.cur_epoch_num, cur_step_in_epoch))\n        self.loss.append(loss)\n        if self._per_print_times != 0 and cb_params.cur_step_num % self._per_print_times == 0:\n            print(\"\\repoch: %+3s step: %+3s time: %5.5s, loss is %5.5s\" % (cb_params.cur_epoch_num, cur_step_in_epoch, time.time() - self.epoch_begin_time, loss), flush=True, end='')\n \n\nimport mindspore as ms\nfrom mindspore import context\nfrom mindspore import Tensor\ncontext.set_context(mode=context.GRAPH_MODE, device_target=\"CPU\")\ncorpus = \"\"\"We are about to study the idea of a computational process.\nComputational processes are abstract beings that inhabit computers.\nAs they evolve, processes manipulate other abstract things called data.\nThe evolution of a process is directed by a pattern of rules\ncalled a program. People create programs to direct processes. In effect,\nwe conjure the spirits of the computer with our spells.\"\"\"\n\nms.set_seed(42)\nwindow_size = 2\nembedding_dim = 10\nhidden_dim = 128\nword_dict, sample = GenerateWordDictAndSample(corpus, window=window_size)\ntrain_x,train_y = GenerateTrainData(sample, word_dict)\n\ntrain_loader = ds.NumpySlicesDataset({\n    \"around\": train_x,\n    \"center\": train_y\n},shuffle=False).batch(3)\nnet = CBOW(len(word_dict), embedding_dim, window_size, 3, 4, hidden_dim)\nnet_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')\nnet_opt = nn.Momentum(net.trainable_params(), 0.01, 0.9)\nloss_monitor = LossMonitorWithCollection(500)\nmodel = Model(net, net_loss, net_opt)\nmodel.train(350, train_loader, callbacks=[loss_monitor], dataset_sink_mode=False)\n",[412],{"type":18,"tag":249,"props":413,"children":414},{"__ignoreMap":7},[415],{"type":24,"value":410},{"type":18,"tag":26,"props":417,"children":418},{},[419],{"type":24,"value":257},{"type":18,"tag":26,"props":421,"children":422},{},[423],{"type":24,"value":424},"epoch: 25 step: 20 time: 36.14, loss is 3.154 epoch: 50 step: 20 time: 36.51, loss is 2.945 epoch: 75 step: 20 time: 36.71, loss is 0.226 epoch: 100 step: 20 time: 36.56, loss is 0.016 Total time used: 3668.7517251968384",{"type":18,"tag":26,"props":426,"children":427},{},[428],{"type":24,"value":429},"打印收敛过程中的损失函数值：",{"type":18,"tag":244,"props":431,"children":433},{"code":432},"import matplotlib.pyplot as plt\n",[434],{"type":18,"tag":249,"props":435,"children":436},{"__ignoreMap":7},[437],{"type":24,"value":432},{"type":18,"tag":26,"props":439,"children":440},{},[441],{"type":18,"tag":30,"props":442,"children":444},{"alt":7,"src":443},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/1b5e77c6b9fe477f9a0de791b5a74b6e.jpg",[],{"type":18,"tag":26,"props":446,"children":447},{},[448],{"type":24,"value":449},"打印量子嵌入层的量子线路中的参数",{"type":18,"tag":26,"props":451,"children":452},{},[453],{"type":24,"value":454},"array([-6.4384632e-02, -1.2658586e-01, 1.0083634e-01, -1.3011757e-01, 1.4005195e-03, -1.9296107e-04, -7.9315618e-02, -2.9339856e-01, 7.6259784e-02, 2.9878360e-01, -1.3091319e-04, 6.8271365e-03, -8.5563213e-02, -2.4168481e-01, -8.2548901e-02, 3.0743122e-01, -7.8157615e-04, -3.2907310e-03, -1.4412615e-01, -1.9241245e-01, -7.5561814e-02, -3.1189525e-03, 3.8330450e-03, -1.4486053e-04, -4.8195502e-01, 5.3657538e-01, 3.8986996e-02, 1.7286544e-01, -3.4090234e-03, -9.5573599e-03, -4.8208281e-01, 5.9604627e-01, -9.7009525e-02, 1.8312852e-01, 9.5267012e-04, -1.2261710e-03, 3.4219343e-02, 8.0031365e-02, -4.5349425e-01, 3.7360430e-01, 8.9665735e-03, 2.1575980e-03, -2.3871836e-01, -2.4819574e-01, -6.2781256e-01, 4.3640310e-01, -9.7688911e-03, -3.9542126e-03, -2.4010721e-01, 4.8120108e-02, -5.6876510e-01, 4.3773583e-01, 4.7241263e-03, 1.4138421e-02, -1.2472854e-03, 1.1096644e-01, 7.1980711e-03, 7.3047012e-02, 2.0803964e-02, 1.1490706e-02, 8.6638138e-02, 2.0503466e-01, 4.7177267e-03, -1.8399477e-02, 1.1631225e-02, 2.0587114e-03, 7.6739892e-02, -6.3548386e-02, 1.7298019e-01, -1.9143591e-02, 4.1606693e-04, -9.2881303e-03], dtype=float32)",{"type":18,"tag":26,"props":456,"children":457},{},[458],{"type":24,"value":459},"6.经典版词向量嵌入层",{"type":18,"tag":244,"props":461,"children":463},{"code":462},"class CBOWClassical(nn.Cell):\n    def __init__(self, num_embedding, embedding_dim, window, hidden_dim):\n        super(CBOWClassical, self).__init__()\n        self.dim = 2 * window * embedding_dim\n        self.embedding = nn.Embedding(num_embedding, embedding_dim, True)\n        self.dense1 = nn.Dense(self.dim, hidden_dim)\n        self.dense2 = nn.Dense(hidden_dim, num_embedding)\n        self.relu = ops.ReLU()\n        self.reshape = ops.Reshape()\n\n    def construct(self, x):\n        embed = self.embedding(x)\n        embed = self.reshape(embed, (-1, self.dim))\n        out = self.dense1(embed)\n        out = self.relu(out)\n        out = self.dense2(out)\n        return out\ntrain_x = []\ntrain_y = []\nfor i in sample:\n    around, center = i\n    train_y.append(word_dict[center])\n    train_x.append([])\n    for j in around:\n        train_x[-1].append(word_dict[j])\ntrain_x = np.array(train_x).astype(np.int32)\ntrain_y = np.array(train_y).astype(np.int32)\nprint(\"train_x shape: \", train_x.shape)\nprint(\"train_y shape: \", train_y.shape)\ntrain_loader = ds.NumpySlicesDataset({\n    \"around\": train_x,\n    \"center\": train_y\n},shuffle=False).batch(3)\nnet = CBOWClassical(len(word_dict), embedding_dim, window_size, hidden_dim)\nnet_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')\nnet_opt = nn.Momentum(net.trainable_params(), 0.01, 0.9)\nloss_monitor = LossMonitorWithCollection(500)\nmodel = Model(net, net_loss, net_opt)\nmodel.train(350, train_loader, callbacks=[loss_monitor], dataset_sink_mode=False)\n",[464],{"type":18,"tag":249,"props":465,"children":466},{"__ignoreMap":7},[467],{"type":24,"value":462},{"type":18,"tag":26,"props":469,"children":470},{},[471],{"type":24,"value":257},{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":24,"value":476},"train_x shape: (58, 4) train_y shape: (58,) epoch: 25 step: 20 time: 0.077, loss is 3.156 epoch: 50 step: 20 time: 0.095, loss is 3.025 epoch: 75 step: 20 time: 0.115, loss is 2.996 epoch: 100 step: 20 time: 0.088, loss is 1.773 epoch: 125 step: 20 time: 0.083, loss is 0.172 epoch: 150 step: 20 time: 0.110, loss is 0.008 epoch: 175 step: 20 time: 0.086, loss is 0.003 epoch: 200 step: 20 time: 0.081, loss is 0.001 epoch: 225 step: 20 time: 0.081, loss is 0.000 epoch: 250 step: 20 time: 0.078, loss is 0.000 epoch: 275 step: 20 time: 0.079, loss is 0.000 epoch: 300 step: 20 time: 0.080, loss is 0.000 epoch: 325 step: 20 time: 0.078, loss is 0.000 epoch: 350 step: 20 time: 0.081, loss is 0.000 Total time used: 30.569124698638916",{"type":18,"tag":26,"props":478,"children":479},{},[480],{"type":24,"value":481},"收敛图：",{"type":18,"tag":26,"props":483,"children":484},{},[485],{"type":18,"tag":30,"props":486,"children":488},{"alt":7,"src":487},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/17852ee2c6974717a101fc3cec028948.jpg",[],{"type":18,"tag":26,"props":490,"children":491},{},[492,494,499],{"type":24,"value":493},"由上可知，",{"type":18,"tag":186,"props":495,"children":496},{},[497],{"type":24,"value":498},"通过量子模拟得到的量子版词嵌入模型也能很好的完成嵌入任务",{"type":24,"value":500},"。当数据集大到经典计算机算力难以承受时，量子计算机将能够轻松处理这类问题。",{"type":18,"tag":26,"props":502,"children":503},{},[504],{"type":24,"value":505},"MindSpore官方资料",{"type":18,"tag":26,"props":507,"children":508},{},[509,511],{"type":24,"value":510},"GitHub : ",{"type":18,"tag":40,"props":512,"children":515},{"href":513,"rel":514},"https://github.com/mindspore-ai/mindspore",[44],[516],{"type":24,"value":513},{"type":18,"tag":26,"props":518,"children":519},{},[520],{"type":24,"value":521},"Gitee:[https : //gitee.com/mindspore/mindspore](https : //gitee.com/mindspore/mindspore)",{"type":18,"tag":26,"props":523,"children":524},{},[525],{"type":24,"value":526},"官方QQ群 : 871543426",{"type":18,"tag":26,"props":528,"children":529},{},[530],{"type":24,"value":531},"长按下方二维码加入MindSpore项目↓",{"type":18,"tag":26,"props":533,"children":534},{},[535],{"type":18,"tag":30,"props":536,"children":538},{"alt":7,"src":537},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/06/11/84aa70f8a73042c997c9592aea0adadd.jpg",[],{"title":7,"searchDepth":540,"depth":540,"links":541},4,[],"markdown","content:technology-blogs:zh:615.md","content","technology-blogs/zh/615.md","technology-blogs/zh/615","md",1776506138713]