LoginSignup
1

More than 5 years have passed since last update.

jsdoでtensorflow.js その2

Posted at

概要

jsdoでtensorflow.jsやってみた。
sandbox2作ってみた。

写真

成果物

サンプルコード

const shape = [2, 3];
const a = tf.tensor([1, 2, 3, 4, 5, 6], shape);
a.print();
const b = tf.tensor([[1, 2], [3, 4], [5, 6]]);
b.print();
const shape = [2, 3];
const a = tf.tensor([1, 2, 3, 4, 5, 6], shape);
a.print();
const b = tf.tensor([[1, 2], [3, 4], [5, 6]]);
b.print();
tf.scalar(3.14, 'int32').print();
tf.scalar(3.14, 'float32').print();
tf.scalar(3.14, 'int32').print();
tf.scalar(3.14, 'float32').print();
tf.tensor1d([1, 2, 3]).print();
tf.tensor1d([1, 2, 3]).print();
tf.tensor2d([[1, 2, 3], [4, 5, 6]]).print();
tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]).print();
tf.tensor2d([[1, 2, 3], [4, 5, 6]]).print();
tf.tensor2d([1, 2, 3, 4, 5, 6], [3, 2]).print();
tf.tensor3d([[[1], [2], [3]], [[4], [5], [6]]]).print();
tf.tensor3d([1, 2, 3, 4, 5, 6], [3, 2, 1]).print();
tf.tensor3d([[[1], [2], [3]], [[4], [5], [6]]]).print();
tf.tensor3d([1, 2, 3, 4, 5, 6], [3, 2, 1]).print();
tf.zeros([3, 5]).print();
const x = tf.tensor([2, 3, 4, 5]);
tf.zerosLike(x).print();
tf.zeros([3, 5]).print();
const x = tf.tensor([2, 3, 4, 5]);
tf.zerosLike(x).print();
tf.fill([2, 3], 8).print();
tf.fill([2, 3], 8).print();
Elements from sequences
tf.linspace(1, 9, 9).print();
tf.linspace(1, 9, 5).print();
tf.linspace(1, 9, 9).print();
tf.linspace(1, 9, 5).print();
tf.range(0, 9).print();
tf.range(0, 9, 3).print();
tf.range(0, 9).print();
tf.range(0, 9, 3).print();
tf.randomNormal([2, 2], 0, 1).print();
tf.randomNormal([2, 2], 0, 1).print();
tf.randomUniform([2, 2], 1, 2).print();
tf.randomUniform([2, 2], 1, 2).print();
const tensor = tf.zeros([3]);
const tvar = tf.variable(tensor);
tvar.print();
tvar.assign(tf.tensor1d([1, 2, 3]));
tvar.print();
const tensor = tf.zeros([3]);
const tvar = tf.variable(tensor);
tvar.print();
tvar.assign(tf.tensor1d([1, 2, 3]));
tvar.print();
const x1 = tf.tensor2d([[1, 1], [1, 1]]);
const y11 = tf.tensor2d([[2, 2], [2, 2]]);
const y12 = tf.scalar(5);
tf.add(x1, y11).print();
x1.add(y11).print();
tf.add(x1, y12).print();
x1.add(y12).print();
const x1 = tf.tensor2d([[1, 1], [1, 1]]);
const y11 = tf.tensor2d([[2, 2], [2, 2]]);
const y12 = tf.scalar(5);
tf.add(x1, y11).print();
x1.add(y11).print();
tf.add(x1, y12).print();
x1.add(y12).print();
const x2 = tf.tensor1d([2, 4, 3]);
const y21 = tf.tensor1d([1, 2, 9]);
const y22 = tf.scalar(3);
tf.maximum(x2, y21).print();
x2.maximum(y21).print();
tf.maximum(x2, y22).print();
x2.maximum(y22).print();
const x2 = tf.tensor1d([2, 4, 3]);
const y21 = tf.tensor1d([1, 2, 9]);
const y22 = tf.scalar(3);
tf.maximum(x2, y21).print();
x2.maximum(y21).print();
tf.maximum(x2, y22).print();
x2.maximum(y22).print();
const x3 = tf.tensor1d([2, 4, 3]);
const y31 = tf.tensor1d([1, 2, 9]);
const y32 = tf.scalar(2);
tf.squaredDifference(x3, y31).print();
x3.squaredDifference(y31).print();
tf.squaredDifference(x3, y32).print();
x3.squaredDifference(y32).print();
const x3 = tf.tensor1d([2, 4, 3]);
const y31 = tf.tensor1d([1, 2, 9]);
const y32 = tf.scalar(2);
tf.squaredDifference(x3, y31).print();
x3.squaredDifference(y31).print();
tf.squaredDifference(x3, y32).print();
x3.squaredDifference(y32).print();
const x4 = tf.tensor2d([[-1, 2], [-3, 4]]);
tf.square(x4).print();
x4.square().print();
tf.sqrt(x4).print();
x4.sqrt().print();
const x4 = tf.tensor2d([[-1, 2], [-3, 4]]);
tf.square(x4).print();
x4.square().print();
tf.sqrt(x4).print();
x4.sqrt().print();
const x5 = tf.tensor1d([-1, 2, -3]);
tf.abs(x5)
x5.abs().print();
const x5 = tf.tensor1d([-1, 2, -3]);
tf.abs(x5)
x5.abs().print();
const x6 = tf.tensor1d([-1, 2, -3, 4]);
tf.relu(x6).print();
x6.relu().print();
const x6 = tf.tensor1d([-1, 2, -3, 4]);
tf.relu(x6).print();
x6.relu().print();
const x7 = tf.tensor2d([1, 2], [1, 2]);
const y7 = tf.tensor2d([1, 2, 3, 4], [2, 2]);
tf.matMul(x7, y7).print();
x7.matMul(y7).print();
const x7 = tf.tensor2d([1, 2], [1, 2]);
const y7 = tf.tensor2d([1, 2, 3, 4], [2, 2]);
tf.matMul(x7, y7).print();
x7.matMul(y7).print();
const x8 = tf.tensor1d([1, 2, 3]);
const y8 = tf.tensor1d([1, 2, 3]);
tf.outerProduct(x8, y8).print();
const x8 = tf.tensor1d([1, 2, 3]);
const y8 = tf.tensor1d([1, 2, 3]);
tf.outerProduct(x8, y8).print();
const x9 = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 3, 4]);
x9.print(true);
x9.transpose().print(true);
x9.transpose([0, 2, 1]).print(true);
x9.transpose([1, 2, 0]).print(true);
const x9 = tf.tensor3d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 3, 4]);
x9.print(true);
x9.transpose().print(true);
x9.transpose([0, 2, 1]).print(true);
x9.transpose([1, 2, 0]).print(true);
const x = tf.tensor1d([-3, 4]);
x.norm().print();
x.norm(2).print();
x.norm(1).print();
x.norm('euclidean').print();
x.norm(Infinity).print();
x.norm(-Infinity).print();
const x = tf.tensor1d([-3, 4]);
x.norm().print();
x.norm(2).print();
x.norm(1).print();
x.norm('euclidean').print();
x.norm(Infinity).print();
x.norm(-Infinity).print();
const y = tf.tensor2d([[-3, 4], [-5, 12]]);
y.norm().print();
y.norm(2).print();
y.norm(1).print();
y.norm('euclidean').print();
y.norm(Infinity).print();
y.norm(-Infinity).print();
const y = tf.tensor2d([[-3, 4], [-5, 12]]);
y.norm().print();
y.norm(2).print();
y.norm(1).print();
y.norm('euclidean').print();
y.norm(Infinity).print();
y.norm(-Infinity).print();
const y = tf.tensor2d([[-3, 4], [-5, 12]]);
y.norm(2, 0).print();
y.norm(2, 1).print();
y.norm(1, 0).print();
y.norm(1, 1).print();
y.norm(Infinity, 0).print();
y.norm(Infinity, 1).print();
y.norm(Infinity, [0, 1]).print();
y.norm(-Infinity, 0).print();
y.norm(-Infinity, 1).print();
y.norm(-Infinity, [0, 1]).print();
const y = tf.tensor2d([[-3, 4], [-5, 12]]);
y.norm(2, 0).print();
y.norm(2, 1).print();
y.norm(1, 0).print();
y.norm(1, 1).print();
y.norm(Infinity, 0).print();
y.norm(Infinity, 1).print();
y.norm(Infinity, [0, 1]).print();
y.norm(-Infinity, 0).print();
y.norm(-Infinity, 1).print();
y.norm(-Infinity, [0, 1]).print();
const a1 = tf.tensor1d([1, 3, 5]);
const b11 = tf.tensor1d([1, 2, 3]);
const b12 = tf.scalar(4);
tf.greater(a1, b11).print();
a1.greater(b11).print();
tf.greater(a1, b12).print();
a1.greater(b12).print();
const a1 = tf.tensor1d([1, 3, 5]);
const b11 = tf.tensor1d([1, 2, 3]);
const b12 = tf.scalar(4);
tf.greater(a1, b11).print();
a1.greater(b11).print();
tf.greater(a1, b12).print();
a1.greater(b12).print();
const a2 = tf.tensor1d([1, 1, 0, 0], 'bool');
const b2 = tf.tensor1d([1, 0, 1, 0], 'bool');
tf.logicalAnd(a2, b2).print();
a2.logicalAnd(b2).print();
const a2 = tf.tensor1d([1, 1, 0, 0], 'bool');
const b2 = tf.tensor1d([1, 0, 1, 0], 'bool');
tf.logicalAnd(a2, b2).print();
a2.logicalAnd(b2).print();
const a3 = tf.tensor1d([1, 3, 5, 7]);
const b3 = tf.tensor1d([2, 3, 4, 9]);
tf.where(tf.greater(a3, b3), a3, b3).print();
const a4 = tf.tensor1d([1, 3, 5, 7]);
const b4 = tf.scalar(4);
const alt = tf.fill([4], 9999);
tf.where(tf.less(a4, b4), a4, alt).print();
const a3 = tf.tensor1d([1, 3, 5, 7]);
const b3 = tf.tensor1d([2, 3, 4, 9]);
tf.where(tf.greater(a3, b3), a3, b3).print();
const a4 = tf.tensor1d([1, 3, 5, 7]);
const b4 = tf.scalar(4);
const alt = tf.fill([4], 9999);
tf.where(tf.less(a4, b4), a4, alt).print();

以上。

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
1