TensorFlow.js 튜토리얼

TensorFlow.js가 무엇인가요?

Machine Learning 을 위한 인기 있는 JavaScript 라이브러리 .

브라우저 에서 기계 학습 모델을 훈련하고 배포 하겠습니다 .

모든 웹 애플리케이션 에 기계 학습 기능을 추가할 수 있습니다 .

텐서플로우 사용

TensorFlow.js를 사용하려면 HTML 파일에 다음 스크립트 태그를 추가하세요.

예시

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>

항상 최신 버전을 사용하려면 다음을 사용하세요.

실시예 2

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

TensorFlow는 Google 내부용 Google Brain Team 에서 개발 했지만 2015년에 공개 소프트웨어로 출시되었습니다.

2019년 1월 Google 개발자는 TensorFlow의 JavaScript 구현인 TensorFlow.js를 출시했습니다.

Tensorflow.js는 Python으로 작성된 원본 TensorFlow 라이브러리와 동일한 기능을 제공하도록 설계되었습니다.


텐서

TensorFlow.js 는 Tensor를 정의하고 작동하기 위한 JavaScript 라이브러리 입니다 .

Tensor는 다차원 배열과 거의 동일합니다.

Tensor는 (하나 이상의) 차원 모양의 숫자 값을 포함합니다.

Tensor에는 다음과 같은 주요 속성이 있습니다.

재산설명
dtype데이터 유형
계급차원의 수
모양각 차원의 크기

텐서 생성

Tensor는 모든 N차원 배열 에서 생성할 수 있습니다 .

실시예 1

const tensorA = tf.tensor([[1, 2], [3, 4]]);

실시예 2

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);


텐서 모양

Tensor는 배열모양 매개변수에서도 생성할 수 있습니다.

예 1

const shape = [2, 2];
const tensorA = tf.tensor([1, 2, 3, 4], shape);

예2

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]);

예3

const tensorA = tf.tensor([[1, 2], [3, 4]], [2, 2]);


텐서 데이터 유형

Tensor는 다음과 같은 데이터 유형을 가질 수 있습니다.

  • 부울
  • int32
  • float32(기본값)
  • 콤플렉스64

텐서를 생성할 때 데이터 유형을 세 번째 매개변수로 지정할 수 있습니다.

예시

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2], "int32");
/*
Results:
tensorA.rank = 2
tensorA.shape = 2,2
tensorA.dtype = int32
*/


텐서 값 검색

tensor.data() 를 사용하여 텐서 뒤의 데이터를 가져올 수 있습니다 .

예시

const tensorA = tf.tensor([[1, 2], [3, 4]]);
tensorA.data().then(data => display(data));

// Result: 1,2,3,4
function display(data) {
  document.getElementById("demo").innerHTML = data;
}

tensor.array() 를 사용하여 텐서 뒤의 배열을 얻을 수 있습니다 .

예시

const tensorA = tf.tensor([[1, 2], [3, 4]]);
tensorA.array().then(array => display(array[0]));

// Result: 1,2
function display(data) {
  document.getElementById("demo").innerHTML = data;
}