| 25 | namespace singa { |
| 26 | |
| 27 | std::vector<Tensor> CSVDecoder::Decode(std::string value) { |
| 28 | std::vector<Tensor> output; |
| 29 | std::stringstream ss; |
| 30 | ss.str(value); |
| 31 | int l = 0; |
| 32 | if (has_label_ == true) |
| 33 | ss >> l; |
| 34 | std::string str; |
| 35 | float d[kMaxCSVBufSize]; |
| 36 | int size = 0; |
| 37 | while (std::getline(ss, str, ',')) { |
| 38 | float temp; |
| 39 | if (std::stringstream(str) >> temp) { |
| 40 | CHECK_LE(size, kMaxCSVBufSize - 1); |
| 41 | d[size++] = temp; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | Tensor data(Shape {static_cast<size_t>(size)}, kFloat32); |
| 46 | data.CopyDataFromHostPtr(d, size); |
| 47 | output.push_back(data); |
| 48 | if (has_label_ == true) { |
| 49 | Tensor label(Shape {1}, kInt); |
| 50 | label.CopyDataFromHostPtr(&l, 1); |
| 51 | output.push_back(label); |
| 52 | } |
| 53 | return output; |
| 54 | } |
| 55 | } // namespace singa |