├── .gitignore ├── .gitmodules ├── .travis.yml ├── AppledocSettings.plist ├── COPYING ├── Logo.png ├── README.md ├── YCML Tests (Swift) ├── Info.plist └── YCML_Tests__Swift_.swift ├── YCML.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ ├── YCML.xccheckout │ │ └── YCML.xcscmblueprint │ └── xcuserdata │ │ └── yanconst.xcuserdatad │ │ └── WorkspaceSettings.xcsettings ├── xcshareddata │ └── xcschemes │ │ └── YCML.xcscheme └── xcuserdata │ └── yanconst.xcuserdatad │ └── xcschemes │ ├── YCMatrix.xcscheme │ └── xcschememanagement.plist ├── YCML ├── Data Frame │ ├── NSIndexSet+Sampling.h │ ├── NSIndexSet+Sampling.m │ ├── OrderedDictionary.h │ ├── OrderedDictionary.m │ ├── YCDataframe+Matrix.h │ ├── YCDataframe+Matrix.m │ ├── YCDataframe+Transform.h │ ├── YCDataframe+Transform.m │ ├── YCDataframe.h │ ├── YCDataframe.m │ ├── YCMissingValue.h │ ├── YCMissingValue.m │ ├── YCMutableArray.h │ └── YCMutableArray.m ├── ELM │ ├── YCELMTrainer.h │ └── YCELMTrainer.m ├── FFN │ ├── YCBackPropProblem.h │ ├── YCBackPropProblem.m │ ├── YCBackPropTrainer.h │ ├── YCBackPropTrainer.m │ ├── YCFFN.h │ ├── YCFFN.m │ ├── YCRpropTrainer.h │ └── YCRpropTrainer.m ├── IO │ ├── YCBinaryRBM+IO.h │ ├── YCBinaryRBM+IO.m │ ├── YCFFN+IO.h │ ├── YCFFN+IO.m │ ├── YCFullyConnectedLayer+IO.h │ ├── YCFullyConnectedLayer+IO.m │ ├── YCGenericModel+IO.h │ ├── YCGenericModel+IO.m │ ├── YCGenericTrainer+IO.h │ ├── YCGenericTrainer+IO.m │ ├── YCModelIO.h │ ├── YCModelLayer+IO.h │ ├── YCModelLayer+IO.m │ ├── YCRBFNet+IO.h │ ├── YCRBFNet+IO.m │ ├── YCSupervisedModel+IO.h │ └── YCSupervisedModel+IO.m ├── Info.plist ├── KPM │ ├── YCKPM.h │ ├── YCKPM.m │ ├── YCKPMTrainer.h │ └── YCKPMTrainer.m ├── Kernels │ ├── YCLinearKernel.h │ ├── YCLinearKernel.m │ ├── YCModelKernel.h │ ├── YCModelKernel.m │ ├── YCRBFKernel.h │ └── YCRBFKernel.m ├── Layers │ ├── YCFullyConnectedLayer.h │ ├── YCFullyConnectedLayer.m │ ├── YCLinearLayer.h │ ├── YCLinearLayer.m │ ├── YCModelLayer.h │ ├── YCModelLayer.m │ ├── YCReLULayer.h │ ├── YCReLULayer.m │ ├── YCSigmoidLayer.h │ ├── YCSigmoidLayer.m │ ├── YCTanhLayer.h │ └── YCTanhLayer.m ├── Linear Regression │ ├── YCLinRegModel.h │ ├── YCLinRegModel.m │ ├── YCLinRegTrainer.h │ ├── YCLinRegTrainer.m │ ├── YCLinearModelProblem.h │ └── YCLinearModelProblem.m ├── Optimization │ ├── Genetic Algorithms │ │ ├── YCHYPE.h │ │ ├── YCHypE.m │ │ ├── YCNSGAII.h │ │ └── YCNSGAII.m │ ├── Gradient Descent │ │ ├── YCGradientDescent.h │ │ └── YCGradientDescent.m │ ├── Metrics │ │ ├── YCHypervolumeMetric.h │ │ └── YCHypervolumeMetric.m │ ├── RProp │ │ ├── YCRProp.h │ │ └── YCRProp.m │ ├── YCCompoundProblem.h │ ├── YCCompoundProblem.m │ ├── YCDerivativeProblem.h │ ├── YCIndividual.h │ ├── YCIndividual.m │ ├── YCOptimizer.h │ ├── YCOptimizer.m │ ├── YCPopulationBasedOptimizer.h │ ├── YCPopulationBasedOptimizer.m │ └── YCProblem.h ├── RBF │ ├── YCOLSPRESSTrainer.h │ ├── YCOLSPRESSTrainer.m │ ├── YCOLSTrainer.h │ ├── YCOLSTrainer.m │ ├── YCRBFNet.h │ └── YCRBFNet.m ├── RBM │ ├── YCBinaryRBM.h │ ├── YCBinaryRBM.m │ ├── YCCDProblem.h │ ├── YCCDProblem.m │ ├── YCCDTrainer.h │ └── YCCDTrainer.m ├── Ranking │ ├── YCRankCentrality.h │ └── YCRankCentrality.m ├── Regression Metrics │ ├── YCRegressionMetrics.h │ └── YCRegressionMetrics.m ├── SVR │ ├── YCLinkedList.h │ ├── YCLinkedList.m │ ├── YCSMOCache.h │ ├── YCSMOCache.m │ ├── YCSMORegressionTrainer.h │ ├── YCSMORegressionTrainer.m │ ├── YCSVR.h │ └── YCSVR.m ├── Surrogate Modeling │ ├── YCSurrogateModel.h │ └── YCSurrogateModel.m ├── Testing │ ├── YCMonteCarloValidation.h │ ├── YCMonteCarloValidation.m │ ├── YCValidation.h │ ├── YCValidation.m │ ├── YCkFoldValidation.h │ └── YCkFoldValidation.m ├── YCGenericModel.h ├── YCGenericModel.m ├── YCGenericTrainer.h ├── YCGenericTrainer.m ├── YCML.h ├── YCSupervisedModel.h ├── YCSupervisedModel.m ├── YCSupervisedTrainer.h └── YCSupervisedTrainer.m ├── YCMLTests ├── CHCSVParser.h ├── CHCSVParser.m ├── Info.plist ├── XCTestCase+Dataframe.h ├── XCTestCase+Dataframe.m ├── YCMLDataframeTests.m ├── YCMLMetricsTests.m ├── YCMLModelExportTests.m ├── YCMLOptimizationTest.m ├── YCMLRBMTests.m ├── YCMLRankingTests.m ├── YCMLTests.m ├── housing.csv └── mnist_train_1_to_37.csv └── YCMatrix ├── Constants.h ├── HaltonInterface.h ├── HaltonInterface.mm ├── Info.plist ├── Matrix+Advanced.h ├── Matrix+Advanced.m ├── Matrix+Manipulate.h ├── Matrix+Manipulate.m ├── Matrix+Map.h ├── Matrix+Map.m ├── Matrix.h ├── Matrix.m ├── NSArray+Matrix.h ├── NSArray+Matrix.m ├── YCMatrix.h ├── halton_sampler.h └── soboldata.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | 4 | ## Build generated 5 | build/ 6 | DerivedData/ 7 | 8 | ## Various settings 9 | *.pbxuser 10 | !default.pbxuser 11 | *.mode1v3 12 | !default.mode1v3 13 | *.mode2v3 14 | !default.mode2v3 15 | *.perspectivev3 16 | !default.perspectivev3 17 | xcuserdata/ 18 | 19 | ## Other 20 | *.moved-aside 21 | *.xccheckout 22 | *.xcscmblueprint 23 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yconst/YCML/65f973bbdc120f1676d8cd11e3d635858c480e6f/.gitmodules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: objective-c 2 | xcode_project: YCML.xcodeproj 3 | osx_image: xcode7 4 | xcode_scheme: YCML 5 | -------------------------------------------------------------------------------- /AppledocSettings.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | --project-name 6 | YCML 7 | --project-version 8 | 0.5.0 9 | --project-company 10 | Ioannis (Yannis) Chatzikonstantinou / yconst.com 11 | --create-html 12 | 13 | --create-docset 14 | 15 | --output 16 | ./YCML 17 | 18 | 19 | -------------------------------------------------------------------------------- /Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yconst/YCML/65f973bbdc120f1676d8cd11e3d635858c480e6f/Logo.png -------------------------------------------------------------------------------- /YCML Tests (Swift)/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /YCML Tests (Swift)/YCML_Tests__Swift_.swift: -------------------------------------------------------------------------------- 1 | // 2 | // YCML_Tests__Swift_.swift 3 | // YCML Tests (Swift) 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 10/3/15. 6 | // Copyright (c) 2015 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class YCML_Tests__Swift_: XCTestCase { 12 | 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /YCML.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /YCML.xcodeproj/project.xcworkspace/xcshareddata/YCML.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | 257FD9D4-C475-4671-BC19-F6A9E5BFD9F5 9 | IDESourceControlProjectName 10 | YCML 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513 14 | https://github.com/yconst/CHCSVParser.git 15 | AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956 16 | https://github.com/yconst/YCMatrix.git 17 | BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0 18 | https://github.com/yconst/YCML.git 19 | 20 | IDESourceControlProjectPath 21 | YCML.xcodeproj 22 | IDESourceControlProjectRelativeInstallPathDictionary 23 | 24 | 89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513 25 | ../../../CHCSVParser 26 | AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956 27 | ../..yconst/YCMatrix 28 | BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0 29 | ../.. 30 | 31 | IDESourceControlProjectURL 32 | https://github.com/yconst/YCML.git 33 | IDESourceControlProjectVersion 34 | 111 35 | IDESourceControlProjectWCCIdentifier 36 | BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0 37 | IDESourceControlProjectWCConfigurations 38 | 39 | 40 | IDESourceControlRepositoryExtensionIdentifierKey 41 | public.vcs.git 42 | IDESourceControlWCCIdentifierKey 43 | 89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513 44 | IDESourceControlWCCName 45 | CHCSVParser 46 | 47 | 48 | IDESourceControlRepositoryExtensionIdentifierKey 49 | public.vcs.git 50 | IDESourceControlWCCIdentifierKey 51 | AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956 52 | IDESourceControlWCCName 53 | YCMatrix 54 | 55 | 56 | IDESourceControlRepositoryExtensionIdentifierKey 57 | public.vcs.git 58 | IDESourceControlWCCIdentifierKey 59 | BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0 60 | IDESourceControlWCCName 61 | YCML 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /YCML.xcodeproj/project.xcworkspace/xcshareddata/YCML.xcscmblueprint: -------------------------------------------------------------------------------- 1 | { 2 | "DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0", 3 | "DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : { 4 | 5 | }, 6 | "DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : { 7 | "89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513" : 0, 8 | "BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0" : 0, 9 | "AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956" : 0 10 | }, 11 | "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "257FD9D4-C475-4671-BC19-F6A9E5BFD9F5", 12 | "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { 13 | "89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513" : "CHCSVParser\/", 14 | "BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0" : "YCML\/", 15 | "AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956" : "YCMLyconst\/YCMatrix" 16 | }, 17 | "DVTSourceControlWorkspaceBlueprintNameKey" : "YCML", 18 | "DVTSourceControlWorkspaceBlueprintVersion" : 204, 19 | "DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "YCML.xcodeproj", 20 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [ 21 | { 22 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/yconst\/CHCSVParser.git", 23 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 24 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "89AB8B7AC69D45E8D6D9FF946DB3C994C65BB513" 25 | }, 26 | { 27 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/yconst\/YCMatrix.git", 28 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 29 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "AAEC269FD8AFFC2CC1EC21B94E683FF215C7A956" 30 | }, 31 | { 32 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/yconst\/YCML.git", 33 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 34 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "BFDA3D4B500CCE27CB2E46E6101343F1B3B707F0" 35 | } 36 | ] 37 | } -------------------------------------------------------------------------------- /YCML.xcodeproj/project.xcworkspace/xcuserdata/yanconst.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /YCML.xcodeproj/xcuserdata/yanconst.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | YCML.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 6 11 | 12 | YCMatrix.xcscheme 13 | 14 | orderHint 15 | 10 16 | 17 | 18 | SuppressBuildableAutocreation 19 | 20 | CB02F7BF1AAF653C00EF1E02 21 | 22 | primary 23 | 24 | 25 | CB265C151B24374F004BE740 26 | 27 | primary 28 | 29 | 30 | CB265C1F1B243750004BE740 31 | 32 | primary 33 | 34 | 35 | CB265C481B24395F004BE740 36 | 37 | primary 38 | 39 | 40 | CB265C521B24395F004BE740 41 | 42 | primary 43 | 44 | 45 | CB265C7A1B2439CA004BE740 46 | 47 | primary 48 | 49 | 50 | CB265C841B2439CA004BE740 51 | 52 | primary 53 | 54 | 55 | CB41BE161B3838DC004E2522 56 | 57 | primary 58 | 59 | 60 | CB41BE201B3838DC004E2522 61 | 62 | primary 63 | 64 | 65 | CB96955C1AAEEB64003BAE48 66 | 67 | primary 68 | 69 | 70 | CB9695671AAEEB64003BAE48 71 | 72 | primary 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /YCML/Data Frame/NSIndexSet+Sampling.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSIndexSet+Sampling.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 1/7/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface NSIndexSet (Sampling) 26 | 27 | + (instancetype)indexesForSampling:(NSUInteger)samples inRange:(NSRange)range replacement:(BOOL)replacement; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /YCML/Data Frame/NSIndexSet+Sampling.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSIndexSet+Sampling.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 1/7/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "NSIndexSet+Sampling.h" 24 | 25 | #define ARC4RANDOM_MAX 0x100000000 26 | 27 | @implementation NSIndexSet (Sampling) 28 | 29 | + (instancetype)indexesForSampling:(NSUInteger)samples inRange:(NSRange)range replacement:(BOOL)replacement 30 | { 31 | NSMutableIndexSet *selectedIndexes = [NSMutableIndexSet indexSet]; 32 | if (replacement) 33 | { 34 | NSUInteger N = range.length; 35 | for (NSUInteger i=0; i 0) 46 | { 47 | if (N * (double)arc4random() / ARC4RANDOM_MAX <= n) 48 | { 49 | [selectedIndexes addIndex:range.location + i]; 50 | n--; 51 | } 52 | i++; 53 | N--; 54 | } 55 | } 56 | return selectedIndexes; 57 | } 58 | 59 | @end 60 | -------------------------------------------------------------------------------- /YCML/Data Frame/OrderedDictionary.h: -------------------------------------------------------------------------------- 1 | // 2 | // OrderedDictionary.h 3 | // 4 | // Version 1.2 5 | // 6 | // Created by Nick Lockwood on 21/09/2010. 7 | // Copyright 2010 Charcoal Design 8 | // 9 | // Distributed under the permissive zlib license 10 | // Get the latest version from here: 11 | // 12 | // https://github.com/nicklockwood/OrderedDictionary 13 | // 14 | // This software is provided 'as-is', without any express or implied 15 | // warranty. In no event will the authors be held liable for any damages 16 | // arising from the use of this software. 17 | // 18 | // Permission is granted to anyone to use this software for any purpose, 19 | // including commercial applications, and to alter it and redistribute it 20 | // freely, subject to the following restrictions: 21 | // 22 | // 1. The origin of this software must not be misrepresented; you must not 23 | // claim that you wrote the original software. If you use this software 24 | // in a product, an acknowledgment in the product documentation would be 25 | // appreciated but is not required. 26 | // 27 | // 2. Altered source versions must be plainly marked as such, and must not be 28 | // misrepresented as being the original software. 29 | // 30 | // 3. This notice may not be removed or altered from any source distribution. 31 | // 32 | 33 | #import 34 | 35 | /** 36 | * Ordered subclass of NSDictionary. 37 | * Supports all the same methods as NSDictionary, plus a few 38 | * new methods for operating on entities by index rather than key. 39 | */ 40 | @interface OrderedDictionary : NSDictionary 41 | 42 | + (instancetype)dictionaryWithContentsOfFile:(NSString *)path; 43 | + (instancetype)dictionaryWithContentsOfURL:(NSURL *)url; 44 | 45 | /** Returns the nth key in the dictionary. */ 46 | - (id)keyAtIndex:(NSUInteger)index; 47 | /** Returns the nth object in the dictionary. */ 48 | - (id)objectAtIndex:(NSUInteger)index; 49 | - (id)objectAtIndexedSubscript:(NSUInteger)index; 50 | /** Returns the index of the specified key, or NSNotFound if key is not found. */ 51 | - (NSUInteger)indexOfKey:(id)key; 52 | /** Returns an enumerator for backwards traversal of the dictionary keys. */ 53 | - (NSEnumerator *)reverseKeyEnumerator; 54 | /** Returns an enumerator for backwards traversal of the dictionary objects. */ 55 | - (NSEnumerator *)reverseObjectEnumerator; 56 | /** Enumerates keys ands objects with index using block. */ 57 | - (void)enumerateKeysAndObjectsWithIndexUsingBlock:(void (^)(id key, id obj, NSUInteger idx, BOOL *stop))block; 58 | 59 | @end 60 | 61 | 62 | /** 63 | * Mutable subclass of OrderedDictionary. 64 | * Supports all the same methods as NSMutableDictionary, plus a few 65 | * new methods for operating on entities by index rather than key. 66 | * Note that although it has the same interface, MutableOrderedDictionary 67 | * is not a subclass of NSMutableDictionary, and cannot be used as one 68 | * without generating compiler warnings (unless you cast it). 69 | */ 70 | @interface MutableOrderedDictionary : OrderedDictionary 71 | 72 | + (instancetype)dictionaryWithCapacity:(NSUInteger)count; 73 | - (instancetype)initWithCapacity:(NSUInteger)count; 74 | 75 | - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; 76 | - (void)removeAllObjects; 77 | - (void)removeObjectForKey:(id)key; 78 | - (void)removeObjectsForKeys:(NSArray *)keyArray; 79 | - (void)setDictionary:(NSDictionary *)otherDictionary; 80 | - (void)setObject:(id)object forKey:(id)key; 81 | - (void)setObject:(id)object forKeyedSubscript:(id )key; 82 | 83 | /** Inserts an object at a specific index in the dictionary. */ 84 | - (void)insertObject:(id)object forKey:(id)key atIndex:(NSUInteger)index; 85 | /** Replace an object at a specific index in the dictionary. */ 86 | - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)object; 87 | - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index; 88 | /** Swap the indexes of two key/value pairs in the dictionary. */ 89 | - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; 90 | /** Removes the nth object in the dictionary. */ 91 | - (void)removeObjectAtIndex:(NSUInteger)index; 92 | 93 | @end 94 | -------------------------------------------------------------------------------- /YCML/Data Frame/YCDataframe+Matrix.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCDataframe+Matrix.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCDataframe.h" 25 | @class Matrix; 26 | 27 | @interface YCDataframe (Matrix) 28 | 29 | + (instancetype)dataframeWithMatrix:(Matrix *)input conversionArray:(NSArray *)array; 30 | 31 | - (Matrix *)getMatrixUsingConversionArray:(NSArray *)conversionArray; 32 | 33 | - (NSArray *)conversionArray; 34 | 35 | - (void)setDataWithMatrix:(Matrix *)inputMatrix conversionArray:(NSArray *)conversionArray; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /YCML/Data Frame/YCDataframe+Transform.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCDataframe+Transform.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 24/4/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCDataframe.h" 25 | 26 | @interface YCDataframe (Transform) 27 | 28 | - (instancetype)uniformSampling:(NSUInteger)count; 29 | 30 | - (instancetype)normalSampling:(NSUInteger)count; 31 | 32 | - (instancetype)sobolSequenceWithCount:(NSUInteger)count; 33 | 34 | - (instancetype)randomWalkSteps:(int)steps restarts:(int)restarts relativeStepSize:(double)stepSize; 35 | 36 | - (void)corruptWithProbability:(double)probability relativeMagnitude:(double)relativeMagnitude; 37 | 38 | - (instancetype)dataframeByRandomSampling:(int)numberOfElements replacement:(BOOL)replacement; 39 | 40 | - (instancetype)splitByRandomSampling:(int)numberOfElements; 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /YCML/Data Frame/YCMissingValue.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCMissingValue.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | 25 | @interface YCMissingValue : NSObject 26 | 27 | + (instancetype)missingValue; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /YCML/Data Frame/YCMissingValue.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCMissingValue.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCMissingValue.h" 24 | 25 | YCMissingValue *uniValue; 26 | 27 | @implementation YCMissingValue 28 | 29 | + (instancetype)missingValue 30 | { 31 | if (!uniValue) 32 | { 33 | uniValue = [[YCMissingValue alloc] init]; 34 | } 35 | return uniValue; 36 | } 37 | 38 | - (instancetype)initWithCoder:(NSCoder *)aDecoder 39 | { 40 | return [YCMissingValue missingValue]; 41 | } 42 | 43 | - (void)encodeWithCoder:(NSCoder *)aCoder {} 44 | 45 | - (instancetype)copyWithZone:(NSZone *)zone 46 | { 47 | return [YCMissingValue missingValue]; 48 | } 49 | 50 | - (id)forwardingTargetForSelector:(SEL)aSelector 51 | { 52 | return @(NAN); 53 | } 54 | 55 | - (NSString *)description 56 | { 57 | return @"Missing Value"; 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /YCML/Data Frame/YCMutableArray.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCMutableArray.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 18/4/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | 25 | @interface Stats : NSObject 26 | 27 | - (instancetype)initWithArray:(NSArray *)array; 28 | 29 | @property (readonly) double sum; 30 | 31 | @property (readonly) double mean; 32 | 33 | @property (readonly) double min; 34 | 35 | @property (readonly) double max; 36 | 37 | @property (readonly) double variance; 38 | 39 | @property (readonly) double sd; 40 | 41 | @end 42 | 43 | @interface YCMutableArray : NSMutableArray 44 | 45 | - (NSArray *)sample:(int)samples replacement:(BOOL)replacement; 46 | 47 | - (NSIndexSet *)indexesOfOutliersWithFenceMultiplier:(double)multiplier; 48 | 49 | - (NSNumber *)quantile:(double)q; 50 | 51 | - (YCMutableArray *)bins:(NSUInteger)numberOfBins; 52 | 53 | @property (readonly) NSNumber *sum; 54 | 55 | @property (readonly) NSNumber *mean; 56 | 57 | @property (readonly) NSNumber *min; 58 | 59 | @property (readonly) NSNumber *max; 60 | 61 | @property (readonly) NSNumber *Q1; 62 | 63 | @property (readonly) NSNumber *median; 64 | 65 | @property (readonly) NSNumber *Q3; 66 | 67 | @property (readonly) NSNumber *variance; 68 | 69 | @property (readonly) NSNumber *sd; 70 | 71 | @property (readonly) Stats *stats; 72 | 73 | @property (readonly) YCMutableArray *bins; 74 | 75 | @end 76 | -------------------------------------------------------------------------------- /YCML/ELM/YCELMTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // ELMTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedTrainer.h" 24 | 25 | @interface YCELMTrainer : YCSupervisedTrainer 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/FFN/YCBackPropProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBackPropProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCDerivativeProblem.h" 25 | 26 | @class YCFFN; 27 | 28 | @interface YCBackPropProblem : NSObject 29 | { 30 | Matrix *_inputMatrix; 31 | NSArray *_inputMatrixArray; 32 | Matrix *_outputMatrix; 33 | NSArray *_outputMatrixArray; 34 | } 35 | 36 | - (instancetype)initWithInputMatrix:(Matrix *)input 37 | outputMatrix:(Matrix *)output 38 | model:(YCFFN *)model; 39 | 40 | - (NSArray *)modelWeightsWithParameters:(Matrix *)parameters; 41 | 42 | - (NSArray *)modelBiasesWithParameters:(Matrix *)parameters; 43 | 44 | - (void)storeWeights:(NSArray *)weights biases:(NSArray *)biases toVector:(Matrix *)vector; 45 | 46 | @property YCFFN *trainedModel; 47 | 48 | @property int sampleCount; 49 | 50 | @property int batchSize; 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /YCML/FFN/YCBackPropTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBackPropTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedTrainer.h" 24 | 25 | @interface YCBackPropTrainer : YCSupervisedTrainer 26 | 27 | + (Class)problemClass; 28 | 29 | + (Class)optimizerClass; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /YCML/FFN/YCFFN.h: -------------------------------------------------------------------------------- 1 | // 2 | // FFNModel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedModel.h" 24 | 25 | @interface YCFFN : YCSupervisedModel 26 | 27 | /** 28 | Returns an array containing the receiver's layers. 29 | */ 30 | @property NSArray *layers; 31 | 32 | /** 33 | Returns the input transformation matrix of the receiver. 34 | */ 35 | @property Matrix *inputTransform; 36 | 37 | /** 38 | Returns the output reverse transformation matrix of the receiver. 39 | */ 40 | @property Matrix *outputTransform; 41 | 42 | /** 43 | Returns the number of hidden layers of the receiver. 44 | */ 45 | @property (readonly) int hiddenLayerCount; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /YCML/FFN/YCFFN.m: -------------------------------------------------------------------------------- 1 | // 2 | // FFNModel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | 24 | // N: Size of input 25 | // S: Number of samples 26 | // H: Size of hidden layer 27 | // O: Size of output 28 | 29 | /* 30 | * Guidelines for matrix sizing 31 | * Input: Should be arranged so that every *sample* is a *column* 32 | * Input: NxS 33 | * 34 | * Weights: 35 | * For every layer n: Let C be the current layer's size, T the next layers size. 36 | * The weights from n to n+1 form a matrix of dimensions CxT (current layer units as columns) 37 | * In order to derive the next layer's z, the output of n is left-multipled with 38 | * the transpose of the weights from n to n+1: 39 | * zn+1 = Wn^T * an 40 | */ 41 | 42 | #import "YCFFN.h" 43 | #import "YCFullyConnectedLayer.h" 44 | @import YCMatrix; 45 | 46 | @implementation YCFFN 47 | 48 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 49 | { 50 | NSAssert([self.layers count], @"Model not trained"); 51 | NSAssert([matrix rows] == self.inputSize, @"Input size mismatch"); 52 | 53 | // 1. Scale input 54 | Matrix *scaledInput = matrix; // NxS 55 | if (self.inputTransform) 56 | { 57 | scaledInput = [matrix matrixByRowWiseMapUsing:self.inputTransform]; 58 | } 59 | 60 | // 2. Calculate layer-by-layer 61 | 62 | Matrix *output = scaledInput; 63 | 64 | for (int i=0, j=(int)[self.layers count]; i. 22 | 23 | #import "YCBackPropTrainer.h" 24 | 25 | @interface YCRpropTrainer : YCBackPropTrainer 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/FFN/YCRpropTrainer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCBackPropTrainer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 21/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCRpropTrainer.h" 24 | #import "YCBackPropTrainer.h" 25 | #import "YCRProp.h" 26 | 27 | @implementation YCRpropTrainer 28 | 29 | + (Class)optimizerClass 30 | { 31 | return [YCRProp class]; 32 | } 33 | 34 | -(id)init 35 | { 36 | if (self = [super init]) 37 | { 38 | self.settings[@"Hidden Layer Count"] = @1; 39 | self.settings[@"Hidden Layer Size"] = @5; 40 | self.settings[@"L2"] = @0.0001; 41 | self.settings[@"Iterations"] = @500; 42 | self.settings[@"Target"] = @-1; 43 | [self.settings removeObjectForKey:@"Alpha"]; 44 | } 45 | return self; 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /YCML/IO/YCBinaryRBM+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBM+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCBinaryRBM (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCBinaryRBM+IO.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBM+IO.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCBinaryRBM+IO.h" 24 | 25 | @implementation YCBinaryRBM (IO) 26 | 27 | #pragma mark Text Description 28 | 29 | - (NSString *)textDescription 30 | { 31 | NSMutableString *description = (NSMutableString *)[super textDescription]; 32 | 33 | // Print RBF function type 34 | [description appendFormat:@"\nFunction is Sigmoid\n"]; 35 | 36 | // Print centers 37 | [description appendFormat:@"\nWeights (%d x %d)\n%@",self.weights.rows, 38 | self.weights.columns, self.weights]; 39 | 40 | // Print bandwidths 41 | [description appendFormat:@"\nVisible Biases (%d x %d)\n%@",self.visibleBiases.rows, 42 | self.visibleBiases.columns, self.visibleBiases]; 43 | 44 | // Print output weights 45 | [description appendFormat:@"\nHidden Biases (%d x %d)\n%@",self.hiddenBiases.rows, 46 | self.hiddenBiases.columns, self.hiddenBiases]; 47 | 48 | return description; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /YCML/IO/YCFFN+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCFFN+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCFFN (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCFullyConnectedLayer+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCFullyConnectedLayer+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCFullyConnectedLayer (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCGenericModel+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericModel+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCGenericModel (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCGenericTrainer+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericTrainer+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCGenericTrainer (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCGenericTrainer+IO.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericTrainer+IO.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCGenericTrainer+IO.h" 24 | 25 | @implementation YCGenericTrainer (IO) 26 | 27 | #pragma mark - NSCoding Implementation 28 | 29 | - (id)initWithCoder:(NSCoder *)decoder 30 | { 31 | if (self = [super init]) 32 | { 33 | NSDictionary *decodedSettings = [decoder decodeObjectForKey:@"settings"]; 34 | [self.settings addEntriesFromDictionary:decodedSettings]; 35 | } 36 | return self; 37 | } 38 | 39 | - (void)encodeWithCoder:(NSCoder *)encoder 40 | { 41 | [encoder encodeObject:self.settings forKey:@"settings"]; 42 | } 43 | 44 | #pragma mark - NSCopying Implementation 45 | 46 | - (id)copyWithZone:(NSZone *)zone 47 | { 48 | id copied = [[self class] trainer]; 49 | if (copied) 50 | { 51 | [copied setSettings:[self.settings mutableCopy]]; 52 | } 53 | return copied; 54 | } 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /YCML/IO/YCModelIO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelIO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | /** 26 | Describes the methods used to export models to various formats, as well as 27 | create models from imported data. 28 | */ 29 | @protocol YCModelIO 30 | 31 | /// @name Decoding 32 | 33 | /** 34 | Returns a model generated using the PMML-encoded data 35 | in the supplied string. 36 | 37 | @param string The string containing PMML data. 38 | 39 | @return The generated model. 40 | */ 41 | + (YCGenericModel *)modelWithPMMLString:(NSString *)string; 42 | 43 | /// @name Encoding 44 | 45 | /** 46 | Encodes information of the receiver, given a root XML element. 47 | To be overridden when subclassing. 48 | 49 | @param root The root XML Element. 50 | @warning This method is only available on MacOS platforms. 51 | */ 52 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 53 | - (void)PMMLEncodeWithRootElement:(NSXMLElement *)root; 54 | #endif 55 | 56 | /** 57 | Produces a human-readable text description of the model, 58 | including all of it's properties. 59 | */ 60 | @property (readonly) NSString *textDescription; 61 | 62 | /** 63 | Produces a PMML (https://en.wikipedia.org/wiki/Predictive_Model_Markup_Language) 64 | file of the model, including all of it's properties. 65 | 66 | @warning This property is only available on MacOS platforms. 67 | */ 68 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 69 | @property (readonly) NSString *PMMLString; 70 | #endif 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /YCML/IO/YCModelLayer+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelLayer+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCModelLayer (IO) 26 | 27 | /** 28 | Produces a human-readable text description of the receiver. 29 | 30 | @param model The model that owns the receiving layer. 31 | @param index The index of the layer in the network. 32 | 33 | @return The text description of the receiving layer. 34 | */ 35 | - (NSString *)textDescriptionWithModel:(YCFFN *)model layerIndex:(NSUInteger)index; 36 | 37 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 38 | /** 39 | Encodes information of the receiver, given a model and a target XML element. 40 | To be overridden when subclassing. 41 | 42 | @param target The XML Element corresponding to the model. 43 | @param model The model that owns the receiving layer. 44 | @param index The index of the layer in the network. 45 | 46 | @warning This method is only available on MacOS platforms. 47 | */ 48 | - (void)PMMLEncodeWithTargetElement:(NSXMLElement *)target 49 | model:(YCFFN *)model 50 | layerIndex:(NSUInteger)index; 51 | #endif 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /YCML/IO/YCModelLayer+IO.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelLayer+IO.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelLayer+IO.h" 24 | 25 | @implementation YCModelLayer (IO) 26 | 27 | #pragma mark - NSCoding Implementation 28 | 29 | - (instancetype)initWithCoder:(NSCoder *)aDecoder 30 | { 31 | self = [super init]; 32 | if (self) 33 | { 34 | self.properties = [aDecoder decodeObjectForKey:@"properties"]; 35 | } 36 | return self; 37 | } 38 | 39 | - (void)encodeWithCoder:(NSCoder *)aCoder 40 | { 41 | [aCoder encodeObject:self.properties forKey:@"properties"]; 42 | } 43 | 44 | #pragma mark - NSCopying Implementation 45 | 46 | - (instancetype)copyWithZone:(NSZone *)zone 47 | { 48 | YCModelLayer *layer = [[[self class] alloc] init]; 49 | layer.properties = [self.properties copy]; 50 | return layer; 51 | } 52 | 53 | #pragma mark - Text Description 54 | 55 | - (NSString *)textDescriptionWithLayerIndex:(NSUInteger)index 56 | { 57 | @throw [NSInternalInconsistencyException initWithFormat: 58 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 59 | } 60 | 61 | #pragma mark - PMML Export 62 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 63 | 64 | - (void)PMMLEncodeWithTargetElement:(NSXMLElement *)target 65 | model:(YCFFN *)model 66 | layerIndex:(NSUInteger)index 67 | { 68 | @throw [NSInternalInconsistencyException initWithFormat: 69 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 70 | } 71 | #endif 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YCML/IO/YCRBFNet+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFNet+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCRBFNet (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/IO/YCRBFNet+IO.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFNet+IO.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCRBFNet+IO.h" 24 | 25 | @implementation YCRBFNet (IO) 26 | 27 | #pragma mark - NSCopying Implementation 28 | 29 | - (instancetype)copyWithZone:(NSZone *)zone 30 | { 31 | YCRBFNet *copy = [super copyWithZone:zone]; 32 | if (copy) 33 | { 34 | copy.centers = [self.centers copy]; 35 | copy.widths = [self.widths copy]; 36 | copy.weights = [self.weights copy]; 37 | copy.inputTransform = [self.inputTransform copy]; 38 | copy.outputTransform = [self.outputTransform copy]; 39 | } 40 | return copy; 41 | } 42 | 43 | #pragma mark - NSCoding Implementation 44 | 45 | - (id)initWithCoder:(NSCoder *)aDecoder 46 | { 47 | self = [super initWithCoder:aDecoder]; 48 | if (self) 49 | { 50 | self.centers = [aDecoder decodeObjectForKey:@"centers"]; 51 | self.widths = [aDecoder decodeObjectForKey:@"widths"]; 52 | self.weights = [aDecoder decodeObjectForKey:@"weights"]; 53 | self.inputTransform = [aDecoder decodeObjectForKey:@"inputTransform"]; 54 | self.outputTransform = [aDecoder decodeObjectForKey:@"outputTransform"]; 55 | } 56 | return self; 57 | } 58 | 59 | - (void)encodeWithCoder:(NSCoder *)aCoder 60 | { 61 | [super encodeWithCoder:aCoder]; 62 | [aCoder encodeObject:self.centers forKey:@"centers"]; 63 | [aCoder encodeObject:self.widths forKey:@"widths"]; 64 | [aCoder encodeObject:self.weights forKey:@"weights"]; 65 | [aCoder encodeObject:self.inputTransform forKey:@"inputTransform"]; 66 | [aCoder encodeObject:self.outputTransform forKey:@"outputTransform"]; 67 | } 68 | 69 | #pragma mark - Text Description 70 | 71 | - (NSString *)textDescription 72 | { 73 | NSMutableString *description = (NSMutableString *)[super textDescription]; 74 | 75 | // Print RBF function type 76 | [description appendFormat:@"\nRBF Function is Gaussian\n"]; 77 | 78 | // Print input and output transform matrices 79 | if (self.inputTransform) 80 | { 81 | [description appendFormat:@"\nInput Transform (%d x %d)\nMapping Function: y = c1*x + c2\n%@",self.inputTransform.rows, 82 | self.inputTransform.columns, self.inputTransform]; 83 | } 84 | if (self.outputTransform) 85 | { 86 | [description appendFormat:@"\nOutput Transform (%d x %d)\nMapping Function: y = c1*x + c2\n%@",self.outputTransform.rows, 87 | self.outputTransform.columns, self.outputTransform]; 88 | } 89 | 90 | // Print centers 91 | [description appendFormat:@"\nCenters (%d x %d)\n%@",self.centers.rows, 92 | self.centers.columns, self.centers]; 93 | 94 | // Print bandwidths 95 | [description appendFormat:@"\nBandwidths (%d x %d)\n%@",self.widths.rows, 96 | self.widths.columns, self.widths]; 97 | 98 | // Print output weights 99 | [description appendFormat:@"\nOutput Weights (%d x %d)\n%@",self.weights.rows, 100 | self.weights.columns, self.weights]; 101 | 102 | return description; 103 | } 104 | 105 | #pragma mark - PMML Export 106 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 107 | 108 | - (void)PMMLEncodeWithRootElement:(NSXMLElement *)root 109 | { 110 | 111 | } 112 | #endif 113 | 114 | @end 115 | -------------------------------------------------------------------------------- /YCML/IO/YCSupervisedModel+IO.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSupervisedModel+IO.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @interface YCSupervisedModel (IO) 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 0.5.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSHumanReadableCopyright 24 | Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 25 | NSPrincipalClass 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /YCML/KPM/YCKPM.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCkNN.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 6/12/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | @class Matrix; 25 | 26 | @interface YCKPM : YCSupervisedModel 27 | 28 | @property Matrix *prototypes; 29 | 30 | @property Matrix *targets; 31 | 32 | /** 33 | Returns the input transformation matrix of the receiver. 34 | */ 35 | @property Matrix *inputTransform; 36 | 37 | /** 38 | Returns the output reverse transformation matrix of the receiver. 39 | */ 40 | @property Matrix *outputTransform; 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /YCML/KPM/YCKPM.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCkNN.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 6/12/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCKPM.h" 24 | 25 | @implementation YCKPM 26 | 27 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 28 | { 29 | double bias = MAX(1E-12, [self.trainingSettings[@"Bias"] doubleValue]); 30 | 31 | // 1. Scale input 32 | Matrix *scaledInput = matrix; 33 | if (self.inputTransform) 34 | { 35 | scaledInput = [matrix matrixByRowWiseMapUsing:self.inputTransform]; 36 | } 37 | 38 | // 2. Prepare output matrix and split input matrix to columns 39 | // TODO: Vectorize this! 40 | Matrix *output = [Matrix matrixOfRows:self.targets.rows columns:scaledInput.columns]; 41 | NSArray *examples = [scaledInput columnsAsNSArray]; 42 | 43 | // 3. For each example: Find similarity with prototypes; weigh each 44 | // prototype's corresponding target and sum them up together 45 | for (int i=0; i. 22 | 23 | #import 24 | 25 | @interface YCKPMTrainer : YCSupervisedTrainer 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/KPM/YCKPMTrainer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCkNNTrainer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 6/12/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCKPMTrainer.h" 24 | #import "YCKPM.h" 25 | 26 | @implementation YCKPMTrainer 27 | 28 | + (Class)modelClass 29 | { 30 | return [YCKPM class]; 31 | } 32 | 33 | - (void)performTrainingModel:(YCKPM *)model 34 | inputMatrix:(Matrix *)input 35 | outputMatrix:(Matrix *)output 36 | { 37 | YCDomain domain = YCMakeDomain(-1, 1); 38 | Matrix *inputTransform = [input rowWiseMapToDomain:domain basis:MinMax]; 39 | Matrix *outputTransform = [output rowWiseMapToDomain:domain basis:MinMax]; 40 | Matrix *invOutTransform = [output rowWiseInverseMapFromDomain:domain basis:MinMax]; 41 | Matrix *scaledInput = [input matrixByRowWiseMapUsing:inputTransform]; 42 | Matrix *scaledOutput = [output matrixByRowWiseMapUsing:outputTransform]; 43 | 44 | model.inputTransform = inputTransform; 45 | model.outputTransform = invOutTransform; 46 | 47 | model.prototypes = scaledInput; 48 | model.targets = scaledOutput; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /YCML/Kernels/YCLinearKernel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinearKernel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelKernel.h" 24 | 25 | @interface YCLinearKernel : YCModelKernel 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/Kernels/YCLinearKernel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinearKernel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCLinearKernel.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCLinearKernel 27 | 28 | - (Matrix *)kernelValueForA:(Matrix *)a b:(Matrix *)b 29 | { 30 | // a: NxP, b: NxQ -> out: PxQ 31 | return [a matrixByTransposingAndMultiplyingWithRight:b]; 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /YCML/Kernels/YCModelKernel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelKernel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | @class Matrix; 25 | 26 | @interface YCModelKernel : NSObject 27 | 28 | + (instancetype)kernel; 29 | 30 | - (Matrix *)kernelValueForA:(Matrix *)a b:(Matrix *)b; 31 | 32 | /** 33 | Holds kernel properties. 34 | */ 35 | @property NSMutableDictionary *properties; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /YCML/Kernels/YCModelKernel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelKernel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelKernel.h" 24 | 25 | @implementation YCModelKernel 26 | 27 | + (instancetype)kernel 28 | { 29 | return [[self alloc] init]; 30 | } 31 | 32 | - (instancetype)init 33 | { 34 | self = [super init]; 35 | if (self) 36 | { 37 | self.properties = [NSMutableDictionary dictionary]; 38 | } 39 | return self; 40 | } 41 | 42 | - (instancetype)initWithCoder:(NSCoder *)aDecoder 43 | { 44 | self = [super init]; 45 | if (self) 46 | { 47 | self.properties = [aDecoder decodeObjectForKey:@"properties"]; 48 | } 49 | return self; 50 | } 51 | 52 | - (Matrix *)kernelValueForA:(Matrix *)a b:(Matrix *)b 53 | { 54 | @throw [NSInternalInconsistencyException initWithFormat: 55 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /YCML/Kernels/YCRBFKernel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFKernel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 27/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelKernel.h" 24 | 25 | @interface YCRBFKernel : YCModelKernel 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/Kernels/YCRBFKernel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFKernel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 27/1/16. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCRBFKernel.h" 24 | @import YCMatrix; 25 | 26 | // N: Size of input 27 | // P1: Number of samples 1 28 | // P2: Number of samples 2 29 | 30 | @implementation YCRBFKernel 31 | 32 | - (instancetype)init 33 | { 34 | self = [super init]; 35 | if (self) 36 | { 37 | self.properties[@"Beta"] = @1.0; 38 | } 39 | return self; 40 | } 41 | 42 | - (Matrix *)kernelValueForA:(Matrix *)a b:(Matrix *)b 43 | { 44 | // a: NxP1, b: NxP2 -> out: P1xP2 45 | double beta2 = pow([self.properties[@"Beta"] doubleValue], 2); 46 | 47 | int N = a.rows; 48 | int P1 = a.columns; 49 | int P2 = b.columns; 50 | 51 | // Generate design matrix of dimensions SxD 52 | Matrix *designmatrix = [Matrix matrixOfRows:P1 columns:P2]; // -> SxD 53 | 54 | // Fill up the design matrix, traversing first row and then column 55 | for (int i=0; imatrix[k*P1 + i] - b->matrix[k*P2 + j]; 64 | sqsum += val*val; 65 | } 66 | double bfvalue = exp( - sqsum / beta2 ); 67 | designmatrix->matrix[i*P2 + j] = bfvalue; 68 | } 69 | } 70 | return designmatrix; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YCML/Layers/YCFullyConnectedLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCFullyConnectedLayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelLayer.h" 24 | @class Matrix; 25 | 26 | /** 27 | A densely connected feed-forward layer. This layer is not directly used 28 | when building models, rather it is used as a superclass for building 29 | usable layers. Does not implement an activation function. 30 | */ 31 | @interface YCFullyConnectedLayer : YCModelLayer 32 | 33 | + (instancetype)layerWithInputSize:(int)inputSize outputSize:(int)outputSize; 34 | 35 | - (instancetype)initWithInputSize:(int)inputSize outputSize:(int)outputSize; 36 | 37 | - (Matrix *)forward:(Matrix *)input; 38 | 39 | - (void)activationFunction:(Matrix *)inputCopy; 40 | 41 | - (void)activationFunctionGradient:(Matrix *)outputCopy; 42 | 43 | - (double)regularizationLoss; 44 | 45 | /** 46 | Returns the weight matrix of the receiver. 47 | */ 48 | @property Matrix *weightMatrix; 49 | 50 | /** 51 | Returns the bias matrix of the receiver. 52 | */ 53 | @property Matrix *biasVector; 54 | 55 | @property Matrix *lastActivation; 56 | 57 | @property double L2; 58 | 59 | @end 60 | -------------------------------------------------------------------------------- /YCML/Layers/YCFullyConnectedLayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCFullyConnectedLayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCFullyConnectedLayer.h" 24 | @import YCMatrix; 25 | 26 | // I: Input size 27 | // O: Output size 28 | // S: Sample count 29 | 30 | @implementation YCFullyConnectedLayer 31 | 32 | + (instancetype)layerWithInputSize:(int)inputSize outputSize:(int)outputSize 33 | { 34 | return [[self alloc] initWithInputSize:inputSize outputSize:outputSize]; 35 | } 36 | 37 | - (instancetype)initWithInputSize:(int)inputSize outputSize:(int)outputSize 38 | { 39 | NSAssert(inputSize > 0 && outputSize > 0, 40 | @"Input and/or Output sizes are equal to or less than zero"); 41 | self = [super init]; 42 | if (self) 43 | { 44 | self.weightMatrix = [Matrix matrixOfRows:inputSize columns:outputSize]; 45 | self.biasVector = [Matrix matrixOfRows:outputSize columns:1]; 46 | } 47 | return self; 48 | } 49 | 50 | - (Matrix *)forward:(Matrix *)input 51 | { 52 | Matrix *output = [self.weightMatrix matrixByTransposingAndMultiplyingWithRight:input]; // (IxO)T * IxS = OxS 53 | [output addColumn:self.biasVector]; 54 | [self activationFunction:output]; 55 | self.lastActivation = [output copy]; 56 | return output; 57 | } 58 | 59 | - (Matrix *)backward:(Matrix *)outputDeltas input:(Matrix *)input 60 | { 61 | @throw [NSInternalInconsistencyException initWithFormat: 62 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 63 | } 64 | 65 | - (NSArray *)gradientsWithInput:(Matrix *)input deltas:(Matrix *)deltas 66 | { 67 | @throw [NSInternalInconsistencyException initWithFormat: 68 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 69 | } 70 | 71 | - (double)regularizationLoss 72 | { 73 | return [[self.weightMatrix matrixByElementWiseMultiplyWith:self.weightMatrix] sum] * self.L2; 74 | } 75 | 76 | - (void)activationFunction:(Matrix *)inputCopy 77 | { 78 | @throw [NSInternalInconsistencyException initWithFormat: 79 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 80 | } 81 | 82 | - (void)activationFunctionGradient:(Matrix *)outputCopy 83 | { 84 | @throw [NSInternalInconsistencyException initWithFormat: 85 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 86 | } 87 | 88 | - (int)inputSize 89 | { 90 | return self.weightMatrix.rows; 91 | } 92 | 93 | - (int)outputSize 94 | { 95 | return self.weightMatrix.columns; 96 | } 97 | 98 | @end 99 | -------------------------------------------------------------------------------- /YCML/Layers/YCLinearLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinearLayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCFullyConnectedLayer.h" 24 | 25 | /** 26 | A simple linear layer implementation. This layer implements the identity activation function. 27 | */ 28 | @interface YCLinearLayer : YCFullyConnectedLayer 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Layers/YCLinearLayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinearLayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCLinearLayer.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCLinearLayer 27 | 28 | - (void)activationFunction:(Matrix *)inputCopy 29 | { 30 | // Do nothing y = x 31 | } 32 | 33 | - (void)activationFunctionGradient:(Matrix *)outputCopy 34 | { 35 | [outputCopy applyFunction:^double(double value) { 36 | return 1.0; 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /YCML/Layers/YCModelLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelLayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | 25 | /** 26 | An abstract class implementing the infrastructure for building a predictive 27 | model connectivity layer. 28 | */ 29 | @interface YCModelLayer : NSObject 30 | 31 | + (instancetype)layer; 32 | 33 | /** 34 | Returns the receiver's input size. 35 | */ 36 | @property (readonly) int inputSize; 37 | 38 | /** 39 | Returns the receiver's output size. 40 | */ 41 | @property (readonly) int outputSize; 42 | 43 | /** 44 | Holds layer properties. 45 | */ 46 | @property NSMutableDictionary *properties; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /YCML/Layers/YCModelLayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCModelLayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCModelLayer.h" 24 | 25 | @implementation YCModelLayer 26 | 27 | + (instancetype)layer 28 | { 29 | return [[self alloc] init]; 30 | } 31 | 32 | - (instancetype)init 33 | { 34 | self = [super init]; 35 | if (self) 36 | { 37 | self.properties = [NSMutableDictionary dictionary]; 38 | } 39 | return self; 40 | } 41 | 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /YCML/Layers/YCReLULayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCReLULayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCFullyConnectedLayer.h" 24 | 25 | /** 26 | Rectifier Linear Unit (ReLU) implementation. 27 | */ 28 | @interface YCReLULayer : YCFullyConnectedLayer 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Layers/YCReLULayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCReLULayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCReLULayer.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCReLULayer 27 | 28 | - (void)activationFunction:(Matrix *)inputCopy 29 | { 30 | [inputCopy applyFunction:^double(double value) { 31 | if (value < 0) 32 | { 33 | return 0.0; 34 | } 35 | return value; 36 | }]; 37 | } 38 | 39 | - (void)activationFunctionGradient:(Matrix *)outputCopy 40 | { 41 | [outputCopy applyFunction:^double(double value) { 42 | if (value == 0) 43 | { 44 | return 0.0; 45 | } 46 | return 1.0; 47 | }]; 48 | } 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /YCML/Layers/YCSigmoidLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSigmoidLayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCFullyConnectedLayer.h" 24 | 25 | /** 26 | Sigmoid layer implementation. 27 | */ 28 | @interface YCSigmoidLayer : YCFullyConnectedLayer 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Layers/YCSigmoidLayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCSigmoidLayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSigmoidLayer.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCSigmoidLayer 27 | 28 | - (void)activationFunction:(Matrix *)inputCopy 29 | { 30 | [inputCopy applyFunction:^double(double value) { 31 | return 1.0 / (1.0 + exp(-value)); 32 | }]; 33 | } 34 | 35 | - (void)activationFunctionGradient:(Matrix *)outputCopy 36 | { 37 | [outputCopy applyFunction:^double(double value) { 38 | return value * (1.0 - value); // f(x) * (1 - f(x)) 39 | }]; 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /YCML/Layers/YCTanhLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCTanhLayer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCFullyConnectedLayer.h" 24 | 25 | /** 26 | Hyperbolic Tangent (Tanh)-based layer implementation. 27 | */ 28 | @interface YCTanhLayer : YCFullyConnectedLayer 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Layers/YCTanhLayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCTanhLayer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCTanhLayer.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCTanhLayer 27 | 28 | - (void)activationFunction:(Matrix *)inputCopy 29 | { 30 | [inputCopy applyFunction:^double(double value) { 31 | return tanh(value); 32 | }]; 33 | } 34 | 35 | - (void)activationFunctionGradient:(Matrix *)outputCopy 36 | { 37 | [outputCopy applyFunction:^double(double value) { 38 | return 1.0 - value*value; // 1 - (f(x)) ^ 2 39 | }]; 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /YCML/Linear Regression/YCLinRegModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinRegModel.h 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 12/12/16. 6 | // Copyright © 2016 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import 10 | @class Matrix; 11 | 12 | @interface YCLinRegModel : YCSupervisedModel 13 | 14 | @property (strong) Matrix *theta; 15 | 16 | @property (strong) Matrix *inputTransform; 17 | 18 | @property (strong) Matrix *outputTransform; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /YCML/Linear Regression/YCLinRegModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinRegModel.m 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 12/12/16. 6 | // Copyright © 2016 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import "YCLinRegModel.h" 10 | 11 | @implementation YCLinRegModel 12 | 13 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 14 | { 15 | NSAssert(self.theta, @"Model not trained"); 16 | NSAssert([matrix rows] == self.inputSize, @"Input size mismatch"); 17 | 18 | // 1. Scale input 19 | Matrix *scaledInput = matrix; // NxS 20 | if (self.inputTransform) 21 | { 22 | scaledInput = [matrix matrixByRowWiseMapUsing:self.inputTransform]; 23 | } 24 | 25 | // 2. Augment with bias term and multiply with weights 26 | // O = W * N 27 | int M = scaledInput->columns; 28 | // TODO: Optimize this to not add a whole row to the 29 | // input! 30 | Matrix *inputWithBias = [scaledInput appendRow:[Matrix matrixOfRows:1 columns:M value:1]]; 31 | Matrix *output = [self.theta matrixByTransposingAndMultiplyingWithRight:inputWithBias]; 32 | 33 | // 5. Scale output and return 34 | if (self.outputTransform) 35 | { 36 | return [output matrixByRowWiseMapUsing:self.outputTransform]; 37 | } 38 | return output; 39 | } 40 | 41 | - (int)inputSize 42 | { 43 | return self.theta.rows - 1; 44 | } 45 | 46 | - (int)outputSize 47 | { 48 | return self.theta.columns; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /YCML/Linear Regression/YCLinRegTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinRegTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 12/12/16. 6 | // Copyright © 2016 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface YCLinRegTrainer : YCSupervisedTrainer 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YCML/Linear Regression/YCLinRegTrainer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinRegTrainer.m 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 12/12/16. 6 | // Copyright © 2016 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import "YCLinRegTrainer.h" 10 | #import "YCLinRegModel.h" 11 | #import "YCLinearModelProblem.h" 12 | 13 | @implementation YCLinRegTrainer 14 | 15 | + (Class)modelClass 16 | { 17 | return [YCLinRegModel class]; 18 | } 19 | 20 | - (instancetype)init 21 | { 22 | if (self = [super init]) 23 | { 24 | self.settings[@"L2"] = @0.001; 25 | self.settings[@"Iterations"] = @2000; 26 | self.settings[@"Alpha"] = @0.2; 27 | self.settings[@"Target"] = @-1; 28 | } 29 | return self; 30 | } 31 | 32 | - (void)performTrainingModel:(YCLinRegModel *)model 33 | inputMatrix:(Matrix *)input 34 | outputMatrix:(Matrix *)output 35 | { 36 | // Input: One sample per column 37 | // Output: One sample per column 38 | // Input: NxS, output: OxS 39 | 40 | // Step I. Scaling inputs & outputs; determining inverse output scaling matrix 41 | YCDomain domain = YCMakeDomain(0, 1); 42 | Matrix *inputTransform = [input rowWiseMapToDomain:domain basis:StDev]; 43 | Matrix *outputTransform = [output rowWiseMapToDomain:domain basis:MinMax]; 44 | Matrix *invOutTransform = [output rowWiseInverseMapFromDomain:domain basis:MinMax]; 45 | Matrix *scaledInput = [input matrixByRowWiseMapUsing:inputTransform]; 46 | Matrix *scaledOutput = [output matrixByRowWiseMapUsing:outputTransform]; 47 | 48 | YCLinearModelProblem *p = [[YCLinearModelProblem alloc] initWithInputMatrix:scaledInput 49 | outputMatrix:scaledOutput 50 | model:model]; 51 | 52 | p.l2 = [self.settings[@"L2"] doubleValue]; 53 | YCGradientDescent *optimizer = [[YCGradientDescent alloc] initWithProblem:p]; 54 | [optimizer.settings addEntriesFromDictionary:self.settings]; 55 | if ([self.settings[@"Target"] doubleValue] <= 0) 56 | { 57 | [optimizer.settings removeObjectForKey:@"Target"]; 58 | } 59 | 60 | [optimizer run]; 61 | 62 | Matrix *theta = [p thetaWithParameters:optimizer.state[@"values"]]; 63 | model.theta = [theta copy]; 64 | 65 | // Step VI. Copy transform matrices to model 66 | // TRANSFORM MATRICES SHOULD BE COPIED AFTER TRAINING OTHERWISE 67 | // THE MODEL WILL SCALE OUTPUTS AND RETURN FALSE ERRORS 68 | model.inputTransform = inputTransform; 69 | model.outputTransform = invOutTransform; 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /YCML/Linear Regression/YCLinearModelProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinearModelProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 12/12/16. 6 | // Copyright © 2016 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "YCDerivativeProblem.h" 11 | @class Matrix, YCLinRegModel; 12 | 13 | @interface YCLinearModelProblem : NSObject 14 | 15 | - (instancetype)initWithInputMatrix:(Matrix *)input 16 | outputMatrix:(Matrix *)output 17 | model:(YCLinRegModel *)model; 18 | 19 | - (Matrix *)thetaWithParameters:(Matrix *)parameters; 20 | 21 | @property YCLinRegModel *model; 22 | 23 | @property double l2; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /YCML/Optimization/Genetic Algorithms/YCHYPE.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCHYPE.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 13/1/16. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCOptimizer.h" 24 | #import "YCPopulationBasedOptimizer.h" 25 | #import "YCIndividual.h" 26 | 27 | @interface YCHypE : YCPopulationBasedOptimizer 28 | 29 | + (BOOL)vector:(Matrix *)vector weaklyDominates:(Matrix *)sample targets:(Matrix *)targets; 30 | 31 | @end 32 | 33 | @interface YCHypEIndividual : YCIndividual 34 | 35 | @property double v; 36 | 37 | @property int rank; 38 | 39 | @property int n; 40 | 41 | @property NSMutableSet *s; 42 | 43 | @end -------------------------------------------------------------------------------- /YCML/Optimization/Genetic Algorithms/YCNSGAII.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCNSGAII.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015-2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCOptimizer.h" 24 | #import "YCPopulationBasedOptimizer.h" 25 | #import "YCIndividual.h" 26 | 27 | @interface YCNSGAII : YCPopulationBasedOptimizer 28 | 29 | - (void)nonDominatedSortingWithPopulation:(NSArray *)population; 30 | 31 | @end 32 | 33 | @interface YCNSGAIndividual : YCIndividual 34 | 35 | @property int rank; 36 | 37 | @property double crowdingDistance; 38 | 39 | @property int n; 40 | 41 | @property NSMutableSet *s; 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /YCML/Optimization/Gradient Descent/YCGradientDescent.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCOptimizer.h" 25 | 26 | @interface YCGradientDescent : YCOptimizer 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /YCML/Optimization/Gradient Descent/YCGradientDescent.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import YCMatrix; 24 | #import "YCGradientDescent.h" 25 | #import "YCDerivativeProblem.h" 26 | 27 | @implementation YCGradientDescent 28 | 29 | - (instancetype)initWithProblem:(NSObject *)aProblem settings:(NSDictionary *)settings 30 | { 31 | self = [super initWithProblem:aProblem settings:settings]; 32 | if (self) 33 | { 34 | self.settings[@"Alpha"] = @0.001; 35 | } 36 | return self; 37 | } 38 | 39 | - (BOOL)iterate:(int)iteration 40 | { 41 | BOOL maximize = [self.problem.modes i:0 j:0] > 0; 42 | int k = self.problem.parameterCount; 43 | 44 | if (!self.state[@"values"]) 45 | { 46 | Matrix *newValues = [Matrix matrixOfRows:k columns:1]; 47 | 48 | Matrix *initialRanges = [self.problem initialValuesRangeHint]; 49 | 50 | for (int i=0; i *)self.problem derivatives:gradients parameters:values]; 66 | [gradients multiplyWithScalar:alpha]; 67 | if (maximize) 68 | { 69 | [values add:gradients]; 70 | } 71 | else 72 | { 73 | [values subtract:gradients]; 74 | } 75 | } 76 | 77 | if (self.settings[@"Target"]) 78 | { 79 | Matrix *values = self.state[@"values"]; 80 | double target = [self.settings[@"Target"] doubleValue]; 81 | 82 | Matrix *objectiveValues = [Matrix matrixOfRows:self.problem.objectiveCount columns:1]; 83 | [self.problem evaluate:objectiveValues parameters:values]; 84 | double best = [objectiveValues sum]; 85 | self.state[@"best"] = @(best); 86 | if ((maximize && best >= target) || (best <= target)) return NO; 87 | } 88 | return YES; 89 | } 90 | 91 | - (NSArray *)bestParameters 92 | { 93 | if (!self.state[@"values"]) return nil; 94 | return @[self.state[@"values"]]; 95 | } 96 | 97 | - (NSArray *)bestObjectives 98 | { 99 | if (!self.state[@"best"]) return nil; 100 | return @[self.state[@"best"]]; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /YCML/Optimization/Metrics/YCHypervolumeMetric.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCHypervolumeMetric.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 18/1/16. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | @class Matrix; 25 | 26 | @interface YCHypervolumeMetric : NSObject 27 | 28 | - (double)estimateHypervolumeForObjectiveFunctionVectors:(NSArray *)vectors 29 | targets:(Matrix *)targets 30 | sampleSize:(int)sampleSize 31 | lowerReference:(Matrix *)lowerReference 32 | upperReference:(Matrix *)upperReference; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /YCML/Optimization/Metrics/YCHypervolumeMetric.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCHypervolumeMetric.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 18/1/16. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCHypervolumeMetric.h" 24 | #import "YCHypE.h" 25 | @import YCMatrix; 26 | 27 | @implementation YCHypervolumeMetric 28 | 29 | - (double)estimateHypervolumeForObjectiveFunctionVectors:(NSArray *)vectors 30 | targets:(Matrix *)targets 31 | sampleSize:(int)sampleSize 32 | lowerReference:(Matrix *)lowerReference 33 | upperReference:(Matrix *)upperReference 34 | { 35 | Matrix *lower = lowerReference ? lowerReference : [vectors matrixMin]; 36 | Matrix *upper = upperReference ? upperReference : [vectors matrixMax]; 37 | 38 | int dominatedSamples = 0; 39 | 40 | for (int i=0; i. 22 | 23 | @import Foundation; 24 | #import "YCOptimizer.h" 25 | 26 | @interface YCRProp : YCOptimizer 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /YCML/Optimization/YCCompoundProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCCompoundProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | #import "YCProblem.h" 25 | 26 | @interface YCCompoundProblem : NSObject 27 | 28 | @property NSMutableArray *problems; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Optimization/YCDerivativeProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCProblem.h" 25 | 26 | @protocol YCDerivativeProblem 27 | 28 | - (void)derivatives:(Matrix *)target parameters:(Matrix *)parameters; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /YCML/Optimization/YCIndividual.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCIndividual.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/6/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | @class Matrix; 25 | @interface YCIndividual : NSObject 26 | 27 | - (instancetype)initWithVariableCount:(int)count; 28 | 29 | - (instancetype)initWithRandomValuesInBounds:(Matrix *)bounds; // nx2 30 | 31 | @property Matrix *decisionVariableValues; 32 | 33 | @property Matrix *objectiveFunctionValues; 34 | 35 | @property Matrix *constraintValues; 36 | 37 | @property (readonly) double constraintViolation; 38 | 39 | @property BOOL evaluated; 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /YCML/Optimization/YCIndividual.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCIndividual.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/6/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #define ARC4RANDOM_MAX 0x100000000 24 | 25 | #import "YCIndividual.h" 26 | @import YCMatrix; 27 | 28 | @implementation YCIndividual 29 | 30 | - (instancetype)init 31 | { 32 | return [self initWithRandomValuesInBounds:[Matrix matrixOfRows:0 columns:0]]; 33 | } 34 | 35 | - (instancetype)initWithRandomValuesInBounds:(Matrix *)bounds 36 | { 37 | int m = (int)[bounds rows]; 38 | self = [self initWithVariableCount:m]; 39 | if (self) 40 | { 41 | for (int i=0; i. 22 | // 23 | 24 | @import Foundation; 25 | #import "YCProblem.h" 26 | 27 | @protocol YCOptimizerDelegate 28 | 29 | - (void)stepComplete:(NSDictionary *)info; 30 | 31 | @end 32 | 33 | @interface YCOptimizer : NSObject 34 | 35 | - (instancetype)initWithProblem:(NSObject *)aProblem; 36 | 37 | - (instancetype)initWithProblem:(NSObject *)aProblem settings:(NSDictionary *)settings; 38 | 39 | - (void)run; 40 | 41 | - (BOOL)iterate:(int)iteration; 42 | 43 | /** 44 | Resets the receiver's state. Settings are kept. 45 | */ 46 | - (void)reset; 47 | 48 | /** 49 | Sends a request to the receiver to stop any ongoing processing. 50 | */ 51 | - (void)stop; 52 | 53 | /** 54 | Holds whether the receiver is bound to stop. 55 | */ 56 | @property BOOL shouldStop; 57 | 58 | @property NSObject *problem; 59 | 60 | @property NSMutableDictionary *state; 61 | 62 | @property NSMutableDictionary *settings; 63 | 64 | @property NSMutableDictionary *statistics; 65 | 66 | @property (readonly) NSArray *bestParameters; 67 | 68 | @property (readonly) NSArray *bestObjectives; 69 | 70 | @property (readonly) NSArray *bestConstraints; 71 | 72 | @property NSObject *delegate; 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /YCML/Optimization/YCPopulationBasedOptimizer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCPopulationBasedOptimizer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/6/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCOptimizer.h" 24 | 25 | @interface YCPopulationBasedOptimizer : YCOptimizer 26 | 27 | + (Class)individualClass; 28 | 29 | - (void)initializePopulation; 30 | 31 | - (void)replacePopulationUsing:(Matrix *)data; 32 | 33 | - (void)evaluateIndividuals:(NSArray *)individuals; 34 | 35 | @property NSMutableArray *population; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /YCML/Optimization/YCProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/3/15. 6 | // Copyright (c) 2015-2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | @class Matrix; 25 | 26 | /* 27 | With respect to concurrency, there are three ways in which a 28 | problem function can be evaluated: 29 | 30 | 1. The function needs to be evaluated sequentially 31 | 2. The function can be evaluated concurrently, but it's implementation is sequential 32 | 3. The function itself provides an (optimized) parallel implementation 33 | 34 | The latter case is the most favorable, as the problem may provide an optimized 35 | parallel implementation that supersedes the performance of mere concurrent evaluation. 36 | 37 | */ 38 | typedef NS_ENUM(int, YCEvaluationMode) { 39 | YCRequiresSequentialEvaluation = 0, 40 | YCSupportsConcurrentEvaluation = 1, 41 | YCProvidesParallelImplementation = 2 42 | }; 43 | 44 | typedef NS_ENUM(int, YCObjectiveTarget) { 45 | YCObjectiveMinimize = 0, 46 | YCObjectiveMaximize = 1 47 | }; 48 | 49 | @protocol YCProblem 50 | 51 | /** 52 | Requests a problem function evaluation from the receiver 53 | 54 | @param target: The target for the objective function and constraint values. 55 | First all objective function values, then constraints. 56 | @param parameters: The decision variable values for which to evaluate 57 | */ 58 | - (void)evaluate:(Matrix *)target parameters:(Matrix *)parameters; 59 | 60 | /** 61 | The numerical bounds for the decision variables 62 | */ 63 | @property (readonly) Matrix *parameterBounds; 64 | 65 | /** 66 | Optional array correponding to initial value ranges 67 | */ 68 | @property (readonly) Matrix *initialValuesRangeHint; 69 | 70 | /** 71 | The number of decision variables 72 | */ 73 | @property (readonly) int parameterCount; 74 | 75 | /** 76 | The number of objectives 77 | */ 78 | @property (readonly) int objectiveCount; 79 | 80 | /** 81 | The number of constraints 82 | */ 83 | @property (readonly) int constraintCount; 84 | 85 | /** 86 | A matrix denoting targets for optimization, per objective: Minimization or Maximization 87 | */ 88 | @property (readonly) Matrix *modes; 89 | 90 | /** 91 | A value showing how the receiver treats cases where more than one solutions 92 | needs to be evaluated. The receiver may return the following values: 93 | i. YCRequiresSequentialEvaluation: the receiver requires that solutions are 94 | presented sequentially for evaluation 95 | ii. YCSupportsConcurrentEvaluation: the receiver allows concurrent evaluation 96 | of more than one solutions 97 | iii. YCProvidesParallelImplementation: the receiver provides its own 98 | evaluation implementation and solutions should be presented all at once, as 99 | part of a single matrix 100 | */ 101 | @property (readonly) YCEvaluationMode supportedEvaluationMode; 102 | 103 | @optional 104 | 105 | /** 106 | Labels associated with decision variables 107 | */ 108 | @property (readonly) NSArray *parameterLabels; 109 | 110 | /** 111 | Labels associated with objectives 112 | */ 113 | @property (readonly) NSArray *objectiveLabels; 114 | 115 | /** 116 | Labels associated with constraints 117 | */ 118 | @property (readonly) NSArray *constraintLabels; 119 | 120 | /** 121 | Additional properties associated with the last set of solution(s) 122 | TODO: This should be implemented as an array instead of dictionary 123 | */ 124 | @property (readonly) NSDictionary *metaProperties; 125 | 126 | @end 127 | -------------------------------------------------------------------------------- /YCML/RBF/YCOLSPRESSTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCOLSPRESSTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 22/4/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCOLSTrainer.h" 24 | 25 | @interface YCOLSPRESSTrainer : YCOLSTrainer 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/RBF/YCOLSTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCOLSTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 22/4/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedTrainer.h" 24 | @class YCRBFNet, Matrix; 25 | 26 | @interface YCOLSTrainer : YCSupervisedTrainer 27 | 28 | - (void)centersAndWidthsFor:(YCRBFNet *)model input:(Matrix *)inp output:(Matrix *)outp; 29 | 30 | - (void)weightsFor:(YCRBFNet *)model input:(Matrix *)input output:(Matrix *)output; 31 | 32 | - (Matrix *)initialDesignMatrixWithInput:(Matrix *)input widths:(Matrix *)widths; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /YCML/RBF/YCRBFNet.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFNet.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 22/4/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedModel.h" 24 | 25 | @interface YCRBFNet : YCSupervisedModel 26 | 27 | /** 28 | Returns the design matrix of the model with input |input| 29 | 30 | @param input The input to the model. 31 | 32 | @return The design matrix. 33 | */ 34 | - (Matrix *)designMatrixWithInput:(Matrix *)input; 35 | 36 | /** 37 | Returns the input transformation matrix of the receiver. 38 | */ 39 | @property Matrix *inputTransform; 40 | 41 | /** 42 | Returns the output reverse transformation matrix of the receiver. 43 | */ 44 | @property Matrix *outputTransform; 45 | 46 | @property Matrix *centers; 47 | 48 | @property Matrix *widths; 49 | 50 | @property Matrix *weights; 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /YCML/RBF/YCRBFNet.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCRBFNet.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 22/4/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCRBFNet.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCRBFNet 27 | 28 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 29 | { 30 | NSAssert([self.weights count], @"Model not trained"); 31 | NSAssert(matrix.rows == self.centers.rows, @"Input size mismatch"); 32 | 33 | // 1. Scale input 34 | Matrix *ScaledInput = matrix; 35 | if (self.inputTransform) 36 | { 37 | ScaledInput = [matrix matrixByRowWiseMapUsing:self.inputTransform]; 38 | } 39 | 40 | // 2. Calculate Basis Function Outputs 41 | // ->SxD 42 | Matrix *H = [self designMatrixWithInput:ScaledInput]; 43 | 44 | // 3. Augment with bias term! 45 | H = [H appendColumn:[Matrix matrixOfRows:H->rows columns:1 value:1.0]]; 46 | 47 | // 4. Linearly combine RBF to get the output (SxD * DxO)' -> OxS 48 | Matrix *Output = [H matrixByMultiplyingWithRight:self.weights AndTransposing:YES]; 49 | 50 | // 5. Scale output and return 51 | if (self.outputTransform) 52 | { 53 | return [Output matrixByRowWiseMapUsing:self.outputTransform]; 54 | } 55 | return Output; 56 | } 57 | 58 | - (Matrix *)designMatrixWithInput:(Matrix *)input 59 | { 60 | int N = input->rows; 61 | int S = input->columns; 62 | int D = self.centers->columns; 63 | 64 | // Generate design matrix of dimensions SxD 65 | Matrix *designmatrix = [Matrix matrixOfRows:S columns:D]; // -> SxD 66 | 67 | // Fill up the design matrix, traversing first row and then column 68 | dispatch_apply(S, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i) 69 | { 70 | @autoreleasepool { 71 | for (int j=0; jmatrix[k*S + i] - self->_centers->matrix[k*D + j]; 78 | sqsum += val*val; 79 | } 80 | double bfvalue = exp( - sqsum / pow(self->_widths->matrix[j], 2)); 81 | designmatrix->matrix[i*D + j] = bfvalue; 82 | } 83 | } 84 | }); 85 | return designmatrix; 86 | } 87 | 88 | #pragma mark - Properties 89 | 90 | - (int)inputSize 91 | { 92 | return self.centers.rows; 93 | } 94 | 95 | - (int)outputSize 96 | { 97 | return self.weights.columns; 98 | } 99 | 100 | @end 101 | -------------------------------------------------------------------------------- /YCML/RBM/YCBinaryRBM.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBM.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | @import YCMatrix; 25 | #import "YCGenericModel.h" 26 | 27 | @interface YCBinaryRBM : YCGenericModel 28 | 29 | - (Matrix *)prePropagateToHidden:(Matrix *)visible; 30 | 31 | - (Matrix *)prePropagateToVisible:(Matrix *)hidden; 32 | 33 | - (Matrix *)propagateToHidden:(Matrix *)visible; 34 | 35 | - (Matrix *)propagateToVisible:(Matrix *)hidden; 36 | 37 | - (Matrix *)sampleHiddenGivenVisible:(Matrix *)visible; 38 | 39 | - (Matrix *)sampleVisibleGivenHidden:(Matrix *)hidden; 40 | 41 | - (Matrix *)gibbsStep:(Matrix *)visible; 42 | 43 | - (Matrix *)freeEnergy:(Matrix *)visible; 44 | 45 | @property Matrix *weights; 46 | 47 | @property Matrix *visibleBiases; 48 | 49 | @property Matrix *hiddenBiases; 50 | 51 | @property (readonly) int visibleSize; 52 | 53 | @property (readonly) int hiddenSize; 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /YCML/RBM/YCBinaryRBM.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBM.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCBinaryRBM.h" 24 | 25 | // N: Size of input 26 | // S: Number of samples 27 | // H: Size of hidden layer 28 | 29 | // Weights, W: HxN 30 | // Visible biases, b: Nx1 31 | // Hidden biases, c: Hx1 32 | 33 | // Input: Nx1 34 | 35 | @implementation YCBinaryRBM 36 | 37 | - (Matrix *)prePropagateToHidden:(Matrix *)visible 38 | { 39 | Matrix *ret = [self.weights matrixByMultiplyingWithRight:visible]; // HxN * NxS = HxS 40 | [ret addColumn:self.hiddenBiases]; 41 | return ret; 42 | } 43 | 44 | - (Matrix *)prePropagateToVisible:(Matrix *)hidden 45 | { 46 | Matrix *ret = [self.weights matrixByTransposingAndMultiplyingWithRight:hidden]; // (HxN)T * HxS = NxS 47 | [ret addColumn:self.visibleBiases]; 48 | return ret; 49 | } 50 | 51 | - (Matrix *)propagateToHidden:(Matrix *)visible 52 | { 53 | Matrix *ret = [self prePropagateToHidden:visible]; 54 | [ret applyFunction:^double(double value) { 55 | return 1.0 / (1.0 + exp(-value)); 56 | }]; 57 | return ret; 58 | } 59 | 60 | - (Matrix *)propagateToVisible:(Matrix *)hidden 61 | { 62 | Matrix *ret = [self prePropagateToVisible:hidden]; 63 | [ret applyFunction:^double(double value) { 64 | return 1.0 / (1.0 + exp(-value)); 65 | }]; 66 | return ret; 67 | } 68 | 69 | - (Matrix *)sampleHiddenGivenVisible:(Matrix *)visible 70 | { 71 | Matrix *hidden = [self propagateToHidden:visible]; 72 | [hidden bernoulli]; 73 | return hidden; 74 | } 75 | 76 | - (Matrix *)sampleVisibleGivenHidden:(Matrix *)hidden 77 | { 78 | Matrix *visible = [self propagateToVisible:hidden]; 79 | [visible bernoulli]; 80 | return visible; 81 | } 82 | 83 | - (Matrix *)gibbsStep:(Matrix *)visible 84 | { 85 | return [self sampleVisibleGivenHidden:[self sampleHiddenGivenVisible:visible]]; 86 | } 87 | 88 | - (Matrix *)freeEnergy:(Matrix *)visible 89 | { 90 | // visible: NxS 91 | Matrix *wxb = [self prePropagateToHidden:visible]; // HxN * NxS = HxS (wxb) 92 | [wxb applyFunction:^double(double value) { return log(1 + exp(value)); }]; 93 | Matrix *ht = [wxb sumsOfColumns]; // 1xS (hidden) 94 | 95 | Matrix *av = [self.visibleBiases matrixByTransposingAndMultiplyingWithRight:visible]; // (Nx1)T * NxS = 1xS (vBias) 96 | 97 | [av add:ht]; // hidden + vis 98 | [av negate]; // - hidden - vis 99 | return av; // 1xS 100 | } 101 | 102 | - (int)visibleSize 103 | { 104 | return self.weights.columns; 105 | } 106 | 107 | - (int)hiddenSize 108 | { 109 | return self.weights.rows; 110 | } 111 | 112 | @end 113 | -------------------------------------------------------------------------------- /YCML/RBM/YCCDProblem.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBMProblem.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCDerivativeProblem.h" 25 | @class YCBinaryRBM, Matrix; 26 | 27 | @interface YCCDProblem : NSObject 28 | { 29 | Matrix *_inputMatrix; 30 | } 31 | 32 | - (instancetype)initWithInputMatrix:(Matrix *)inputMatrix model:(YCBinaryRBM *)model; 33 | 34 | - (Matrix *)weightsWithParameters:(Matrix *)parameters; 35 | 36 | - (Matrix *)visibleBiasWithParameters:(Matrix *)parameters; 37 | 38 | - (Matrix *)hiddenBiasWithParameters:(Matrix *)parameters; 39 | 40 | - (void)storeWeights:(Matrix *)weights 41 | visibleBiases:(Matrix *)vBiases 42 | hiddenBiases:(Matrix *)hBiases 43 | toVector:(Matrix *)vector; 44 | 45 | @property YCBinaryRBM *trainedModel; 46 | 47 | @property double lambda; 48 | 49 | @property int sampleCount; 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /YCML/RBM/YCCDTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCBinaryRBMTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | #import "YCGenericTrainer.h" 25 | @class YCBinaryRBM, YCDataframe, Matrix; 26 | 27 | @interface YCCDTrainer : YCGenericTrainer 28 | 29 | + (Class)optimizerClass; 30 | 31 | /** 32 | Trains a model using the receiver's training algorithm and a (binary) input. 33 | 34 | @param model The model to train. Can be nil. 35 | @param input The training input. 36 | 37 | @return The trained model (if input != nil, it is the same as the input) 38 | */ 39 | - (YCBinaryRBM *)train:(YCBinaryRBM *)model input:(YCDataframe *)input; 40 | 41 | /** 42 | Trains a model using the receiver's training algorithm, and a (binary) matrix as input. 43 | 44 | @param model The model to train. Can be nil. 45 | @param input The training input. 46 | 47 | @return The trained model (if input != nil, it is the same as the input) 48 | */ 49 | - (YCBinaryRBM *)train:(YCBinaryRBM *)model inputMatrix:(Matrix *)input; 50 | 51 | /** 52 | Implements the actual training routine. This method should be implemented 53 | when subclassing. 54 | 55 | @param model The model to train. 56 | @param input The training input. 57 | */ 58 | - (void)performTrainingModel:(YCBinaryRBM *)model inputMatrix:(Matrix *)input; 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /YCML/Ranking/YCRankCentrality.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCRankCentrality.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 24/11/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | @class Matrix; 25 | 26 | /** 27 | RankCentrality 28 | 29 | This class implements a mechanism to obtain absolute scores from 30 | pair-wise comparisons for a population of individuals. It is based on the 31 | paper "Iterative Ranking from Pair-wise Comparisons". It accepts a diagonal nxn matrix 32 | containing comparison scores for n individuals, and produces a vector (nx1 matrix) 33 | with values corresponding to each of the n items absolute score. 34 | */ 35 | 36 | @interface YCRankCentrality : NSObject 37 | 38 | /** 39 | Calculates the transition matrix for a given matrix containing the number 40 | of losses for each individual. 41 | 42 | @param comparisons: Matrix containing the number 43 | of losses for each individual. The value in cell at i,j refers to the number 44 | of times j lost to i. Conversely, the value at j, i refers to the number of 45 | times i lost to j. Summing these up gives us the total number of matches played. 46 | 47 | @return The transition matrix containing the probabilities of the Markov chain 48 | */ 49 | + (Matrix *)transitionMatrixWithComparisons:(Matrix *)comparisons; 50 | 51 | /** 52 | Calculates the scores of a population of individuals according to the 53 | Bradley-Terry-Luce model of comparative judgement. 54 | 55 | @param transitionMatrix: Matrix containing the probabilities of Markov chain transitions. 56 | 57 | @return Vector containing the scores 58 | */ 59 | + (Matrix *)scoresWithTransitionMatrix:(Matrix *)transitionMatrix; 60 | 61 | /** 62 | Calculates the scores of a population of individuals according to the 63 | Bradley-Terry-Luce model of comparative judgement. 64 | 65 | @param comparisons: Matrix containing the number 66 | of losses for each individual. The value in cell at i,j refers to the number 67 | of times j lost to i. Conversely, the value at j, i refers to the number of 68 | times i lost to j. Summing these up gives us the total number of matches played. 69 | 70 | @return Vector containing the scores 71 | */ 72 | + (Matrix *)scoresWithComparisons:(Matrix *)comparisons; 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /YCML/Ranking/YCRankCentrality.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCRankCentrality.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 24/11/16. 6 | // Copyright © 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCRankCentrality.h" 24 | @import YCMatrix; 25 | 26 | @implementation YCRankCentrality 27 | 28 | + (Matrix *)transitionMatrixWithComparisons:(Matrix *)comparisons 29 | { 30 | NSAssert(comparisons.rows == comparisons.columns && comparisons.rows >= 2, 31 | @"Incorrect Matrix size"); 32 | 33 | Matrix *comparisonsCopy = [comparisons copy]; 34 | // Normalize losses counts to fractions for each pair 35 | for (int i=0, k=comparisonsCopy.columns; i= 2, 100 | @"Incorrect Matrix size"); 101 | 102 | Matrix *transitions = [self transitionMatrixWithComparisons:comparisons]; 103 | return [self scoresWithTransitionMatrix:transitions]; 104 | } 105 | 106 | @end 107 | -------------------------------------------------------------------------------- /YCML/Regression Metrics/YCRegressionMetrics.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCRegressionMetrics.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | // This is a file implementing various metrics to evaluate regression performance. 24 | // The functions can evaluate single attribute as well as multi attribute datasets 25 | // by taking the mean of the values associated with each attribute. 26 | // Both YCDataframe as well as Matrix objects are acceptable as parameters. In the 27 | // latter case, each matrix column is considered as a sample. 28 | 29 | @import Foundation; 30 | @class YCDataframe; 31 | 32 | double MSE(id trueData, id predictedData); 33 | 34 | double RSquared(id trueData, id predictedData); -------------------------------------------------------------------------------- /YCML/Regression Metrics/YCRegressionMetrics.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCRegressionMetrics.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | // References: 24 | // http://scikit-learn.org/stable/modules/model_evaluation.html#regression-metrics 25 | // http://stackoverflow.com/questions/9739460/weird-error-nsassert 26 | 27 | #import "YCRegressionMetrics.h" 28 | @import YCMatrix; 29 | #import "YCDataframe.h" 30 | #import "YCDataframe+Matrix.h" 31 | 32 | Matrix *conditionalConvertToMatrix(id object); 33 | 34 | // TODO: 35 | // These methods say they accept dataframes, but they can also be 36 | // provided with matrices. In fact, this is what the -conditionalConvertToMatrix 37 | // method is doing. This should be better implemented and clearly explained. 38 | 39 | double MSE(id trueData, id predictedData) 40 | { 41 | Matrix *trueMatrix = conditionalConvertToMatrix(trueData); 42 | Matrix *predictedMatrix = conditionalConvertToMatrix(predictedData); 43 | Matrix *sv = [trueMatrix matrixBySubtracting:predictedMatrix]; 44 | [sv elementWiseMultiply:sv]; 45 | Matrix *means = [sv meansOfRows]; 46 | return [means meansOfColumns]->matrix[0]; 47 | } 48 | 49 | 50 | double RSquared(id trueData, id predictedData) 51 | { 52 | // Calculates 1 - RSS/TSS 53 | 54 | Matrix *trueMatrix = conditionalConvertToMatrix(trueData); 55 | Matrix *predictedMatrix = conditionalConvertToMatrix(predictedData); 56 | 57 | Matrix *numMatrix = [trueMatrix matrixBySubtracting:predictedMatrix]; 58 | [numMatrix elementWiseMultiply:numMatrix]; 59 | numMatrix = [numMatrix sumsOfRows]; 60 | 61 | Matrix *denMatrix = [trueMatrix matrixBySubtractingColumn:[trueMatrix meansOfRows]]; 62 | [denMatrix elementWiseMultiply:denMatrix]; 63 | denMatrix = [denMatrix sumsOfRows]; 64 | 65 | NSUInteger outCount = [numMatrix count]; 66 | 67 | Matrix *resMatrix = [Matrix matrixLike:numMatrix]; 68 | 69 | for (int i=0; imatrix[i]; 72 | double den = denMatrix->matrix[i]; 73 | if (den == 0) 74 | { 75 | if (num == 0) 76 | { 77 | resMatrix->matrix[i] = 1.0; 78 | } 79 | resMatrix->matrix[i] = 0.0; 80 | } 81 | resMatrix->matrix[i] = 1.0 - num/den; 82 | } 83 | return [resMatrix meansOfColumns]->matrix[0]; 84 | } 85 | 86 | Matrix *conditionalConvertToMatrix(id object) 87 | { 88 | if ([object isKindOfClass:[Matrix class]]) return object; 89 | NSCAssert([object isKindOfClass:[YCDataframe class]], @"Wrong parameter type"); 90 | YCDataframe *df = object; 91 | NSArray *ca = [df conversionArray]; 92 | return [df getMatrixUsingConversionArray:ca]; 93 | } 94 | -------------------------------------------------------------------------------- /YCML/SVR/YCLinkedList.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCLinkedList.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/1/16. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | // Adapted from CKLinkedList https://github.com/mschettler/CKLinkedList 24 | 25 | @import Foundation; 26 | 27 | typedef struct LNode LNode; 28 | 29 | struct LNode 30 | { 31 | NSUInteger index; 32 | LNode *headSide; 33 | LNode *tailSide; 34 | }; 35 | 36 | /** 37 | A linked list implementation 38 | 39 | @warning: This list does not memory manage it's members at all! 40 | */ 41 | @interface YCLinkedList : NSObject 42 | { 43 | LNode *head; 44 | LNode *tail; 45 | NSUInteger size; 46 | } 47 | 48 | - (void)pushTail:(LNode *)n; // adds a node object to the end of the list 49 | 50 | - (void)pushHead:(LNode *)n; // adds a node object to the beginning of the list 51 | 52 | - (void *)pop:(LNode *)aNode; // remove and return a given node 53 | 54 | - (void *)popTail; // pops a node object from the end of the list 55 | 56 | - (void *)popHead; // pops a node object from the beginning of the list 57 | 58 | ///@ Properties 59 | 60 | @property (readonly) LNode *headNode; 61 | 62 | @property (readonly) LNode *tailNode; 63 | 64 | @property (readonly) NSUInteger count; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /YCML/SVR/YCSMOCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSMOCache.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 29/1/16. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCLinkedList.h" 25 | @class Matrix; 26 | 27 | typedef enum cacheStatus { notIncluded, included } cacheStatus; 28 | 29 | @interface YCSMOCache : NSObject 30 | 31 | - (instancetype)initWithDatasetSize:(NSUInteger)datasetSize cacheSize:(NSUInteger)cacheSize; 32 | 33 | - (cacheStatus)queryI:(NSUInteger)i j:(NSUInteger)j; 34 | 35 | - (double)getI:(NSUInteger)i j:(NSUInteger)j tickle:(BOOL)tickle; 36 | 37 | - (void)setI:(NSUInteger)i j:(NSUInteger)j value:(double)value; 38 | 39 | ///@name Properties 40 | 41 | /** 42 | An array containing indexes in cache of each example in the dataset 43 | i.e. index[i] == index in cache of example i 44 | */ 45 | @property NSUInteger *index; 46 | 47 | /** 48 | An array containing all indexes in dataset for each cache element 49 | i.e. inverseIndex[n] == index in dataset of cache element n 50 | */ 51 | @property NSUInteger *inverseIndex; 52 | 53 | /** 54 | The cache values, MxM 55 | */ 56 | @property Matrix *values; 57 | 58 | /** 59 | The LRU nodes 60 | */ 61 | @property LNode *nodes; // LRU nodes 62 | 63 | /** 64 | The order of LRU indexes 65 | */ 66 | @property YCLinkedList *order; // LRU order 67 | 68 | /** 69 | The permanent diagonal kernel cache 70 | */ 71 | @property Matrix *diagonalCache; 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YCML/SVR/YCSMORegressionTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSMORegressionTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/12/15. 6 | // Copyright (c) 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedTrainer.h" 24 | @class YCSVR, YCSMOCache, Matrix; 25 | 26 | @interface YCSMORegressionTrainer : YCSupervisedTrainer 27 | 28 | - (double)outputForModel:(YCSVR *)model 29 | input:(Matrix *)input 30 | lambdas:(Matrix *)lambdas 31 | previousOutputs:(Matrix *)previousOutputs 32 | lastModified:(Matrix *)lastModified 33 | exampleIndex:(int)index 34 | bias:(double)bias 35 | tickleCache:(BOOL)tickle; 36 | 37 | - (double)errorForModel:(YCSVR *)model 38 | input:(Matrix *)input 39 | target:(Matrix *)output 40 | lambdas:(Matrix *)lambdas 41 | previousOutputs:(Matrix *)previousOutputs 42 | lastModified:(Matrix *)lastModified 43 | exampleIndex:(int)index 44 | bias:(double)bias 45 | tickleCache:(BOOL)tickle; 46 | 47 | - (BOOL)step:(YCSVR *)model 48 | input:(Matrix *)input 49 | output:(Matrix *)output 50 | lambdas:(Matrix *)lambdas 51 | previousOutputs:(Matrix *)previousOutputs 52 | lastModified:(Matrix *)lastModified 53 | i1:(int)iu 54 | i2:(int)iv 55 | bias:(double *)bias 56 | epsilon:(double)epsilon 57 | C:(double)C 58 | tickleCache:(BOOL)tickle; 59 | 60 | - (double)kernelValueForA:(NSUInteger)a B:(NSUInteger)b input:(Matrix *)input 61 | model:(YCSVR *)model tickle:(BOOL)tickle replace:(BOOL)replace; 62 | 63 | @property YCSMOCache *cache; 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /YCML/SVR/YCSVR.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSVR.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/12/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedModel.h" 24 | @class Matrix, YCModelKernel; 25 | 26 | @interface YCSVR : YCSupervisedModel 27 | 28 | /** 29 | Returns the kernel instance of this model. 30 | */ 31 | @property YCModelKernel *kernel; 32 | 33 | /** 34 | Returns a matrix corresponding to the support vectors of the receiver. 35 | */ 36 | @property Matrix *sv; 37 | 38 | /** 39 | Returns a vector corresponding to the lambdas of the receiver. 40 | */ 41 | @property Matrix *lambda; 42 | 43 | /** 44 | Returns the bias of the receiver 45 | */ 46 | @property double b; 47 | 48 | /** 49 | Returns the input transformation matrix of the receiver. 50 | */ 51 | @property Matrix *inputTransform; 52 | 53 | /** 54 | Returns the output reverse transformation matrix of the receiver. 55 | */ 56 | @property Matrix *outputTransform; 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /YCML/SVR/YCSVR.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCSVR.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 11/12/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | // N: Size of input 24 | // S: Number of samples 25 | // V: Support Vector count 26 | // O: Size of output (for SVM-Regression O == 1) 27 | 28 | #import "YCSVR.h" 29 | #import "YCModelKernel.h" 30 | @import YCMatrix; 31 | 32 | @implementation YCSVR 33 | 34 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 35 | { 36 | NSAssert([self.sv count], @"Model not trained"); 37 | NSAssert([matrix rows] == self.inputSize, @"Input size mismatch"); 38 | 39 | // 1. Scale input 40 | Matrix *scaledInput = matrix; 41 | if (self.inputTransform) 42 | { 43 | scaledInput = [matrix matrixByRowWiseMapUsing:self.inputTransform]; 44 | } 45 | 46 | // 2. Calculate kernel 47 | Matrix *k = [self.kernel kernelValueForA:self.sv b:scaledInput]; //(NxV)T * NxS = VxS 48 | 49 | // 3. Calculate output (algorithm is single output!) 50 | Matrix *output = [self.lambda matrixByMultiplyingWithRight:k]; 51 | 52 | [output incrementAll:self.b]; 53 | 54 | // 4. Reverse-scale output and return 55 | if (self.outputTransform) 56 | { 57 | return [output matrixByRowWiseMapUsing:self.outputTransform]; 58 | } 59 | return output; 60 | } 61 | 62 | - (int)inputSize 63 | { 64 | return self.sv.rows; 65 | } 66 | 67 | - (int)outputSize 68 | { 69 | return 1; 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /YCML/Surrogate Modeling/YCSurrogateModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSurrogateModel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCProblem.h" 25 | #import "YCSupervisedModel.h" 26 | 27 | @interface YCSurrogateModel : NSObject 28 | 29 | @property YCSupervisedModel *model; 30 | 31 | @property BOOL maximize; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /YCML/Testing/YCMonteCarloValidation.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCMonteCraloValidation.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 19/10/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCValidation.h" 24 | 25 | @interface YCMonteCarloValidation : YCValidation 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/Testing/YCValidation.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCValidation.h 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 19/10/15. 6 | // Copyright © 2015 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | @import Foundation; 10 | #import "YCGenericTrainer.h" 11 | @class YCSupervisedTrainer, YCDataframe; 12 | 13 | @protocol YCValidationDelegate 14 | 15 | - (void)stepComplete:(NSDictionary *)info; 16 | 17 | @end 18 | 19 | @interface YCValidation : NSObject 20 | 21 | + (instancetype)validationWithSettings:(NSDictionary *)settings; 22 | 23 | - (instancetype)initWithSettings:(NSDictionary *)settings; 24 | 25 | - (instancetype)initWithSettings:(NSDictionary *)settings 26 | evaluator:(NSDictionary *(^)(YCDataframe *, YCDataframe *, 27 | YCDataframe *))evaluator; 28 | 29 | - (NSDictionary *)test:(YCSupervisedTrainer *)trainer 30 | input:(YCDataframe *)trainingInput 31 | output:(YCDataframe *)trainingOutput; 32 | 33 | - (NSDictionary *)performTest:(YCSupervisedTrainer *)trainer 34 | input:(YCDataframe *)trainingInput 35 | output:(YCDataframe *)trainingOutput; 36 | 37 | @property (copy) NSDictionary *(^evaluator)(YCDataframe *, YCDataframe *, 38 | YCDataframe *); 39 | 40 | @property NSMutableDictionary *settings; 41 | 42 | @property NSDictionary *results; 43 | 44 | @property NSArray *models; 45 | 46 | @property YCSupervisedTrainer *activeTrainer; 47 | 48 | @property NSObject *delegate; 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /YCML/Testing/YCValidation.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCValidation.m 3 | // YCML 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 19/10/15. 6 | // Copyright © 2015 Yannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import "YCValidation.h" 10 | #import "YCRegressionMetrics.h" 11 | #import "YCSupervisedTrainer.h" 12 | 13 | @implementation YCValidation 14 | 15 | + (instancetype)validationWithSettings:(NSDictionary *)settings 16 | { 17 | return [[self alloc] initWithSettings:settings]; 18 | } 19 | 20 | - (instancetype)init 21 | { 22 | return [self initWithSettings:nil evaluator:nil]; 23 | } 24 | 25 | - (instancetype)initWithSettings:(NSDictionary *)settings 26 | { 27 | return [self initWithSettings:settings evaluator:nil]; 28 | } 29 | 30 | - (instancetype)initWithSettings:(NSDictionary *)settings 31 | evaluator:(NSDictionary *(^)(YCDataframe *, YCDataframe *, 32 | YCDataframe *))evaluator 33 | { 34 | self = [super init]; 35 | if (self) 36 | { 37 | self.settings = [NSMutableDictionary dictionary]; 38 | if (settings) [self.settings addEntriesFromDictionary:settings]; 39 | if (evaluator) 40 | { 41 | self.evaluator = evaluator; 42 | } 43 | else 44 | { 45 | self.evaluator = ^NSDictionary *(YCDataframe *ti, YCDataframe *to, YCDataframe *po) 46 | { 47 | return @{@"RMSE" : @(sqrt(MSE(to, po))), 48 | @"RSquared" : @(RSquared(to, po))}; 49 | }; 50 | } 51 | } 52 | return self; 53 | } 54 | 55 | - (NSDictionary *)test:(YCSupervisedTrainer *)trainer 56 | input:(YCDataframe *)trainingInput 57 | output:(YCDataframe *)trainingOutput 58 | { 59 | _activeTrainer = trainer; 60 | trainer.delegate = self; 61 | id results = [self performTest:trainer input:trainingInput output:trainingOutput]; 62 | _activeTrainer = nil; 63 | return results; 64 | } 65 | 66 | - (NSDictionary *)performTest:(YCSupervisedTrainer *)trainer 67 | input:(YCDataframe *)trainingInput 68 | output:(YCDataframe *)trainingOutput 69 | { 70 | @throw [NSInternalInconsistencyException initWithFormat: 71 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 72 | } 73 | 74 | - (void)stepComplete:(NSDictionary *)info 75 | { 76 | @throw [NSInternalInconsistencyException initWithFormat: 77 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 78 | } 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /YCML/Testing/YCkFoldValidation.h: -------------------------------------------------------------------------------- 1 | // 2 | // CrossValidation.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 28/4/15. 6 | // Copyright © 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCValidation.h" 24 | 25 | @interface YCkFoldValidation : YCValidation 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /YCML/YCGenericModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericModel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | /** 26 | A generic predictive model class. Includes basic facilities for 27 | storing settings and statistics. 28 | */ 29 | @interface YCGenericModel : NSObject 30 | 31 | /** 32 | Allocates and initializes a new instance of the receiver class. 33 | 34 | @return The new instance. 35 | */ 36 | + (instancetype)model; 37 | 38 | /** 39 | Holds statistics about the model, usually related to the learning process. 40 | */ 41 | @property NSMutableDictionary *statistics; 42 | 43 | /** 44 | Holds model properties. 45 | */ 46 | @property NSMutableDictionary *properties; 47 | 48 | /** 49 | Holds training settings used to train this model. 50 | */ 51 | @property NSMutableDictionary *trainingSettings; 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /YCML/YCGenericModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericModel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCGenericModel.h" 24 | 25 | @implementation YCGenericModel 26 | 27 | + (instancetype)model 28 | { 29 | return [[self alloc] init]; 30 | } 31 | 32 | #pragma mark Initialization 33 | 34 | - (instancetype)init 35 | { 36 | self = [super init]; 37 | if (self) 38 | { 39 | self.statistics = [NSMutableDictionary dictionary]; 40 | self.properties = [NSMutableDictionary dictionary]; 41 | self.trainingSettings = [NSMutableDictionary dictionary]; 42 | } 43 | return self; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /YCML/YCGenericTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | @protocol YCTrainerDelegate 26 | 27 | - (void)stepComplete:(NSDictionary *)info; 28 | 29 | @end 30 | 31 | @interface YCGenericTrainer : NSObject 32 | 33 | /** 34 | Allocates and initializes a new instance of the receiving class. 35 | 36 | @return The new instance 37 | */ 38 | + (instancetype)trainer; 39 | 40 | /** 41 | Returns the model class associated with the receiver. This method 42 | should be implemented when subclassing. 43 | 44 | @return The model class associated with the receiver. 45 | */ 46 | + (Class)modelClass; 47 | 48 | /** 49 | Sends a request to the receiver to stop any ongoing processing. 50 | */ 51 | - (void)stop; 52 | 53 | /** 54 | Holds whether the receiver is bound to stop. 55 | */ 56 | @property BOOL shouldStop; 57 | 58 | /** 59 | Holds training algorithm settings. 60 | */ 61 | @property NSMutableDictionary *settings; 62 | 63 | @property NSObject *delegate; 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /YCML/YCGenericTrainer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCGenericTrainer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCGenericTrainer.h" 24 | 25 | @implementation YCGenericTrainer 26 | 27 | + (instancetype)trainer 28 | { 29 | return [[self alloc] init]; 30 | } 31 | 32 | + (Class)modelClass 33 | { 34 | @throw [NSInternalInconsistencyException initWithFormat: 35 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 36 | } 37 | 38 | -(id)init 39 | { 40 | if (self = [super init]) 41 | { 42 | self.settings = [NSMutableDictionary dictionary]; 43 | } 44 | return self; 45 | } 46 | 47 | - (void)stop 48 | { 49 | self.shouldStop = true; 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /YCML/YCML.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCML.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | 25 | //! Project version number for YCML. 26 | FOUNDATION_EXPORT double YCMLVersionNumber; 27 | 28 | //! Project version string for YCML. 29 | FOUNDATION_EXPORT const unsigned char YCMLVersionString[]; 30 | 31 | // In this header, you should import all the public headers of your framework using statements like #import 32 | #import "YCGenericModel.h" 33 | #import "YCGenericTrainer.h" 34 | #import "YCSupervisedModel.h" 35 | #import "YCSupervisedTrainer.h" 36 | 37 | #import "YCLinRegModel.h" 38 | #import "YCLinRegTrainer.h" 39 | 40 | #import "YCSVR.h" 41 | #import "YCSMORegressionTrainer.h" 42 | #import "YCModelKernel.h" 43 | #import "YCLinearKernel.h" 44 | #import "YCRBFKernel.h" 45 | 46 | #import "YCFFN.h" 47 | #import "YCELMTrainer.h" 48 | 49 | #import "YCDerivativeProblem.h" 50 | #import "YCBackPropProblem.h" 51 | #import "YCBackPropTrainer.h" 52 | #import "YCRProp.h" 53 | #import "YCRpropTrainer.h" 54 | #import "YCSVR.h" 55 | #import "YCSMORegressionTrainer.h" 56 | #import "YCRBFNet.h" 57 | #import "YCOLSTrainer.h" 58 | #import "YCOLSPRESSTrainer.h" 59 | 60 | #import "YCModelLayer.h" 61 | #import "YCFullyConnectedLayer.h" 62 | #import "YCSigmoidLayer.h" 63 | #import "YCTanhLayer.h" 64 | #import "YCLinearLayer.h" 65 | #import "YCReLULayer.h" 66 | 67 | #import "YCKPM.h" 68 | #import "YCKPMTrainer.h" 69 | 70 | #import "YCBinaryRBM.h" 71 | #import "YCCDTrainer.h" 72 | #import "YCCDProblem.h" 73 | 74 | #import "YCProblem.h" 75 | #import "YCGradientDescent.h" 76 | #import "YCPopulationBasedOptimizer.h" 77 | #import "YCIndividual.h" 78 | #import "YCNSGAII.h" 79 | #import "YCHypE.h" 80 | #import "YCHypervolumeMetric.h" 81 | #import "YCSurrogateModel.h" 82 | #import "YCCompoundProblem.h" 83 | 84 | #import "YCRankCentrality.h" 85 | 86 | #import "YCDataframe.h" 87 | #import "YCDataframe+Matrix.h" 88 | #import "YCDataframe+Transform.h" 89 | #import "OrderedDictionary.h" 90 | #import "YCMissingValue.h" 91 | #import "NSIndexSet+Sampling.h" 92 | 93 | #import "YCMutableArray.h" 94 | #import "YCRegressionMetrics.h" 95 | #import "YCValidation.h" 96 | #import "YCkFoldValidation.h" 97 | #import "YCMonteCarloValidation.h" 98 | 99 | #import "YCModelIO.h" 100 | #import "YCGenericModel+IO.h" 101 | #import "YCSupervisedModel+IO.h" 102 | #import "YCModelLayer+IO.h" 103 | #import "YCFullyConnectedLayer+IO.h" 104 | #import "YCGenericTrainer+IO.h" 105 | -------------------------------------------------------------------------------- /YCML/YCSupervisedModel.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSupervisedModel.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCGenericModel.h" 25 | @class Matrix, YCDataframe; 26 | 27 | /** 28 | The base class for all supervised predictive models. Extends the base model 29 | class with methods for activating the receiver using either datasets or matrices, 30 | as well as properties for determining the input and output size of the receiver. 31 | */ 32 | @interface YCSupervisedModel : YCGenericModel 33 | 34 | /** 35 | Activates the receiver using the passed dataframe. 36 | 37 | @param input The dataframe used as input for activation. 38 | 39 | @return The output dataframe resulting from the prediction. 40 | */ 41 | - (YCDataframe *)activateWithDataframe:(YCDataframe *)input; 42 | 43 | /** 44 | Activates the receiver using the passed matrix. 45 | 46 | @param matrix The matrix to use as input for the activation. 47 | 48 | @return The output matrix resulting from the prediction. 49 | */ 50 | - (Matrix *)activateWithMatrix:(Matrix *)matrix; 51 | 52 | /** 53 | Returns the receiver's input size. 54 | */ 55 | @property (readonly) int inputSize; 56 | 57 | /** 58 | Returns the receiver's output size. 59 | */ 60 | @property (readonly) int outputSize; 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /YCML/YCSupervisedModel.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCSupervisedModel.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedModel.h" 24 | #import "YCDataframe.h" 25 | #import "YCDataframe+Matrix.h" 26 | @import YCMatrix; 27 | 28 | @implementation YCSupervisedModel 29 | 30 | @synthesize properties, statistics; 31 | 32 | #pragma mark - Learner Implementation 33 | 34 | - (YCDataframe *)activateWithDataframe:(YCDataframe *)input 35 | { 36 | Matrix *matrix = [input getMatrixUsingConversionArray:self.properties[@"InputConversionArray"]]; 37 | Matrix *predictedMatrix = [self activateWithMatrix:matrix]; 38 | return [YCDataframe dataframeWithMatrix:predictedMatrix 39 | conversionArray:self.properties[@"OutputConversionArray"]]; 40 | } 41 | 42 | - (Matrix *)activateWithMatrix:(Matrix *)matrix 43 | { 44 | @throw [NSInternalInconsistencyException initWithFormat: 45 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 46 | } 47 | 48 | - (int)inputSize 49 | { 50 | return 0; 51 | } 52 | 53 | - (int)outputSize 54 | { 55 | return 0; 56 | } 57 | 58 | @end 59 | 60 | -------------------------------------------------------------------------------- /YCML/YCSupervisedTrainer.h: -------------------------------------------------------------------------------- 1 | // 2 | // YCSupervisedTrainer.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import Foundation; 24 | #import "YCGenericTrainer.h" 25 | @class YCSupervisedModel, YCDataframe, Matrix; 26 | 27 | @interface YCSupervisedTrainer : YCGenericTrainer 28 | 29 | /** 30 | Trains a model using the receiver's training algorithm, and two dataframes as input and output. 31 | 32 | @param model The model to train. Can be nil. 33 | @param input The training input. 34 | @param output The training output. 35 | 36 | @return The trained model (if input != nil, it is the same as the input) 37 | */ 38 | - (YCSupervisedModel *)train:(YCSupervisedModel *)model 39 | input:(YCDataframe *)input 40 | output:(YCDataframe *)output; 41 | 42 | /** 43 | Trains a model using the receiver's training algorithm, and two matrices as input and output. 44 | 45 | @param model The model to train. Can be nil. 46 | @param input The training input. 47 | @param output The training output. 48 | 49 | @return The trained model (if input != nil, it is the same as the input) 50 | */ 51 | - (YCSupervisedModel *)train:(YCSupervisedModel *)model 52 | inputMatrix:(Matrix *)input 53 | outputMatrix:(Matrix *)output; 54 | 55 | /** 56 | Implements the actual training routine. This method should be implemented 57 | when subclassing. 58 | 59 | @param model The model to train. 60 | @param input The training input. 61 | @param output The training output. 62 | */ 63 | - (void)performTrainingModel:(YCSupervisedModel *)model 64 | inputMatrix:(Matrix *)input 65 | outputMatrix:(Matrix *)output; 66 | 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /YCML/YCSupervisedTrainer.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCSupervisedTrainer.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 2/3/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "YCSupervisedTrainer.h" 24 | #import "YCSupervisedModel.h" 25 | #import "YCDataframe.h" 26 | #import "YCDataframe+Matrix.h" 27 | @import YCMatrix; 28 | 29 | @implementation YCSupervisedTrainer 30 | 31 | - (YCSupervisedModel *)train:(YCSupervisedModel *)model 32 | input:(YCDataframe *)input 33 | output:(YCDataframe *)output 34 | { 35 | YCSupervisedModel *theModel = model; 36 | if (!theModel) 37 | { 38 | theModel = [[[[self class] modelClass] alloc] init]; 39 | } 40 | theModel.properties[@"InputMinValues"] = [input stat:@"min"]; 41 | theModel.properties[@"InputMaxValues"] = [input stat:@"max"]; 42 | theModel.properties[@"OutputMinValues"] = [output stat:@"min"]; 43 | theModel.properties[@"OutputMaxValues"] = [output stat:@"max"]; 44 | theModel.properties[@"InputConversionArray"] = [input conversionArray]; 45 | theModel.properties[@"OutputConversionArray"] = [output conversionArray]; 46 | [theModel.trainingSettings addEntriesFromDictionary:self.settings]; 47 | Matrix *inputM = [input getMatrixUsingConversionArray:theModel.properties[@"InputConversionArray"]]; 48 | Matrix *outputM = [output getMatrixUsingConversionArray:theModel.properties[@"OutputConversionArray"]]; 49 | [self train:theModel inputMatrix:inputM outputMatrix:outputM]; 50 | return self.shouldStop ? nil : theModel; 51 | } 52 | 53 | - (YCSupervisedModel *)train:(YCSupervisedModel *)model inputMatrix:(Matrix *)input outputMatrix:(Matrix *)output 54 | { 55 | self.shouldStop = NO; 56 | YCSupervisedModel *theModel = model; 57 | if (!theModel) 58 | { 59 | theModel = [[[[self class] modelClass] alloc] init]; 60 | } 61 | [self performTrainingModel:theModel inputMatrix:input outputMatrix:output]; 62 | return self.shouldStop ? nil : theModel; 63 | } 64 | 65 | - (void)performTrainingModel:(YCSupervisedModel *)model 66 | inputMatrix:(Matrix *)input 67 | outputMatrix:(Matrix *)output 68 | { 69 | @throw [NSInternalInconsistencyException initWithFormat: 70 | @"You must override %@ in subclass %@", NSStringFromSelector(_cmd), [self class]]; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /YCMLTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /YCMLTests/XCTestCase+Dataframe.h: -------------------------------------------------------------------------------- 1 | // 2 | // XCTestCase+Dataframe.h 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 21/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | @class YCSupervisedTrainer, YCDataframe; 25 | 26 | @interface XCTestCase (Dataframe) 27 | 28 | - (void)testWithTrainer:(YCSupervisedTrainer *)trainer 29 | dataset:(NSString *)dataset 30 | dependentVariableLabel:(NSString *)label 31 | rmse:(double)rmse; 32 | 33 | - (YCDataframe *)dataframeWithCSVName:(NSString *)path; 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /YCMLTests/XCTestCase+Dataframe.m: -------------------------------------------------------------------------------- 1 | // 2 | // XCTestCase+Dataframe.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 21/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import "XCTestCase+Dataframe.h" 24 | @import YCML; 25 | @import YCMatrix; 26 | #import "CHCSVParser.h" 27 | 28 | // Convenience logging function (without date/object) 29 | #define CleanLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); 30 | 31 | @implementation XCTestCase (Dataframe) 32 | 33 | #pragma mark - Utility Functions 34 | 35 | - (void)testWithTrainer:(YCSupervisedTrainer *)trainer 36 | dataset:(NSString *)dataset 37 | dependentVariableLabel:(NSString *)label 38 | rmse:(double)rmse 39 | { 40 | YCDataframe *input = [self dataframeWithCSVName:dataset]; 41 | YCDataframe *output = [YCDataframe dataframeWithDictionary:@{label : [input allValuesForAttribute:label]}]; 42 | [input removeAttributeWithIdentifier:label]; 43 | NSDictionary *results = [[YCkFoldValidation validationWithSettings:nil] test:trainer 44 | input:input 45 | output:output]; 46 | double RMSE = [results[@"RMSE"] doubleValue]; 47 | 48 | CleanLog(@"RMSE (CV): %f", RMSE); 49 | XCTAssertLessThan(RMSE, rmse, @"RMSE above threshold"); 50 | } 51 | 52 | - (YCDataframe *)dataframeWithCSVName:(NSString *)path 53 | { 54 | YCDataframe *output = [YCDataframe dataframe]; 55 | NSBundle *bundle = [NSBundle bundleForClass:[self class]]; 56 | NSString *filePath = [bundle pathForResource:path ofType:@"csv"]; 57 | NSString* fileContents = [NSString stringWithContentsOfFile:filePath 58 | encoding:NSUTF8StringEncoding 59 | error:nil]; 60 | NSMutableArray *rows = [[fileContents CSVComponents] mutableCopy]; 61 | 62 | NSArray *labels = rows[0]; 63 | [rows removeObjectAtIndex:0]; 64 | 65 | for (NSArray *sampleData in rows) 66 | { 67 | // The provided dataframes should be made up of NSNumbers. 68 | // Thus we need t convert anything coming from the CSV file to NSNumber. 69 | NSMutableArray *dataAsNumbers = [NSMutableArray array]; 70 | for (id record in sampleData) 71 | { 72 | [dataAsNumbers addObject:@([record doubleValue])]; 73 | } 74 | [output addSampleWithData:[NSDictionary dictionaryWithObjects:dataAsNumbers forKeys:labels]]; 75 | } 76 | 77 | return output; 78 | } 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /YCMLTests/YCMLMetricsTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TCMLMetricsTests.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 6/10/15. 6 | // Copyright (c) 2015 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | @import XCTest; 24 | @import YCML; 25 | @import YCMatrix; 26 | 27 | // Convenience logging function (without date/object) 28 | #define CleanLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); 29 | 30 | @interface YCMLMetricsTests : XCTestCase 31 | 32 | @end 33 | 34 | @implementation YCMLMetricsTests 35 | 36 | - (void)testRSquared 37 | { 38 | Matrix *groundTruth = [Matrix matrixFromNSArray:@[@10, @0, @5, @20, @3, @17, @12, @8, @0, @15] rows:1 columns:10]; 39 | double r2 = RSquared(groundTruth, groundTruth); 40 | XCTAssert(r2 == 1, "Identical Predictor R2 != 1"); 41 | CleanLog(@"Identical Predictor: %f", r2); 42 | 43 | Matrix *prediction = [Matrix matrixFromNSArray:@[@9, @1, @5, @18, @3, @13, @13, @9, @1, @14] rows:1 columns:10]; 44 | r2 = RSquared(groundTruth, prediction); 45 | XCTAssert(r2 > 0.9, "Good Predictor R2 < 0.9"); 46 | CleanLog(@"Good Predictor: %f", r2); 47 | 48 | prediction = [Matrix matrixOfRows:1 columns:10 value:[groundTruth meansOfRows]->matrix[0]]; 49 | r2 = RSquared(groundTruth, prediction); 50 | XCTAssert(r2 == 0, "Mean Predictor R2 != 0"); 51 | CleanLog(@"Mean Predictor: %f", r2); 52 | 53 | prediction = [Matrix uniformRandomRows:1 columns:10 domain:YCMakeDomain(0, 20)]; 54 | r2 = RSquared(groundTruth, prediction); 55 | CleanLog(@"Uniform Random Predictor: %f", r2); 56 | 57 | prediction = [Matrix matrixFromNSArray:@[@19, @0, @15, @4, @3, @2, @19, @1, @7, @12] rows:1 columns:10]; 58 | r2 = RSquared(groundTruth, prediction); 59 | CleanLog(@"Bad Predictor: %f", r2); 60 | 61 | Matrix *rand1 = [Matrix uniformRandomRows:1 columns:10 domain:YCMakeDomain(0, 1)]; 62 | Matrix *rand2 = [Matrix uniformRandomRows:1 columns:10 domain:YCMakeDomain(0, 1)]; 63 | 64 | double r2_2 = RSquared(rand1, rand2); 65 | CleanLog(@"Uni Random Ground Truth / Uni Random Predictor: %f", r2_2); 66 | } 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /YCMLTests/YCMLModelExportTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // YCMLModelExportTests.m 3 | // YCML 4 | // 5 | // Created by Ioannis (Yannis) Chatzikonstantinou on 20/4/16. 6 | // Copyright © 2016 (Yannis) Chatzikonstantinou. All rights reserved. 7 | // 8 | // This file is part of YCML. 9 | // 10 | // YCML is free software: you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation, either version 3 of the License, or 13 | // (at your option) any later version. 14 | // 15 | // YCML is distributed in the hope that it will be useful, 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | // GNU General Public License for more details. 19 | // 20 | // You should have received a copy of the GNU General Public License 21 | // along with YCML. If not, see . 22 | 23 | #import 24 | @import YCML; 25 | #import "XCTestCase+Dataframe.h" 26 | 27 | @interface YCMLModelExportTests : XCTestCase 28 | 29 | @end 30 | 31 | @implementation YCMLModelExportTests 32 | 33 | - (void)testFFN_TextExport 34 | { 35 | YCFullyConnectedLayer *hl = [YCSigmoidLayer layerWithInputSize:13 outputSize:2]; 36 | YCFullyConnectedLayer *ol = [YCSigmoidLayer layerWithInputSize:2 outputSize:1]; 37 | 38 | YCFFN *model = [[YCFFN alloc] init]; 39 | 40 | model.layers = @[hl, ol]; 41 | 42 | YCRpropTrainer *trainer = [YCRpropTrainer trainer]; 43 | 44 | YCDataframe *input = [self dataframeWithCSVName:@"housing"]; 45 | YCDataframe *output = [YCDataframe dataframeWithDictionary: 46 | @{@"MedV" : [input allValuesForAttribute:@"MedV"]}]; 47 | [input removeAttributeWithIdentifier:@"MedV"]; 48 | 49 | model = (YCFFN *)[trainer train:model input:input output:output]; 50 | 51 | NSString *text = [model textDescription]; 52 | 53 | NSLog(@"%@", text); 54 | } 55 | 56 | #if (TARGET_OS_MAC && !(TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)) 57 | - (void)testFFN_PMMLExport 58 | { 59 | YCFullyConnectedLayer *hl = [YCSigmoidLayer layerWithInputSize:13 outputSize:2]; 60 | YCFullyConnectedLayer *ol = [YCSigmoidLayer layerWithInputSize:2 outputSize:1]; 61 | 62 | YCFFN *model = [[YCFFN alloc] init]; 63 | 64 | model.layers = @[hl, ol]; 65 | 66 | YCRpropTrainer *trainer = [YCRpropTrainer trainer]; 67 | 68 | YCDataframe *input = [self dataframeWithCSVName:@"housing"]; 69 | YCDataframe *output = [YCDataframe dataframeWithDictionary: 70 | @{@"MedV" : [input allValuesForAttribute:@"MedV"]}]; 71 | [input removeAttributeWithIdentifier:@"MedV"]; 72 | 73 | model = (YCFFN *)[trainer train:model input:input output:output]; 74 | 75 | NSString *PMML = [model PMMLString]; 76 | 77 | NSLog(@"%@", PMML); 78 | } 79 | #endif 80 | 81 | @end 82 | -------------------------------------------------------------------------------- /YCMatrix/Constants.h: -------------------------------------------------------------------------------- 1 | // 2 | // Constants.h 3 | // YCMatrix 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 25/9/14. 6 | // Copyright (c) 2014 Ioannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #ifndef YCMatrix_Constants_h 10 | #define YCMatrix_Constants_h 11 | 12 | #define ARC4RANDOM_MAX 0x100000000 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /YCMatrix/HaltonInterface.h: -------------------------------------------------------------------------------- 1 | // 2 | // HaltonInterface.h 3 | // YCMatrix 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 21/12/15. 6 | // Copyright © 2015 Ioannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import 10 | @class Matrix; 11 | 12 | @interface HaltonInterface : NSObject 13 | 14 | + (Matrix *)sampleWithDimension:(int)dimension count:(int)count; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /YCMatrix/HaltonInterface.mm: -------------------------------------------------------------------------------- 1 | // 2 | // HaltonInterface.m 3 | // YCMatrix 4 | // 5 | // Created by Ioannis Chatzikonstantinou on 21/12/15. 6 | // Copyright © 2015 Ioannis Chatzikonstantinou. All rights reserved. 7 | // 8 | 9 | #import "HaltonInterface.h" 10 | #import "Matrix.h" 11 | #import "halton_sampler.h" 12 | 13 | @implementation HaltonInterface 14 | 15 | + (Matrix *)sampleWithDimension:(int)dimension count:(int)count 16 | { 17 | Halton_sampler halton_sampler; 18 | halton_sampler.init_faure(); 19 | 20 | Matrix *result = [Matrix matrixOfRows:dimension columns:count]; 21 | 22 | for (unsigned i = 0; i < dimension; ++i) // Iterate over rows. 23 | { 24 | for (unsigned j = 0; j < count; ++j) // Iterate over columns. 25 | { 26 | [result i:i j:j set:halton_sampler.sample(i, j)]; 27 | } 28 | } 29 | return result; 30 | } 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /YCMatrix/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSHumanReadableCopyright 24 | Copyright © 2013 - 2015 Yannis Chatzikonstantinou. All rights reserved. 25 | NSPrincipalClass 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /YCMatrix/NSArray+Matrix.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Matrix.h 3 | // 4 | // YCMatrix 5 | // 6 | // Copyright (c) 2013 - 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // http://yconst.com 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | 27 | #import 28 | #import "Matrix.h" 29 | 30 | @interface NSArray (Matrix) 31 | 32 | /** 33 | Returns a matrix that contains the sum of all matrices 34 | contained in the receiver. 35 | */ 36 | @property (readonly) Matrix *matrixSum; 37 | 38 | /** 39 | Returna a matrix that contains the element-wise product of 40 | all matrices contained in the receiver. 41 | */ 42 | @property (readonly) Matrix *matrixProduct; 43 | 44 | /** 45 | Returns a matrix that contains the element-wise mean values of 46 | all matrices contained in the receiver. 47 | */ 48 | @property (readonly) Matrix *matrixMean; 49 | 50 | /** 51 | Returns a matrix that contains the element-wise maximum values of 52 | all matrices contained in the receiver. 53 | */ 54 | @property (readonly) Matrix *matrixMax; 55 | 56 | /** 57 | Returns a matrix that contains the element-wise minimum values of 58 | all matrices contained in the receiver. 59 | */ 60 | @property (readonly) Matrix *matrixMin; 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /YCMatrix/NSArray+Matrix.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Matrix.m 3 | // 4 | // YCMatrix 5 | // 6 | // Copyright (c) 2013 - 2016 Ioannis (Yannis) Chatzikonstantinou. All rights reserved. 7 | // http://yconst.com 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | 27 | #import "NSArray+Matrix.h" 28 | 29 | @implementation NSArray (Matrix) 30 | 31 | - (Matrix *)matrixSum 32 | { 33 | Matrix *result; 34 | for (Matrix *m in self) 35 | { 36 | NSAssert([m isKindOfClass:[Matrix class]], @"Array element is not a matrix"); 37 | if (!result) 38 | { 39 | result = [m copy]; 40 | } 41 | else 42 | { 43 | [result add:m]; 44 | } 45 | } 46 | return result; 47 | } 48 | 49 | - (Matrix *)matrixMean 50 | { 51 | Matrix *result = [self matrixSum]; 52 | [result multiplyWithScalar:1.0 / (double)self.count]; 53 | return result; 54 | } 55 | 56 | - (Matrix *)matrixProduct 57 | { 58 | Matrix *result; 59 | for (Matrix *m in self) 60 | { 61 | NSAssert([m isKindOfClass:[Matrix class]], @"Array element is not a matrix"); 62 | if (!result) 63 | { 64 | result = [m copy]; 65 | } 66 | else 67 | { 68 | [result elementWiseMultiply:m]; 69 | } 70 | } 71 | return result; 72 | } 73 | 74 | - (Matrix *)matrixMax 75 | { 76 | Matrix *result; 77 | for (Matrix *m in self) 78 | { 79 | NSAssert([m isKindOfClass:[Matrix class]], @"Array element is not a matrix"); 80 | if (!result) 81 | { 82 | result = [m copy]; 83 | } 84 | else 85 | { 86 | for (int i=0, n=(int)result.count; i 28 | 29 | #import "Matrix+Advanced.h" 30 | #import "Matrix+Manipulate.h" 31 | #import "Matrix+Map.h" 32 | #import "NSArray+Matrix.h" --------------------------------------------------------------------------------