mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-15 21:00:47 +00:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/9939 Pull Request resolved: https://github.com/facebookresearch/weakly-supervised-action-detection/pull/13 Pull Request resolved: https://github.com/pytorch/translate/pull/166 Pull Request resolved: https://github.com/pytorch/pytorch/pull/9125 Closes https://github.com/pytorch/pytorch/pull/9125 Use inheritance for polymorphism, and remove template parameter This is to change the templating in call sites, the core implementations will change later Before Caffe2 Tensor class was compile-time fixed to bind to a particular device/context. With this change, we're making it a runtime property (stored inside the tensor), but preserve the same semantics. For example, one has to specify device type in order to create a Tensor - there are no uninitialized tensors. More specifically the changes are: 1. We added an extra argument *DeviceType* to most of the constructors of the tensor, e.g. (Tensor(DeviceType type)), 2. Semantics of constructor Tensor(const Tensor<SrcContext>& src, ContextForCopy* context); is changed, in this constructor, the second context is passed in to enable us to call the templated Copy function, it could be in a different context as source and target previously, now we'll enforce that the context should have same device type as src, if it is provided. 3. To preserve 'get-or-construct' semantics of Blob, we added specialized getter Blob::GetMutableTensor that verifies both that Blob contains a Tensor and that it's of a correct type 4. Specifically, Tensor type is not default-constructible any more (as we don't have unknown device tensors) and thus some of the code handling STL containers needs to change Note: Some changes are postponed just to keep this diff a bit smaller. Please see `TODO`s. Reviewed By: ezyang, houseroad Differential Revision: D9024330 fbshipit-source-id: e0b8295d2dc6ebe2963383ded5af799ad17164ba
40 lines
1.2 KiB
C++
40 lines
1.2 KiB
C++
#include "caffe2/operators/perplexity_op.h"
|
|
|
|
namespace caffe2 {
|
|
|
|
template <>
|
|
bool PerplexityOp<float, CPUContext>::RunOnDevice() {
|
|
auto& X = Input(0);
|
|
auto* Y = Output(0);
|
|
|
|
DCHECK_EQ(X.ndim(), 1);
|
|
int N = X.dim32(0);
|
|
|
|
Y->Resize(vector<TIndex>());
|
|
const auto* Xdata = X.data<float>();
|
|
|
|
float perplexity = 1.0;
|
|
for (int i = 0; i < N; ++i) {
|
|
perplexity *= pow(Xdata[i], -1.0/N);
|
|
}
|
|
*(Y->template mutable_data<float>()) = perplexity;
|
|
return true;
|
|
}
|
|
|
|
REGISTER_CPU_OPERATOR(Perplexity, PerplexityOp<float, CPUContext>);
|
|
|
|
OPERATOR_SCHEMA(Perplexity).NumInputs(1).NumOutputs(1)
|
|
.SetDoc(R"DOC(
|
|
Perplexity calculates how well a probability distribution predicts a sample.
|
|
Perplexity takes a 1-D tensor containing a batch of probabilities. Each value
|
|
in the tensor belongs to a different sample and represents the probability of
|
|
the model predicting the true label for that sample. The operator returns a
|
|
single (float) perplexity value for the batch.
|
|
)DOC")
|
|
.Input(0, "probabilities", "The input data as Tensor. It contains a batch of"
|
|
"true label or target probabilities")
|
|
.Output(0, "output", "The output- a single (float) perplexity value for the "
|
|
"batch");
|
|
|
|
SHOULD_NOT_DO_GRADIENT(Perplexity);
|
|
} // namespace caffe2
|