MCPcopy Create free account
hub / github.com/clab/dynet / backward_dev_impl

Method backward_dev_impl

dynet/nodes-hinge.cc:66–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64
65template<class MyDevice>
66void Hinge::backward_dev_impl(const MyDevice & dev,
67 const vector<const Tensor*>& xs,
68 const Tensor& fx,
69 const Tensor& dEdf,
70 unsigned i,
71 Tensor& dEdxi) const {
72 DYNET_ASSERT(i == 0, "Failed dimension check in Hinge::backward");
73 // TODO: Can we do this on device?
74 if(pelement != nullptr) {
75 if(as_scalar(fx)) { // there was some loss
76 const float d = as_scalar(dEdf);
77 Tensor eloss(xs[0]->d, static_cast<float*>(aux_mem), fx.device, DeviceMempool::FXS);
78 // TODO: The > comparison should not be calculated twice. Keep it in auxiliary memory?
79 tvec(dEdxi).device(*dev.edevice) += (tvec(eloss) > 0.f).cast<float>() * d;
80#if defined(__CUDACC__) && defined(EIGEN_NO_MALLOC)
81 DYNET_RUNTIME_ERR("CUDA memory allocation in hinge");
82#endif
83 tvec(dEdxi).chip<0>(*pelement).device(*dev.edevice) -= (tvec(eloss) > 0.f).template cast<float>().sum() * d;
84 }
85 } else {
86 DYNET_ASSERT(pelements != nullptr, "Hinge::backward has neither pointer to single element nor vector");
87 vector<float> fx_vec = as_vector(fx);
88 vector<float> d_vec = as_vector(dEdf);
89 Tensor eloss(xs[0]->d, static_cast<float*>(aux_mem), fx.device, DeviceMempool::FXS);
90 for(size_t b = 0; b < fx.d.bd; b++) {
91 if(fx_vec[b]) { // there was some loss
92 tb<1>(dEdxi).chip<1>(b).device(*dev.edevice) += (tb<1>(eloss).chip<1>(b) > 0.f).cast<float>() * d_vec[b];
93#if defined(__CUDACC__) && defined(EIGEN_NO_MALLOC)
94 DYNET_RUNTIME_ERR("CUDA memory allocation in hinge");
95#endif
96 tb<1>(dEdxi).chip<1>(b).chip<0>((*pelements)[b]).device(*dev.edevice) -= (tb<1>(eloss).chip<1>(b) > 0.f).template cast<float>().sum() * d_vec[b];
97 }
98 }
99 }
100}
101DYNET_NODE_INST_DEV_IMPL(Hinge)
102
103// ************* HingeDim *************

Callers

nothing calls this directly

Calls 4

as_scalarFunction · 0.85
tvecFunction · 0.85
as_vectorFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected