상세 컨텐츠

본문 제목

2023.05.18

인공지능

by 연을 2023. 5. 18. 11:26

본문

728x90

학습량을 많이 한다고 해서 좋은 것은 아니다.

실제 데이터 = 자바데이터

[example]

<!DOCTYPE html>
<html>
<head>
<title>TensorFlow.js Tutorial - boston housing </title>
<!-- Import TensorFlow.js -->
<script
src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis"></script>
<script src="10.3.js"></script>
</head>
<body>
<script>
var 보스톤_원인 = [
[0.00632,18,2.31,0,0.538,6.575,65.2,4.09,1,296,15.3,396.9,4.98],
[0.02731,0,7.07,0,0.469,6.421,78.9,4.9671,2,242,17.8,396.9,9.14]
];
var 보스톤_결과 = [[24],[21.6]];
// 1. 과거의 데이터를 준비합니다.
var 원인 = tf.tensor(보스톤_원인);
var 결과 = tf.tensor(보스톤_결과);
// 2. 모델의 모양을 만듭니다.
var X = tf.input({ shape: [13] });
var H1 = tf.layers.dense({ units: 13, activation:'relu' }).apply(X);
var H2 = tf.layers.dense({ units: 13, activation:'relu' }).apply(H1);
var Y = tf.layers.dense({ units: 1 }).apply(H2);
var model = tf.model({ inputs: X, outputs: Y });
var compileParam = { optimizer: tf.train.adam(), loss:
tf.losses.meanSquaredError }
model.compile(compileParam);
tfvis.show.modelSummary({name:'요약', tab:'모델'}, model);
// 3. 데이터로 모델을 학습시킵니다.
// var fitParam = {epochs: 100}
var _history = [];
var fitParam = {
epochs: 100,
callbacks:{
onEpochEnd:
function(epoch, logs){
console.log('epoch', epoch, logs, 'RMSE=>',
Math.sqrt(logs.loss));
_history.push(logs);
tfvis.show.history({name:'loss', tab:'역사'}, _history,
['loss']);
}
}
} // loss 추가 예제
model.fit(원인, 결과, fitParam).then(function (result) {
// 4. 모델을 이용합니다.
// 4.1 기존의 데이터를 이용
var 예측한결과 = model.predict(원인);
예측한결과.print();
});
// 4.2 새로운 데이터를 이용
// var 다음주온도 = [15,16,17,18,19]
// var 다음주원인 = tf.tensor(다음주온도);
// var 다음주결과 = model.predict(다음주원인);
// 다음주결과.print();
</script>
</body>
</html>

[결과]

[pandas]

<html>
<head>
<!-- 버전에 따라서 예제가 동작하지 않는 경우가 있습니다. 아래 버전을 권장합니다. -->
<script src="https://cdn.jsdelivr.net/npm/danfojs@0.1.2/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.4.0/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis"></script>
</head>
<body>
<script>
//dfd는 danfojs의 모듈의 이름이다. read_csv를 이용하여 해당 링크의 데이터를 읽어온다.
//웹에서 제공하는 분꽃 데이터를 읽어오는 부분이다.
dfd.read_csv('https://raw.githubusercontent.com/blackdew/tensorflow1/master/csv/iris.csv').then(function(data){
console.log(data);
data.print(); //읽어온 데이터를 표형태로 출력한다.
//종속변수 컬럼 선정
독립변수 = data.loc({columns:['꽃잎길이','꽃잎폭','꽃받침길이','꽃받침폭']});
독립변수.print(); //선정한 독립변수 출력
//하나의 종속변수를 여러개로 분리 0과 1의 데이터를 넣는다.
var encoder = new dfd.OneHotEncoder();
종속변수 = encoder.fit(data['품종']);
data['품종'].print();// 종속변인 품종 1개의 컬럼 출력
종속변수.print();// 하나의 칼럼을 여러 개의 컬럼으로 01의 값을 넣어 분리한 컬럼 출력
//입력층 4개의 컬럼 설정
var X = tf.input({ shape: [4]});
//딥러닝을 위한 히든 레이어
var H = tf.layers.dense({ units: 4, activation:'relu'}).apply(X);
//출력층 3개의 컬럼 설정
var Y = tf.layers.dense({ units: 3, activation:'softmax'}).apply(H);
//모델 생성
model = tf.model({ inputs: X, outputs: Y });
var compileParam = { optimizer: tf.train.adam(), loss: 'categoricalCrossentropy',
metrics:['accuracy'] }
model.compile(compileParam);
//결과 출력
tfvis.show.modelSummary({name:'요약', tab:'모델'}, model);
//결과 출력시 왼쪽가 같은모델을 얻을 수 있따.
// 3. 데이터로 모델을 학습시킵니다.
_history = [];
var fitParam = {
epochs: 100, //몇번 학습할 것인가
callbacks:{
onEpochEnd:
function(epoch, logs){
console.log('epoch', epoch, logs, 'RMSE=>', Math.sqrt(logs.loss));
_history.push(logs);
tfvis.show.history({name:'loss', tab:'역사'}, _history, ['loss']);
tfvis.show.history({name:'accuracy', tab:'역사'}, _history, ['acc']);
}
}
}
//학습결과 확인
model.fit(독립변수.tensor, 종속변수.tensor, fitParam).then(function (result) {
// 4. 모델을 이용합니다.
// 4.1 기존의 데이터를 이용
예측한결과 = new dfd.DataFrame(model.predict(독립변수.tensor));
예측한결과.print();
종속변수.print();
});
})
</script>
</body>
</html>

[결과]

이렇게 표 형태로 여러개 나온다.

 

 

 

728x90

'인공지능' 카테고리의 다른 글

2023.05.17  (0) 2023.05.17
2023.05.16  (0) 2023.05.16

관련글 더보기