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

Add assertEqual #457

Merged
merged 2 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { bo } from "./backend";
import * as ops from "./ops";
import { convert, Tensor } from "./tensor";
import * as types from "./types";
import { assert } from "./util";
import { assertEqual } from "./util";

export { DType, TensorLike } from "./types";
export { params, Params } from "./params";
Expand Down Expand Up @@ -163,10 +163,10 @@ export function conv2d(input: Tensor, filter: Tensor,
stride: 1,
padding: "valid",
};
assert(input.dtype === "float32");
assert(filter.dtype === "float32");
assert(input.rank === 4);
assert(filter.rank === 4);
assertEqual(input.dtype, "float32");
assertEqual(filter.dtype, "float32");
assertEqual(input.rank, 4);
assertEqual(filter.rank, 4);
return ops.conv2d(input, filter, Object.assign(defaults, opts));
}

Expand Down
64 changes: 32 additions & 32 deletions src/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as api from "./api";
import { assertAllClose, assertAllEqual, assertClose,
assertShapesEqual } from "./tensor_util";
import * as types from "./types";
import { assert, IS_NODE } from "./util";
import { assert, assertEqual, IS_NODE } from "./util";

function checkGrad(f, g, val = 1.0) {
const epsilon = 0.01;
Expand Down Expand Up @@ -84,13 +84,13 @@ test(async function api_randn() {

test(async function api_convertWithType() {
const t = tensor([1, 2, 3], {dtype: "int32"});
assert(t.dtype === "int32");
assertEqual(t.dtype, "int32");
const ta = t.dataSync();
assert(ta instanceof Int32Array);

const ta2 = new Int32Array([1, 2, 3]);
const t2 = tensor(ta2);
assert(t2.dtype === "int32");
assertEqual(t2.dtype, "int32");
assert(t2.dataSync() instanceof Int32Array);
});

Expand Down Expand Up @@ -457,7 +457,7 @@ testDevices(async function api_reduceMean(tensor, device) {
[9, 8, 7],
[6, 5, 4],
]);
assert(a.device === device);
assertEqual(a.device, device);
assertAllClose(a.reduceMean([0]), [7.5, 6.5, 5.5]);
assertAllClose(a.reduceMean([1]), [8, 5]);
assertAllClose(a.reduceMean(), 6.5);
Expand Down Expand Up @@ -525,8 +525,8 @@ testDevices(async function api_onesAndZerosLike(tensor, device) {
]);
const ones = a.onesLike();
const zeros = a.zerosLike();
assert(ones.device === device);
assert(zeros.device === device);
assertEqual(ones.device, device);
assertEqual(zeros.device, device);
assertAllEqual(ones, [ [1, 1, 1], [1, 1, 1] ]);
assertAllEqual(zeros, [ [0, 0, 0], [0, 0, 0] ]);
});
Expand All @@ -541,7 +541,7 @@ test(async function api_equal() {
[0, 8, 2],
]);
const r = a.equal(b);
assert(r.dtype === "bool");
assertEqual(r.dtype, "bool");
// TODO Allow assertAllEqual to handle boolean.
assertAllEqual(r, [ [1, 0, 1], [0, 1, 0] ]);

Expand All @@ -563,7 +563,7 @@ test(async function api_greater() {
[0, 8, 4],
]);
const r = a.greater(b);
assert(r.dtype === "bool");
assertEqual(r.dtype, "bool");
// TODO Allow assertAllEqual to handle boolean.
assertAllEqual(r, [ [0, 1, 0], [1, 0, 0] ]);
// greater isn't differentiable but it should have the same behavior as
Expand All @@ -584,7 +584,7 @@ test(async function api_greaterEqual() {
[0, 8, 4],
]);
const r = a.greaterEqual(b);
assert(r.dtype === "bool");
assertEqual(r.dtype, "bool");
// TODO Allow assertAllEqual to handle boolean.
assertAllEqual(r, [ [1, 1, 1], [1, 1, 0] ]);
// greaterEqual isn't differentiable but it should have the same behavior as
Expand All @@ -605,7 +605,7 @@ test(async function api_less() {
[0, 8, 4],
]);
const r = a.less(b);
assert(r.dtype === "bool");
assertEqual(r.dtype, "bool");
// TODO Allow assertAllEqual to handle boolean.
assertAllEqual(r, [ [0, 0, 0], [0, 0, 1] ]);
// less isn't differentiable but it should have the same behavior as
Expand All @@ -626,7 +626,7 @@ test(async function api_lessEqual() {
[0, 8, 4],
]);
const r = a.lessEqual(b);
assert(r.dtype === "bool");
assertEqual(r.dtype, "bool");
// TODO Allow assertAllEqual to handle boolean.
assertAllEqual(r, [ [1, 0, 1], [0, 1, 1] ]);
// lessEqual isn't differentiable but it should have the same behavior as
Expand Down Expand Up @@ -691,7 +691,7 @@ testDevices(async function api_reshape(tensor, device) {
const f = (x) => tensor(x).reshape([3, 2]);
const g = grad(f);
const ga = g(a);
assert(ga.device === device);
assertEqual(ga.device, device);
assertAllEqual(ga, [
[1, 1, 1],
[1, 1, 1],
Expand Down Expand Up @@ -825,19 +825,19 @@ test(async function api_dot() {
test(async function api_zerosOnes() {
const z1 = zeros([2, 3]);
assertAllEqual(z1, [[0, 0, 0], [0, 0, 0]]);
assert(z1.dtype === "float32");
assertEqual(z1.dtype, "float32");

const z2 = zeros([2, 3], {dtype: "int32"});
assertAllEqual(z2, [[0, 0, 0], [0, 0, 0]]);
assert(z2.dtype === "int32");
assertEqual(z2.dtype, "int32");

const o1 = ones([2, 3]);
assertAllEqual(o1, [[1, 1, 1], [1, 1, 1]]);
assert(o1.dtype === "float32");
assertEqual(o1.dtype, "float32");

const o2 = ones([2, 3], {dtype: "int32"});
assertAllEqual(o2, [[1, 1, 1], [1, 1, 1]]);
assert(o2.dtype === "int32");
assertEqual(o2.dtype, "int32");
});

test(async function api_bcastAdd() {
Expand All @@ -857,7 +857,7 @@ test(async function api_bcastAdd() {
]);
const g = multigrad(f, [0, 1]);
const gab = g(a, b);
assert(gab.length === 2);
assertEqual(gab.length, 2);
assertAllEqual(gab[0], [
[1, 1],
[1, 1],
Expand All @@ -884,7 +884,7 @@ test(async function api_bcastSub() {
]);
const g = multigrad(f, [0, 1]);
const gab = g(a, b);
assert(gab.length === 2);
assertEqual(gab.length, 2);
assertAllEqual(gab[0], [
[1, 1],
[1, 1],
Expand All @@ -911,7 +911,7 @@ test(async function api_bcastMul() {
]);
const g = multigrad(f, [0, 1]);
const gab = g(a, b);
assert(gab.length === 2);
assertEqual(gab.length, 2);
assertAllEqual(gab[0], [
[42, 43],
[42, 43],
Expand All @@ -938,7 +938,7 @@ test(async function api_bcastDiv() {
]);
const g = multigrad(f, [0, 1]);
const gab = g(a, b);
assert(gab.length === 2);
assertEqual(gab.length, 2);
assertAllClose(gab[0], [
[0.02380952, 0.02325581],
[0.02380952, 0.02325581],
Expand Down Expand Up @@ -981,7 +981,7 @@ testDevices(async function api_slice(tensor, device) {
assertAllEqual(a.slice(1, 1), [[[3, 3, 3], [4, 4, 4]]]);

const s2 = tensor([1, 2, 3], {dtype: "int32"}).slice([1], [1]);
assert(s2.dtype === "int32");
assertEqual(s2.dtype, "int32");
assertAllEqual(s2, [2]);
// Backwards pass.
const f = (x) => tensor(x).slice([1, 0, 0], [2, 1, 3]);
Expand Down Expand Up @@ -1052,7 +1052,7 @@ testDevices(async function api_stack(tensor, device) {

test(async function api_cast() {
const a = tensor([255, 127, 0], {dtype: "uint8"});
assert(a.dtype === "uint8");
assertEqual(a.dtype, "uint8");
const r = a.cast("float32").div(255);
assertAllClose(r, [1.0, 127 / 255, 0]);
});
Expand Down Expand Up @@ -1118,10 +1118,10 @@ test(async function api_devicePlacement() {
const s = tensor([3, 4]).gpu();
const r = t.mul(s);
const rCpu = r.cpu();
assert(t.device === "GPU:0");
assert(s.device === "GPU:0");
assert(r.device === "GPU:0");
assert(rCpu.device === "CPU:0");
assertEqual(t.device, "GPU:0");
assertEqual(s.device, "GPU:0");
assertEqual(r.device, "GPU:0");
assertEqual(rCpu.device, "CPU:0");
assertAllEqual(rCpu, [3, 8]);
});

Expand Down Expand Up @@ -1201,7 +1201,7 @@ if (IS_NODE) {
test(async function api_inspect() {
const t = tensor([1, 2, 3]);
const actual = require("util").inspect(t);
assert("[ 1., 2., 3.]" === actual);
assertEqual("[ 1., 2., 3.]", actual);
});
}

Expand Down Expand Up @@ -1237,13 +1237,13 @@ test(async function api_data() {

test(async function api_dtypeConstructors() {
const ui8 = api.uint8([254, 255, 256, 257]);
assert(ui8.dtype === "uint8");
assertEqual(ui8.dtype, "uint8");
assertAllEqual(ui8, [254, 255, 0, 1]);
const i32 = api.int32([1, 2, 3]);
assert(i32.dtype === "int32");
assertEqual(i32.dtype, "int32");
assertAllEqual(i32, [1, 2, 3]);
const f32 = api.float32([1.5, 3.5]);
assert(f32.dtype === "float32");
assertEqual(f32.dtype, "float32");
assertAllEqual(f32, [1.5, 3.5]);
});

Expand Down Expand Up @@ -1308,6 +1308,6 @@ test(async function api_sin_cos_tan() {
});

test(async function api_size() {
assert(api.range(20).size === 20);
assert(api.range(20).reshape([4, 5]).size === 20);
assertEqual(api.range(20).size, 20);
assertEqual(api.range(20).reshape([4, 5]).size, 20);
});
6 changes: 3 additions & 3 deletions src/backprop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { fill, Params } from "./api";
import { getBackwardFunc } from "./ops";
import { convert, NamedTensors, Tensor } from "./tensor";
import * as types from "./types";
import { assert, CounterMap, log } from "./util";
import { assert, assertEqual, CounterMap, log } from "./util";

// Hacky. How does one define methods on interfaces?
function tapeEntryToString(e: types.TapeEntry): string {
Expand Down Expand Up @@ -116,7 +116,7 @@ export function gradParams(f: ParamsFn, names?: string[]) {
const tape = popTape();
const grads = imperativeGrad(result, targs, tape);
// Now grads is an array, which we want to turn back into a params object.
assert(grads.length === order.length);
assertEqual(grads.length, order.length);
const out = {};
for (let i = 0; i < order.length; ++i) {
const n = order[i];
Expand Down Expand Up @@ -203,7 +203,7 @@ function imperativeGrad(target: Tensor,
const op = oidLookup.get(oid);

// TODO(scalar) Currently assuming ops have single output.
assert(op.outputIds.length === 1);
assertEqual(op.outputIds.length, 1);
const outGrad = gradients.aggregate(op.outputIds[0]);

log("backprop", tapeEntryToString(op));
Expand Down
5 changes: 3 additions & 2 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import { test } from "../tools/tester";
import * as cache from "./cache";
import {
assert,
assertEqual,
IS_NODE,
localServer,
nodeRequire,
process,
tmpdir,
tmpdir
} from "./util";

// Some large datasets are external to the repository, and we would like
Expand Down Expand Up @@ -68,7 +69,7 @@ test(async function cache_fetchWithCache() {
await localServer(async function(url: string) {
url += "/data/mnist/train-images-idx3-ubyte.bin";
const ab = await cache.fetchWithCache(url);
assert(ab.byteLength === 47040016);
assertEqual(ab.byteLength, 47040016);
if (IS_NODE) {
assert(fs.existsSync(cache.url2Filename(url)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as cache from "./cache";
import * as mnist from "./mnist";
import * as npy from "./npy";
import { NamedTensors } from "./tensor";
import { assert, delay } from "./util";
import { assertEqual, delay } from "./util";

export function datasetFromSlices(tensors: NamedTensors): Dataset {
return new SliceDataset(tensors);
Expand Down Expand Up @@ -308,7 +308,7 @@ async function loadData(fn: string):
const nSamples = Number(header.shift());
const nFeatures = Number(header.shift());
// let labelNames = header;
assert(lines.length === nSamples);
assertEqual(lines.length, nSamples);
const features: number[][] = [];
const labels: number[] = [];
for (const line of lines) {
Expand Down
11 changes: 8 additions & 3 deletions src/dataset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import { test } from "../tools/tester";
import * as pr from "./api";
import * as dataset from "./dataset";
import { assert, assertAllClose, assertAllEqual, assertShapesEqual }
from "./tensor_util";
import {
assert,
assertAllClose,
assertAllEqual,
assertEqual,
assertShapesEqual
} from "./tensor_util";
import { IS_NODE } from "./util";

test(async function dataset_datasetFromSlices() {
Expand Down Expand Up @@ -142,7 +147,7 @@ test(async function dataset_iterableEndCondition() {
const { features } = await p;
count += features.shape[0];
}
assert(count === 12);
assertEqual(count, 12);
});

test(async function dataset_shuffleSmoke() {
Expand Down
10 changes: 8 additions & 2 deletions src/disk_experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ import * as rimraf from "rimraf";
import { Experiment, ExperimentOpts, print } from "./experiment";
import * as npy from "./npy";
import { Params, params as createParams } from "./params";
import { assert, Buffer, nodeRequire, process } from "./util";
import {
assert,
assertEqual,
Buffer,
nodeRequire,
process
} from "./util";
import { isDir, propelDir } from "./util_node";

const fs = nodeRequire("fs");
Expand Down Expand Up @@ -52,7 +58,7 @@ export class DiskExperiment extends Experiment {
const latestCheckpoint = checkpoints[0];
console.log("Restore checkpoint", latestCheckpoint);
await this.restore(latestCheckpoint);
assert(this.step_ === latestCheckpoint);
assertEqual(this.step_, latestCheckpoint);
} else {
this.currentParams = createParams();
this.step_ = 0;
Expand Down
Loading