Skip to content
This repository has been archived by the owner on May 30, 2019. It is now read-only.

Fix assertEqual (add to util.ts) #423

Open
ry opened this issue Mar 22, 2018 · 0 comments
Open

Fix assertEqual (add to util.ts) #423

ry opened this issue Mar 22, 2018 · 0 comments

Comments

@ry
Copy link
Contributor

ry commented Mar 22, 2018

should not depend on Tensor, so it can be used in util.ts

should be share code with toEqual in the jasmine_shim:

toEqual(value: any, expected: any): boolean {
const seen = new Map();
return (function compare(a, b) {
if (a === b) {
return true;
}
if (typeof a === "number" && typeof b === "number" &&
isNaN(a) && isNaN(b)) {
return true;
}
if (a && typeof a === "object" && b && typeof b === "object") {
if (seen.get(a) === b) {
return true;
}
for (const key in { ...a, ...b }) {
if (!compare(a[key], b[key])) {
return false;
}
}
seen.set(a, b);
return true;
}
return false;
})(value, expected);

however the assertEqual in tensor_util should continue to work with Tensors and also share code

export function assertEqual(actual: TensorLike, expected: number | boolean,
msg = null) {
actual = toNumber(actual);
if (!msg) { msg = `actual: ${actual} expected: ${expected}`; }
assert(actual === expected, msg);
}

Also note there exists this lonely function objectsEqual(), which should be used or replaced.

propel/src/util.ts

Lines 123 to 134 in 8b570f0

export function objectsEqual(a: any, b: any): boolean {
const aProps = Object.getOwnPropertyNames(a);
const bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) return false;
for (let i = 0; i < aProps.length; i++) {
const k = aProps[i];
if (a[k] !== b[k]) {
return false;
}
}
return true;
}

Should also modify our many tests that use assert when they should use assertEqual:

~/src/propel> grep -R assert website/ src | grep "==="
website//nb_transpiler_test.ts:    assert(transpiled === expected,
website//nb_transpiler_test.ts:    assert(typeof fn === "function");
website//nb_transpiler_test.ts:    assert(actual === expected,
website//nb_transpiler_test.ts:  assert(checkEntryPointCalls === 4,
website//nb_transpiler.ts:    assert(functionExpression.type === "FunctionExpression");
website//nb.ts:      assert(pres.length === 1);
website//rpc_test.ts:    assert((await rpc2.call("getNumber")) === 3.14);
website//rpc_test.ts:    assert((await rpc2.call("getString")) === "hello");
website//rpc_test.ts:    assert((await rpc1.call("addTwo", 0)) === 42);
website//rpc_test.ts:    assert((await rpc2.call("addOne", 0)) === 42);
website//nb_test.ts:  assert(c.className === "notebook");
website//nb_test.ts:  assert(1 === blurbs.length);
website//nb_test.ts:  assert(1 === title.length);
website//nb_test.ts:  assert("Sample Notebook" === title[0].innerHTML);
website//nb_test.ts:  assert(0 === editButtons.length);
website//nb_test.ts:  assert(0 === clones.length);
website//nb_test.ts:  assert(db.defaultDoc.title === title.innerText);
website//nb_test.ts:  assert(titleInput.value === db.defaultDoc.title);
website//nb_test.ts:  assert("New Title" === title.innerText);
website//nb_test.ts:  assert(deleteButtons.length === numCells);
website//nb_test.ts:  assert(cells.length === numCells);
website//nb_test.ts:  assert(cells.length === 1);
src/mnist.ts:    assert(littleEndianToBig(i32[i++]) === 28);
src/mnist.ts:    assert(littleEndianToBig(i32[i++]) === 28);
src/npy.ts:  util.assert((unpaddedLength + padding.length) % 16 === 0);
src/npy.ts:  util.assert(data.length === numEls(tensor.shape));
src/npy.ts:    util.assert(bytesLeft === size * 8);
src/npy.ts:    util.assert(bytesLeft === size * 4);
src/npy.ts:    util.assert(bytesLeft === size * 8);
src/backprop.ts:    assert(grads.length === order.length);
src/backprop.ts:    assert(op.outputIds.length === 1);
src/mnist_test.ts:  // assert(labels.dtype === "uint8");
src/api_test.ts:  assert(t.dtype === "int32");
src/api_test.ts:  assert(t2.dtype === "int32");
src/api_test.ts:  assert(a.device === device);
src/api_test.ts:  assert(ones.device === device);
src/api_test.ts:  assert(zeros.device === device);
src/api_test.ts:  assert(r.dtype === "bool");
src/api_test.ts:  assert(r.dtype === "bool");
src/api_test.ts:  assert(r.dtype === "bool");
src/api_test.ts:  assert(r.dtype === "bool");
src/api_test.ts:  assert(r.dtype === "bool");
src/api_test.ts:  assert(ga.device === device);
src/api_test.ts:  assert(z1.dtype === "float32");
src/api_test.ts:  assert(z2.dtype === "int32");
src/api_test.ts:  assert(o1.dtype === "float32");
src/api_test.ts:  assert(o2.dtype === "int32");
src/api_test.ts:  assert(gab.length === 2);
src/api_test.ts:  assert(gab.length === 2);
src/api_test.ts:  assert(gab.length === 2);
src/api_test.ts:  assert(gab.length === 2);
src/api_test.ts:  assert(s2.dtype === "int32");
src/api_test.ts:  assert(a.dtype === "uint8");
src/api_test.ts:  assert(t.device === "GPU:0");
src/api_test.ts:  assert(s.device === "GPU:0");
src/api_test.ts:  assert(r.device === "GPU:0");
src/api_test.ts:  assert(rCpu.device === "CPU:0");
src/api_test.ts:    assert("[ 1.,  2.,  3.]" === actual);
src/api_test.ts:  assert(ui8.dtype === "uint8");
src/api_test.ts:  assert(i32.dtype === "int32");
src/api_test.ts:  assert(f32.dtype === "float32");
src/api_test.ts:  assert(api.range(20).size === 20);
src/api_test.ts:  assert(api.range(20).reshape([4, 5]).size === 20);
src/layers.ts:  assert(x.rank === 4);
src/layers.ts:  assert(x.rank === 4);
src/disk_experiment_test.ts:  assert(fs.readdirSync(expDir).length === 0);
src/disk_experiment_test.ts:  assert(checkpoints.length === 0);
src/disk_experiment_test.ts:  assert(exp.params.length === 0);
src/disk_experiment_test.ts:  assert(checkpointDirs.length === 1);
src/disk_experiment_test.ts:  assert(exp_.params.length === 1);
src/npy_test.ts:  util.assert(t.dtype === "float32");
src/npy_test.ts:  util.assert(t.dtype === "float32");
src/npy_test.ts:  util.assert(t.dtype === "int32");
src/npy_test.ts:  util.assert(t.dtype === "float32");
src/disk_experiment.ts:      assert(this.step_ === latestCheckpoint);
src/im_test.ts:  assert(data[0] === 49);
src/im_test.ts:  assert(data[4 * 34] === 41);
src/im_test.ts:  assert(data[4 * 3254] === 192);
src/im_test.ts:  assert(data.length === 64 * 64);
src/im_test.ts:  assert(rawImage.width === 64);
src/im_test.ts:  assert(rawImage.height === 64);
src/im_test.ts:  assert(rawImage.data.length === 64 * 64 * 4);
src/im_test.ts:  assert(rawImage.width === 64);
src/im_test.ts:  assert(rawImage.height === 64);
src/im_test.ts:  assert(rawImage.data.length === 64 * 64 * 4);
src/im_test.ts:  assert(rawImage.width === 64);
src/im_test.ts:  assert(rawImage.height === 64);
src/im_test.ts:  assert(rawImage.data.length === 64 * 64 * 4);
src/experiment.ts:      assert(loss.rank === 0);
src/ops.ts:  assert(globalSavedForBackward === null);
src/ops.ts:  assert(x.shape.length === 2);
src/ops.ts:  assert(x.shape.length === 2);
src/api.ts:  assert(input.dtype === "float32");
src/api.ts:  assert(filter.dtype === "float32");
src/api.ts:  assert(input.rank === 4);
src/api.ts:  assert(filter.rank === 4);
src/tf_binding_test.ts:  assert(binding.getDevice(r) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDevice(a) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDevice(b) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDevice(r) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDevice(a) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDevice(b) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDType(a) === binding.TF_FLOAT);
src/tf_binding_test.ts:  assert(binding.getDType(b) === binding.TF_FLOAT);
src/tf_binding_test.ts:  assert(binding.getDevice(r) === "CPU:0");
src/tf_binding_test.ts:  assert(binding.getDType(r) === binding.TF_BOOL);
src/tf_binding_test.ts:  assert(binding.getDType(t) === binding.TF_BOOL);
src/tf_binding_test.ts:  assert(cpuDevice["deviceType"] === "CPU");
src/tf_binding_test.ts:    assert(binding.getDevice(h) === "CPU:0");
src/tf_binding_test.ts:    assert(binding.getShape(h).length === 0);
src/tf_binding_test.ts:    assert(binding.getDType(h) === tftype);
src/tf_binding_test.ts:    assert(binding.getDevice(h) === "CPU:0");
src/tf_binding_test.ts:    assert(binding.getDType(h) === tftype);
src/tf_binding_test.ts:    assert(binding.getShape(h).length === 0);
src/tf_binding_test.ts:    assert(binding.getDType(h) === tftype);
src/tf_binding_test.ts:    assert(binding.getDType(h) === tftype);
src/tensor.ts:    // assert(t.device === this.device);
src/tensor.ts:    assert(t.dtype === this.dtype);
src/tensor.ts:    assert(indicesT.rank === 1, "indices must be rank1 int32");
src/tensor.ts:      assert(this.shape[0] === xx.shape[0]);
src/tensor.ts:      assert(this.shape[this.rank - 1] === xx.shape[0]);
src/tensor.ts:    assert(labelsT.rank === 2);
src/tensor.ts:    assert(logits.rank === 2);
src/tensor.ts:    assert(logits.rank === 2);
src/tensor.ts:    assert(this.rank === 4);
src/tensor.ts:    assert(s === scopes.pop());
src/dataset_test.ts:  assert(count === 12);
src/params_test.ts:  assert(p.get("L1/weights") === w);
src/params_test.ts:  assert(p.get("L1/bias") === b);
src/dataset.ts:  assert(lines.length === nSamples);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/format_test.ts:  assert(actual === expected);
src/util_test.ts:  assert(m.get(0) === 0);
src/util_test.ts:  assert(m.get(0) === 2);
src/util_test.ts:  assert(m.get(1) === 0);
src/util_test.ts:  assert(m.get(1) === 1);
src/util_test.ts:  assert(m.get(0) === 1);
src/util_test.ts:  assert(m.get(1) === 2);
src/util_test.ts:  assert(arr1[0][0] === arr2[0][0]);
src/util_test.ts:  assert(arr1[1][0] === arr2[1][0]);
src/util_test.ts:  assert(arr1[0][1] === arr2[0][1]);
src/util_test.ts:  assert(arr1[1][1] === arr2[1][1]);
src/util_test.ts:    assert(s.length === 10, "should be 10 chars long");
src/tensor_util.ts:  assert(actual === expected, msg);
src/tensor_util.ts:    assert(actualFlat[i] === expectedFlat[i],
src/tensor_util.ts:      assert(xDim === 1 && yDim === 1, "Incompatible broadcast shapes.");
src/dl/math/ndarray.ts:    util.assert(this.size === 1, "The array must have only 1 element.");
src/dl/math/ndarray.ts:    util.assert(shape.length === 2, "Shape should be of length 2");
src/dl/math/ndarray.ts:    util.assert(shape.length === 3, "Shape should be of length 3");
src/dl/math/ndarray.ts:    util.assert(shape.length === 4, "Shape should be of length 4");
src/dl.ts:    assert(x.shape.length === 2 && y.shape.length === 2);
src/dl.ts:    assert(ones.math === x.math);
src/dl.ts:    assert(zeros.math === x.math);
src/dl.ts:    assert(out.math === value.math);
src/dl.ts:        assert(d === -1, "Bad value in size");
src/tf.ts:    assert(dims.dtype === "bool");
src/tf.ts:    assert(s.length === 2);
@ry ry changed the title Fix assertEqual Fix assertEqual (add to util.ts) Mar 22, 2018
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 29, 2018
jasmine_shim.ts and tensor_util.ts use assertEqual from util.ts
relates propelml#423
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 29, 2018
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 29, 2018
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 29, 2018
jasmine_shim.ts and tensor_util.ts use assertEqual from util.ts
relates propelml#423
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 29, 2018
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 30, 2018
jasmine_shim.ts and tensor_util.ts use assertEqual from util.ts
relates propelml#423
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 30, 2018
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 31, 2018
Works with objects. jasmine_shim.ts and tensor_util.ts use
equal/assertEqual from util.ts
Relates propelml#423
dangerdak added a commit to dangerdak/propel that referenced this issue Mar 31, 2018
as assertEqual and equal work with objects (including
nested objects).
Relates propelml#423
ry pushed a commit that referenced this issue Mar 31, 2018
Works with objects. jasmine_shim.ts and tensor_util.ts use
equal/assertEqual from util.ts
Relates #423
ry pushed a commit that referenced this issue Mar 31, 2018
as assertEqual and equal work with objects (including
nested objects).
Relates #423
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant