本文整理汇总了TypeScript中deeplearn.Graph.subtract方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Graph.subtract方法的具体用法?TypeScript Graph.subtract怎么用?TypeScript Graph.subtract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deeplearn.Graph
的用法示例。
在下文中一共展示了Graph.subtract方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
export const learnXOR = async () => {
const iterations = getRandomIntegerInRange(800, 1000);
const timeStart: number = performance.now();
let loss: number;
let cost: dl.Scalar;
const graph = new dl.Graph();
const input = graph.placeholder('input', [2]);
const y = graph.placeholder('y', [1]);
const hiddenLayer = graph.layers.dense(
'hiddenLayer', input, 10, (x: dl.SymbolicTensor) => graph.relu(x), true);
const output = graph.layers.dense(
'outputLayer', hiddenLayer, 1, (x: dl.SymbolicTensor) => graph.sigmoid(x),
true);
const costTensor = graph.reduceSum(graph.add(
graph.multiply(
graph.constant([-1]),
graph.multiply(
y, graph.log(graph.add(output, graph.constant([EPSILON]))))),
graph.multiply(
graph.constant([-1]),
graph.multiply(
graph.subtract(graph.constant([1]), y),
graph.log(graph.add(
graph.subtract(graph.constant([1]), output),
graph.constant([EPSILON])))))));
const session = new dl.Session(graph, dl.ENV.math);
const optimizer = new dl.SGDOptimizer(0.2);
const inputArray = [
dl.tensor1d([0, 0]), dl.tensor1d([0, 1]), dl.tensor1d([1, 0]),
dl.tensor1d([1, 1])
];
const targetArray =
[dl.tensor1d([0]), dl.tensor1d([1]), dl.tensor1d([1]), dl.tensor1d([0])];
const shuffledInputProviderBuilder =
new dl.InCPUMemoryShuffledInputProviderBuilder([inputArray, targetArray]);
const [inputProvider, targetProvider] =
shuffledInputProviderBuilder.getInputProviders();
const feedEntries =
[{tensor: input, data: inputProvider}, {tensor: y, data: targetProvider}];
/**
* Train the model
*/
await dl.tidy(async () => {
for (let i = 0; i < iterations; i += 1) {
cost = session.train(
costTensor, feedEntries, 4, optimizer, dl.CostReduction.MEAN);
}
loss = await cost.val();
});
const result = [];
/**
* Test the model
*/
for (let i = 0; i < 4; i += 1) {
const inputData = inputArray[i];
const expectedOutput = targetArray[i];
const val = session.eval(output, [{tensor: input, data: inputData}]);
result.push({
input: await inputData.data(),
expected: await expectedOutput.data(),
output: await val.data()
});
}
const timeEnd: number = performance.now();
const time = timeEnd - timeStart;
return {iterations, loss, time, result};
};