Skip to content

Commit 52954d7

Browse files
author
Ronan Collobert
committed
Matrix bug corrected in encodeWithCoder:
git-archimport-id: [email protected]/torch--devel--1.0--patch-72
1 parent 2a08603 commit 52954d7

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

base/T4Matrix.m

+9-2
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,8 @@ -(NSString*)description
811811

812812
-initWithCoder: (NSCoder*)aCoder
813813
{
814+
int c;
815+
814816
self = [super initWithCoder: aCoder];
815817

816818
[aCoder decodeValueOfObjCType: @encode(int) at: &numRows];
@@ -820,17 +822,22 @@ -(NSString*)description
820822
dataSize = numRows*numColumns;
821823
data = [allocator allocRealArrayWithCapacity: dataSize];
822824

823-
[aCoder decodeArrayOfObjCType: @encode(real) count: dataSize at: data];
825+
for(c = 0; c < numColumns; c++)
826+
[aCoder decodeArrayOfObjCType: @encode(real) count: numRows at: data+c*stride];
824827

825828
return self;
826829
}
827830

828831
-(void)encodeWithCoder: (NSCoder *)aCoder
829832
{
833+
int c;
834+
830835
[super encodeWithCoder: aCoder];
831836
[aCoder encodeValueOfObjCType: @encode(int) at: &numRows];
832837
[aCoder encodeValueOfObjCType: @encode(int) at: &numColumns];
833-
[aCoder encodeArrayOfObjCType: @encode(real) count: numRows*numColumns at: data];
838+
839+
for(c = 0; c < numColumns; c++)
840+
[aCoder encodeArrayOfObjCType: @encode(real) count: numRows at: data+c*stride];
834841
}
835842

836843
@end

examples/discriminatives/lenet1.m

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ int main( int argc, char *argv[] )
3333
NSString *validFileName;
3434
NSString *modelFileName;
3535
int seed;
36+
int maxLoad, maxLoadValid;
3637

3738
int inputWidth, inputHeight;
3839
int convNumPlanes, convKW, convKH, convDW, convDH;
@@ -71,12 +72,17 @@ int main( int argc, char *argv[] )
7172
[cmdLine addIntOption: @"-seed" at: &seed default: 5776 help: @"the random seed"];
7273
[cmdLine addStringOption: @"-valid" at: &validFileName default: @"" help: @"validation file"];
7374
[cmdLine addStringOption: @"-save" at: &modelFileName default: @"" help: @"save into a model file"];
75+
[cmdLine addIntOption: @"-load" at: &maxLoad default: -1 help: @"max number of examples to load"];
76+
[cmdLine addIntOption: @"-loadValid" at: &maxLoadValid default: -1 help: @"max number of examples to load for validation"];
7477

7578
[cmdLine addMasterSwitch: @"--test"];
7679
[cmdLine addStringArgument: @"model" at: &modelFileName help: @"model file"];
7780
[cmdLine addStringArgument: @"file" at: &trainFileName help: @"testing file"];
7881
[cmdLine addText: @"\nTesting options:\n"];
7982
[cmdLine addIntOption: @"-class" at: &trainingClass default: -1 help: @"class to train against the others"];
83+
84+
[cmdLine addText: @"\nMisc options:\n"];
85+
[cmdLine addIntOption: @"-load" at: &maxLoad default: -1 help: @"max number of examples to load"];
8086

8187
[cmdLine addText: @"\n"];
8288
int cmdLineMode = [cmdLine read];
@@ -101,7 +107,8 @@ int main( int argc, char *argv[] )
101107
T4ExampleDealer *dealer = [[[T4ExampleDealer alloc] init] autorelease];
102108
[T4DiskFile setLittleEndianEncoding];
103109
[loader setEnforcesFloatEncoding: YES];
104-
110+
[loader setMaxNumberOfColumns: maxLoad];
111+
105112
NSArray *examples = [dealer columnExamplesWithMatrix: [loader loadMatrixAtPath: trainFileName] elementSize: -1 elementSize: 1];
106113

107114
T4StandardNormalizer *normalizer;
@@ -118,6 +125,7 @@ int main( int argc, char *argv[] )
118125
{
119126
if(![validFileName isEqualToString: @""])
120127
{
128+
[loader setMaxNumberOfColumns: maxLoadValid];
121129
validExamples = [dealer columnExamplesWithMatrix: [loader loadMatrixAtPath: validFileName] elementSize: -1 elementSize: 1];
122130
[normalizer normalizeDataset: validExamples];
123131
}
@@ -244,7 +252,7 @@ int main( int argc, char *argv[] )
244252
archiver = [[[NSArchiver alloc] initForWritingWithMutableData: data] autorelease];
245253
[archiver encodeObject: normalizer];
246254
[archiver encodeObject: mlp];
247-
[data writeToFile: modelFileName atomically: YES];
255+
[data writeToFile: modelFileName atomically: NO];
248256
}
249257
}
250258
else

gradients/T4GradientMachine.m

+2
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ -(void)encodeWithCoder: (NSCoder*)aCoder
425425
[aCoder encodeValueOfObjCType: @encode(int) at: &numOutputs];
426426
[aCoder encodeObject: parameters];
427427
[aCoder encodeObject: gradParameters];
428+
if(partialBackpropagation) // otherwise valgrind yells
429+
[gradInputs zero];
428430
[aCoder encodeObject: gradInputs];
429431
[aCoder encodeObject: outputs];
430432

0 commit comments

Comments
 (0)