1

点错误:输入必须都是 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 行,请帮忙!!

4

1 回答 1

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));
于 2019-06-20T10:10:59.327 回答