| 62 | } |
| 63 | |
| 64 | Input::Initializer::Initializer( |
| 65 | const std::initializer_list<Input::Initializer>& v) { |
| 66 | if (v.size() < 1) { |
| 67 | // Empty initializer list defaults to float tensor with shape (0,) |
| 68 | tensor = Tensor(DT_FLOAT, TensorShape{0}); |
| 69 | return; |
| 70 | } |
| 71 | auto const& first = *v.begin(); |
| 72 | // Check to make sure that the constituent Initializers are all the same |
| 73 | // type and same shape. |
| 74 | for (auto const& e : v) { |
| 75 | if (e.tensor.dtype() != first.tensor.dtype()) { |
| 76 | status = errors::InvalidArgument( |
| 77 | "Initializer list components should all have the same type"); |
| 78 | return; |
| 79 | } |
| 80 | if (!TensorShape{e.tensor.shape()}.IsSameSize( |
| 81 | TensorShape{first.tensor.shape()})) { |
| 82 | status = errors::InvalidArgument( |
| 83 | "Initializer list components should all have the same shape"); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Form the new shape. |
| 89 | TensorShape shape{static_cast<int64>(v.size())}; |
| 90 | shape.AppendShape(TensorShape{first.tensor.shape()}); |
| 91 | |
| 92 | Tensor t(first.tensor.dtype(), shape); |
| 93 | |
| 94 | // Collate the constituent Tensors. |
| 95 | size_t offset = 0; |
| 96 | for (auto const& e : v) { |
| 97 | Tensor elem = e.tensor; |
| 98 | if (first.tensor.dtype() == DT_STRING) { |
| 99 | for (int i = 0; i < elem.NumElements(); ++i) { |
| 100 | t.flat<tstring>()(offset + i) = elem.flat<tstring>()(i); |
| 101 | } |
| 102 | offset += elem.NumElements(); |
| 103 | } else { |
| 104 | std::copy_n(elem.tensor_data().data(), elem.TotalBytes(), |
| 105 | const_cast<char*>(t.tensor_data().data()) + offset); |
| 106 | offset += elem.TotalBytes(); |
| 107 | } |
| 108 | } |
| 109 | tensor = t; |
| 110 | } |
| 111 | |
| 112 | } // namespace tensorflow |
nothing calls this directly
no test coverage detected