LoginSignup
0

More than 5 years have passed since last update.

概要

jsdoでtensorflow.jsやってみた。
backendを、呼び出してみた。

写真

テストコード

var zeroTensor = K.getScalar(0.0);
zeroTensor.print();
var one = K.getScalar(1.0);
one.print();

サンプルコード

var K = tf.backend;
var isObject = function(value) {
    if ((Object.prototype.toString.call(value) === '[object Object]') && (!Array.isArray(value)) && (value !== null) && (typeof value !== 'undefined'))
    {
        return true;
    }
    return false;
};
var objToString = function(obj) {
    var items = JSON.stringify(obj).split(',');
    items = items.map(function(element, index, array) {
        return element.replace(/(.+:)(.*)/, function(string, capture1, capture2) {
            return capture1.replace(/\"/g, '') + capture2;
        }).replace(/:/g, ': ');
    });
    return items.join(', ');
};
var consoleExt = {};
consoleExt.originalConsoleLog = console.log;
consoleExt.result = '';
consoleExt.delimiter = ';';
consoleExt.logOutput = true;
consoleExt.log = function(message) {
    if (consoleExt.logOutput) 
    {
        alert(message);
    }
    if (isObject(message)) 
    {
        consoleExt.result += objToString(message) + consoleExt.delimiter;
    } 
    else    
    {
        consoleExt.result += message + consoleExt.delimiter;
    }
};
consoleExt.hook = function() {
    if (consoleExt.originalConsoleLog === console.log)
    {
        console.log = consoleExt.log;
    }
};
consoleExt.unhook = function() {
    if (consoleExt.originalConsoleLog !== console.log) 
    {
        console.log = consoleExt.originalConsoleLog;
    }
};
consoleExt.hook();
consoleExt.result = '';
consoleExt.logOutput = true;

var out = document.getElementById("out");
var src = document.getElementById("src"); 
out.innerHTML = '';
function run() {
    try
    {
        eval(src.value);
    }
    catch(e)
    {
        var b = document.createElement('b');
        b.appendChild(document.createTextNode(e));
        b.style.color = '#F00';
        out.appendChild(b);
    }
}

成果物

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0