本文整理汇总了TypeScript中deeplearn.Graph.matmul方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Graph.matmul方法的具体用法?TypeScript Graph.matmul怎么用?TypeScript Graph.matmul使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deeplearn.Graph
的用法示例。
在下文中一共展示了Graph.matmul方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: intro
// This file parallels (some of) the code in the introduction tutorial.
/**
* 'Math with WebGL backend' section of tutorial
*/
async function intro() {
const a = dl.tensor2d([1.0, 2.0, 3.0, 4.0], [2, 2]);
const b = dl.tensor2d([0.0, 2.0, 4.0, 6.0], [2, 2]);
const size = dl.scalar(a.size);
// Non-blocking math calls.
const average = a.sub(b).square().sum().div(size);
console.log(`mean squared difference: ${await average.val()}`);
/**
* 'Graphs and Tensors' section of tutorial
*/
const g = new dl.Graph();
// Placeholders are input containers. This is the container for where we
// will feed an input Tensor when we execute the graph.
const inputShape = [3];
const inputTensor = g.placeholder('input', inputShape);
const labelShape = [1];
const labelTensor = g.placeholder('label', labelShape);
// Variables are containers that hold a value that can be updated from
// training.
// Here we initialize the multiplier variable randomly.
const multiplier = g.variable('multiplier', dl.randomNormal([1, 3]));
// Top level graph methods take Tensors and return Tensors.
const outputTensor = g.matmul(multiplier, inputTensor);
const costTensor = g.meanSquaredCost(labelTensor, outputTensor);
// Tensors, like Tensors, have a shape attribute.
console.log(outputTensor.shape);
/**
* 'dl.Session and dl.FeedEntry' section of the tutorial.
*/
const learningRate = .00001;
const batchSize = 3;
const session = new dl.Session(g, dl.ENV.math);
const optimizer = dl.train.sgd(learningRate);
const inputs: dl.Tensor1D[] = [
dl.tensor1d([1.0, 2.0, 3.0]), dl.tensor1d([10.0, 20.0, 30.0]),
dl.tensor1d([100.0, 200.0, 300.0])
];
const labels: dl.Tensor1D[] =
[dl.tensor1d([4.0]), dl.tensor1d([40.0]), dl.tensor1d([400.0])];
// Shuffles inputs and labels and keeps them mutually in sync.
const shuffledInputProviderBuilder =
new dl.InCPUMemoryShuffledInputProviderBuilder([inputs, labels]);
const [inputProvider, labelProvider] =
shuffledInputProviderBuilder.getInputProviders();
// Maps tensors to InputProviders.
const feedEntries: dl.FeedEntry[] = [
{tensor: inputTensor, data: inputProvider},
{tensor: labelTensor, data: labelProvider}
];
const NUM_BATCHES = 10;
for (let i = 0; i < NUM_BATCHES; i++) {
// Wrap session.train in a scope so the cost gets cleaned up
// automatically.
await dl.tidy(async () => {
// Train takes a cost tensor to minimize. Trains one batch. Returns the
// average cost as a dl.Scalar.
const cost = session.train(
costTensor, feedEntries, batchSize, optimizer, dl.CostReduction.MEAN);
console.log(`last average cost (${i}): ${await cost.val()}`);
});
}
const testInput = dl.tensor1d([0.1, 0.2, 0.3]);
// session.eval can take Tensors as input data.
const testFeedEntries: dl.FeedEntry[] =
[{tensor: inputTensor, data: testInput}];
const testOutput = session.eval(outputTensor, testFeedEntries);
console.log('---inference output---');
console.log(`shape: ${testOutput.shape}`);
console.log(`value: ${await testOutput.val(0)}`);
}