人工智能(AI) 、机器学习(ML)、深度学习(DL)、神经网络(CNN)
IT技术圈的人怼这些词汇大家都一定耳熟能详了,可能圈外的也不陌生,但是作为一个作为一个“攻城狮”,还是JAVA系的攻城狮。我感觉实在不从下手,搜索的资料大都是PYTHON,GO,C++。为此深感觉遗憾,于是只能去用python 完成一些工作和个人需求。在工作过程中总于发现了一些可以支持java的框架了,下面就以 IgniteML举例:
IgniteML(机器学习)是一组简单的、可扩展以及高效的工具,在不需要成本高昂的数据转换的前提下,就可以构建可预测的机器学习模型。
将机器和深度学习加入Ignite的原理是很简单的,当前,如果要想让机器学习成为主流,数据科学家要解决两个主要的问题:
- 首先,模型是在不同的系统中训练和部署(训练结束之后)的,数据科学家需要等待ETL或者其它的数据传输过程,来将数据移至比如Apache Mahout或者Apache Spark这样的系统进行训练,然后还要等待这个过程结束并且将模型部署到生产环境。在系统间移动TB级的数据可能花费数小时的时间,此外,训练部分通常发生在旧的数据集上;
- 第二个问题和扩展性有关。机器学习和深度学习需要处理的数据量不断增长,已经无法放在单一的服务器上。这促使数据科学家要么提出更复杂的解决方案,要么切换到比如Spark或者TensorFlow这样的分布式计算平台上。但是这些平台通常只能解决模型训练的一部分问题,这给开发者之后的生产部署带来了很多的困难。
无ETL和大规模可扩展性
IgniteML依赖于Ignite基于内存的存储,这给机器学习和深度学习任务带来了大规模的扩展性,并且取消了在不同系统间进行ETL产生的等待。比如,在Ignite集群的内存和磁盘中存储的数据上,开发者可以直接进行深度学习和机器学习的训练和推理,然后,Ignite提供了一系列的机器学习和深度学习算法,对Ignite的分布式并置处理进行优化,这样在处理大规模的数据集或者不断增长的输入数据流时,这样的实现提供了内存级的速度和近乎无限的扩展性,而不需要将数据移到另外的存储。通过消除数据的移动以及长时间的处理等待,IgniteML可以持续地进行学习,可以在最新数据到来之时实时地对决策进行改进。
容错和持续学习
IgniteML能够对节点的故障容错。这意味着如果在学习期间节点出现故障,所有的恢复过程对用户是透明的,学习过程不会被中断,就像所有节点都正常那样获得结果。
官网地址: 传送门
下面举例一个ANN的实现需要的jar包:
<!-- ignite内存数据库start --><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-core</artifactId><version>${ignite.version}</version></dependency><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-spring</artifactId><version>${ignite.version}</version></dependency><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-indexing</artifactId><version>${ignite.version}</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>${h2.version}</version></dependency><!-- ignite内存数据库end --><!-- ignite机器学习start --><dependency><groupId>org.apache.ignite</groupId><artifactId>ignite-ml</artifactId><version>${ignite.version}</version></dependency>
实例代码:
import java.util.Arrays;
import java.util.UUID;import javax.cache.Cache;import org.apache.commons.math3.util.Precision;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.ml.dataset.feature.extractor.Vectorizer;
import org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer;
import org.apache.ignite.ml.knn.NNClassificationModel;
import org.apache.ignite.ml.knn.ann.ANNClassificationTrainer;
import org.apache.ignite.ml.math.distances.EuclideanDistance;
import org.apache.ignite.ml.math.distances.ManhattanDistance;
import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector;import com.gitee.kinbug.flying.plugin.ignite.igniteConfig;public class ANNClassificationExample {public static void main(String[] args) {System.out.println();System.out.println(">>> 基于缓存数据集的ANN多类分类算法使用实例启动。");// Start ignite grid.try (Ignite ignite = igniteConfig.igniteStart()) {System.out.println(">>> Ignite grid started.");IgniteCache<Integer, double[]> dataCache = null;try {dataCache = getTestCache(ignite);ANNClassificationTrainer trainer = new ANNClassificationTrainer().withDistance(new ManhattanDistance()).withK(50).withMaxIterations(1000).withEpsilon(1e-2);long startTrainingTime = System.currentTimeMillis();NNClassificationModel knnMdl = trainer.fit(ignite,dataCache,new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST)).withK(5).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);long endTrainingTime = System.currentTimeMillis();System.err.println(">>> ---------------------------------");System.err.println(">>> | 预测\t| 实际数据\t|");System.err.println(">>> ---------------------------------");int amountOfErrors = 0;int totalAmount = 0;long totalPredictionTime = 0L;try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {for (Cache.Entry<Integer, double[]> observation : observations) {double[] val = observation.getValue();double[] inputs = Arrays.copyOfRange(val, 1, val.length);double groundTruth = val[0];long startPredictionTime = System.currentTimeMillis();double prediction = knnMdl.predict(new DenseVector(inputs));long endPredictionTime = System.currentTimeMillis();totalPredictionTime += (endPredictionTime - startPredictionTime);totalAmount++;if (!Precision.equals(groundTruth, prediction, Precision.EPSILON))amountOfErrors++;System.err.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);}System.err.println(">>> ---------------------------------");System.err.println("样本数量 = "+totalAmount);System.err.println("训练时间成本 = " + (endTrainingTime - startTrainingTime));System.err.println("预测时间成本 = " + totalPredictionTime);System.err.println("\n>>> 绝对误差量 " + amountOfErrors);System.err.println("\n>>> 准确性 " + (1 - amountOfErrors / (double)totalAmount));System.err.println(">>> ANN多类分类算法在缓存数据集上的使用实例完成。");}}finally {dataCache.destroy();}}finally {System.out.flush();}}/*** 用数据填充缓存并返回数据。** @param ignite Ignite 实例.* @return Filled Ignite 缓存.*/private static IgniteCache<Integer, double[]> getTestCache(Ignite ignite) {CacheConfiguration<Integer, double[]> cacheConfiguration = new CacheConfiguration<>();cacheConfiguration.setName("TEST_" + UUID.randomUUID());cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 10));IgniteCache<Integer, double[]> cache = ignite.createCache(cacheConfiguration);for (int k = 0; k < 10; k++) { // multiplies the Iris dataset k times.for (int i = 0; i < data.length; i++)cache.put(k * 10000 + i, data[i]);}return cache;}/*** Iris数据集。*/private static final double[][] data = {{1, 5.1, 3.5, 1.4, 0.3},{1, 4.9, 3, 1.4, 0.2},{1, 4.7, 3.2, 1.3, 0.2},{1, 4.6, 3.1, 1.5, 0.2},{1, 5, 3.6, 1.4, 0.2},{1, 5.4, 3.9, 1.7, 0.4},{1, 4.6, 3.4, 1.4, 0.3},{1, 5, 3.4, 1.5, 0.2},{1, 4.4, 2.9, 1.4, 0.2},{1, 4.9, 3.1, 1.5, 0.1},{1, 5.4, 3.7, 1.5, 0.2},{1, 4.8, 3.4, 1.6, 0.2},{1, 4.8, 3, 1.4, 0.1},{1, 4.3, 3, 1.1, 0.1},{1, 5.8, 4, 1.2, 0.2},{1, 5.7, 4.4, 1.5, 0.4},{1, 5.4, 3.9, 1.3, 0.4},{1, 5.1, 3.5, 1.4, 0.3},{1, 5.7, 3.8, 1.7, 0.3},{1, 5.1, 3.8, 1.5, 0.3},{1, 5.4, 3.4, 1.7, 0.2},{1, 5.1, 3.7, 1.5, 0.4},{1, 4.6, 3.6, 1, 0.2},{1, 5.1, 3.3, 1.7, 0.5},{1, 4.8, 3.4, 1.9, 0.2},{1, 5, 3, 1.6, 0.2},{1, 5, 3.4, 1.6, 0.4},{1, 5.2, 3.5, 1.5, 0.2},{1, 5.2, 3.4, 1.4, 0.2},{1, 4.7, 3.2, 1.6, 0.2},{1, 4.8, 3.1, 1.6, 0.2},{1, 5.4, 3.4, 1.5, 0.4},{1, 5.2, 4.1, 1.5, 0.1},{1, 5.5, 4.2, 1.4, 0.2},{1, 4.9, 3.1, 1.5, 0.1},{1, 5, 3.2, 1.2, 0.2},{1, 5.5, 3.5, 1.3, 0.2},{1, 4.9, 3.1, 1.5, 0.1},{1, 4.4, 3, 1.3, 0.2},{1, 5.1, 3.4, 1.5, 0.2},{1, 5, 3.5, 1.3, 0.3},{1, 4.5, 2.3, 1.3, 0.3},{1, 4.4, 3.2, 1.3, 0.2},{1, 5, 3.5, 1.6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