@tensorflow/tfjs 初探
- 一维张量+标量
- 二维张量+标量
- 三维张量+标量
名词标注翻译
- tensor 张量
张量是向量或矩阵等更高维度的核心数据结构,常用的有标量、一维、二维、三维和四维张量
- scalar 标量
- buffer 缓冲区
Tensor方法
- flatten()
- asScalar()
- as1D()
- as2D()
- as3D()
- as4D()
- asType()
- buffer()
- data()
- dataSync()
- dispose()
- toFloat()
- toInt()
- toBool()
- print(verbose: boolean)
- reshape(newShape)
- reshapeAs(x: Tensor)
- expandDims (axis?)
- squeeze (axis?)
- clone ()
- toString ()
- Model
- compile()
- evaluate()
- predict()
- predictOnBatch()
- fit()
- Sequential
- add(layer)
- evaluate (x, y, config?)
- predict (x, config?)
- fit (x, y, config?)
- Layer
- apply (inputs, kwargs?)
- Optimizer
- minimize(f, returnCost?, varList?)
封装成自己 service
- tf.service.ts
import { Injectable } from "@angular/core";
import {scalar,Scalar,Tensor,tensor,Tensor1D,tensor1d,Tensor2D,tensor2d,Tensor3D,tensor3d,Tensor4D,tensor4d,buffer,TensorBuffer,fill,fromPixels,linspace,oneHot,ones,onesLike,print,randomNormal,randomUniform,range,truncatedNormal,Rank,variable,Variable,zeros,zerosLike,cast,expandDims,pad,reshape,squeeze,concat,gather,reverse,slice,stack,tile,Sequential,sequential,SequentialConfig,model,Model,SymbolicTensor,input,loadModel
} from "@tensorflow/tfjs";
import { Subject } from "rxjs/Subject";
import { DType } from "@tensorflow/tfjs-layers/dist/types";
export declare type Shape = number[];
export interface InputConfig {shape?: Shape;batchShape?: Shape;name?: string;dtype?: DType;sparse?: boolean;
}
export interface ContainerConfig {inputs: SymbolicTensor | SymbolicTensor[];outputs: SymbolicTensor | SymbolicTensor[];name?: string;
}
export interface ShapeMap {R0: number[];R1: [number];R2: [number, number];R3: [number, number, number];R4: [number, number, number, number];
}
export interface DataTypeMap {float32: Float32Array;int32: Int32Array;bool: Uint8Array;
}
export declare type DataType = keyof DataTypeMap;/**
- tensor 张量
- scalar 标量
*/ ()
export class TfService {constructor() {}tensor(values: any, shape?: number[], dtype?: DType): Tensor {return tensor(values, shape, dtype);}// 生成一维张量tensor1d(arr: any[], dtype?: DType): Tensor1D {return tensor1d(arr, dtype);}// 生成二维张量tensor2d(arr: any[][], shape?: [number, number], dtype?: DType): Tensor2D {return tensor2d(arr, shape, dtype);}// 生成三维张量tensor3d(arr: any[][][],shape?: [number, number, number],dtype?: DType): Tensor3D {return tensor3d(arr, shape, dtype);}// 生成四维张量tensor4d(arr: number[][][][],shape?: [number, number, number, number],dtype?: DType): Tensor4D {return tensor4d(arr, shape, dtype);}// 生成标量scalar(value: number | boolean,dtype?: "float32" | "int32" | "bool"): Scalar {return scalar(value);}// 缓冲区buffer(shape: number[], dtype?: DType, values?: any): TensorBuffer<any> {return buffer(shape, dtype, values);}// 复制一个 张量clone(x: Tensor): Tensor {return x.clone();}// 填充获取 张量fill(shape: number[], value: number, dtype?: DType): Tensor {return fill(shape, value, dtype);}// 通过图片创建一个 3D张量fromPixels(pixels: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement,numChannels?: number): Tensor3D {return fromPixels(pixels, numChannels);}// 制定区间返回一个均匀分布的一维张量linspace(start, stop, num): Tensor1D {return linspace(start, stop, num);}// 在制定位置放置制定数值 创建一个二维张量oneHot(indices: Tensor1D,depth: number,onValue?: number,offValue?: number): Tensor2D {return oneHot(indices, depth, onValue, offValue);}// 制定形状填充1ones(shape: number[], dtype?: DType): Tensor {return ones(shape, dtype);}// 创建一个与之相同形状的填充为1的张量onesLike(x: Tensor): Tensor {return onesLike(x);}// 打印数据print(x: Tensor, verbose?: boolean): void {return print(x, verbose);}// 根据参数随机填充张量randomNormal(shape: number[],mean?: number,stdDev?: number,dtype?: "float32" | "int32",seed?: number): Tensor {return randomNormal(shape, mean, stdDev, dtype, seed);}// 制定范围填充张量randomUniform
}
实例
创建一个2行3列的矩阵
const a &#61; this.tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], [2, 3]);
a.print();
/**
Tensor
[[1 , 2 , 3 ],[10, 20, 30]
]
*/