jsdoでdeeplearn.js その5

#概要
jsdoでdeeplearn.jsやってみた。
tensorflowで学習したモデルを使ってみた。

#サンプルコード

```
var MANIFEST_FILE = '/assets/q/K/x/K/qKxKH';
function CheckpointLoader(urlPath) {
	this.urlPath = urlPath;
	if (this.urlPath.charAt(this.urlPath.length - 1) !== '/')
    {
		this.urlPath += '/';
	}
}
CheckpointLoader.prototype.loadManifest = function() {
	var _this = this;
	return new Promise(function(resolve, reject) {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', _this.urlPath + MANIFEST_FILE);
		xhr.onload = function() {
			_this.checkpointManifest = JSON.parse(xhr.responseText);
			resolve();
		};
		xhr.onerror = function(error) {
			throw new Error(MANIFEST_FILE + " not found at " + _this.urlPath + ". " + error);
		};
		xhr.send();
	});
};
CheckpointLoader.prototype.getCheckpointManifest = function() {
	var _this = this;
	if (this.checkpointManifest == null) 
    {
		return new Promise(function(resolve, reject) {
			_this.loadManifest().then(function() {
				resolve(_this.checkpointManifest);
			});
		});
	}
	return new Promise(function(resolve, reject) {
		resolve(_this.checkpointManifest);
	});
};
CheckpointLoader.prototype.getAllVariables = function() {
	var _this = this;
	if (this.variables != null) 
    {
		return new Promise(function(resolve, reject) {
			resolve(_this.variables);
		});
	}
	return new Promise(function(resolve, reject) {
		_this.getCheckpointManifest().then(function(checkpointDefinition) {
			var variableNames = Object.keys(_this.checkpointManifest);
			var variablePromises = [];
			for (var i = 0; i < variableNames.length; i++) 
            {
				variablePromises.push(_this.getVariable(variableNames[i]));
			}
			Promise.all(variablePromises).then(function(variables) {
				_this.variables = {};
				for (var i = 0; i < variables.length; i++)
                {
					_this.variables[variableNames[i]] = variables[i];
				}
				resolve(_this.variables);
			});
		});
	});
};
CheckpointLoader.prototype.getVariable = function(varName) {
	var _this = this;
	if (!(varName in this.checkpointManifest))
    {
		throw new Error('Cannot load non-existant variable ' + varName);
	}
	var variableRequestPromiseMethod = function(resolve, reject) {
		var xhr = new XMLHttpRequest();
		xhr.responseType = 'arraybuffer';
		var fname = _this.checkpointManifest[varName].filename;
		xhr.open('GET', _this.urlPath + fname);
		xhr.onload = function() {
			var values = new Float32Array(xhr.response);
			var ndarray = dl.NDArray.make(_this.checkpointManifest[varName].shape, {
                values: values 
            });
			resolve(ndarray);
		};
		xhr.onerror = function(error) {
			throw new Error('Could not fetch variable ' + varName + ': ' + error);
		};
		xhr.send();
	};
	if (this.checkpointManifest == null)
    {
		return new Promise(function(resolve, reject) {
			_this.loadManifest().then(function() {
				new Promise(variableRequestPromiseMethod).then(resolve);
			});
		});
	}
	return new Promise(variableRequestPromiseMethod);
};
var dl = deeplearn;
var g = new dl.Graph();
var math = new dl.NDArrayMathCPU();
var reader = new CheckpointLoader('http://jsrun.it');
reader.getAllVariables().then(function(vars) {
	var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://jsrun.it/assets/O/U/w/U/OUwUs');
	xhr.onload = function() {
		var data = JSON.parse(xhr.responseText);
		var _a = buildModelLayersAPI(data, vars),
            input = _a[0],
            probs = _a[1];
		var sess = new dl.Session(input.node.graph, math);
		math.scope(function() {
			var numCorrect = 0;
			for (var i = 0; i < data.images.length; i++) 
            {
				var inputData = dl.Array1D.new(data.images[i]);
				var probsVal = sess.eval(probs, [{
                    tensor: input,
                    data: inputData
                }]);
				if (data.labels[i] === probsVal.get())
                {
					numCorrect++;
				}
			}
			var accuracy = numCorrect * 100 / data.images.length;
			document.getElementById('helloWorld').innerHTML = accuracy + '%';
		});
	};
	xhr.onerror = function (err) {
        return alert(err); 
    };
	xhr.send();
});
function buildModelMathAPI(math, data, vars) {
	var hidden1W = vars['hidden1/weights'];
	var hidden1B = vars['hidden1/biases'];
	var hidden2W = vars['hidden2/weights'];
	var hidden2B = vars['hidden2/biases'];
	var softmaxW = vars['softmax_linear/weights'];
	var softmaxB = vars['softmax_linear/biases'];
	return function (x) {
		return math.scope(function() {
			var hidden1 = math.relu(math.add(math.vectorTimesMatrix(x, hidden1W), hidden1B));
			var hidden2 = math.relu(math.add(math.vectorTimesMatrix(hidden1, hidden2W), hidden2B));
			var logits = math.add(math.vectorTimesMatrix(hidden2, softmaxW), softmaxB);
			return math.argMax(logits);
		});
	};
}
function buildModelGraphAPI(data, vars) {
	var input = g.placeholder('input', [784]);
	var hidden1W = g.constant(vars['hidden1/weights']);
	var hidden1B = g.constant(vars['hidden1/biases']);
	var hidden1 = g.relu(g.add(g.matmul(input, hidden1W), hidden1B));
	var hidden2W = g.constant(vars['hidden2/weights']);
	var hidden2B = g.constant(vars['hidden2/biases']);
	var hidden2 = g.relu(g.add(g.matmul(hidden1, hidden2W), hidden2B));
	var softmaxW = g.constant(vars['softmax_linear/weights']);
	var softmaxB = g.constant(vars['softmax_linear/biases']);
	var logits = g.add(g.matmul(hidden2, softmaxW), softmaxB);
	return [input, g.argmax(logits)];
}
function buildModelLayersAPI(data, vars) {
	var input = g.placeholder('input', [784]);
	var hidden1W = vars['hidden1/weights'];
	var hidden1B = vars['hidden1/biases'];
	var hidden1 = g.layers.dense('hidden1', input, hidden1W.shape[1], function(x) {
        return g.relu(x);
    }, true, new dl.NDArrayInitializer(hidden1W), new dl.NDArrayInitializer(hidden1B));
	var hidden2W = vars['hidden2/weights'];
	var hidden2B = vars['hidden2/biases'];
	var hidden2 = g.layers.dense('hidden2', hidden1, hidden2W.shape[1], function(x) {
        return g.relu(x);
    }, true, new dl.NDArrayInitializer(hidden2W), new dl.NDArrayInitializer(hidden2B));
	var softmaxW = vars['softmax_linear/weights'];
	var softmaxB = vars['softmax_linear/biases'];
	var logits = g.layers.dense('softmax', hidden2, softmaxW.shape[1], null, true, new dl.NDArrayInitializer(softmaxW), new dl.NDArrayInitializer(softmaxB));
	return [input, g.argmax(logits)];
}

```

#成果物

http://jsdo.it/ohisama1/IRrp

以上