机器学习概念及安装day01

机器学习是什么?

  1. 机器学习是对能通过经验自动改进的计算机算法的研究。
  2. 机器学习是用数据或以往的经验,以此优化计算机程序的性能标准。

为何要用机器学习?

  1. 有些棘手问题只能用机器学习来解决。
  2. 获取数据比编写规则更加容易。
  3. GPU等计算能力显著提升。

机器学习如何运作?

  1. 神经网络(重点)
  2. 决策树、支持向量机、贝叶斯分类器、强化学习…

#### 什么是神经网络?

  1. 指的是人工神经网络。
  2. 人工神经网络是一种运算模型(就是输入输出的映射),由大量的节点(或神经元)之间相互联接构成。
  3. 每个神经元里存储着若干权重,偏置和一个激活函数。
  4. 输入乘上权重加上偏置,经过激活函数得到输出
  5. 激活函数用于添加一些非线性变换。
  6. 神经网络通常包括一个输入层,若干隐藏层,一个输出层
  7. 输出层通常不用于计算神经网络的层数

安装TensorFlow

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>

标签将引入方式不带提示

依赖引入:
npm install @tensorflow/tfjs

在node.js中安装:

  1. 安装带有原生c++绑定的TensorFlow.js(底层支持,效率很快)
  2. 安装纯JavaScript版本,这是性能方面最慢的选项。
    新建node.js
    `const tf = require('@tensorflow/tfjs');

const a = tf.tensor([1,2]);
a.print();`
运行:node node.js
打印:

Hi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
Tensor
[1, 2]

安装打包工具parcel

中文官网:
https://www.parceljs.cn/getting_started.html
npm install -g parcel-bundler

新建index2.html
<script src="./script.js"></script>

新建script.js
输入一些语法,会有智能提示
import * as tf from "@tensorflow/tfjs"; const a = tf.tensor([1,2]); a.print();

运行:
parcel index2.html

为何要用Tensor

  1. 中文名叫张量
  2. 张量是向量和矩阵向更高维度的推广
  3. 相当于多维数组(嵌套数组)

定义数组demo(分析打印属性:shape,size)
import * as tf from '@tensorflow/tfjs'; const t0 = tf.tensor(1); t0.print(); console.log(t0); const t1 = tf.tensor([1,2]); t1.print(); console.log(t1); const t2 = tf.tensor([[1,2],[3,4]]); t2.print(); console.log(t2);

神经网络数据结构设计

  1. 神经网络的每一层要存储N维数据
  2. N层的For循环运算
  3. Tensor作为高维数据结构完美解决以上问题

对比循环:
`// 传统for循环
const input = [1,2,3,4];
const w = [[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]];
const output = [0,0,0,0];
for(let i=0;i<w.length;i++){
for(let j=0;j<input.length;j++){
output[i] += input[j]*w[i][j];
}
}

console.log(output);`

运行: parcel tensor/*html

// tensor语法 tf.tensor(w).dot(tf.tensor(input)).print();

posted @ 2021-07-04 09:46  小白咚  阅读(52)  评论(0编辑  收藏  举报