- Published on
๐STUDY ์น ๋ธ๋ผ์ฐ์ ์ ๋จธ์ ๋ฌ๋ Tensorflow.js | ๋ก์ง์คํฑ ํ๊ท๋ฅผ ์ฌ์ฉํ ๋ถ๋ฅ
์ด์ง ๋ถ๋ฅ
Classification์ ์ง๋ํ์ต ์ค ํ ์ข ๋ฅ. ์ด์ง ๋ถ๋ฅ๋ linearly seperatable ๋ฌธ์ ์ ํด๋นํ๋ค.์๊ทธ๋ชจ์ด๋ ํจ์๋ (0,1)์ ๋ฒ์๋ฅผ ๊ฐ๋๋ค. ๋ฒ ์ด์ฆ ์ ๋ฆฌ์ ๋ฐ๋ฅด๋ฉด ์๋์ ๊ฐ์ด ์ ์๋๋ค.
- logit function: ์๊ทธ๋ชจ์ด๋ ํจ์์ ์ญํจ์, ๋ ํด๋์ค์ ์ํ ํ๋ฅ ์ ๋ํ ๋น์จ
๊ฐ ํด๋์ค ๋ณ ์กฐ๊ฑด๋ถ ํ๋ฅ ์ ๋ก์ง์คํฑ ์๊ทธ๋ชจ์ด๋ ํจ์๋ก ํํ์ด ๊ฐ๋ฅํจ.
- ๋ชจ๋ธ์ ์
๋ ฅ ๋ฒกํฐ์ ๊ฐ์ค์น ํ๋ผ๋ฏธํฐ ์ฌ์ด์ ์ ํ ๊ด๊ณ ์ ์ ์กฐ๊ฑด:
- ๊ฐ ํด๋์ค์ ๊ฐ์ฐ์์ ๋ถํฌ๊ฐ ๋์ผํ ๊ณต๋ถ์ฐ ํ๋ ฌ์ ๊ฐ์ ธ์ผ ํจ.
- ์ด๋ ๋ชจ๋ ์ํ์ด ๊ฐ์ ๊ณต๋ถ์ฐ ํ๋ ฌ์ ๊ณต์ ํ๋ ๊ฐ์ฐ์์ ๋ถํฌ์์ ์์ฑ๋์๋ค๋ ๊ฐ์ ์ผ๋ก ๋ชฉ์ ํจ์๋ฅผ ์ป์ ์ ์์.
- loss function: cross-entropy โก ๋ฐ๋ณต ์ต์ ํ ์ฒ๋ฆฌํ ๋ ์ ์ฉ
CORE API๋ฅผ ์ด์ฉํ Logistic regression
import * as tf from "@tensorflow/tfjs";
const N = 100;
const c1 = tf.randomNormal([N, 2]).add([2.0, 1.0]);
const c2 = tf.randomNormal([N, 2]).add([-2.0, -1.0]);
const l1 = tf.ones([N, 1]);
const l2 = tf.ones([N, 1]);
const xs = c1.concat(c2);
const input = xs.concat([2 * N, 1], 1); // 1 as bias
const ys = l1.concat(l2);
const w = tf.randomNormal([3, 1]).sub(-0.5).variable();
const f_x = (x) => {
return tf.sigmoid(x.matmul(w));
};
const loss = (pred, label) => {
return tf.losses.sigmoidCrossEntropy(pred, label).asScalar();
};
const optimizer = tf.train.adam(0.07);
for (let i = 0; i < 100; i++) {
const l = optimizer.minimize(() => loss(f_x(input), ys), true);
losses.push(l.dataSync());
}
Layers API๋ฅผ ์ด์ฉํ Logistic regression
const model = tf.sequential();
model.add(tf.layers.dense(units: 1, batchInputShape: [null,2]));
const loss = (pred, label) => {
return tf.losses.sigmoidCrossEntropy(pred, label).asScalar();
};
model.compile({
loss: loss,
optimizer: 'adam',
metrics: ['accuracy']
});
async function training() {
const history = await model.fit(xs, ys, {epochs: 100});
...
model.predict(xs);
}
training();
machinelearning.js
๋ฅผ ์ด์ฉํ์ฌ ๋ก์ง์คํฑ ํ๊ท ๋ชจ๋ธ ํ๋ จ๋ ๊ฐ๋ฅํ๋ค.
Read More
- Authors
- Name
- Amelia Young
- GitHub
- @ameliacode