022-2345 2937
185 2247 0110
business@forenose.com
QQ:2779623375
用户QQ群3:606835039
用户QQ群4:795287153
订阅号
服务号
客服微信:qianxiu0106
ForeSpider
数据采集引擎
科技资讯:
科技学院:
科技百科:
科技书籍:
网站大全:
软件大全:
CaffeNet | 224 x 224 | 233 MB | 724 MFLOPs | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
VGG-VD-16 | 224 x 224 | 528 MB | 16 GFLOPs | ||||||||
VGG-VD-19 | 224 x 224 | 548 MB | 20 GFLOPs | ||||||||
GoogleNet | 224 x 224 | 51 MB | 2 GFLOPs ResNet-34 | ResNet-152 | SENet 224 x 224 | 224 x 224 | 224 x 224 83 MB | 230 MB | 440 MB 4 GFLOPs | 11 GFLOPs | 21 GFLOPs 数据来源 上表列举了,ImageNet图像识别中常见算法的模型大小以及单张图片一次训练(One Pass)所需要的计算量。 自2012年,Hinton的学生Alex Krizhevsky提出AlexNet,一举摘下ILSVRC 2012的桂冠后,ILSVRC比赛冠军的准确率越来越高。与此同时,其中使用到的深度学习算法也越来越复杂,所需要的计算量也越来越大。SENet与AlexNet相比,计算量多了近30倍。我们知道,ImageNet大概有120万张图片,以SENet为例,如果要完成100个epoch的完整训练,将需要2.52 * 10^18的计算量。如此庞大的计算量,已经远远超出传统的机器学习算法的范畴。更别说,Google在论文 《Revisiting Unreasonable Effectiveness of Data in Deep Learning Era》 中提及的、比ImageNet大300倍的数据集。 物理计算性能 面对如此庞大的计算量,那么,我们业界当前常用的计算单元的计算力是多少呢? CPU 物理核:一般浮点运算能力在10^10 FLOPS量级。一台16 Cores的服务器,大致上有200 GFLOPS的运算能力。实际运行,CPU 大概能用到80%的性能,那就160 GFLOPS的运算能力。完成上述SENet运行,需要182天。 NVIDIA GPGPU: 目前的V100,单精度浮点运算的峰值大概为14 TFLOPS, 实际运行中,我们假设能用到50%的峰值性能,那就是7 TFLOPS,需要4天。 根据以上数据结果可以看出:在深度学习领域,GPU训练数据集所需要耗费的时间,远远少于CPU,这也是当前深度学习训练都是采用GPU的重要原因。 业界的解决方案 从前面的计算可知,即使使用GPU来计算,训练一次ImageNet 也需要4天的时间。但对于算法工程师做实验、调参而言,这种耗时数天的等待是难以忍受的。为此,目前业界针对深度学习训练的加速,提出了各种各样的解决方案。 异构计算的并行方案 数据并行(Data Parallelism) 数据并行,即每个计算单元都保留一份完整的模型拷贝,分别训练不同的数据,经过一个Iteration或若干个Iteration后,把各个计算单元的模型做一次同步。这是最常见的深度学习训练方式,好处在于逻辑简单、代码实现方便。 模型并行(Model Parallelism) 模型并行,即各个计算单元存储同一层模型数据的不同部分,训练相同的数据。相对于数据并行,因为各个运算单元每训练完一层神经网络,就必须要同步一次,频繁的同步通信导致系统不能充分地利用硬件的运算能力,所以更为少见。但是在一些业务场景下,Softmax层需要分类的类别可能会有很多,导致Softmax层太大,单个计算单元无法存储,这个时候,需要把模型切割成若干部分,存储在不同的运算单元。模型并行常见于NLU、推荐、金融等领域。 流式并行(Stream Parallelism) 流式并行,即每个计算单元都存储不同层的模型数据,训练相同的数据。如上图所示,GPU1只负责第一层神经网络的计算,GPU2只负责2~5层神经网络的计算,GPU3只负责第6层的计算。流式并行的好处在于每个运算单元之间的通信和计算重叠(overlap),如果配置得当,可以非常充分地利用硬件资源。缺点在于,根据不同的模型,需要平衡好各个计算单元的计算量,如果配置不好,很容易形成“堰塞湖”。如上图所示,很有可能出现GPU1 负责的运算量太少,而GPU2 负责的运算量太多,导致GPU1 和GPU2 之间堵塞住大量的Mini-batch,更常见于线上环境。 混合并行(Hybrid Parallelism) 混合并行,即上面提到的并行方式的混合。如对于一些图像识别任务来说,可能前几层使用数据并行,最后的Softmax层,使用模型并行。 异构计算的硬件解决方案 单机单卡:一个主机内安装上一块GPU运算卡。常见于个人计算机。 单机多卡:一个主机内安装上多块GPU运算卡。常见的有:1机4卡,1机8卡,甚至有1机10卡。一般公司都采取这种硬件方案。 多机多卡:多台主机内安装多块GPU运算卡。常见于公司内部的计算集群,一般多机之间采取Infiniband 来实现网络的快速通信。 定制化:即类似于Google的TPU解决方案。常见于“巨无霸”公司内部。 异构计算的通信解决方案 根据上面的硬件解决方案,我们以ResNet为例:模型的大小为230M,单张图片运算量为11 GFLPOS,Mini-batch假设为128。可以计算出各个硬件模块在深度学习训练中的耗时比较: GPU:对于V100,假设有6 TFLOPS,一次Mini-batch 理论耗时:0.23s。 PCI-E:常见PCI-E 3.0 * 16,速度为10 GB/s,传输一个模型的理论耗时为:0.023s。 网络:假设为10 GB/s的高速网络,传输一个模型的理论耗时:0.023s。 Disk:普通的磁盘,我们假设200M/s的读取速度,读取一次Mini-batch所需要的图片耗时:0.094s。 根据上面的数据结果,我们似乎可以得出一个结论:PCI-E和网络的传输耗时,相对于GPU来说,整整少了一个数量级,所以网络通信同步的时间可以忽略不计。然而问题并没有那么简单,上面例子中的耗时只是单个模型的耗时,但是对于8卡的集群来说,如果使用数据并行,每次同步就需要传输8份模型,这就导致数据传输的时间和GPU的计算时间“旗鼓相当”。这样的话,GPU就得每训练完一个Mini-batch,都得等候很久的一段时间(采取同步更新),这会浪费很多计算资源。因此,网络通信也需要制定对应的解决方案。下面我们以Nvidia NCCL中单机多卡的通信解决方案为例介绍,而多机多卡的通信解决方案其实是类似的。 上图是单机4卡机器,在硬件上,两种不同的通信体系。左边为普通的PCI-E通信,即4个GPU之间组成一个环状。右边为NVLink通信,即两两之间相互连接。 常见的通信类型如下图所示: 对于深度学习训练而言,关键的两种通信类型为:Broadcast和Reduce。Broadcast用于Master分发最新的模型给各个GPU。Reduce 用于各个GPU计算完Mini-batch后,把模型更新值汇总到Master上。以Broadcast为例,最简单的通信方式是Master往各个GPU上发送数据,这样的耗时就是4次模型传输的时间,通信时间就会太长,一种简单的优化方法如下图所示: 即把所需要传输的数据分成若干块,然后通过接力的方式逐个传递,每个GPU都把自己最新的一块数据发送到下一个GPU卡上。这种传输方式能充分利用硬件层面的通信结构,使得需要的耗时大幅缩减。与此类似的,Reduce的通信优化也可以采取相同的方式进行提速。 美团的定制化深度学习系统 尽管目前在业界已经推出了很多著名的深度学习训练平台,通用的训练平台如TensorFlow、MxNet等等,还有领域专用的训练平台,如语音识别中的Kaldi,但是我们经过调研后,决定内部自主开发一套深度学习系统,理由如下: 通用的训练平台,缺乏了领域特色的功能。如语音识别中的特征提取模块和算法。 通用的训练平台,通常是基于Data-flow Graph,来对计算图中的每个operator进行建模,所以颗粒度很小,需要调度的单元多,导任务调度复杂。 领域特色的训练平台,如Kaldi,在神经网络训练的时候,性能不足。 线上业务存在很多特殊性,如果使用TensorFlow之类作为训练平台,不太适合线上业务的情景。 NLU线上系统 线上系统的业务特点 我们在设计NLU线上系统时,考虑了NLU业务的一些特性。发现其具备如下的一些特点: 随着业务和技术的变化,算法流程也经常发生变化。 算法流程是多个算法串联组成的,不单纯的只有深度学习算法。如分词等算法就不是DL算法。 为了能够快速响应一些紧急问题,需要经常对模型进行热更新。 更重要的是,我们希望构建一个能以“数据驱动”的自动迭代闭环。 业务多变 NLU任务的算法流程是多层级的,并且业务经常发生变化。如下图所示: 即随着业务要求的变化,NLU系统一开始的算法流程,只需要把一个Query分为两个类,但是到后面,极有可能会变成需要分为三个类别。 热更新 根据业务需求,或者为了紧急处理一些特殊问题,NLU线上系统经常需要做出快速响应,热更新算法模型。如最近的热点词“skr”,几乎是一夜之间,突然火爆起来。如下图所示的微博,如果不能正确理解“skr”的正确语义,可能就不能准确理解这条微博想要表达的意思。 为了避免影响用户体验,我们可能会对NLU系统,马上进行热更新,把新模型紧急进行上线。 数据驱动的自动迭代闭环 对于线上系统而言,构建如上图所示的自动迭代闭环,能更好地利用业务数据来提升服务质量。 NLU线上系统的核心设计 算法流程的抽象 为了适应线上系统串联、多变的算法流程,我们把线上系统的算法进行抽象,如下图所示: 即每一个算法,都依赖于若干个槽位(Slot)和资源(Resource),一旦槽位和资源就位,就会触发对应的算法执行。算法的执行先通过算法适配器,来适配槽位和资源中的数据,转换成算子的输入格式。然后算子执行算法本身,执行完算子后,再经过算法解析器。算法解析器主要用于解析算法执行的结果,触发对应的槽位。如根据算法的结果,触发Top 3的结果。 多个算法串联起来,就构建成如下结果: 热更新流程的设计 如上图所示,我们把算法的热更新流程设计如上。初试状态为左上角,即多个Query使用同一份模型数据。当遇到模型更新的请求后,系统将会block住新的query(右上角状态)。然后更新模型完后,新的query使用新的模型,旧query依然使用旧模型(右下角状态)。最后,当使用旧模型的query结束后,把旧的模型从内存中删除(左下角),然后系统恢复到初始状态。 声学模型训练系统 因为TensorFlow等通用深度学习训练平台,缺乏了特征提取等业务相关的领域功能,而Kaldi的声学模型训练过程又太慢。所以美团开发了一个声学模型训练系统——Mimir,其具备如下特性: 使用比TensorFlow更粗颗粒度的建模单元,使得任务调度、优化更简单方便易行。 使用数据并行的并行方案,单机多卡可达到近线性加速。(采取同步更新策略下,4卡加速比达到3.8) 移植了Kaldi的一些特有的训练算法。 速度上为Kaldi的6~7倍。(800个小时的训练数据,单机单卡的条件下,Kaldi需要6~7天, Mimir只需20个小时) 业务上,移植了Kaldi的特征提取等领域的相关模块。 参考资料 NCCL: ACCELERATED MULTI-GPU COLLECTIVE COMMUNICATIONS 【深度学习】老师木讲架构:深度学习平台技术演进 作者简介 剑鹏,美团点评算法专家。2017年加入美团,目前作为语音识别团队的声学模型负责人,负责声学模型相关的算法和系统设计与开发。 人工智能 2018-10-26 09:57:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 这篇文章是系列文章的第1部分,第2部分将阐述AutoML和神经架构搜索、第3部分将特别地介绍Google的AutoML。 关于机器学习人才的稀缺和公司声称他们的产品能够自动化机器学习而且能完全消除对ML专业知识需求的承诺经常登上媒体的新闻头条。在TensorFlow DevSummit的主题演讲中,Google的AI总指挥Jeff Dean估计,有数千万拥有可用于机器学习的数据而缺乏必要的专业知识和技能的组织。因为我在fast.ai主要专注于让更多的人去使用机器学习并且让它更容易使用,所以我密切关注刚才所提的机器学习人才稀缺等问题。 在考虑如何使机器学习的一些工作自动化以及让具有更广泛背景的人更容易使用这项技术,首先有必要问的是:机器学习行业从业者到底在做什么?任何用来解决机器学习专业知识稀缺的方案都需要回答这个问题:我们是否知道去教什么技能、去建立什么工具或者去自动化什么工序。 从事机器学习行业的人做什么? 构建数据产品是一项复杂的工作 虽然许多关于机器学习的学术来源几乎都是预测建模,但这只是从事机器学习在正常情况下做的其中一件事。适当地分析商业问题,收集和清理数据,构建模型,实施结果,然后监控变化的过程在很多方式中是相互关联的,这往往很难仅仅通过单个部分进行孤立(至少不知道其他部分需要什么)。正如Jeremy Howard等人在《设计出色的数据产品》上写道:伟大的预测建模是解决方案的重要组成部分,但它不再独立;随着产品变得越来越复杂,它就会消失在管道中。 构建数据产品是一项复杂的工作 人工智能 2018-10-25 13:49:00 「深度学习福利」大神带你进阶工程师,立即查看>>> TP/TN/FP/FN的定义 TP: 预测为1(Positive),实际也为1(Truth-预测对了) TN: 预测为0(Negative),实际也为0(Truth-预测对了) FP: 预测为1(Positive),实际为0(False-预测错了) FN: 预测为0(Negative),实际为1(False-预测错了) Accuracy/Precision/Recall/F1的定义 Accuracy = (预测正确的样本数)/(总样本数)=(TP+TN)/(TP+TN+FP+FN) Precision = (预测为1且正确预测的样本数)/(所有预测为1的样本数) = TP/(TP+FP) Recall = (预测为1且正确预测的样本数)/(所有真实情况为1的样本数) = TP/(TP+FN) F1 = 2*(Precision*Recall)/(Precision+Recall) IOU的定义 IoU = TP/(TP+TN+FP) 人工智能 2018-10-25 11:48:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 问题描述 DL中我们可能会根据tensor中元素的值进行不同的操作(比如loss阶段会根据grandtruth或outputs中元素的大小进行不同的loss操作),这时就要对tensor中的元素进行判断。在python中可以用for + if语句进行判断。但TF中输入是Tensor,for和if语句失效。 tf.where说明 格式:tf.where(condition, x=None, y=None, name=None) 参数: condition: 一个元素为bool型的tensor。元素内容为false,或true。 x: 一个和condition有相同shape的tensor,如果x是一个高维的tensor,x的第一维size必须和condition一样。 y: 和x有一样shape的tensor 返回: 一个和x,y有同样shape的tensor 功能: 遍历condition Tensor中的元素,如果该元素为true,则output Tensor中对应位置的元素来自x Tensor中对应位置的元素;否则output Tensor中对应位置的元素来自Y tensor中对应位置的元素。 使用例子 比如当tensor中元素x大于等于5时,对应输出tensor中元素y=x * 2;否则 y= x * 3的计算 import os import sys import tensorflow as tf import numpy as np # # y = x * 2 (x >= 5) # y = x * 3 (x < 5) # a = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9]) tmp = tf.constant([0, 0, 0, 0, 0, 0, 0, 0, 0]) condition = tf.less(a, 5) smaller = tf.where(condition, a, tmp) bigger = tf.where(condition, tmp, a) compute_smaller = smaller * 3 compute_bigger = bigger * 2 result = compute_smaller + compute_bigger with tf.Session() as sess: print(sess.run(result)) # # 结果: [ 3 6 9 12 10 12 14 16 18] 上述过程也可以改成如下 a = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9]) condition = tf.less(a, 5) result = tf.where(condition, a * 3, a * 2) with tf.Session() as sess: print(sess.run(result)) 整个过程中核心部分是condition条件的设置,该条件可以用tf提供的less,equal等操作实现(详细查看tf文档)。 tmp变量为引入的一个临时变量,目的是为了保证where按条件选择后输出的Tensor大小不变(tmp的0元素在乘法中是无意义计算,用该方法保证未背选择的元素在smaller和bigger中不参与计算) 详细的condition 和 tmp需要根据实际的计算进行不同的设置 参考 https://stackoverflow.com/questions/42689342/compare-two-tensors-elementwise-tensorflow https://stackoverflow.com/questions/37912161/how-can-i-compute-element-wise-conditionals-on-batches-in-tensorflow 人工智能 2018-10-23 17:09:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 什么是稀疏矩阵 将矩阵中的元素按照零和非零分为两个集合,对于零元素,其占整个矩阵的元素个数比成为稠密度。 稠密度 > 0.05 稠密矩阵 稠密度 < 0.05 稀疏矩阵 稀疏矩阵算法 通过压缩稀疏矩阵中的零元素,节省运算空间。 常用算法 BLAS sparse matrix (稀疏矩阵)algorithms 1. coordinate storage - coo 一个直接的思路是只存储非零元素。而 coo 是其中最简单的存储结构。 coo index strucutre: 三个稀疏向量 three sparse vectors coo 第一个 vector:存储所有的非零元素 coo 第二个 vector:存储非零元素的 column index coo 第三个 vector:存储非零元素的 row index 2. compressed sparse row - csr 与 coo 类似,唯一的区别是在第三个 vector 第三个 vector 存储的是 row 的指针,如下图所示,存储某个 row 的第一个元素 3. compressed sparse column - csc 与 csr 同理, 第二个 vector 存储的是 column 的指针 4. block sparse row - bsr bsr 有四个 vector 第一个 vector 存储非零元素 第二个 vector 存储 column index 第三个 vector 和 第四个 vector 存储的都是 row 指针,但是分别是第一个元素和最后一个元素 pointer begin and pointer end 人工智能 2018-10-10 10:49:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 之前在spark环境中一直用的是portable版本,词条数量不是很够,且有心想把jieba,swcs词典加进来, 其他像ik,ansi-seg等分词词典由于没有词性并没有加进来. 本次修改主要是采用jar包方包将词典目录 data与hanlp.properties合成一个data.jar文件. 1. pom.xml 过滤资源文件的配置 这里把properties文件从jar包文件中去掉,因而结果文件是没有properties文件的. 可根据需要来确定是否把properties加入jar包中.由于我打算把hanlp.properties与词典目录写在一起 这里是要过滤掉hanlp.properties文件 2. 修改hanlp.properties文件 root= #将根目录置为空,或者注释掉root CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; scws.txt; jieba.txt; 现代汉语补充词库.txt; 全国地名大全.txt ns; 人名词典.txt; 机构名词典.txt; 上海地名.txt ns;data/dictionary/person/nrf.txt nrf; #增加更多的配置文件,这里增加了结巴分词,scws分词 #IOAdapter=com.hankcs.hanlp.corpus.io.FileIOAdapter IOAdapter=com.hankcs.hanlp.corpus.io.JarIOAdapter #修改IOAdapter,以便使用jar包形式加载词典 3. 修改HanLP.java if ( root.length() != 0 && !root.endsWith("/")) root += "/"; 当root的长度为0时,不用在root字符串后面添加'/' 4. 增加处理词典jar包的代码文件: JarIOAdapter.java package com.hankcs.hanlp.corpus.io; import java.io.*; /** * 基于普通文件系统的IO适配器 * * @author hankcs */ public class JarIOAdapter implements IIOAdapter { @Override public InputStream open(String path) throws FileNotFoundException { /* 采用第一行的方式加载资料会在分布式环境报错 改用第二行的方式 */ //return ClassLoader.getSystemClassLoader().getResourceAsStream(path); return JarIOAdapter.class.getClassLoader().getResourceAsStream(path); } @Override public OutputStream create(String path) throws FileNotFoundException { return new FileOutputStream(path); } } 在跑DemoStopWord时,发现 java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoStopWord 报错,原因是接口不统一导致. 修改 DMAG.java如下: public MDAG(File dataFile) throws IOException { BufferedReader dataFileBufferedReader = new BufferedReader(new InputStreamReader(IOAdapter == null ? new FileInputStream(dataFile) : //IOAdapter.open(dataFile.getAbsolutePath()) IOAdapter.open(dataFile.getPath()) , "UTF-8")); 即可. 5. 如何将词典与配置文件打成一个jar包 最好是把txt格式的文件做成bin或dat格式的文件,然后做成jar包,否则打包运行后无法再写成bin或dat格式文件. 简单的办法是跑一下示例,即可生成相应的bin或dat格式文件. java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoAtFirstSight java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoChineseNameRecognition java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoJapaneseNameRecognition java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoPinyin java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoPlaceRecognition java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoOrganizationRecognition java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoTokenizerConfig #命名实体识别,包括上面的人名,地名等 java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoTraditionalChinese2SimplifiedChinese java -cp .:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoStopWord 或者用以下shell脚本完成 :>a;while read cl; do echo $cl; echo "=========="$cl"=======" >>a;java -cp .:test.jar:hanlp-1.3.2.jar $cl 1>> a 2>&1;done < <(jar tvf test.jar | awk '$(NF)~"Demo"{print $(NF)}' | sed 's/.class$//;s/\//./g') 我们把data目录与hanlp.properties文件放在一个目录,比如xxx目录 cd xxx jar cvf data.jar . 即可生成data.jar包 6. 如何运行 [dxp@Flyme-SearchTag-32-220 makeNewDict]$ ls data.jar hanlp-1.3.2.jar README.md test test.jar [dxp@Flyme-SearchTag-32-220 makeNewDict]$ java -cp data.jar:hanlp-1.3.2.jar:test.jar com.hankcs.demo.DemoAtFirstSight 7. 在spark中应用 IDE如(intellij idea)中maven项目 引入以下依赖: spark-submit提交任务时增加 --jar hanlp-1.3.2.jar,data.jar 转载自 cicido的个人空间 人工智能 2018-10-10 09:18:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 【一】代码下载 https://github.com/tensorflow/tensorflow/releases/ PS:本次源码分析采用1.11版本 【二】Session简介 在TensorFlow中,session是沟通tf的桥梁,模型的训练、推理,都需要通过session,session持有graph的引用。tf支持单机与分布式,因此session也分为单机版与分布式版。 【三】session类图 Session ::tensorflow\core\public\session.h DirectSession ::tensorflow\core\common_runtime\direct_session.h GrpcSession ::tensorflow\core\distributed_runtime\rpc\grpc_session.h class DirectSession : public Session --->单机版session class GrpcSession : public Session ---> 分布式版session 【四】session创建 tf对外提供了一套C API,负责给client语言使用。在tensorflow\c\c_api.h中,session的创建流程我们从这里开始。 TF_NewSession NewSession (tensorflow\core\public\session.h) 1. NewSession实现 tensorflow\core\common_runtime\session.cc Status NewSession(const SessionOptions& options, Session** out_session) { SessionFactory* factory; Status s = SessionFactory::GetFactory(options, &factory); // 通过SessionOptions来选择不同的factory s = factory->NewSession(options, out_session); // 调用对应的factory来创建对应的session,也就是创建DirectSession 还GrpcSession } 2. 关于SessionOptions 这里看一下该定义,省略掉其他成员,仅仅看看target,注释中说,如果target为空,则创建DirectSession,如果target以 'grpc’开头,则创建GrpcSession /// Configuration information for a Session. struct SessionOptions { /// \brief The TensorFlow runtime to connect to. /// /// If 'target' is empty or unspecified, the local TensorFlow runtime /// implementation will be used. Otherwise, the TensorFlow engine /// defined by 'target' will be used to perform all computations. /// /// "target" can be either a single entry or a comma separated list /// of entries. Each entry is a resolvable address of the /// following format: /// local /// ip:port /// host:port /// ... other system-specific formats to identify tasks and jobs ... /// /// NOTE: at the moment 'local' maps to an in-process service-based /// runtime. /// /// Upon creation, a single session affines itself to one of the /// remote processes, with possible load balancing choices when the /// "target" resolves to a list of possible processes. /// /// If the session disconnects from the remote process during its /// lifetime, session calls may fail immediately. string target; }; 3. SessionFactory::GetFactory 这里的关键在于:session_factory.second->AcceptsOptions(options) 依据选项来,这里实际上是调用对应的factory方法 DirectSessionFactory bool AcceptsOptions(const SessionOptions& options) override { return options.target.empty(); // target为空则为true } GrpcSessionFactory bool AcceptsOptions(const SessionOptions& options) override { return str_util::StartsWith(options.target, kSchemePrefix); // const char* const kSchemePrefix = "grpc://"; target为grpc开头 } class GrpcSessionFactory : public SessionFactory class DirectSessionFactory : public SessionFactory 【五】总结 session的创建通过option(可以理解为配置)来选择性创建,而session的真正创建由sessionfactory来完成,这里采用了工厂设计模式,将各个factory通过注册的方式注册到sessionfactory中,这样在系统启动后,这些factory就可以用了,新增一个factory也很容易,且不影响上层代码。 人工智能 2018-10-09 20:42:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 一、前言 计算机视觉长久以来没有大的突破,卷积神经网络的出现,给这一领域带来了突破,本篇博客,将通过具体的实例来看看卷积神经网络在图像识别上的应用。 导读 1、问题描述 2、解决问题的思路 3、用DL4J进行实现 二、问题 有如下一组验证码的图片,图片大小为60*160,验证码由5个数字组成,数字的范围为0到9,并且每个验证码图片上都加上了干扰背景,图片的文件名,表示验证码上的数字,样本图片如下: 穷举每张图片的可能性几乎不可能,所以传统的程序思路不可能解这个问题,那么必须让计算机通过自我学习,获取识别验证码的能力。先让计算机看大量的验证码的图片,并告诉计算机这些图片的结果,让计算机自我学习,慢慢地计算机就学会了识别验证码。 三、解决思路 1、特征 每个数字的形状各异,各自特征明显,这里的特征实际上指的是线条的走向、弯曲程度等等形状上的不同表征,那么对于侦测图形上的形状,卷积神经网络加上Relu和Max采样,可以很精确的做到这一点,本质原因在于,把卷积核拉直了看,本质上所做的事情就算向量的点积运算,求一个向量在另一个向量上的投影。对于卷积神经网络的原理可以看看 《有趣的卷积神经网络》 2、网络结构设计 对于每张图片而言,有5个数字作为输出结果,那么得设计一个有5个output的深度神经网络,首先用多个卷积核+Max采样层的结构来抽取明显特征,最后获得的特征经过两个全连接层逼近,这里加全连接层有两个目的,第一:经过sigmoid函数把值压缩到0到1之间,便于softmax计算,第二,加上全连接层可以更加抽象特征,让函数的逼近更加容易。最终的网络结构如下: 3、张量表示 对于Label的表示用one-hot来表示,这样可以很好的配合softmax,下图展示了从0到9的数字表示,沿着行的方向,由上而下,分别表示0到9 对于图片上的像素点,值域在0到255之间,图片如果是彩色,那么实际上会有三个通道,这里都是黑白色,所以,只有一个通道,取图片上真实像素点的值,除以255进行归一化即可。 四、代码实现 1、网络结构 public static ComputationGraph createModel() { ComputationGraphConfiguration config = new NeuralNetConfiguration.Builder() .seed(seed) .gradientNormalization(GradientNormalization.RenormalizeL2PerLayer) .l2(1e-3) .updater(new Adam(1e-3)) .weightInit( WeightInit.XAVIER_UNIFORM) .graphBuilder() .addInputs("trainFeatures") .setInputTypes(InputType.convolutional(60, 160, 1)) .setOutputs("out1", "out2", "out3", "out4", "out5", "out6") .addLayer("cnn1", new ConvolutionLayer.Builder(new int[]{5, 5}, new int[]{1, 1}, new int[]{0, 0}) .nIn(1).nOut(48).activation( Activation.RELU).build(), "trainFeatures") .addLayer("maxpool1", new SubsamplingLayer.Builder(PoolingType.MAX, new int[]{2,2}, new int[]{2, 2}, new int[]{0, 0}) .build(), "cnn1") .addLayer("cnn2", new ConvolutionLayer.Builder(new int[]{5, 5}, new int[]{1, 1}, new int[]{0, 0}) .nOut(64).activation( Activation.RELU).build(), "maxpool1") .addLayer("maxpool2", new SubsamplingLayer.Builder(PoolingType.MAX, new int[]{2,1}, new int[]{2, 1}, new int[]{0, 0}) .build(), "cnn2") .addLayer("cnn3", new ConvolutionLayer.Builder(new int[]{3, 3}, new int[]{1, 1}, new int[]{0, 0}) .nOut(128).activation( Activation.RELU).build(), "maxpool2") .addLayer("maxpool3", new SubsamplingLayer.Builder(PoolingType.MAX, new int[]{2,2}, new int[]{2, 2}, new int[]{0, 0}) .build(), "cnn3") .addLayer("cnn4", new ConvolutionLayer.Builder(new int[]{4, 4}, new int[]{1, 1}, new int[]{0, 0}) .nOut(256).activation( Activation.RELU).build(), "maxpool3") .addLayer("maxpool4", new SubsamplingLayer.Builder(PoolingType.MAX, new int[]{2,2}, new int[]{2, 2}, new int[]{0, 0}) .build(), "cnn4") .addLayer("ffn0", new DenseLayer.Builder().nOut(3072) .build(), "maxpool4") .addLayer("ffn1", new DenseLayer.Builder().nOut(3072) .build(), "ffn0") .addLayer("out1", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .addLayer("out2", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .addLayer("out3", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .addLayer("out4", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .addLayer("out5", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .addLayer("out6", new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) .nOut(10).activation(Activation.SOFTMAX).build(), "ffn1") .pretrain(false).backprop(true) .build(); ComputationGraph model = new ComputationGraph(config); model.init(); return model; } 2、训练集构建 public MultiDataSet convertDataSet(int num) throws Exception { int batchNumCount = 0; INDArray[] featuresMask = null; INDArray[] labelMask = null; List 五、后记 用deeplearning4j构建一个深度神经网络,几乎没有多余的代码,非常优雅就可以解一个复杂的图像识别问题,对于上述代码有几点说明: 1、对于DenseLayer层,这里没有设置网络输入的size,实际上在dl4j内部已经做了这个set操作 2、对于梯度更新优化,这里选用Adam,Adam融合了动量和自适应learningRate两方面的因素,通常会有更好的效果 3、损失函数用的类Log函数,和交叉熵有相同的效果 4、模型训练好可以使用 ModelSerializer.writeModel(model, modelPath, true)来保存网络结构,就可以用于图像识别了 完整的代码,可以查看deeplearning4j的example --------------------------------------------------------------------------------------------------------- 快乐源于分享。 此博客乃作者原创, 转载请注明出处 人工智能 2018-10-09 20:02:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 最近在看《机器学习实战》这本书,因为自己本身很想深入的了解机器学习算法,加之想学python,就在朋友的推荐之下选择了这本书进行学习。 一 . K-近邻算法(KNN)概述 最简单最初级的分类器是将全部的训练数据所对应的类别都记录下来,当测试对象的属性和某个训练对象的属性完全匹配时,便可以对其进行分类。但是怎么可能所有测试对象都会找到与之完全匹配的训练对象呢,其次就是存在一个测试对象同时与多个训练对象匹配,导致一个训练对象被分到了多个类的问题,基于这些问题呢,就产生了KNN。 KNN是通过测量不同特征值之间的距离进行分类。它的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别,其中K通常是不大于20的整数。KNN算法中,所选择的邻居都是已经正确分类的对象。该方法在定类决策上只依据最邻近的一个或者几个样本的类别来决定待分样本所属的类别。 下面通过一个简单的例子说明一下:如下图,绿色圆要被决定赋予哪个类,是红色三角形还是蓝色四方形?如果K=3,由于红色三角形所占比例为2/3,绿色圆将被赋予红色三角形那个类,如果K=5,由于蓝色四方形比例为3/5,因此绿色圆被赋予蓝色四方形类。 由此也说明了KNN算法的结果很大程度取决于K的选择。 在KNN中,通过计算对象间距离来作为各个对象之间的非相似性指标,避免了对象之间的匹配问题,在这里距离一般使用欧氏距离或曼哈顿距离: 同时,KNN通过依据k个对象中占优的类别进行决策,而不是单一的对象类别决策。这两点就是KNN算法的优势。 接下来对KNN算法的思想总结一下:就是在训练集中数据和标签已知的情况下,输入测试数据,将测试数据的特征与训练集中对应的特征进行相互比较,找到训练集中与之最为相似的前K个数据,则该测试数据对应的类别就是K个数据中出现次数最多的那个分类,其算法的描述为: 1)计算测试数据与各个训练数据之间的距离; 2)按照距离的递增关系进行排序; 3)选取距离最小的K个点; 4)确定前K个点所在类别的出现频率; 5)返回前K个点中出现频率最高的类别作为测试数据的预测分类。 二 .python实现 首先呢,需要说明的是我用的是python3.4.3,里面有一些用法与2.7还是有些出入。 建立一个KNN.py文件对算法的可行性进行验证,如下: #coding:utf-8 from numpy import * import operator ##给出训练数据以及对应的类别 def createDataSet(): group = array([[1.0,2.0],[1.2,0.1],[0.1,1.4],[0.3,3.5]]) labels = ['A','A','B','B'] return group,labels ###通过KNN进行分类 def classify(input,dataSe t,label,k): dataSize = dataSet.shape[0] ####计算欧式距离 diff = tile(input,(dataSize,1)) - dataSet sqdiff = diff ** 2 squareDist = sum(sqdiff,axis = 1)###行向量分别相加,从而得到新的一个行向量 dist = squareDist ** 0.5 ##对距离进行排序 sortedDistIndex = argsort(dist)##argsort()根据元素的值从大到小对元素进行排序,返回下标 classCount={} for i in range(k): voteLabel = label[sortedDistIndex[i]] ###对选取的K个样本所属的类别个数进行统计 classCount[voteLabel] = classCount.get(voteLabel,0) + 1 ###选取出现的类别次数最多的类别 maxCount = 0 for key,value in classCount.items(): if value > maxCount: maxCount = value classes = key return classes 接下来,在命令行窗口输入如下代码: #-*-coding:utf-8 -*- import sys sys.path.append("...文件路径...") import KNN from numpy import * dataSet,labels = KNN.createDataSet() input = array([1.1,0.3]) K = 3 output = KNN.classify(input,dataSet,labels,K) print("测试数据为:",input,"分类结果为:",output) 回车之后的结果为: 测试数据为: [ 1.1 0.3] 分类为: A 答案符合我们的预期,要证明算法的准确性,势必还需要通过处理复杂问题进行验证,之后另行说明。 这是第一次用python编的一个小程序,势必会遇到各种问题,在此次编程调试过程中遇到了如下问题: 导入.py文件路径有问题,因此需要在最开始加如下代码: import sys sys.path.append("文件路径") ; 在python提示代码存在问题时,一定要及时改正,改正之后保存之后再执行命令行,这一点跟MATLAB是不一样的,所以在python中最好是敲代码的同时在命令行中一段一段的验证; 在调用文件时函数名一定要写正确,否则会出现:'module' object has no attribute 'creatDataSet'; 'int' object has no attribute 'kclassify',这个问题出现的原因是之前我讲文件保存名为k.py,在执行output = K.classify(input,dataSet,labels,K)这一句就会出错。根据函数式编程的思想,每个函数都可以看为是一个变量而将K赋值后,调用k.py时就会出现问题。 三 MATLAB实现 之前一直在用MATLAB做聚类算法的一些优化,其次就是数模的一些常用算法,对于别的算法,还真是没有上手编过,基础还在,思想还在,当然要动手编一下,也是不希望在学python的同时对MATLAB逐渐陌生吧,走走停停,停很重要。 首先,建立KNN.m文件,如下: %% KNN clear all clc %% data trainData = [1.0,2.0;1.2,0.1;0.1,1.4;0.3,3.5]; trainClass = [1,1,2,2]; testData = [0.5,2.3]; k = 3; %% distance row = size(trainData,1); col = size(trainData,2); test = repmat(testData,row,1); dis = zeros(1,row); for i = 1:row diff = 0; for j = 1:col diff = diff + (test(i,j) - trainData(i,j)).^2; end dis(1,i) = diff.^0.5; end %% sort jointDis = [dis;trainClass]; sortDis= sortrows(jointDis'); sortDisClass = sortDis'; %% find class = sort(2:1:k); member = unique(class); num = size(member); max = 0; for i = 1:num count = find(class == member(i)); if count > max max = count; label = member(i); end end disp('最终的分类结果为:'); fprintf('%d\n',label) 运行之后的结果是,最终的分类结果为:2。和预期结果一样。 四 实战 之前,对KNN进行了一个简单的验证,今天我们使用KNN改进约会网站的效果,个人理解,这个问题也可以转化为其它的比如各个网站迎合客户的喜好所作出的推荐之类的,当然,今天的这个例子功能也实在有限。 在这里根据一个人收集的约会数据,根据主要的样本特征以及得到的分类,对一些未知类别的数据进行分类,大致就是这样。 我使用的是python 3.4.3,首先建立一个文件,例如date.py,具体的代码如下: #coding:utf-8 from numpy import * import operator from collections import Counter import matplotlib import matplotlib.pyplot as plt ###导入特征数据 def file2matrix(filename): fr = open(filename) contain = fr.readlines()###读取文件的所有内容 count = len(contain) returnMat = zeros((count,3)) classLabelVector = [] index = 0 for line in contain: line = line.strip() ###截取所有的回车字符 listFromLine = line.split('\t') returnMat[index,:] = listFromLine[0:3]###选取前三个元素,存储在特征矩阵中 classLabelVector.append(listFromLine[-1])###将列表的最后一列存储到向量classLabelVector中 index += 1 ##将列表的最后一列由字符串转化为数字,便于以后的计算 dictClassLabel = Counter(classLabelVector) classLabel = [] kind = list(dictClassLabel) for item in classLabelVector: if item == kind[0]: item = 1 elif item == kind[1]: item = 2 else: item = 3 classLabel.append(item) return returnMat,classLabel#####将文本中的数据导入到列表 ##绘图(可以直观的表示出各特征对分类结果的影响程度) datingDataMat,datingLabels = file2matrix('D:\python\Mechine learing in Action\KNN\datingTestSet.txt') fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(datingDataMat[:,0],datingDataMat[:,1],15.0*array(datingLabels),15.0*array(datingLabels)) plt.show() ## 归一化数据,保证特征等权重 def autoNorm(dataSet): minVals = dataSet.min(0) maxVals = dataSet.max(0) ranges = maxVals - minVals normDataSet = zeros(shape(dataSet))##建立与dataSet结构一样的矩阵 m = dataSet.shape[0] for i in range(1,m): normDataSet[i,:] = (dataSet[i,:] - minVals) / ranges return normDataSet,ranges,minVals ##KNN算法 def classify(input,dataSet,label,k): dataSize = dataSet.shape[0] ####计算欧式距离 diff = tile(input,(dataSize,1)) - dataSet sqdiff = diff ** 2 squareDist = sum(sqdiff,axis = 1)###行向量分别相加,从而得到新的一个行向量 dist = squareDist ** 0.5 ##对距离进行排序 sortedDistIndex = argsort(dist)##argsort()根据元素的值从大到小对元素进行排序,返回下标 classCount={} for i in range(k): voteLabel = label[sortedDistIndex[i]] ###对选取的K个样本所属的类别个数进行统计 classCount[voteLabel] = classCount.get(voteLabel,0) + 1 ###选取出现的类别次数最多的类别 maxCount = 0 for key,value in classCount.items(): if value > maxCount: maxCount = value classes = key return classes ##测试(选取10%测试) def datingTest(): rate = 0.10 datingDataMat,datingLabels = file2matrix('D:\python\Mechine learing in Action\KNN\datingTestSet.txt') normMat,ranges,minVals = autoNorm(datingDataMat) m = normMat.shape[0] testNum = int(m * rate) errorCount = 0.0 for i in range(1,testNum): classifyResult = classify(normMat[i,:],normMat[testNum:m,:],datingLabels[testNum:m],3) print("分类后的结果为:,", classifyResult) print("原结果为:",datingLabels[i]) if(classifyResult != datingLabels[i]): errorCount += 1.0 print("误分率为:",(errorCount/float(testNum))) ###预测函数 def classifyPerson(): resultList = ['一点也不喜欢','有一丢丢喜欢','灰常喜欢'] percentTats = float(input("玩视频所占的时间比?")) miles = float(input("每年获得的飞行常客里程数?")) iceCream = float(input("每周所消费的冰淇淋公升数?")) datingDataMat,datingLabels = file2matrix('D:\python\Mechine learing in Action\KNN\datingTestSet2.txt') normMat,ranges,minVals = autoNorm(datingDataMat) inArr = array([miles,percentTats,iceCream]) classifierResult = classify((inArr-minVals)/ranges,normMat,datingLabels,3) print("你对这个人的喜欢程度:",resultList[classifierResult - 1]) 新建test.py文件了解程序的运行结果,代码: #coding:utf-8 from numpy import * import operator from collections import Counter import matplotlib import matplotlib.pyplot as plt import sys sys.path.append("D:\python\Mechine learing in Action\KNN") import date date.classifyPerson() 运行结果如下图: 以上,是对本次算法的整理和总结。 人工智能 2018-10-09 14:59:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 前不久,主题为“人工智能赋能新时代”的2018年世界人工智能大会在上海举行,吸引了众多世界顶尖科技企业参与。国家主席习近平在贺信中指出,“新一代人工智能正在全球范围内蓬勃兴起,为经济社会发展注入了新动能,正在深刻改变人们的生产生活方式”。 乘着互联网、大数据、人工智能等技术发展的东风,近年来数字经济在我国得到了很好的成长。根据《中国“互联网+”指数报告(2018)》的数据,2017年全国数字经济体量为26.70万亿元,较上年同期的22.77万亿元增长17.24%,数字经济占国内生产总值(GDP)的比重也由30.61%提升至32.28%。 可以看到,在大数据、人工智能等技术的支持下,互联网经济正呈现蓬勃发展之势,不仅成为拉动我国经济发展的新引擎,更为实体经济的发展提供了新机遇。 改革开放四十年来,我国以制造业为主体的实体经济实现了跨越式发展。在我国成为全球制造业第一大国的同时,产业技术能力也正在逐渐追上世界先进水平。经济新常态需要新动能的出现,这在党的十九大报告中即有指出,“推动互联网、大数据、人工智能和实体经济的深度融合,在中高端消费、创新引领、绿色低碳、共享经济、现代供应链、人力资本服务等领域培育新增长点,形成新动能”。 这正是解决当前实体经济发展所遇问题的新方案。在新一轮产业革命的背景下,融合发展是供给侧结构性改革的必由之路,也有助于化解当前人民日益增长的美好生活需要和不平衡不充分的发展之间的矛盾。 作为新一代信息技术的典型,互联网、大数据、人工智能在全世界范围内都受到了高度重视,尤其是人工智能技术,被认为是下一个超级风口,其中包含的机会不言而喻。传统行业若能与之深度融合,必然会为实体经济提供有力驱动,也将为高质量发展打下坚实的基础。 事实上,当前部分行业已经成功引入了相关新技术。以零售业为例,在线上线下“共振”的新模式出现之前,传统零售业和电商一直处于比较对立的位置,一边是电商的快速发展扩张,一边是传统零售的“半死不活”。而近年来新出现的“无界零售”“新零售”模式,则是利用大数据、人工智能技术,旨在推动线上线下一体化。新模式出现之后,不仅线下实体呈现出起死回生的趋势,线上消费购物体验不足的弊端也得到了有效缓解,“互联网+”通过赋能的形式造就了双赢的局面。 人工智能 2018-10-09 11:11:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 美国和中国已经是毋庸置疑的AI超级体了,那么欧盟有可能成为下一个吗? 德国总理安格拉·默克尔以冷酷无情著称,但需要的时候她也可以很幽默。 在最近一次由科技杂志《Ada》举办的季度会议上,有人问她是否认为机器人应当拥有权利,她回答说:“你的意思是用电的权利?还是定期维护的权利?” 导致这次访谈如此引人入胜的还有另一个原因。默克尔表示,她对人工智能(AI)及其地缘政治很感兴趣。“在美国,对个人数据的控制很大程度上是私人公司在做的。而在中国正好相反,政府控制一切。”她同时表明,欧洲会介于这两者之间。 这种思想是欧洲更广泛的现实化的一部分,即AI在未来应当成为像电力、蒸汽机等基础科技同样重要的技术。一些国家如芬兰、法国已经制定了国家级人工智能战略,而德国正在制定中,预计将在今年末完成。届时,欧洲将会把这些努力统合到统一的人工智能计划中。预料之中的是,这个计划完全由欧盟成员组成,涉及了十几个委员会和其他实体。 但默克尔女士提出的问题对于欧洲就像英国脱欧和移民问题一样重要:欧洲能否在美国和中国的AI超级力量之间获得一席之地? 欧洲能否成为下一个AI超级力量? 人工智能 2018-10-09 11:08:03 「深度学习福利」大神带你进阶工程师,立即查看>>> 微软语音识别SDK总结 代码 CComPtr m_pSREngine;// 语音识别引擎(recognition)的接口。 CComPtr m_pSRContext;// 识别引擎上下文(context)的接口。 CComPtr m_pSRGrammar;// 识别文法(grammar)的接口。 CComPtr m_pInputStream;// 流()的接口。 CComPtr m_pToken;// 语音特征的(token)接口。 CComPtr m_pAudio;// 音频(Audio)的接口。(用来保存原来默认的输入流) ULONGLONG ullGrammerID ; CoInitialize(NULL); m_pSREngine.CoCreateInstance ( CLSID_SpInprocRecognizer ); m_pSREngine->CreateRecoContext ( &m_pSRContext );//建立上下文 //这里是设置事件 HWND hwnd = GetSafeHwnd(); hr = m_pSRContext->SetNotifyWindowMessage(hwnd,WM_RECORD,0,0); hr=m_pSRContext->SetInterest(SPFEI(SPEI_RECOGNITION),SPFEI(SPEI_RECOGNITION)); //这里是设置默认的音频输入 hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &m_pAudio); m_pSREngine->SetInput(m_pAudio,true); //这里是加载默认的语法规则 ullGrammerID = 1000; hr=m_pSRContext->CreateGrammar(ullGrammerID,&m_pSRGrammar); WCHAR wszXMLFile[20]=L""; MultiByteToWideChar(CP_ACP, 0,(LPCSTR)"cmd.xml" , -1, wszXMLFile, 256); //这里修改XML的目录 hr=m_pSRGrammar->LoadCmdFromFile(wszXMLFile,SPLO_DYNAMIC); //开启语音识别 m_pSRGrammar->SetRuleState( NULL,NULL,SPRS_ACTIVE ); hr=m_pSREngine->SetRecoState(SPRST_ACTIVE); 简单介绍 ISpRecognizer There are two implementations of the ISpRecognizer and ISpRecoContext in SAPI. One is for recognition "in-process" (InProc), where the SR engine is created in the same process as the application. Only this application can connect to this recognizer. The other implementation is the "shared-recognizer," where the SR engine is created in a separate process. There will only be one shared engine running on a system, and all applications using the shared engine connect to the same recognizer. This allows several speech applications to work simultaneously, and allows the user to speak to any application, as recognition is done from the grammars of all applications. For desktop-based speech applications it is recommended to use the shared recognizer because of the way it allows multiple SAPI applications to work at once. For other types of application, such as recognizing from wave files or a telephony server application where multiple SR engines will be required, the InProc recognizer should be used. When to Use Call methods of the ISpRecognizer interface to configure or retrieve the attributes of the SR engine. How Created There are two objects that implement this interface. These are created by applications by creating a COM object with either of the following CLSIDs: SpInprocRecognizer (CLSID_SpInprocRecognizer) SpSharedRecognizer (CLSID_SpSharedRecognizer) Alternatively, the shared recognizer can be created by creating a SpSharedRecoContext (CLSID_SpSharedRecoContext), and then calling ISpRecoContext::GetRecognizer on this object to get a reference to the SpSharedRecognizer object. Methods in Vtable Order ISpRecognizer Methods Description SetRecognizer Specifies the SR engine to be used. GetRecognizer Retrieves which SR engine is currently being used. SetInput Specifies which input stream the SR engine should use. GetInputObjectToken Retrieves the input token object for the stream. GetInputStream Retrieves the input stream. CreateRecoContext Creates a recognition context for this instance of an SR engine. GetRecoProfile Retrieves the current recognition profile token. SetRecoProfile Sets the recognition profile to be used by the recognizer. IsSharedInstance Determines if the recognizer is the shared or InProc implementation. GetRecoState Retrieves the state of the recognition engine. SetRecoState Sets the state of the recognition engine. GetStatus Retrieves current status information for the engine. GetFormat Retrieves the format of the current audio input. IsUISupported Checks if the SR engine supports a particular user interface component. DisplayUI Displays a user interface component. EmulateRecognition Emulates a recognition from a text phrase rather than from spoken audio. context ISpRecognizer::CreateRecoContext creates a recognition context for this instance of an SR engine. The recognition context is used to load recognition grammars, start and stop recognition, and receive events and recognition results. SetNotifyWindowMessage ISpNotifySource::SetNotifyWindowMessage ISpNotifySource::SetNotifyWindowMessage sets up the instance to send window messages to a specified window. HRESULT SetNotifyWindowMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); input SpCreateDefaultObjectFromCategoryId SpCreateDefaultObjectFromCategoryId creates the object instance from the default object token of a specified category. Found in: sphelper.h SpCreateDefaultObjectFromCategoryId( const WCHAR *pszCategoryId, T **ppObject, IUnknown *pUnkOuter = NULL, DWORD dwClsCtxt = CLSCTX_ALL ); 人工智能 2018-10-09 00:29:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 解决方案 // QT,把#define Q_ENABLE_OPENGL_FUNCTIONS_DEBUG打开,这样所有调用OpenGL接口时就会有提醒 new THREE.SkinnedMesh(geometry,new THREE.MeshFaceMaterial(materials), false ) THREE.SkinnedMesh原始官方API为两参数,qml改造后为三参数,第三个参数默认为true,当模型没有使用节点渲染方式将会产生严重问题。故第三个参数设置为false 问题原因分析 Starting C:\Qt\Qt5.6.2\Examples\Qt-5.6\canvas3d\build-threejs-Desktop_Qt_5_6_2_MinGW_32bit-Debug\cellphone\debug\cellphone.exe... QML debugging is enabled. Only use this in a safe environment. Debug: THREE.Canvas3DRenderer 71 (qrc:/three.js:29042, ) Warning: QOpenGLShader::compile(Vertex): ERROR: 3:217: '' : array size must be a positive integer (opengl\qopenglshaderprogram.cpp:328, bool QOpenGLShaderPrivate::compile(QOpenGLShader*)) Warning: *** Problematic Vertex shader source code *** (opengl\qopenglshaderprogram.cpp:334, bool QOpenGLShaderPrivate::compile(QOpenGLShader*)) Warning: #version 120 #define lowp #define mediump #define highp #line 2 #define precision precision highp float; precision highp int; #define VERTEX_TEXTURES #define GAMMA_FACTOR 2 #define MAX_DIR_LIGHTS 1 #define MAX_POINT_LIGHTS 0 #define MAX_SPOT_LIGHTS 0 #define MAX_HEMI_LIGHTS 0 #define MAX_SHADOWS 0 #define MAX_BONES 0 #define USE_COLOR #define USE_SKINNING uniform mat4 modelMatrix; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; uniform mat3 normalMatrix; uniform vec3 cameraPosition; attribute vec3 position; attribute vec3 normal; attribute vec2 uv; attribute vec2 uv2; #ifdef USE_COLOR attribute vec3 color; #endif #ifdef USE_MORPHTARGETS attribute vec3 morphTarget0; attribute vec3 morphTarget1; attribute vec3 morphTarget2; attribute vec3 morphTarget3; #ifdef USE_MORPHNORMALS attribute vec3 morphNormal0; attribute vec3 morphNormal1; attribute vec3 morphNormal2; attribute vec3 morphNormal3; #else attribute vec3 morphTarget4; attribute vec3 morphTarget5; attribute vec3 morphTarget6; attribute vec3 morphTarget7; #endif #endif #ifdef USE_SKINNING attribute vec4 skinIndex; attribute vec4 skinWeight; #endif #define PHONG varying vec3 vViewPosition; #ifndef FLAT_SHADED varying vec3 vNormal; #endif #define PI 3.14159 #define PI2 6.28318 #define RECIPROCAL_PI2 0.15915494 #define LOG2 1.442695 #define EPSILON 1e-6 float square( in float a ) { return a*a; } vec2 square( in vec2 a ) { return vec2( a.x*a.x, a.y*a.y ); } vec3 square( in vec3 a ) { return vec3( a.x*a.x, a.y*a.y, a.z*a.z ); } vec4 square( in vec4 a ) { return vec4( a.x*a.x, a.y*a.y, a.z*a.z, a.w*a.w ); } float saturate( in float a ) { return clamp( a, 0.0, 1.0 ); } vec2 saturate( in vec2 a ) { return clamp( a, 0.0, 1.0 ); } vec3 saturate( in vec3 a ) { return clamp( a, 0.0, 1.0 ); } vec4 saturate( in vec4 a ) { return clamp( a, 0.0, 1.0 ); } float average( in float a ) { return a; } float average( in vec2 a ) { return ( a.x + a.y) * 0.5; } float average( in vec3 a ) { return ( a.x + a.y + a.z) / 3.0; } float average( in vec4 a ) { return ( a.x + a.y + a.z + a.w) * 0.25; } float whiteCompliment( in float a ) { return saturate( 1.0 - a ); } vec2 whiteCompliment( in vec2 a ) { return saturate( vec2(1.0) - a ); } vec3 whiteCompliment( in vec3 a ) { return saturate( vec3(1.0) - a ); } vec4 whiteCompliment( in vec4 a ) { return saturate( vec4(1.0) - a ); } vec3 transformDirection( in vec3 normal, in mat4 matrix ) { return normalize( ( matrix * vec4( normal, 0.0 ) ).xyz ); } // http://en.wikibooks.org/wiki/GLSL_Programming/Applying_Matrix_Transformations vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) { return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz ); } vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { float distance = dot( planeNormal, point-pointOnPlane ); return point - distance * planeNormal; } float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) { return sign( dot( point - pointOnPlane, planeNormal ) ); } vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) { return pointOnLine + lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ); } float calcLightAttenuation( float lightDistance, float cutoffDistance, float decayExponent ) { if ( decayExponent > 0.0 ) { return pow( saturate( 1.0 - lightDistance / cutoffDistance ), decayExponent ); } return 1.0; } vec3 inputToLinear( in vec3 a ) { #ifdef GAMMA_INPUT return pow( a, vec3( float( GAMMA_FACTOR ) ) ); #else return a; #endif } vec3 linearToOutput( in vec3 a ) { #ifdef GAMMA_OUTPUT return pow( a, vec3( 1.0 / float( GAMMA_FACTOR ) ) ); #else return a; #endif } #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) varying vec2 vUv; uniform vec4 offsetRepeat; #endif #ifdef USE_LIGHTMAP varying vec2 vUv2; #endif #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) varying vec3 vReflect; uniform float refractionRatio; #endif #if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP ) varying vec3 vWorldPosition; #endif #ifdef USE_COLOR varying vec3 vColor; #endif #ifdef USE_MORPHTARGETS #ifndef USE_MORPHNORMALS uniform float morphTargetInfluences[ 8 ]; #else uniform float morphTargetInfluences[ 4 ]; #endif #endif #ifdef USE_SKINNING uniform mat4 bindMatrix; uniform mat4 bindMatrixInverse; #ifdef BONE_TEXTURE uniform sampler2D boneTexture; uniform int boneTextureWidth; uniform int boneTextureHeight; mat4 getBoneMatrix( const in float i ) { float j = i * 4.0; float x = mod( j, float( boneTextureWidth ) ); float y = floor( j / float( boneTextureWidth ) ); float dx = 1.0 / float( boneTextureWidth ); float dy = 1.0 / float( boneTextureHeight ); y = dy * ( y + 0.5 ); vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) ); vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) ); vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) ); vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) ); mat4 bone = mat4( v1, v2, v3, v4 ); return bone; } #else uniform mat4 boneGlobalMatrices[ MAX_BONES ]; mat4 getBoneMatrix( const in float i ) { mat4 bone = boneGlobalMatrices[ int(i) ]; return bone; } #endif #endif #ifdef USE_SHADOWMAP varying vec4 vShadowCoord[ MAX_SHADOWS ]; uniform mat4 shadowMatrix[ MAX_SHADOWS ]; #endif #ifdef USE_LOGDEPTHBUF #ifdef USE_LOGDEPTHBUF_EXT varying float vFragDepth; #endif uniform float logDepthBufFC; #endif void main() { #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) vUv = uv * offsetRepeat.zw + offsetRepeat.xy; #endif #ifdef USE_LIGHTMAP vUv2 = uv2; #endif #ifdef USE_COLOR vColor.xyz = inputToLinear( color.xyz ); #endif #ifdef USE_MORPHNORMALS vec3 morphedNormal = vec3( 0.0 ); morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ]; morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ]; morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ]; morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ]; morphedNormal += normal; #endif #ifdef USE_SKINNING mat4 boneMatX = getBoneMatrix( skinIndex.x ); mat4 boneMatY = getBoneMatrix( skinIndex.y ); mat4 boneMatZ = getBoneMatrix( skinIndex.z ); mat4 boneMatW = getBoneMatrix( skinIndex.w ); #endif #ifdef USE_SKINNING mat4 skinMatrix = mat4( 0.0 ); skinMatrix += skinWeight.x * boneMatX; skinMatrix += skinWeight.y * boneMatY; skinMatrix += skinWeight.z * boneMatZ; skinMatrix += skinWeight.w * boneMatW; skinMatrix = bindMatrixInverse * skinMatrix * bindMatrix; #ifdef USE_MORPHNORMALS vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 ); #else vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 ); #endif #endif #ifdef USE_SKINNING vec3 objectNormal = skinnedNormal.xyz; #elif defined( USE_MORPHNORMALS ) vec3 objectNormal = morphedNormal; #else vec3 objectNormal = normal; #endif #ifdef FLIP_SIDED objectNormal = -objectNormal; #endif vec3 transformedNormal = normalMatrix * objectNormal; #ifndef FLAT_SHADED vNormal = normalize( transformedNormal ); #endif #ifdef USE_MORPHTARGETS vec3 morphed = vec3( 0.0 ); morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ]; morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ]; morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ]; morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ]; #ifndef USE_MORPHNORMALS morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ]; morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ]; morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ]; morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ]; #endif morphed += position; #endif #ifdef USE_SKINNING #ifdef USE_MORPHTARGETS vec4 skinVertex = bindMatrix * vec4( morphed, 1.0 ); #else vec4 skinVertex = bindMatrix * vec4( position, 1.0 ); #endif vec4 skinned = vec4( 0.0 ); skinned += boneMatX * skinVertex * skinWeight.x; skinned += boneMatY * skinVertex * skinWeight.y; skinned += boneMatZ * skinVertex * skinWeight.z; skinned += boneMatW * skinVertex * skinWeight.w; skinned = bindMatrixInverse * skinned; #endif #ifdef USE_SKINNING vec4 mvPosition = modelViewMatrix * skinned; #elif defined( USE_MORPHTARGETS ) vec4 mvPosition = modelViewMatrix * vec4( morphed, 1.0 ); #else vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); #endif gl_Position = projectionMatrix * mvPosition; #ifdef USE_LOGDEPTHBUF gl_Position.z = log2(max( EPSILON, gl_Position.w + 1.0 )) * logDepthBufFC; #ifdef USE_LOGDEPTHBUF_EXT vFragDepth = 1.0 + gl_Position.w; #else gl_Position.z = (gl_Position.z - 1.0) * gl_Position.w; #endif #endif vViewPosition = -mvPosition.xyz; #if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP ) #ifdef USE_SKINNING vec4 worldPosition = modelMatrix * skinned; #elif defined( USE_MORPHTARGETS ) vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 ); #else vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); #endif #endif #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) vec3 worldNormal = transformDirection( objectNormal, modelMatrix ); vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition ); #ifdef ENVMAP_MODE_REFLECTION vReflect = reflect( cameraToVertex, worldNormal ); #else vReflect = refract( cameraToVertex, worldNormal, refractionRatio ); #endif #endif #if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP ) vWorldPosition = worldPosition.xyz; #endif #ifdef USE_SHADOWMAP for( int i = 0; i < MAX_SHADOWS; i ++ ) { vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition; } #endif } (opengl\qopenglshaderprogram.cpp:335, bool QOpenGLShaderPrivate::compile(QOpenGLShader*)) Warning: *** (opengl\qopenglshaderprogram.cpp:336, bool QOpenGLShaderPrivate::compile(QOpenGLShader*)) Warning: CanvasRenderer::executeCommandQueue: Failed to compile shader QOpenGLShader(0x2f6756f0) (canvasrenderer.cpp:1063, void QtCanvas3D::CanvasRenderer::executeCommandQueue()) Critical: THREE.WebGLShader: Shader couldn't compile. (qrc:/three.js:69, ) Warning: THREE.WebGLShader: gl.getShaderInfoLog() ERROR: 3:217: '' : array size must be a positive integer 1: precision highp float; 2: precision highp int; 3: 4: #define VERTEX_TEXTURES 5: 6: 7: #define GAMMA_FACTOR 2 8: #define MAX_DIR_LIGHTS 1 9: #define MAX_POINT_LIGHTS 0 10: #define MAX_SPOT_LIGHTS 0 11: #define MAX_HEMI_LIGHTS 0 12: #define MAX_SHADOWS 0 13: #define MAX_BONES 0 14: 15: 16: 17: 18: 19: 20: 21: 22: #define USE_COLOR 23: 24: #define USE_SKINNING 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: uniform mat4 modelMatrix; 38: uniform mat4 modelViewMatrix; 39: uniform mat4 projectionMatrix; 40: uniform mat4 viewMatrix; 41: uniform mat3 normalMatrix; 42: uniform vec3 cameraPosition; 43: attribute vec3 position; 44: attribute vec3 normal; 45: attribute vec2 uv; 46: attribute vec2 uv2; 47: #ifdef USE_COLOR 48: attribute vec3 color; 49: #endif 50: #ifdef USE_MORPHTARGETS 51: attribute vec3 morphTarget0; 52: attribute vec3 morphTarget1; 53: attribute vec3 morphTarget2; 54: attribute vec3 morphTarget3; 55: #ifdef USE_MORPHNORMALS 56: attribute vec3 morphNormal0; 57: attribute vec3 morphNormal1; 58: attribute vec3 morphNormal2; 59: attribute vec3 morphNormal3; 60: #else 61: attribute vec3 morphTarget4; 62: attribute vec3 morphTarget5; 63: attribute vec3 morphTarget6; 64: attribute vec3 morphTarget7; 65: #endif 66: #endif 67: #ifdef USE_SKINNING 68: attribute vec4 skinIndex; 69: attribute vec4 skinWeight; 70: #endif 71: #define PHONG 72: varying vec3 vViewPosition; 73: #ifndef FLAT_SHADED 74: varying vec3 vNormal; 75: #endif 76: #define PI 3.14159 77: #define PI2 6.28318 78: #define RECIPROCAL_PI2 0.15915494 79: #define LOG2 1.442695 80: #define EPSILON 1e-6 81: 82: float square( in float a ) { return a*a; } 83: vec2 square( in vec2 a ) { return vec2( a.x*a.x, a.y*a.y ); } 84: vec3 square( in vec3 a ) { return vec3( a.x*a.x, a.y*a.y, a.z*a.z ); } 85: vec4 square( in vec4 a ) { return vec4( a.x*a.x, a.y*a.y, a.z*a.z, a.w*a.w ); } 86: float saturate( in float a ) { return clamp( a, 0.0, 1.0 ); } 87: vec2 saturate( in vec2 a ) { return clamp( a, 0.0, 1.0 ); } 88: vec3 saturate( in vec3 a ) { return clamp( a, 0.0, 1.0 ); } 89: vec4 saturate( in vec4 a ) { return clamp( a, 0.0, 1.0 ); } 90: float average( in float a ) { return a; } 91: float average( in vec2 a ) { return ( a.x + a.y) * 0.5; } 92: float average( in vec3 a ) { return ( a.x + a.y + a.z) / 3.0; } 93: float average( in vec4 a ) { return ( a.x + a.y + a.z + a.w) * 0.25; } 94: float whiteCompliment( in float a ) { return saturate( 1.0 - a ); } 95: vec2 whiteCompliment( in vec2 a ) { return saturate( vec2(1.0) - a ); } 96: vec3 whiteCompliment( in vec3 a ) { return saturate( vec3(1.0) - a ); } 97: vec4 whiteCompliment( in vec4 a ) { return saturate( vec4(1.0) - a ); } 98: vec3 transformDirection( in vec3 normal, in mat4 matrix ) { 99: return normalize( ( matrix * vec4( normal, 0.0 ) ).xyz ); 100: } 101: // http://en.wikibooks.org/wiki/GLSL_Programming/Applying_Matrix_Transformations 102: vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) { 103: return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz ); 104: } 105: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { 106: float distance = dot( planeNormal, point-pointOnPlane ); 107: return point - distance * planeNormal; 108: } 109: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) { 110: return sign( dot( point - pointOnPlane, planeNormal ) ); 111: } 112: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) { 113: return pointOnLine + lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ); 114: } 115: float calcLightAttenuation( float lightDistance, float cutoffDistance, float decayExponent ) { 116: if ( decayExponent > 0.0 ) { 117: return pow( saturate( 1.0 - lightDistance / cutoffDistance ), decayExponent ); 118: } 119: return 1.0; 120: } 121: 122: vec3 inputToLinear( in vec3 a ) { 123: #ifdef GAMMA_INPUT 124: return pow( a, vec3( float( GAMMA_FACTOR ) ) ); 125: #else 126: return a; 127: #endif 128: } 129: vec3 linearToOutput( in vec3 a ) { 130: #ifdef GAMMA_OUTPUT 131: return pow( a, vec3( 1.0 / float( GAMMA_FACTOR ) ) ); 132: #else 133: return a; 134: #endif 135: } 136: 137: #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) 138: 139: varying vec2 vUv; 140: uniform vec4 offsetRepeat; 141: 142: #endif 143: 144: #ifdef USE_LIGHTMAP 145: 146: varying vec2 vUv2; 147: 148: #endif 149: #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) 150: 151: varying vec3 vReflect; 152: 153: uniform float refractionRatio; 154: 155: #endif 156: 157: #if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP ) 158: 159: varying vec3 vWorldPosition; 160: 161: #endif 162: 163: #ifdef USE_COLOR 164: 165: varying vec3 vColor; 166: 167: #endif 168: #ifdef USE_MORPHTARGETS 169: 170: #ifndef USE_MORPHNORMALS 171: 172: uniform float morphTargetInfluences[ 8 ]; 173: 174: #else 175: 176: uniform float morphTargetInfluences[ 4 ]; 177: 178: #endif 179: 180: #endif 181: #ifdef USE_SKINNING 182: 183: uniform mat4 bindMatrix; 184: uniform mat4 bindMatrixInverse; 185: 186: #ifdef BONE_TEXTURE 187: 188: uniform sampler2D boneTexture; 189: uniform int boneTextureWidth; 190: uniform int boneTextureHeight; 191: 192: mat4 getBoneMatrix( const in float i ) { 193: 194: float j = i * 4.0; 195: float x = mod( j, float( boneTextureWidth ) ); 196: float y = floor( j / float( boneTextureWidth ) ); 197: 198: float dx = 1.0 / float( boneTextureWidth ); 199: float dy = 1.0 / float( boneTextureHeight ); 200: 201: y = dy * ( y + 0.5 ); 202: 203: vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) ); 204: vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) ); 205: vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) ); 206: vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) ); 207: 208: mat4 bone = mat4( v1, v2, v3, v4 ); 209: 210: return bone; 211: 212: } 213: 214: #else 215: 216: uniform mat4 boneGlobalMatrices[ MAX_BONES ]; 217: 218: mat4 getBoneMatrix( const in float i ) { 219: 220: mat4 bone = boneGlobalMatrices[ int(i) ]; 221: return bone; 222: 223: } 224: 225: #endif 226: 227: #endif 228: 229: #ifdef USE_SHADOWMAP 230: 231: varying vec4 vShadowCoord[ MAX_SHADOWS ]; 232: uniform mat4 shadowMatrix[ MAX_SHADOWS ]; 233: 234: #endif 235: #ifdef USE_LOGDEPTHBUF 236: 237: #ifdef USE_LOGDEPTHBUF_EXT 238: 239: varying float vFragDepth; 240: 241: #endif 242: 243: uniform float logDepthBufFC; 244: 245: #endif 246: void main() { 247: #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) 248: 249: vUv = uv * offsetRepeat.zw + offsetRepeat.xy; 250: 251: #endif 252: #ifdef USE_LIGHTMAP 253: 254: vUv2 = uv2; 255: 256: #endif 257: #ifdef USE_COLOR 258: 259: vColor.xyz = inputToLinear( color.xyz ); 260: 261: #endif 262: #ifdef USE_MORPHNORMALS 263: 264: vec3 morphedNormal = vec3( 0.0 ); 265: 266: morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ]; 267: morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ]; 268: morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ]; 269: morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ]; 270: 271: morphedNormal += normal; 272: 273: #endif 274: #ifdef USE_SKINNING 275: 276: mat4 boneMatX = getBoneMatrix( skinIndex.x ); 277: mat4 boneMatY = getBoneMatrix( skinIndex.y ); 278: mat4 boneMatZ = getBoneMatrix( skinIndex.z ); 279: mat4 boneMatW = getBoneMatrix( skinIndex.w ); 280: 281: #endif 282: #ifdef USE_SKINNING 283: 284: mat4 skinMatrix = mat4( 0.0 ); 285: skinMatrix += skinWeight.x * boneMatX; 286: skinMatrix += skinWeight.y * boneMatY; 287: skinMatrix += skinWeight.z * boneMatZ; 288: skinMatrix += skinWeight.w * boneMatW; 289: skinMatrix = bindMatrixInverse * skinMatrix * bindMatrix; 290: 291: #ifdef USE_MORPHNORMALS 292: 293: vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 ); 294: 295: #else 296: 297: vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 ); 298: 299: #endif 300: 301: #endif 302: 303: #ifdef USE_SKINNING 304: 305: vec3 objectNormal = skinnedNormal.xyz; 306: 307: #elif defined( USE_MORPHNORMALS ) 308: 309: vec3 objectNormal = morphedNormal; 310: 311: #else 312: 313: vec3 objectNormal = normal; 314: 315: #endif 316: 317: #ifdef FLIP_SIDED 318: 319: objectNormal = -objectNormal; 320: 321: #endif 322: 323: vec3 transformedNormal = normalMatrix * objectNormal; 324: 325: #ifndef FLAT_SHADED 326: vNormal = normalize( transformedNormal ); 327: #endif 328: #ifdef USE_MORPHTARGETS 329: 330: vec3 morphed = vec3( 0.0 ); 331: morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ]; 332: morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ]; 333: morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ]; 334: morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ]; 335: 336: #ifndef USE_MORPHNORMALS 337: 338: morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ]; 339: morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ]; 340: morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ]; 341: morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ]; 342: 343: #endif 344: 345: morphed += position; 346: 347: #endif 348: #ifdef USE_SKINNING 349: 350: #ifdef USE_MORPHTARGETS 351: 352: vec4 skinVertex = bindMatrix * vec4( morphed, 1.0 ); 353: 354: #else 355: 356: vec4 skinVertex = bindMatrix * vec4( position, 1.0 ); 357: 358: #endif 359: 360: vec4 skinned = vec4( 0.0 ); 361: skinned += boneMatX * skinVertex * skinWeight.x; 362: skinned += boneMatY * skinVertex * skinWeight.y; 363: skinned += boneMatZ * skinVertex * skinWeight.z; 364: skinned += boneMatW * skinVertex * skinWeight.w; 365: skinned = bindMatrixInverse * skinned; 366: 367: #endif 368: 369: #ifdef USE_SKINNING 370: 371: vec4 mvPosition = modelViewMatrix * skinned; 372: 373: #elif defined( USE_MORPHTARGETS ) 374: 375: vec4 mvPosition = modelViewMatrix * vec4( morphed, 1.0 ); 376: 377: #else 378: 379: vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); 380: 381: #endif 382: 383: gl_Position = projectionMatrix * mvPosition; 384: 385: #ifdef USE_LOGDEPTHBUF 386: 387: gl_Position.z = log2(max( EPSILON, gl_Position.w + 1.0 )) * logDepthBufFC; 388: 389: #ifdef USE_LOGDEPTHBUF_EXT 390: 391: vFragDepth = 1.0 + gl_Position.w; 392: 393: #else 394: 395: gl_Position.z = (gl_Position.z - 1.0) * gl_Position.w; 396: 397: #endif 398: 399: #endif 400: vViewPosition = -mvPosition.xyz; 401: #if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP ) 402: 403: #ifdef USE_SKINNING 404: 405: vec4 worldPosition = modelMatrix * skinned; 406: 407: #elif defined( USE_MORPHTARGETS ) 408: 409: vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 ); 410: 411: #else 412: 413: vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); 414: 415: #endif 416: 417: #endif 418: 419: #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) 420: 421: vec3 worldNormal = transformDirection( objectNormal, modelMatrix ); 422: 423: vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition ); 424: 425: #ifdef ENVMAP_MODE_REFLECTION 426: 427: vReflect = reflect( cameraToVertex, worldNormal ); 428: 429: #else 430: 431: vReflect = refract( cameraToVertex, worldNormal, refractionRatio ); 432: 433: #endif 434: 435: #endif 436: 437: #if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP ) 438: 439: vWorldPosition = worldPosition.xyz; 440: 441: #endif 442: #ifdef USE_SHADOWMAP 443: 444: for( int i = 0; i < MAX_SHADOWS; i ++ ) { 445: 446: vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition; 447: 448: } 449: 450: #endif 451: } (qrc:/three.js:68, ) Warning: QOpenGLShader::link: "Attached vertex shader is not compiled.\n" (opengl\qopenglshaderprogram.cpp:1049, virtual bool QOpenGLShaderProgram::link()) Warning: CanvasRenderer::executeCommandQueue: Failed to link program: QOpenGLShaderProgram(0x2f6746a0) (canvasrenderer.cpp:1252, void QtCanvas3D::CanvasRenderer::executeCommandQueue()) Critical: THREE.WebGLProgram: shader error: 1281 gl.VALIDATE_STATUS false gl.getPRogramInfoLog Attached vertex shader is not compiled. (qrc:/three.js:69, ) Warning: THREE.WebGLProgram: gl.getProgramInfoLog()Attached vertex shader is not compiled. (qrc:/three.js:68, ) Warning: QOpenGLShaderProgram::uniformLocation( viewMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( modelViewMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( projectionMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( normalMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( modelMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( cameraPosition ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( morphTargetInfluences ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( bindMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( bindMatrixInverse ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( boneGlobalMatrices ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( alphaMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( diffuse ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( envMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( flipEnvMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( lightMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( map ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( morphTargetInfluences ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( offsetRepeat ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( opacity ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( reflectivity ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( refractionRatio ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( specularMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( bumpMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( bumpScale ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( normalMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( normalScale ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( fogColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( fogDensity ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( fogFar ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( fogNear ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( ambientLightColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( directionalLightColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( directionalLightDirection ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( hemisphereLightDirection ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( hemisphereLightGroundColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( hemisphereLightSkyColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( pointLightColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( pointLightDecay ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( pointLightDistance ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( pointLightPosition ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightAngleCos ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightColor ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightDecay ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightDirection ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightDistance ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightExponent ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( spotLightPosition ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shadowBias ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shadowDarkness ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shadowMap ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shadowMapSize ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shadowMatrix ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( emissive ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( shininess ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( specular ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::uniformLocation( wrapRGB ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1890, int QOpenGLShaderProgram::uniformLocation(const char*) const) Warning: QOpenGLShaderProgram::attributeLocation( position ): shader program is not linked (opengl\qopenglshaderprogram.cpp:1214, int QOpenGLShaderProgram::attributeLocation(const char*) const) Warning: CanvasRenderer::bindCurrentRenderTarget: OpenGL ERROR: 1282 (canvasrenderer.cpp:639, bool QtCanvas3D::CanvasRenderer::updateGlError(const char*)) 上述是问题日志输出,由于在独立显卡的openlg4.1版本运行正常,opengl4.0版本的平板运行失败,目前平板的集显,opengl4.0,windows10,很无奈,不知道为什么希望有大神能够指点迷津。模型使用blender转json,io_three多个版本都实验的,不成功。 人工智能 2018-10-08 15:14:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 人工智能技术的透明度和道德伦理问题,正引发越来越多的关注,这促使云计算服务提供商推出新工具,解释人工智能算法背后的决策过程。 会计和金融等强监管行业的高管表示,数据科学家和非技术业务经理都必须能理解算法决策背后的流程,这至关重要。这样的理解在防范潜在道德违规和监管违规方面可能会带来深远影响,尤其考虑到企业级人工智能算法正变得越来越普遍。 毕马威创新和企业解决方案部门负责智能自动化、认知和人工智能的高管维诺德·斯瓦米纳桑(Vinodh Swaminathan)表示:“我认为,除非具备这种解释能力,否则人工智能在企业中的规模不可能超过数百个试点应用。” 对人工智能的解释问题已经促使IBM和谷歌等公司在云计算人工智能服务产品中引入透明度和道德伦理工具。比如,IBM商业价值研究所近期的一项研究调查了5000名企业高管。约60%的受访者表示,他们关心如何解释人工智能使用数据作出决策,以达到监管和合规标准。这个比例较2016年时的29%大幅上升。 甚至对数据科学家和相关企业高管来说,人工智能的决策过程有时都是“黑盒”。在深度学习工具,例如用于模式识别的神经网络中尤其如此。这样的神经网络试图模拟人脑的运转方式。尽管这些系统可以以前所未有的准确率和速度得出结论,但外界并不总是非常清楚,计算网络是如何做出具体的决策。 毕马威内部的数据科学家正在开发自主的可解释性工具,此外该公司也在利用IBM新的透明度工具。斯瓦米纳桑表示,这样做的目的是确保技术和业务两方面的员工都能“打开黑盒”,准确地了解人工智能算法是如何做出结论的。 IBM上周发布了新的云计算人工智能工具,可以向用户展示哪些主要因素影响了人工智能做出的建议。这些工具还可以实时分析人工智能决策,以识别固有偏见,并推荐数据和方法来解决这些偏见。IBM负责认知解决方案的高级副总裁大卫·肯尼(David Kenny)表示,这些工具可以与IBM人工智能服务,以及谷歌等其他云计算服务提供商的工具配合使用。 人工智能 2018-10-08 10:37:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 损失函数可以有两种 1.点到直线的竖直距离 2.点到直线的垂直距离 import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from sklearn import datasets sess = tf.Session() iris = datasets.load_iris() x_vals = np.array([x[3] for x in iris.data]) y_vals = np.array([y[0] for y in iris.data]) learn_rate = 0.05 batchsize = 50 x_data = tf.placeholder(shape=[None,1],dtype=tf.float32) y_target = tf.placeholder(shape=[None,1],dtype=tf.float32) A = tf.Variable(tf.random_normal(shape=[1,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) demming_upper = tf.abs(tf.subtract(y_target,tf.add(tf.matmul(x_data,A),b))) demming_down = tf.sqrt(tf.add(tf.square(A),1)) loss = tf.reduce_mean(tf.truediv(demming_upper,demming_down)) init = tf.global_variables_initializer() sess.run(init) my_opt = tf.train.GradientDescentOptimizer(0.1) train_step = my_opt.minimize(loss) loss_dev = [] for i in range(250): rand_index = np.random.choice(len(x_vals), size=batchsize) rand_x = np.transpose([x_vals[rand_index]]) rand_y = np.transpose([y_vals[rand_index]]) sess.run(train_step,feed_dict={x_data:rand_x,y_target:rand_y}) temp_loss = sess.run(loss,feed_dict={x_data:rand_x,y_target:rand_y}) loss_dev.append(temp_loss) [slope] = sess.run(A) [intercept] = sess.run(b) best_line = [] for x in x_vals: best_line.append(slope*x+intercept) plt.plot(x_vals,y_vals,'bo',label = 'data') plt.plot(x_vals,best_line,'r--',label = 'best line') plt.legend(loc = 'upper right') plt.show() plt.plot(loss_dev) plt.show() 人工智能 2018-10-07 16:29:00 「深度学习福利」大神带你进阶工程师,立即查看>>> import matplotlib.pyplot as plt import numpy as np import tensorflow as tf sess = tf.Session() x_vals = np.linspace(0,10,100) y_vals = x_vals+np.random.normal(0,1,100) x_vals_column = np.transpose(np.matrix(x_vals)) one_clumn = np.transpose(np.matrix(np.repeat(1,100))) A = np.column_stack((x_vals_column,one_clumn)) b =np.transpose(np.matrix(y_vals)) A_tensor = tf.constant(A) b_tensor = tf.constant(b) tA_A = tf.matmul(tf.transpose(A_tensor),A_tensor) tA_A_inv = tf.matrix_inverse(tA_A) product = tf.matmul(tA_A_inv,tf.transpose(A_tensor)) solution = tf.matmul(product,b_tensor) solution_eval = sess.run(solution) print(solution_eval) slope = solution_eval[0][0] y_intercept = solution_eval[1][0] best_fit =[] for i in x_vals: best_fit.append(slope*i+y_intercept) plt.plot(x_vals,y_vals,'o',label = 'data') plt.plot(x_vals,best_fit,'r--',label='best fit line') plt.legend(loc = 'upper left') plt.show() 人工智能 2018-10-06 19:16:00 「深度学习福利」大神带你进阶工程师,立即查看>>> import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from sklearn import datasets from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.Session() iris = datasets.load_iris() x_vals = np.array([x[3] for x in iris.data]) y_vals = np.array([y[0] for y in iris.data]) learn_rate = 0.05 batch_size = 25 x_data = tf.placeholder(shape=[None,1],dtype=tf.float32) y_target = tf.placeholder(shape=[None,1],dtype=tf.float32) A = tf.Variable(tf.random_normal(shape=[1,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) model_output = tf.add(tf.matmul(x_data,A),b) loss = tf.reduce_mean(tf.square(y_target-model_output)) init = tf.global_variables_initializer() sess.run(init) my_opt = tf.train.GradientDescentOptimizer(learn_rate) train_step = my_opt.minimize(loss) loss_vec = [] for i in range(200): rand_index = np.random.choice(len(x_vals),size=batch_size) rand_x = np.transpose([x_vals[rand_index]]) rand_y = np.transpose([y_vals[rand_index]]) sess.run(train_step,feed_dict={x_data:rand_x,y_target:rand_y}) temp_loss = sess.run(loss,feed_dict={x_data:rand_x,y_target:rand_y}) loss_vec.append(temp_loss) [slope] = sess.run(A) [intercept] = sess.run(b) best_fit =[] for i in x_vals: best_fit.append(i*slope+intercept) plt.plot(x_vals,y_vals,'x',label='data') plt.plot(x_vals,best_fit,'r--',label='best line') plt.legend(loc ='upper right') plt.show() plt.plot(loss_vec,'k-') plt.show() 人工智能 2018-10-06 19:14:00 「深度学习福利」大神带你进阶工程师,立即查看>>> https://blog.csdn.net/gavinmiaoc/article/details/80802967 深度学习的Image Inpainting (图像修复)论文推荐(持续更新) https://blog.csdn.net/gavinmiaoc/article/details/80802967 人工智能 2018-10-04 14:09:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 定义:采用测量不同特征值之间的距离方法进行分类。 优点:精度高,对异常值不敏感,无数据输入假定。 缺点:计算复杂度高,空间度高。 适用数据范围: 数值型和标称型。 工作原理: 存在一个样本数据集合,并且样本中每个数据都存在标签,输入没有标签的数据后,将新数据的每个特征值与样本集中的每个特征值比对,然后算法提取样本中特征值最相似数据(最近邻)的分类标签。最后选择k个最相似数据中出现次数最多的分类,作为最新的分类。 人工智能 2018-10-04 10:17:00 「深度学习福利」大神带你进阶工程师,立即查看>>> import matplotlib.pyplot as plt import numpy as np import pandas as pd from mpl_toolkits.mplot3d import Axes3D t = np.arange(0,5,0.2) plt.plot(t,t**2,'g^') plt.ylabel('nubers') plt.xlabel('seed') plt.title('title') plt.grid(True) plt.yscale('log') plt.show() #sinx的函数图 t = np.arange(0,2.0,0.01) s= np.sin(2 * np.pi*t) fig,ax = plt.subplots() ax.set_ylabel('sin') ax.set_xlabel('df') ax.plot(t,s) plt.grid(True) plt.show() #饼状图 labels ='frogs','hogs','dogs','logs' sizes =[15,45,10,30] explode=(0,0.1,0,0) fig,ax = plt.subplots() ax.pie(sizes,explode=explode,labels=labels,autopct='%1.lf%%', shadow=True,startangle=90) plt.show() #ax.scatter表示散点图 #化三维图形 fig,ax = plt.subplots() fig = plt.figure() ax = fig.add_subplot(111,projection ='3d') x,y=np.random.rand(2,100)*4 ax.bar3d(x,y,x,x,y,x**y) plt.show() df=pd.DataFrame(np.random.randn(1000,4), index=pd.date_range('1/1/2000',periods=1000),columns=list('abcd')) df =df.cumsum() plt.show() df = pd.DataFrame(np.random.randn(5,4),index=['A','B','C','D','E'],columns=pd.Index([1,2,3,4])) df.plot(kind='bar') plt.show() import numpy as np data = np.array([[1,2,3],[3,4,5]]) data = np.zeros((2,3)) data1 = np.empty((2,3)) data2 = np.arange(3,6,2) data3 = np.random.rand(2,3,) data4 = np.arange(100) data5 = np.arange(0,20,2) data5[1:4:2]=1 data6 = np.array([[1,2,3],[4,5,6],[7,8,9]]) data6.sum(axis=1) data6.min(1) print(data6.std())#计算标准差 print(data6.var())#计算方差 人工智能 2018-09-30 21:01:00 「深度学习福利」大神带你进阶工程师,立即查看>>> simple-car-plate-recognition 简单车牌识别-Mask_RCNN定位车牌+手写方法分割字符+CNN单个字符识别 数据准备 准备用于车牌定位的数据集,要收集250张车辆图片,200张用于训练,50张用于测试,然后在这些图片上标注出车牌区域。这里有图片 https://gitee.com/easypr/EasyPR/tree/master/resources/image/general_test 。标注工具使用VGG Image Annotator (VIA),就是一个网页程序,可以导入图片,使用多边形标注,标注好了以后,导出json。我已经标注好的数据集可以从这里下载 https://github.com/airxiechao/simple-car-plate-recognition/blob/master/dataset/carplate.zip ,用7zip解压。 准备用于字符识别的数据集,包含分隔好的单个车牌汉子、字母和数字。这里有 https://gitee.com/easypr/EasyPR/blob/master/resources/train/ann.7z 。 训练Mask-RCNN定位车牌 这篇文章 https://engineering.matterport.com/splash-of-color-instance-segmentation-with-mask-r-cnn-and-tensorflow-7c761e238b46 讲了如何用Mask-RCNN识别图片中的气球,仿照其方法。在 https://github.com/matterport/Mask_RCNN/releases 下载预先用COCO数据集训练好的模型mask_rcnn_coco.h5,按照文章的方法编写carplate.py用于载入车辆图片数据和训练,用inspect_data.ipynb浏览标注数据。执行python carplate.py train --dataset=../dataset/carplate --weights=coco 进行训练,训练完后,在logs文件夹中找到最后一轮的h5模型文件,比如mask_rcnn_carplate_0030.h5,复制出来。用inspect_model.ipynd查看模型训练的效果。这部分代码在 https://github.com/airxiechao/simple-car-plate-recognition/tree/master/Mask_RCNN 下载。 训练CNN单个字符识别 仿照keras的mnist_cnn例子 https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py 训练,将训练好的模型导出为char_cnn.h5。这部分代码在 https://github.com/airxiechao/simple-car-plate-recognition/blob/master/char_cnn/char_cnn.ipynb 下载。 分割车牌字符 把车牌区域转换成灰度图像,利用边缘特征分割出区域,再筛选出字符区域。字符分割代码是 https://github.com/airxiechao/simple-car-plate-recognition/blob/master/character_segmentation.ipynb 。 执行推理 代码在 https://github.com/airxiechao/simple-car-plate-recognition/blob/master/inference.ipynb 。 人工智能 2018-10-10 14:52:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 项目的目的 在做问答系统研究的时候,想通过deeplearning的方法获得句子语义,并计算两个问句的相似度,为此需要相似问题的数据集,但是一般相似问题数据集很难获取,特别是质量较高的数据集。为此,想到用目前最先进的翻译系统来实现构建数据。 另外,当前NLP比较成功应用就是机器翻译,之所以deeplearning能成功应用到翻译,得意于庞大的、高质量的翻译语料数据。 1.爬取goole翻译 说明 谷歌翻译直接通过request请求是获取不到结果的,需要tk值,tk值需要由问句+tkk值来生成。 获取tkk: requests获取主页面,只需re正则在主页面上获取tkk值(之前需要通过js脚本来实现, 参考 ) 获取tk 通过js脚本实现:gettk.js var b = function (a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c), c = "+" == b.charAt(d + 1) ? a >>> c : a << c; a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a } var tk = function (a,TKK) { //console.log(a,TKK); for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) { var c = a.charCodeAt(f); 128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128) } a = h; for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6"); a = b(a, "+-3^+b+-f"); a ^= Number(e[1]) || 0; 0 > a && (a = (a & 2147483647) + 2147483648); a %= 1E6; return a.toString() + "." + (a ^ h) } 最终地址 将tk值和需要翻译的句子代人如下格式 中-英 : https://translate.google.cn/translate_a/single?client=t&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=bh&ssel=0&tsel=0&kc=1&tk=xxxxxx&q=xxxxxxx 英-中: https://translate.google.cn/translate_a/single?client=t&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=bh&ssel=0&tsel=0&kc=1&tk=xxxxxx&q=xxxxxxx 程序中简单增加了中英文判断。 requests获取结果 获取的结果在三维的列表中,list[0][0][0]即为所需的结果 程序 goole_trans.py import requests import urllib import re import json import execjs class Goole_translate(): def __init__(self): self.url_base = 'https://translate.google.cn' self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'} self.get_tkk() def get_tkk(self): page = requests.get(self.url_base, headers= self.headers ) tkks = re.findall(r"TKK='(.+?)';", page.text) if tkks: self.tkk = tkks[0] return self.tkk else: raise ('no found tkk') def translate(self, query_string): last_url = self.get_last_url(query_string) response = requests.get(last_url, headers=self.headers) if response.status_code != 200: self.get_tkk() last_url = self.get_last_url(query_string) response = requests.get(last_url, headers=self.headers) content = response.content # bytes类型 text = content.decode() # str类型 , 两步可以用text=response.text替换 dict_text = json.loads(text) # 数据是json各式 result = dict_text[0][0][0] return result def get_tk(self, query_string): tem = execjs.compile(open(r"gettk.js").read()) tk = tem.call('tk', query_string, self.tkk) return tk def query_string(self, query_string): '''将字符串转换为utf8格式的字符串,本身已utf8格式定义的字符串可以不需要''' query_url_trans = urllib.parse.quote(query_string) # 汉字url编码, 转为utf-8各式 return query_url_trans def get_last_url(self, query_string): url_parm = 'sl=en&tl=zh-CN' for uchar in query_string: if uchar >= u'一' and uchar <= u'龥': url_parm = 'sl=zh-CN&tl=en' break url_param_part = self.url_base + "/translate_a/single?" url_param = url_param_part + "client=t&"+ url_parm+"&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=btn&ssel=3&tsel=3&kc=0&" url_get = url_param + "tk=" + str(self.get_tk(query_string)) + "&q=" + str(self.query_string(query_string)) return url_get if __name__=="__main__": query_string = 'how are you' gt = Goole_translate() en = gt.translate(query_string) print(en) 注意 频繁访问可能被封,没有测试过,可以设置延时或ip代理 参考 http://www.cnblogs.com/by-dream/p/6554340.html https://blog.csdn.net/boyheroes/article/details/78681357 2.爬取百度翻译 自动检测中英文 获取百度翻译结果 程序 baidu_trans.py # coding=utf-8 import requests import json class Baidu_translate(): def __init__(self): self.headers = {"User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"} self.lang_detect_url = "https://fanyi.baidu.com/langdetect" self.trans_url = "https://fanyi.baidu.com/basetrans" def get_lang(self,query_string): '''自动检测语言''' data = {'query':query_string} response = requests.post(self.lang_detect_url, data=data, headers=self.headers) return json.loads(response.text)['lan'] def translate(self,query_string): '''翻译''' lang = self.get_lang(query_string) data = {"query":query_string,"from":"zh","to":"en"} if lang== "zh" else {"query":query_string,"from":"en","to":"zh"} response = requests.post(self.trans_url, data=data, headers=self.headers) result = json.loads(response.text)["trans"][0]["dst"] return result if __name__ == '__main__': query_string = 'how are you' bt = Baidu_translate() en = bt.translate(query_string) print(en) 参考 https://blog.csdn.net/blues_f/article/details/79319461 3.中文-英文-中文 通过中-英-中可以产生相似问答对语料。 q_zh:中文问句 g_en:谷歌对中文问句中-英翻译 b_zh:百度对谷歌结果进行英-中翻译 程序 zh_en_zh_translate.py ''' 通过翻译实现中文问句的相似问法,来产生相似问题对数据集。可用于语义相似模型训练。 goole翻译:中文-英文 baidu翻译:英文-中文 注意:本程序未设置ip代理,频繁访问谨防被封。(只做了简单的随机延迟措施) ''' import time import random from goole_trans import Goole_translate from baidu_trans import Baidu_translate gt = Goole_translate() bt = Baidu_translate() r_file = 'data/zh.txt' w_file = 'data/zh_en_zh.txt' fw = open(w_file,'w',encoding='utf8') with open(r_file,'r',encoding='utf8') as f: for line in f: r = random.random()*10 time.sleep(r) ls = line.strip().split('\t') query_string = ls[0] g_en = gt.translate(query_string) b_zh = bt.translate(g_en) fw.write(query_string+'\t'+g_en+'\t'+b_zh+'\n') print('q_zh:',query_string) print('g_en:',g_en) print('b_zh:',b_zh) print('\n') fw.close() 运行结果 q_zh: 下周有什么好产品? g_en: What are the good products next week? b_zh: 下周的好产品是什么? ------------ q_zh: 第一次使用,额度多少? g_en: What is the amount of the first use? b_zh: 第一次使用的数量是多少? ------------ q_zh: 我什么时候可以通过微粒贷借钱 g_en: When can I borrow money from micro-credit? b_zh: 我什么时候可以从小额信贷中借钱? ------------ q_zh: 借款后多长时间给打电话 g_en: How long does it take to make a call after borrowing? b_zh: 借钱后打电话需要多长时间? ------------ q_zh: 没看到微粒贷 g_en: Didn't see the micro-credit b_zh: 没有看到小额信贷 ------------ q_zh: 原来的手机号不用了,怎么换 g_en: The original mobile phone number is not used, how to change b_zh: 原来的手机号码没有用,怎么改 注意 程序均未设置ip代理,频繁访问谨防被封。(只做了简单的随机延迟措施),后期若更新,见github: github地址 人工智能 2018-09-25 18:00:00 「深度学习福利」大神带你进阶工程师,立即查看>>> 使用方式如下: 类名::方法名 方法名后面没有括号“()”。Lambda有懒加载嘛,不要括号就是说,看情况调用方法。 person -> person.getAge(); 可以替换成 Person::getAge 人工智能 2018-09-25 16:48:00 |