我正在 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
它会导致相同的错误..
我有一个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)