机器学习概念及安装day01
机器学习是什么?
- 机器学习是对能通过经验自动改进的计算机算法的研究。
- 机器学习是用数据或以往的经验,以此优化计算机程序的性能标准。
为何要用机器学习?
- 有些棘手问题只能用机器学习来解决。
- 获取数据比编写规则更加容易。
- GPU等计算能力显著提升。
机器学习如何运作?
- 神经网络(重点)
- 决策树、支持向量机、贝叶斯分类器、强化学习…
#### 什么是神经网络?
- 指的是人工神经网络。
- 人工神经网络是一种运算模型(就是输入输出的映射),由大量的节点(或神经元)之间相互联接构成。
- 每个神经元里存储着若干权重,偏置和一个激活函数。
- 输入乘上权重加上偏置,经过激活函数得到输出
- 激活函数用于添加一些非线性变换。
- 神经网络通常包括一个输入层,若干隐藏层,一个输出层
- 输出层通常不用于计算神经网络的层数
安装TensorFlow
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
标签将引入方式不带提示
依赖引入:
npm install @tensorflow/tfjs
在node.js中安装:
- 安装带有原生c++绑定的TensorFlow.js(底层支持,效率很快)
- 安装纯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
- 中文名叫张量
- 张量是向量和矩阵向更高维度的推广
- 相当于多维数组(嵌套数组)
定义数组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);
神经网络数据结构设计
- 神经网络的每一层要存储N维数据
- N层的For循环运算
- 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();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?