Browse Source

Fix Array reduce initialValue

Lutz Roeder 4 years ago
parent
commit
b1d4c7d558
11 changed files with 15 additions and 15 deletions
  1. 1 1
      source/darknet.js
  2. 1 1
      source/keras.js
  3. 1 1
      source/mxnet.js
  4. 1 1
      source/numpy.js
  5. 1 1
      source/paddle.js
  6. 3 3
      source/python.js
  7. 2 2
      source/pytorch.js
  8. 1 1
      source/rknn.js
  9. 1 1
      source/tf.js
  10. 1 1
      source/tflite.js
  11. 2 2
      source/view.js

+ 1 - 1
source/darknet.js

@@ -161,7 +161,7 @@ darknet.Graph = class {
         };
 
         const load_weights = (name, shape, visible) => {
-            const data = weights ? weights.read(4 * shape.reduce((a, b) => a * b)) : null;
+            const data = weights ? weights.read(4 * shape.reduce((a, b) => a * b, 1)) : null;
             const type = new darknet.TensorType('float32', make_shape(shape, 'load_weights'));
             const initializer = new darknet.Tensor(type, data);
             const argument = new darknet.Argument('', null, initializer);

+ 1 - 1
source/keras.js

@@ -252,7 +252,7 @@ keras.ModelFactory = class {
                                 throw new keras.Error("Unknown weight data type size '" + dtype + "'.");
                             }
                             const itemsize = dtype_size_map.get(dtype);
-                            const size = weight.shape.length > 0 ? weight.shape.reduce((a, b) => a * b) : 1;
+                            const size = weight.shape.reduce((a, b) => a * b, 1);
                             const length = itemsize * size;
                             const data = buffer ? buffer.slice(offset, offset + length) : null;
                             weights.add(weight.identifier, new keras.Tensor(weight.name, weight.shape, dtype, weight.quantization, true, data));

+ 1 - 1
source/mxnet.js

@@ -1153,7 +1153,7 @@ ndarray.Shape = class {
     }
 
     size() {
-        return this._dimensions.reduce((a, b) => a * b);
+        return this._dimensions.reduce((a, b) => a * b, 1);
     }
 };
 

+ 1 - 1
source/numpy.js

@@ -133,7 +133,7 @@ numpy.Array = class {
         header += ' '.repeat(16 - ((header.length + 2 + 8 + 1) & 0x0f)) + '\n';
         writer.string(header);
 
-        const size = context.itemSize * this._shape.reduce((a, b) => a * b);
+        const size = context.itemSize * this._shape.reduce((a, b) => a * b, 1);
         context.data = new Uint8Array(size);
         context.view = new DataView(context.data.buffer, context.data.byteOffset, size);
         numpy.Array._encodeDimension(context, this._data, 0);

+ 1 - 1
source/paddle.js

@@ -513,7 +513,7 @@ paddle.Tensor = class {
                 const length = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength).getUint32(0, true);
                 const reader = protobuf.Reader.create(stream.read(length));
                 const tensorDesc = paddle.proto.VarType.TensorDesc.decode(reader);
-                const size = tensorDesc.dims.reduce((a, b) => a * b.toNumber());
+                const size = tensorDesc.dims.reduce((a, b) => a * b.toNumber(), 1);
                 let itemsize = 0;
                 switch (tensorDesc.data_type) {
                     case paddle.proto.VarType.Type.FP32: itemsize = 4; break;

+ 3 - 3
source/python.js

@@ -1803,7 +1803,7 @@ python.Execution = class {
                     return unpickler.load((name, args) => self.invoke(name, args), null);
                 }
                 else {
-                    const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b);
+                    const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1);
                     this.data = unpickler.read(size);
                 }
                 return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]);
@@ -1835,7 +1835,7 @@ python.Execution = class {
                 this.data = state[4];
             }
             __read__(unpickler) {
-                const dims = this.shape && this.shape.length > 0 ? this.shape.reduce((a, b) => a * b) : 1;
+                const dims = (this.shape || []).reduce((a, b) => a * b, 1);
                 const size = this.dtype.itemsize * dims;
                 if (typeof this.data == 'string') {
                     this.data = unpickler.unescape(this.data, size);
@@ -1891,7 +1891,7 @@ python.Execution = class {
                     return unpickler.load((name, args) => self.invoke(name, args), null);
                 }
                 else {
-                    const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b);
+                    const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1);
                     this.data = unpickler.read(size);
                 }
                 return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]);

+ 2 - 2
source/pytorch.js

@@ -1453,7 +1453,7 @@ pytorch.Execution = class extends python.Execution {
             if (tensor && tensor.size) {
                 const size = tensor.size();
                 if (size) {
-                    return size.length === 0 ? 1 : size.reduce((a, b) => a * b);
+                    return size.reduce((a, b) => a * b, 1);
                 }
             }
             return NaN;
@@ -2329,7 +2329,7 @@ pytorch.Container.Zip = class {
                         const type = tensorTypeMap.get(constant.dataType);
                         const shape = constant.dims ? constant.dims.map((dim) => parseInt(dim, 10)) : null;
                         const storage_type = this.execution.type('torch.' + type + 'Storage');
-                        const size = shape && shape.length > 0 ? shape.reduce((a, b) => a * b) : 1;
+                        const size = (shape || []).reduce((a, b) => a * b, 1);
                         const offset = parseInt(constant.offset, 10) || 0;
                         const storage = new storage_type([ size ]);
                         const itemsize = storage.dtype.itemsize();

+ 1 - 1
source/rknn.js

@@ -312,7 +312,7 @@ rknn.Tensor = class {
             case 'float32': size = 4; break;
         }
         const shape = type.shape.dimensions;
-        size = size * (shape.length === 0 ? 1 : shape.reduce((a, b) => a * b));
+        size = size * shape.reduce((a, b) => a * b, 1);
         if (size > 0) {
             this._data = weights.slice(offset, offset + size);
         }

+ 1 - 1
source/tf.js

@@ -267,7 +267,7 @@ tf.ModelFactory = class {
                                     throw new tf.Error("Unknown weight data type size '" + dtype + "'.");
                                 }
                                 const itemsize = dtype_size_map.get(dtype);
-                                const size = weight.shape.length > 0 ? weight.shape.reduce((a, b) => a * b) : 1;
+                                const size = weight.shape.reduce((a, b) => a * b, 1);
                                 const length = itemsize * size;
                                 const tensor_content = buffer ? buffer.slice(offset, offset + length) : null;
                                 offset += length;

+ 1 - 1
source/tflite.js

@@ -643,7 +643,7 @@ tflite.Tensor = class {
             if (!itemsize.has(dataType)) {
                 throw new tflite.Error("Tensor data type '" + this.type.dataType + "' is not implemented.");
             }
-            const size = shape.length > 0 ? shape.reduce((a, b) => a * b) : 1;
+            const size = shape.reduce((a, b) => a * b, 1);
             if (this._data.length < itemsize.get(dataType) * size) {
                 context.state = "Invalid tensor data size.";
                 return context;

+ 2 - 2
source/view.js

@@ -694,7 +694,7 @@ view.View = class {
                                 let x = xs[0];
                                 const y = ys[0];
                                 if (ys.every(y => y === ys[0])) {
-                                    x = xs.reduce((a,b) => { return a + b; }) / xs.length;
+                                    x = xs.reduce((a, b) => a + b, 0) / xs.length;
                                 }
                                 const sx = (svgSize.width / (this._showHorizontal ? 4 : 2)) - x;
                                 const sy = (svgSize.height / (this._showHorizontal ? 2 : 4)) - y;
@@ -737,7 +737,7 @@ view.View = class {
                                 let x = xs[0];
                                 const y = ys[0];
                                 if (ys.every(y => y === ys[0])) {
-                                    x = xs.reduce((a,b) => { return a + b; }) / xs.length;
+                                    x = xs.reduce((a, b) => a + b, 0) / xs.length;
                                 }
                                 // const canvasRect = graphElement.getBoundingClientRect();
                                 const graphRect = graphElement.getBoundingClientRect();