|
| 1 | +#import "T4ClassAbsCriterion.h" |
| 2 | + |
| 3 | +@implementation T4ClassAbsCriterion |
| 4 | + |
| 5 | +-initWithDatasetClassFormat: (T4ClassFormat*)aClassFormat inputClassFormat: (T4ClassFormat*)anotherClassFormat |
| 6 | +{ |
| 7 | + if( (self = [super initWithNumberOfInputs: [anotherClassFormat encodingSize]]) ) |
| 8 | + { |
| 9 | + datasetClassFormat = [aClassFormat retainAndKeepWithAllocator: allocator]; |
| 10 | + inputClassFormat = [anotherClassFormat retainAndKeepWithAllocator: allocator]; |
| 11 | + [self setAveragesWithNumberOfRows: YES]; |
| 12 | + [self setAveragesWithNumberOfColumns: YES]; |
| 13 | + } |
| 14 | + |
| 15 | + return self; |
| 16 | +} |
| 17 | + |
| 18 | +-(real)forwardExampleAtIndex: (int)anIndex inputs: (T4Matrix*)someInputs |
| 19 | +{ |
| 20 | + T4Matrix *targets = [[dataset objectAtIndex: anIndex] objectAtIndex: 1]; |
| 21 | + int numColumns = [someInputs numberOfColumns]; |
| 22 | + int c, r; |
| 23 | + |
| 24 | + output = 0; |
| 25 | + for(c = 0; c < numColumns; c++) |
| 26 | + { |
| 27 | + real *targetColumn = [inputClassFormat encodingForClass: [datasetClassFormat classFromRealArray: [targets columnAtIndex: c]]]; |
| 28 | + real *inputColumn = [someInputs columnAtIndex: c]; |
| 29 | + |
| 30 | + for(r = 0; r < numInputs; r++) |
| 31 | + output += fabs(targetColumn[r] - inputColumn[r]); |
| 32 | + } |
| 33 | + |
| 34 | + if(averageWithNumberOfRows) |
| 35 | + output /= (real)numInputs; |
| 36 | + |
| 37 | + if(averageWithNumberOfColumns) |
| 38 | + output /= (real)numColumns; |
| 39 | + |
| 40 | + return output; |
| 41 | +} |
| 42 | + |
| 43 | +-(T4Matrix*)backwardExampleAtIndex: (int)anIndex inputs: (T4Matrix*)someInputs |
| 44 | +{ |
| 45 | + T4Matrix *targets = [[dataset objectAtIndex: anIndex] objectAtIndex: 1]; |
| 46 | + int numColumns = [someInputs numberOfColumns]; |
| 47 | + real norm = 1.; |
| 48 | + int c, r; |
| 49 | + |
| 50 | + [gradInputs resizeWithNumberOfColumns: numColumns]; |
| 51 | + |
| 52 | + if(averageWithNumberOfRows) |
| 53 | + norm /= (real)numInputs; |
| 54 | + |
| 55 | + if(averageWithNumberOfColumns) |
| 56 | + norm /= (real)numColumns; |
| 57 | + |
| 58 | + for(c = 0; c < numColumns; c++) |
| 59 | + { |
| 60 | + real *targetColumn = [inputClassFormat encodingForClass: [datasetClassFormat classFromRealArray: [targets firstColumn]]]; |
| 61 | + real *inputColumn = [someInputs columnAtIndex: c]; |
| 62 | + real *gradInputColumn = [gradInputs columnAtIndex: c]; |
| 63 | + |
| 64 | + for(r = 0; r < numInputs; r++) |
| 65 | + { |
| 66 | + real z = targetColumn[r] - inputColumn[r]; |
| 67 | + if(z > 0) |
| 68 | + gradInputColumn[r] = -norm; |
| 69 | + else |
| 70 | + gradInputColumn[r] = norm; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return gradInputs; |
| 75 | +} |
| 76 | + |
| 77 | +-setAveragesWithNumberOfRows: (BOOL)aFlag |
| 78 | +{ |
| 79 | + averageWithNumberOfRows = aFlag; |
| 80 | + return self; |
| 81 | +} |
| 82 | + |
| 83 | +-setAveragesWithNumberOfColumns: (BOOL)aFlag |
| 84 | +{ |
| 85 | + averageWithNumberOfColumns = aFlag; |
| 86 | + return self; |
| 87 | +} |
| 88 | + |
| 89 | +-initWithCoder: (NSCoder*)aCoder |
| 90 | +{ |
| 91 | + self = [super initWithCoder: aCoder]; |
| 92 | + |
| 93 | + datasetClassFormat = [[aCoder decodeObject] retainAndKeepWithAllocator: allocator]; |
| 94 | + inputClassFormat = [[aCoder decodeObject] retainAndKeepWithAllocator: allocator]; |
| 95 | + [aCoder decodeValueOfObjCType: @encode(BOOL) at: &averageWithNumberOfRows]; |
| 96 | + [aCoder decodeValueOfObjCType: @encode(BOOL) at: &averageWithNumberOfColumns]; |
| 97 | + |
| 98 | + return self; |
| 99 | +} |
| 100 | + |
| 101 | +-(void)encodeWithCoder: (NSCoder*)aCoder |
| 102 | +{ |
| 103 | + [super encodeWithCoder: aCoder]; |
| 104 | + |
| 105 | + [aCoder encodeObject: datasetClassFormat]; |
| 106 | + [aCoder encodeObject: inputClassFormat]; |
| 107 | + [aCoder encodeValueOfObjCType: @encode(BOOL) at: &averageWithNumberOfRows]; |
| 108 | + [aCoder encodeValueOfObjCType: @encode(BOOL) at: &averageWithNumberOfColumns]; |
| 109 | +} |
| 110 | + |
| 111 | +@end |
0 commit comments