| 103 | /// construct an Initializer. Initializer stores the value it got constructed |
| 104 | /// with in a Tensor object. |
| 105 | struct Initializer { |
| 106 | /// Construct from a scalar value of an arithmetic type or a type that can |
| 107 | /// be converted to a string (eg. a string literal). |
| 108 | template <typename T, typename = typename std::enable_if< |
| 109 | std::is_arithmetic<T>::value || |
| 110 | std::is_convertible<T, string>::value>::type> |
| 111 | Initializer(const T& v) { // NOLINT(runtime/explicit) |
| 112 | typedef typename RealType<T>::type RealT; |
| 113 | Tensor t(DataTypeToEnum<RealT>::v(), TensorShape()); |
| 114 | t.flat<RealT>()(0) = RealT(v); |
| 115 | tensor = t; |
| 116 | } |
| 117 | |
| 118 | Initializer(const Tensor& t) : tensor(t) {} // NOLINT(runtime/explicit) |
| 119 | |
| 120 | /// Construct from a scalar value and an explicit shape |
| 121 | template <typename T, typename = typename std::enable_if< |
| 122 | std::is_arithmetic<T>::value || |
| 123 | std::is_convertible<T, string>::value>::type> |
| 124 | Initializer(const T& v, const TensorShape& shape) { |
| 125 | typedef typename RealType<T>::type RealT; |
| 126 | Tensor t(DataTypeToEnum<RealT>::v(), shape); |
| 127 | for (int64 i = 0; i < t.NumElements(); ++i) { |
| 128 | t.flat<RealT>()(i) = RealT(v); |
| 129 | } |
| 130 | tensor = t; |
| 131 | } |
| 132 | |
| 133 | /// Construct from a initializer list of scalars (a one-dimensional tensor). |
| 134 | template <typename T, typename = typename std::enable_if< |
| 135 | std::is_arithmetic<T>::value || |
| 136 | std::is_convertible<T, string>::value>::type> |
| 137 | Initializer( |
| 138 | const std::initializer_list<T>& v) { // NOLINT(runtime/explicit) |
| 139 | typedef typename RealType<T>::type RealT; |
| 140 | Tensor t(DataTypeToEnum<RealT>::v(), |
| 141 | TensorShape{static_cast<int>(v.size())}); |
| 142 | std::copy_n(v.begin(), v.size(), t.flat<RealT>().data()); |
| 143 | tensor = t; |
| 144 | } |
| 145 | |
| 146 | /// Construct from a initializer list of scalars and an explicit shape. |
| 147 | template <typename T, typename = typename std::enable_if< |
| 148 | std::is_arithmetic<T>::value || |
| 149 | std::is_convertible<T, string>::value>::type> |
| 150 | Initializer(const std::initializer_list<T>& v, const TensorShape& shape) { |
| 151 | typedef typename RealType<T>::type RealT; |
| 152 | Tensor t(DataTypeToEnum<RealT>::v(), shape); |
| 153 | if (t.NumElements() != static_cast<int64>(v.size())) { |
| 154 | status = errors::InvalidArgument( |
| 155 | "Cannot construct a tensor with ", t.NumElements(), |
| 156 | " from an initializer list with ", v.size(), " elements"); |
| 157 | return; |
| 158 | } |
| 159 | std::copy_n(v.begin(), v.size(), t.flat<RealT>().data()); |
| 160 | tensor = t; |
| 161 | } |
| 162 |
no outgoing calls