点错误:输入必须全部为1或2,但得到的等级为2和0。 我尝试做点积时被抛出
我试图重塑它,但是那里没有运气
var xs= tf.randomUniform([20,1],0,150,'float32');
//console.log(xs.print());
var noise = tf.randomUniform([20,1],0,10 ,'float32');
var targets = ((xs.mul(3)).add(9)).add(noise);
//console.log(targets.print());
var c = document.getElementById("myCanvas");
for(var i=0;i<20;i++){
var x = xs.slice([i, 0], 1).as1D().dataSync()[0];
var y = targets.slice([i, 0], 1).as1D().dataSync()[0];
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(x,y,4,0,2*Math.PI);
ctx.stroke();
ctx.fillStyle = "Blue";
ctx.fill();
if(i<19){
var x2 = xs.slice([i+1, 0], 1).as1D().dataSync()[0];
var y2 = targets.slice([i+1, 0], 1).as1D().dataSync()[0];
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
ctx.strokeStyle = "#02e5f9";
ctx.stroke();
}
}
var weights = tf.randomUniform([1,1],-0.1,0.1,'float32');
var baises = tf.randomUniform([1],-0.1,0.1,'float32');
var learning_rate =0.02;
var outputs;
var delta;
var loss;
var deltas_scaled;
for(var i=0;i<20;i++){
outputs=(xs.dot(weights)).add(baises);
delta = targets.sub(outputs);
loss = ((outputs.squaredDifference(targets)).sum()).div(2).div(20);
console.log("Loss::"+loss);
deltas_scaled = delta.div(20);
console.log("deltas sc: ");
console.log(deltas_scaled.reshape([20,1]).print());
console.log("XS:");
console.log(xs.transpose().reshape([1,20]).print());
console.log("xs shape:"+xs.shape);
console.log("deltasc shape:"+deltas_scaled.shape);
weights = weights - ((xs.transpose().reshape([1,20])).dot(deltas_scaled.reshape([20,1]))).mul(learning_rate);
baises = baises - ((deltas_scaled).sum()).mul(learning_rate);
}
console.log(outputs);
实际结果应为点积。 错误是 错误:点错误:输入必须全部为1或2,但必须为2和0。
第48行 请帮忙!
答案 0 :(得分:0)
该错误是由以下原因引起的:
weights = weights - ((xs.transpose().reshape([1,20])).dot(deltas_scaled.reshape([20,1]))).mul(learning_rate);
baises = baises - ((deltas_scaled).sum()).mul(learning_rate);
使用tf.sub
运算符将解决此问题。
weights = weights.sub(xs.transpose().reshape([1,20]).dot(deltas_scaled.reshape([20,1])).mul(learning_rate));
baises = baises.sub(((deltas_scaled).sum()).mul(learning_rate));