plunkerでtensorflow.js その27
概要
plunkerでtensorflow.jsやってみた。
関数フィッティングやってみた。
写真
サンプルコード
function predict(x) {
return tf.tidy(() => {
return tf.div(tf.exp(tf.mul(tf.mul(x.sub(a), x.sub(a)), tf.neg(b))), c)
});
}
function loss(pred, label) {
return tf.losses.meanSquaredError(pred, label).mean();
}
function train(xs, ys, numIterations) {
for (let iter = 0; iter < numIterations; iter++)
{
optimizer.minimize(() => {
const pred = predict(xs);
return loss(pred, ys);
});
}
}
const a = tf.variable(tf.scalar(Math.random()+5));
const b = tf.variable(tf.scalar(Math.random()));
const c = tf.variable(tf.scalar(Math.random()));
const learningRate = 0.01;
const optimizer = tf.train.adamax(learningRate);
const numIterations = 150;
const yy = [3, 8, 28, 75, 221, 291, 255, 235, 190, 125, 70, 28, 12, 5];
const xs = tf.tensor2d([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]], [14, 1]);
const ys = tf.tensor2d([[3], [8], [28], [75], [221], [291], [255], [235], [190], [125], [70], [28], [12], [5]], [14, 1]);
train(xs, ys, numIterations);
const aa = Number(a.dataSync());
const bb = Number(b.dataSync());
const cc = Number(c.dataSync());
var values = [];
for (var i = 0; i < 14; i++)
{
values.push({
x: i,
y: yy[i],
pred: Math.exp((i - aa) * (i - aa) * - bb) / cc
});
}
const spec = {
'$schema': 'https://vega.github.io/schema/vega-lite/v2.json',
'width': 300,
'height': 300,
'data': {
'values': values
},
'layer': [{
'mark': 'point',
'encoding': {
'x': {
'field': 'x',
'type': 'quantitative'
},
'y': {
'field': 'y',
'type': 'quantitative'
}
}
}, {
'mark': 'line',
'encoding': {
'x': {
'field': 'x',
'type': 'quantitative'
},
'y': {
'field': 'pred',
'type': 'quantitative'
},
'color': {
'value': 'tomato'
}
}
}]
};
vegaEmbed('#vis', spec);
成果物
function predict(x) {
return tf.tidy(() => {
return tf.div(tf.exp(tf.mul(tf.mul(x.sub(a), x.sub(a)), tf.neg(b))), c)
});
}
function loss(pred, label) {
return tf.losses.meanSquaredError(pred, label).mean();
}
function train(xs, ys, numIterations) {
for (let iter = 0; iter < numIterations; iter++)
{
optimizer.minimize(() => {
const pred = predict(xs);
return loss(pred, ys);
});
}
}
const a = tf.variable(tf.scalar(Math.random()+5));
const b = tf.variable(tf.scalar(Math.random()));
const c = tf.variable(tf.scalar(Math.random()));
const learningRate = 0.01;
const optimizer = tf.train.adamax(learningRate);
const numIterations = 150;
const yy = [3, 8, 28, 75, 221, 291, 255, 235, 190, 125, 70, 28, 12, 5];
const xs = tf.tensor2d([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]], [14, 1]);
const ys = tf.tensor2d([[3], [8], [28], [75], [221], [291], [255], [235], [190], [125], [70], [28], [12], [5]], [14, 1]);
train(xs, ys, numIterations);
const aa = Number(a.dataSync());
const bb = Number(b.dataSync());
const cc = Number(c.dataSync());
var values = [];
for (var i = 0; i < 14; i++)
{
values.push({
x: i,
y: yy[i],
pred: Math.exp((i - aa) * (i - aa) * - bb) / cc
});
}
const spec = {
'$schema': 'https://vega.github.io/schema/vega-lite/v2.json',
'width': 300,
'height': 300,
'data': {
'values': values
},
'layer': [{
'mark': 'point',
'encoding': {
'x': {
'field': 'x',
'type': 'quantitative'
},
'y': {
'field': 'y',
'type': 'quantitative'
}
}
}, {
'mark': 'line',
'encoding': {
'x': {
'field': 'x',
'type': 'quantitative'
},
'y': {
'field': 'pred',
'type': 'quantitative'
},
'color': {
'value': 'tomato'
}
}
}]
};
vegaEmbed('#vis', spec);
以上。
Author And Source
この問題について(plunkerでtensorflow.js その27), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/72047663946d5dcb46fb著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .