https://www.tensorflow.org/js/tutorials?hl=ko 

 

- 브라우저에서 스크립트를 이용한 실행 샘플

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>텐서플로우 js 테스트</title>
	
	<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
	
	<script>
		let model 	=	null;
		let xs 		=	null;
		let ys 		=	null;
		
		window.addEventListener("load", () => {

			// Create a simple model.
			model = tf.sequential();
			model.add(tf.layers.dense({units: 1, inputShape: [1]}));

			// Prepare the model for training: Specify the loss and the optimizer.
			model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

			// Generate some synthetic data for training. (y = 2x - 1)
			xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
			ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);

			// Train the model using the data.
			model.fit(xs, ys, {epochs: 250});	

			console.log("This function is executed once the page is fully loaded");		

		});
	  
		function exec()
		{
			//console.log( model.predict(tf.tensor2d([20], [1, 1])).dataSync() );

			document.getElementById('result').innerText =
			model.predict(tf.tensor2d([20], [1, 1])).dataSync();		
		}//	end function
		
	</script>	
	
	
</head>
<body>

<button onclick="exec()">학습된 모델을 이용한 계산 실행</button>
<div id="result"></div>
</body>
</html>