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

Commit

Permalink
Remove non-nullable "!" operator (#60)
Browse files Browse the repository at this point in the history
* Remove non-nullable "!" operator

* fix lint errors
  • Loading branch information
dsmilkov authored Aug 21, 2017
1 parent 156c227 commit 27ca171
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 233 deletions.
6 changes: 3 additions & 3 deletions demos/adder/adder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ adder.setupSession();
const outputEl = document.getElementById('output');
if (!outputEl) throw new Error('output element not found');
function printOutput(out: number) {
outputEl!.innerText = String(out);
outputEl.innerText = String(out);
}

const inA: HTMLInputElement = document.getElementById('A') as HTMLInputElement;
Expand All @@ -59,8 +59,8 @@ const inB: HTMLInputElement = document.getElementById('B') as HTMLInputElement;
if (!inB) throw new Error('output B not found');

export function execute(event?: Event) {
const a = +inA!.value;
const b = +inB!.value;
const a = +inA.value;
const b = +inB.value;

printOutput(adder.computeSum(a, b));
}
Expand Down
47 changes: 27 additions & 20 deletions demos/homepage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ function isSafari() {
}
function isMobile() {
// tslint:disable-next-line:no-any
const a = navigator.userAgent||navigator.vendor||(window as any).opera;
const a = navigator.userAgent || navigator.vendor || (window as any).opera;
// tslint:disable-next-line:max-line-length
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4));
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i
.test(a) ||
// tslint:disable-next-line:max-line-length
/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i
.test(a.substr(0, 4));
}

const inferenceCanvas =
Expand Down Expand Up @@ -78,32 +82,35 @@ function startCPPN() {
const currentColorElement =
document.querySelector('#colormode') as HTMLInputElement;

document.querySelector('#color-selector')!.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const colorMode =
(event.target as HTMLElement).getAttribute('data-val') as ColorMode;
currentColorElement.value = colorMode;
cppn.setColorMode(colorMode);
});
document.querySelector('#color-selector')
.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const colorMode =
(event.target as HTMLElement).getAttribute('data-val') as
ColorMode;
currentColorElement.value = colorMode;
cppn.setColorMode(colorMode);
});

const currentActivationFnElement =
document.querySelector('#activation-fn') as HTMLInputElement;
document.querySelector('#activation-selector')!.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const activationFn =
(event.target as HTMLElement).getAttribute('data-val') as
ActivationFunction;
currentActivationFnElement.value = activationFn;
cppn.setActivationFunction(activationFn);
});
document.querySelector('#activation-selector')
.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const activationFn =
(event.target as HTMLElement).getAttribute('data-val') as
ActivationFunction;
currentActivationFnElement.value = activationFn;
cppn.setActivationFunction(activationFn);
});

const layersSlider =
document.querySelector('#layers-slider') as HTMLInputElement;
const layersCountElement =
document.querySelector('#layers-count') as HTMLDivElement;
layersSlider!.addEventListener('input', (event) => {
layersSlider.addEventListener('input', (event) => {
// tslint:disable-next-line:no-any
const numLayers = parseInt((event as any).target.value, 10);
layersCountElement.innerText = '' + numLayers;
Expand Down
24 changes: 12 additions & 12 deletions demos/imagenet/imagenet-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import {PolymerElement, PolymerHTMLElement} from '../polymer-spec';

// tslint:disable-next-line:variable-name
export const ImagenetDemoPolymer: new () => PolymerHTMLElement =
PolymerElement({
is: 'imagenet-demo',
properties: {
layerNames: Array,
selectedLayerName: String,
inputNames: Array,
selectedInputName: String
}
});
PolymerElement({
is: 'imagenet-demo',
properties: {
layerNames: Array,
selectedLayerName: String,
inputNames: Array,
selectedInputName: String
}
});

/**
* NOTE: To use the webcam without SSL, use the chrome flag:
Expand Down Expand Up @@ -73,7 +73,7 @@ export class ImagenetDemo extends ImagenetDemoPolymer {
this.layerNames = [];
this.selectedLayerName = 'conv_1';

const inputDropdown = this.querySelector('#input-dropdown')!;
const inputDropdown = this.querySelector('#input-dropdown');
// tslint:disable-next-line:no-any
inputDropdown.addEventListener('iron-activate', (event: any) => {
const selectedInputName = event.detail.selected;
Expand Down Expand Up @@ -170,8 +170,8 @@ export class ImagenetDemo extends ImagenetDemoPolymer {
if (!(className in topClassesToProbability)) {
continue;
}
document.getElementById('class' + count)!.innerHTML = className;
document.getElementById('prob' + count)!.innerHTML =
document.getElementById('class' + count).innerHTML = className;
document.getElementById('prob' + count).innerHTML =
'' + Math.floor(1000 * topClassesToProbability[className]) / 1000;
count++;
}
Expand Down
12 changes: 7 additions & 5 deletions demos/mnist/mnist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ reader.getAllVariables().then(vars => {
}
}
const accuracy = numCorrect * 100 / data.images.length;
document.getElementById('accuracy')!.innerHTML = accuracy + '%';
document.getElementById('accuracy').innerHTML = accuracy + '%';
});
};
xhr.onerror = (err) => console.error(err);
Expand Down Expand Up @@ -68,10 +68,12 @@ export function buildModelMathAPI(

return (x: Array1D): Scalar => {
return math.scope(() => {
const hidden1 = math.relu(
math.add(math.vectorTimesMatrix(x, hidden1W), hidden1B)) as Array1D;
const hidden2 = math.relu(math.add(
math.vectorTimesMatrix(hidden1, hidden2W), hidden2B)) as Array1D;
const hidden1 =
math.relu(math.add(math.vectorTimesMatrix(x, hidden1W), hidden1B)) as
Array1D;
const hidden2 =
math.relu(math.add(
math.vectorTimesMatrix(hidden1, hidden2W), hidden2B)) as Array1D;
const logits =
math.add(math.vectorTimesMatrix(hidden2, softmaxW), softmaxB);
return math.argMax(logits);
Expand Down
65 changes: 34 additions & 31 deletions demos/model-builder/model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import './model-layer';
import '../demo-header';
import '../demo-footer';

import {PolymerElement, PolymerHTMLElement} from '../polymer-spec';
// tslint:disable-next-line:max-line-length
import {Array1D, Array3D, DataStats, FeedEntry, Graph, GraphRunner, GraphRunnerEventObserver, InCPUMemoryShuffledInputProviderBuilder, InMemoryDataset, MetricReduction, NDArray, NDArrayMath, NDArrayMathCPU, NDArrayMathGPU, Optimizer, Scalar, Session, SGDOptimizer, Tensor, util} from '../deeplearnjs';
import {NDArrayImageVisualizer} from '../ndarray-image-visualizer';
import {NDArrayLogitsVisualizer} from '../ndarray-logits-visualizer';
import {PolymerElement, PolymerHTMLElement} from '../polymer-spec';
import * as xhr_dataset from '../xhr-dataset';
import {XhrDataset, XhrDatasetConfig} from '../xhr-dataset';

Expand Down Expand Up @@ -190,27 +190,29 @@ export class ModelBuilder extends ModelBuilderPolymer {
// Set up datasets.
this.populateDatasets();

this.querySelector('#dataset-dropdown .dropdown-content')!.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
// Update the dataset.
const datasetName = event.detail.selected;
this.updateSelectedDataset(datasetName);
this.querySelector('#dataset-dropdown .dropdown-content')
.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
// Update the dataset.
const datasetName = event.detail.selected;
this.updateSelectedDataset(datasetName);

// TODO(nsthorat): Remember the last model used for each dataset.
this.removeAllLayers();
});
this.querySelector('#model-dropdown .dropdown-content')!.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
// Update the model.
const modelName = event.detail.selected;
this.updateSelectedModel(modelName);
});
// TODO(nsthorat): Remember the last model used for each dataset.
this.removeAllLayers();
});
this.querySelector('#model-dropdown .dropdown-content')
.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
// Update the model.
const modelName = event.detail.selected;
this.updateSelectedModel(modelName);
});

{
const normalizationDropdown =
this.querySelector('#normalization-dropdown .dropdown-content')!;
this.querySelector('#normalization-dropdown .dropdown-content');
// tslint:disable-next-line:no-any
normalizationDropdown.addEventListener('iron-activate', (event: any) => {
const selectedNormalizationOption = event.detail.selected;
Expand All @@ -225,20 +227,20 @@ export class ModelBuilder extends ModelBuilderPolymer {
this.showTrainStats = false;
this.showDatasetStats = false;

const addButton = this.querySelector('#add-layer')!;
const addButton = this.querySelector('#add-layer');
addButton.addEventListener('click', () => this.addLayer());

const downloadModelButton = this.querySelector('#download-model')!;
const downloadModelButton = this.querySelector('#download-model');
downloadModelButton.addEventListener('click', () => this.downloadModel());
const uploadModelButton = this.querySelector('#upload-model')!;
const uploadModelButton = this.querySelector('#upload-model');
uploadModelButton.addEventListener('click', () => this.uploadModel());
this.setupUploadModelButton();

const uploadWeightsButton = this.querySelector('#upload-weights')!;
const uploadWeightsButton = this.querySelector('#upload-weights');
uploadWeightsButton.addEventListener('click', () => this.uploadWeights());
this.setupUploadWeightsButton();

const stopButton = this.querySelector('#stop')!;
const stopButton = this.querySelector('#stop');
stopButton.addEventListener('click', () => {
this.applicationState = ApplicationState.IDLE;
this.graphRunner.stopTraining();
Expand All @@ -250,12 +252,13 @@ export class ModelBuilder extends ModelBuilderPolymer {
this.startTraining();
});

this.querySelector(
'#environment-toggle')!.addEventListener('change', (event) => {
// tslint:disable-next-line:no-any
this.math = (event.target as any).active ? this.mathGPU : this.mathCPU;
this.graphRunner.setMath(this.math);
});
this.querySelector('#environment-toggle')
.addEventListener('change', (event) => {
this.math =
// tslint:disable-next-line:no-any
(event.target as any).active ? this.mathGPU : this.mathCPU;
this.graphRunner.setMath(this.math);
});

this.hiddenLayers = [];
this.examplesPerSec = 0;
Expand Down Expand Up @@ -774,7 +777,7 @@ export class ModelBuilder extends ModelBuilderPolymer {
// Show and setup the load view button.
const fileInput = this.querySelector('#model-file') as HTMLInputElement;
fileInput.addEventListener('change', event => {
const file = fileInput.files![0];
const file = fileInput.files[0];
// Clear out the value of the file chooser. This ensures that if the user
// selects the same file, we'll re-read it.
fileInput.value = '';
Expand Down Expand Up @@ -816,7 +819,7 @@ export class ModelBuilder extends ModelBuilderPolymer {
// Show and setup the load view button.
const fileInput = this.querySelector('#weights-file') as HTMLInputElement;
fileInput.addEventListener('change', event => {
const file = fileInput.files![0];
const file = fileInput.files[0];
// Clear out the value of the file chooser. This ensures that if the user
// selects the same file, we'll re-read it.
fileInput.value = '';
Expand Down
17 changes: 9 additions & 8 deletions demos/model-builder/model-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ export class ModelLayer extends ModelLayerPolymer {
this.inputShape = inputShape;
this.buildParamsUI('Fully connected', this.inputShape);

this.querySelector('.dropdown-content')!.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
this.buildParamsUI(
event.detail.selected as LayerName, this.inputShape);
});

this.querySelector('#remove-layer')!.addEventListener('click', (event) => {
this.querySelector('.dropdown-content')
.addEventListener(
// tslint:disable-next-line:no-any
'iron-activate', (event: any) => {
this.buildParamsUI(
event.detail.selected as LayerName, this.inputShape);
});

this.querySelector('#remove-layer').addEventListener('click', (event) => {
modelBuilder.removeLayer(this);
});
}
Expand Down
45 changes: 23 additions & 22 deletions demos/nn-art/nn-art.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ const MAT_WIDTH = 30;
const WEIGHTS_STDEV = .6;

// tslint:disable-next-line:variable-name
const NNArtPolymer: new () => PolymerHTMLElement = PolymerElement({
is: 'nn-art', properties: {}
});
const NNArtPolymer: new () => PolymerHTMLElement =
PolymerElement({is: 'nn-art', properties: {}});

class NNArt extends NNArtPolymer {
private cppn: CPPN;
Expand All @@ -48,35 +47,37 @@ class NNArt extends NNArtPolymer {

const currentColorElement =
this.querySelector('#colormode') as HTMLInputElement;
this.querySelector('#color-selector')!.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const colorMode =
(event.target as HTMLElement).getAttribute('data-val') as
ColorMode;
currentColorElement.value = colorMode;
this.cppn.setColorMode(colorMode);
});
this.querySelector('#color-selector')
.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const colorMode =
(event.target as HTMLElement).getAttribute('data-val') as
ColorMode;
currentColorElement.value = colorMode;
this.cppn.setColorMode(colorMode);
});
this.cppn.setColorMode('rgb');

const currentActivationFnElement =
this.querySelector('#activation-fn') as HTMLInputElement;
this.querySelector('#activation-selector')!.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const activationFn =
(event.target as HTMLElement).getAttribute('data-val') as
ActivationFunction;
currentActivationFnElement.value = activationFn;
this.cppn.setActivationFunction(activationFn);
});
this.querySelector('#activation-selector')
.addEventListener(
// tslint:disable-next-line:no-any
'click', (event: any) => {
const activationFn =
(event.target as HTMLElement).getAttribute('data-val') as
ActivationFunction;
currentActivationFnElement.value = activationFn;
this.cppn.setActivationFunction(activationFn);
});
this.cppn.setActivationFunction('tanh');

const layersSlider =
this.querySelector('#layers-slider') as HTMLInputElement;
const layersCountElement =
this.querySelector('#layers-count') as HTMLDivElement;
layersSlider!.addEventListener('input', (event) => {
layersSlider.addEventListener('input', (event) => {
// tslint:disable-next-line:no-any
this.numLayers = parseInt((event as any).target.value, 10);
layersCountElement.innerText = '' + this.numLayers;
Expand Down
Loading

0 comments on commit 27ca171

Please sign in to comment.