Reprinted from: https://blog.csdn.net/weixin_34061042/article/details/89700664
Its one-dimensional vector arithmetic
TensorFlow.js tensor unit of the data center: a group of values consisting of a one-dimensional or multidimensional array. In TensorFlow.js, the constructor mainly one-dimensional vector: tf.tensor () and tf.tensor1d ()
It can be set () and get () function and the set values of the elements respectively acquire vector.
One-dimensional vector arithmetic function has many, as follows:
- tf.add () element corresponding to the two vectors and
- () Element corresponding to the difference between two vectors tf.sub
- () Element corresponding to the product of the two vectors tf.mul
- tf.div () is corresponding elements of two vectors
- () Corresponding to the maximum element of the two vectors tf.maximum
- () Element corresponding to the minimum of the two vectors tf.minimum
- tf.pow () element corresponding to a power of two vectors
Only part of the above, there are many more functions such as: tf.abs (), tf.sin (), tf.cos (), tf.tan (), tf.tan () and the like.
A simple example
Through a small example, the method will be described using the one-dimensional vector TensorFlow.js in.
<html> <head> <!-- Load TensorFlow.js --> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script> <!-- Place your code in the script tag below.--> <script> a = [1,2,3,4,5]; b = [2,3,4,5,6]; function show(){ const vector1 = tf.tensor(a); const vector2 = tf.tensor(b); const res = vector2.add(vector1); document.getElementById("first_element").innerHTML = "第一个元素为" + res.get(0)+ "."; document.getElementById("whole_array").innerHTML = "向量:"+res; } </script> </head> <body> <script type="text/javascript"> function printa() { document.write("a: " + a + "<br>"); } function printb() { document.write("b: " + b + "<br>"); } printa(); printb(); </script> <p id="first_element"></p> <p id="whole_array"></p> <button onclick="show()" id="show" value="show">show</button> </body> </html>
Next, we achieved slightly more complex functions, perform other operations tensor
<html> <head> <!-- Load TensorFlow.js --> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="calculate.js"></script> </head> < Body > < Center > < H2 > TensorFlow vectors (one-dimensional) Learning </ H2 > < br > < br > < div style = "width: 600px" > < div > < label class = "COL-SM-2 label-Control " > vector operation </ label > < div class =" COL-SM-10 " > < label class =" Radio-inline " > <input type="radio" name="optionsRadiosinline" value="add" checked="checked"> 加 </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="sub"> 减 </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="mul"> 乘 </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="div"> 除 </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="max"> max </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="min"> min </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="abs"> abs </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="sin"> sin </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="cos"> cos </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="tan"> tan </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="exp"> exp </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="log"> log </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="sqrt"> sqrt </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="square"> square </label> <label class="radio-inline"> <input type="radio" name="optionsRadiosinline" value="cubic"> cubic </label> <br><br> </div> </div> <div> <label for="vector1" class="col-sm-2 control-label">向量1</label> <div class="col-sm-10"> <input type="text" class="form-control" id="vector1" placeholder="向量1"> <br> </div> </div> <div> <label for="vector2" class="col-sm-2 control-label">向量2</label> <div class="col-sm-10"> <input type="text" class="form-control" id="vector2" placeholder="向量2"> <br> </div> </div> <div > <div class="col-sm-offset-2 col-sm-10"> <button class="btn btn-default" id="result">显示结果</button> <button class="btn btn-default" id="clc">清空</button> </div> </div> </div> <table class="table" style="width:600px"> <caption id="tf">运行结果</caption> <tbody> <tr class="success" id="table"> </tr> </tbody> </table> </center> </body> </html>
$ (Document) .ready (function () { var In Flag; / * In Flag =. 1 represents a unary flag = 2 indicates a binary operation * / // clear inputs two input boxes $ ( "# clc") click (. function () { $ ( "# vector1") Val ( "");. $ ( "# Vector2") Val. ( ""); }); // allows "vector 2" input box input $ ( " # vector1 ") the Click (function () {. var OP = $ (" INPUT [name = 'optionsRadiosinline']: the checked "); Val (). the Add", "Sub", "MUL", var OPS = [ " "div", "max", "min"]; IF (ops.indexOf (OP) == -1) In Flag =. 1; the else Flag = 2; // text box "Vector 2" Disable if (flag == 1) { . $ ( "# vector2") val ( ""); $ ( "INPUT [type = 'text']") each (function () {. $ ( "# Vector2") attr ( "Disabled", to true);. }); } // text box "Vector 2" Enable IF (In Flag == 2) { $ ( "INPUT [type = 'text']") each. (function () { $ ( "# Vector2") attr ( "Disabled", to false);. }); } } ); // use tensorflow.js operation function, outputs the calculation result $ (. "result #") the Click (function () { IF (in Flag ==. 1) { . vector1 var = $ ( "# vector1") Val ( ) .split ( ',') Map (Number The);. } if(flag == 2){ var vector1 = $ ( "# vector1") Val ()..toString().split(',').map(Number); Vector2 $ = var ( "# Vector2") Val () toString () Split ( ',') Map (Number The);.... (! = vector1.length vector2.length) IF Alert ( "two input vectors not the same as the length "); } // function operation using tensorflow.js IF (in Flag ==. 1 || ((in Flag == 2) && (vector1.length == vector2.length))) { var $ = OP ( "INPUT [name = 'optionsRadiosinline']: the checked") Val ();. const = pow2 tf.tensor (2) .toInt (); // calculate square const pow3 = tf.tensor (3) .toInt (); // calculation cubic switch (op) // JavaScript a switch structure { Case "add ": // addition RES = tf.tensor (vector1) .add (tf.tensor (Vector2)); BREAK; case "sub": // subtraction tf.tensor = RES (vector1) .sub (tf.tensor (Vector2)); BREAK; Case "MUL": // multiplication RES = tf.tensor (vector1) .mul (tf.tensor (Vector2)); BREAK; case "div": // division RES = tf.tensor (vector1) .div (tf.tensor (Vector2)); BREAK; Case "max": // maximum of two vectors, Element-Wise RES = TF .tensor (vector1) .maximum (tf.tensor (Vector2)); BREAK; Case" min ": // minimum two vectors value, element-wise tf.tensor = RES (vector1) .minimum (tf.tensor (Vector2)); BREAK; Case "ABS": // absolute value Case "exp": // exponential function, base e tf.tensor res = (vector1) .abs (); BREAK; Case "SiN": // sine function res = tf.tensor (vector1) .sin (); BREAK; Case "COS": // cosine function res = tf.tensor (vector1) .cos (); BREAK; Case "Tan": // tangent RES = tf.tensor (vector1) .tan (); BREAK; RES = tf.tensor (vector1) .exp (); BREAK; Case "log": // logarithmic function, base e RES = tf.tensor (vector1) .log (); BREAK; case "sqrt": // root RES = tf.tensor (vector1) .sqrt (); BREAK; Case "Square": // square RES = tf.tensor (vector1) .pow (pow2); BREAK; Case "Cubic ": // cubic RES = tf.tensor (vector1) .pow (Pow3); BREAK; default: RES = tf.tensor ([]); } $ (" Table # ") ("); / HTML. " / emptying original table data // clear the original data in the table // input results for (var I = 0; I < RES .shape; I ++) { . $ ( "TR") the append ( "<TD >"+res.get(i)+"</td>;"); } } }); });
Results are as follows:
All code is visible: GitHub