小编ben*_*ene的帖子

Pytorch,无法获得 <class 'torch.Tensor'> 的代表

我正在 PyTorch 中实现一些 RL,并且不得不编写我自己的 mse_loss 函数(我在 Stackoverflow 上找到了它;))。损失函数为:

def mse_loss(input_, target_):    
    return torch.sum(
        (input_ - target_) * (input_ - target_)) / input_.data.nelement()
Run Code Online (Sandbox Code Playgroud)

现在,在我的训练循环中,第一个输入类似于:

tensor([-1.7610e+10]), tensor([-6.5097e+10])
Run Code Online (Sandbox Code Playgroud)

输入张量

有了这个输入,我会得到错误:

Unable to get repr for <class 'torch.Tensor'>
Run Code Online (Sandbox Code Playgroud)

计算a = (input_ - target_)工作正常,而b = a * a分别b = torch.pow(a, 2)会因上述错误而失败。

有谁知道解决这个问题?

非常感谢!

更新:我刚刚尝试使用torch.nn.functional.mse_loss它会导致相同的错误..

mse pytorch

6
推荐指数
1
解决办法
3790
查看次数

使用Jasmine进行Angular2测试,mouseenter/mouseleave-test

我有一个HighlightDirective,如果鼠标进入某个区域会突出显示,如:

@Directive({
  selector: '[myHighlight]',
  host: {
    '(mouseenter)': 'onMouseEnter()',
    '(mouseleave)': 'onMouseLeave()'
  }
})
export class HighlightDirective {
  private _defaultColor = 'Gainsboro';
  private el: HTMLElement;

  constructor(el: ElementRef) { this.el = el.nativeElement; }

  @Input('myHighlight') highlightColor: string;

  onMouseEnter() { this.highlight(this.highlightColor || this._defaultColor); }
  onMouseLeave() { this.highlight(null); }

  private highlight(color:string) {
    this.el.style.backgroundColor = color;
  }

}
Run Code Online (Sandbox Code Playgroud)

现在我想测试,如果在事件上调用(右)方法.所以像这样:

  it('Check if item will be highlighted', inject( [TestComponentBuilder], (_tcb: TestComponentBuilder) => {
    return _tcb
      .createAsync(TestHighlight)
      .then( (fixture) => {
        fixture.detectChanges();
        let element = fixture.nativeElement;
        let component = fixture.componentInstance; …
Run Code Online (Sandbox Code Playgroud)

unit-testing angular2-directives angular2-testing angular

5
推荐指数
1
解决办法
2428
查看次数