热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

@tensorflow/tfjs初探

tensorflowtfjs初探一维张量标量二维张量标量三维张量标量名词标注翻译tensor张量张量是向量或矩阵等更高维度的核心数据结构,常用的有标量、一维、二维、三

@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 标量
*/
@Injectable()
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);}// 制定范围填充张量randomUniformextends Rank>(shape: ShapeMap[R],minval?: number,maxval?: number,dtype?: DataType): Tensor {return randomUniform(shape, minval, maxval, dtype);}// 在所提供的范围内填充数字range(start: number,stop: number,step?: number,dtype?: "float32" | "int32"): Tensor1D {return range(start, stop, step, dtype);}// 具有截断正态分布的采样值truncatedNormalextends Rank>(shape: ShapeMap[R],mean?: number,stdDev?: number,dtype?: "float32" | "int32",seed?: number): Tensor {return truncatedNormal(shape, mean, stdDev, dtype, seed);}// 用所提供的初始值创建一个新变量variableextends Rank>(initialValue: Tensor,trainable?: boolean,name?: string,dtype?: DataType): Variable {return variable(initialValue, trainable, name, dtype);}// 创建一个所有元素设置为0的张量zerosextends Rank>(shape: ShapeMap[R], dtype?: DataType): Tensor {return zeros(shape, dtype);}// 创建一个张量&#xff0c;所有元素设置为0&#xff0c;其形状与给定的张量相同zerosLikeextends Tensor>(x: T): T {return zerosLike(x);}// 投射到一个新的张量。castextends Tensor>(x: T, dtype: DataType): T {return cast(x, dtype);}// 通过在张量的形状中插入一个维度来扩展秩expandDimsextends Rank>(x: Tensor, axis?: number): Tensor {return expandDims(x, axis);}// 铺垫padextends Tensor>(x: T,paddings: Array<[number, number]>,constantValue?: number): T {return pad(x, paddings, constantValue);}// 改变形状reshapeextends Rank>(x: Tensor, shape: ShapeMap[R2]): Tensor {return reshape(x, shape);}// 消除尺寸squeezeextends Tensor>(x: Tensor, axis?: number[]): T {return squeeze(x, axis);}// 合并concatextends Tensor>(tensors: T[], axis?: number): T {return concat(tensors, axis);}// 根据指数收集切片gatherextends Tensor>(x: T, indices: Tensor1D, axis?: number): T {return gather(x, indices, axis);}// 反转reverseextends Tensor>(x: T, axis?: number | number[]): T {return reverse(x, axis);}// 分割sliceextends Rank, T extends Tensor>(x: T,begin: ShapeMap[R],size: ShapeMap[R]): T {return slice(x, begin, size);}// 堆砌stackextends Tensor>(tensors: T[], axis?: number): Tensor {return stack(tensors, axis);}// 重复tileextends Tensor>(x: T, reps: number[]): T {return tile(x, reps);}// sequential模型sequential(config?: SequentialConfig): Sequential {return sequential(config);}// 创建模型model(config: ContainerConfig): Model {return model(config);}// 输入input(config: InputConfig): SymbolicTensor {return input(config);}// 加载模型loadModel(modelConfigPath: string): Promise {return loadModel(modelConfigPath);}// 相加add(a: Tensor1D | Tensor2D | Tensor3D | Tensor4D, b: Scalar) {let sub: Subject<any> &#61; new Subject();let result &#61; a.add(b).data().then(res &#61;> {sub.next(res);});return sub;}
}

实例

创建一个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]
]
*/




推荐阅读
author-avatar
fggdsfgwjff4
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有