Repository navigation
Expand file tree
/
Copy pathops.cc
More file actions
70 lines (60 loc) · 2.28 KB
/
Copy pathops.cc
File metadata and controls
70 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/cc/framework/ops.h"
namespace tensorflow {
namespace ops {
Input::Initializer::Initializer(
const std::initializer_list<Input::Initializer>& v) {
if (v.size() < 1) {
// Empty initializer list defaults to float tensor with shape (0,)
tensor = Tensor(DT_FLOAT, TensorShape{0});
return;
}
auto const& first = *v.begin();
// Check to make sure that the constituent Initializers are all the same
// type and same shape.
for (auto const& e : v) {
if (e.tensor.dtype() != first.tensor.dtype()) {
status = errors::InvalidArgument(
"Initializer list components should all have the same type");
return;
}
if (!TensorShape{e.tensor.shape()}.IsSameSize(
TensorShape{first.tensor.shape()})) {
status = errors::InvalidArgument(
"Initializer list components should all have the same shape");
return;
}
}
// Form the new shape.
TensorShape shape{static_cast<int64>(v.size())};
shape.AppendShape(TensorShape{first.tensor.shape()});
Tensor t(first.tensor.dtype(), shape);
// Collate the constituent Tensors.
size_t offset = 0;
for (auto const& e : v) {
Tensor elem = e.tensor;
if (first.tensor.dtype() == DT_STRING) {
for (int i = 0; i < elem.NumElements(); ++i) {
t.flat<string>()(offset + i) = elem.flat<string>()(i);
}
offset += elem.NumElements();
} else {
std::copy_n(elem.tensor_data().data(), elem.TotalBytes(),
const_cast<char*>(t.tensor_data().data()) + offset);
offset += elem.TotalBytes();
}
}
tensor = t;
}
} // namespace ops
} // namespace tensorflow