WriteCSV WriteCSV writes a tensor to a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg). Outer-most dims are rows in the file, and inner-most is column -- Reading just grabs all values and doesn't care about shape.
(tsr Tensor, w io.Writer, delim rune)
| 52 | // Outer-most dims are rows in the file, and inner-most is column -- |
| 53 | // Reading just grabs all values and doesn't care about shape. |
| 54 | func WriteCSV(tsr Tensor, w io.Writer, delim rune) error { |
| 55 | prec := -1 |
| 56 | if ps, ok := tsr.MetaData("precision"); ok { |
| 57 | prec, _ = strconv.Atoi(ps) |
| 58 | } |
| 59 | cw := csv.NewWriter(w) |
| 60 | if delim != 0 { |
| 61 | cw.Comma = delim |
| 62 | } |
| 63 | nrow := tsr.DimSize(0) |
| 64 | nin := tsr.Len() / nrow |
| 65 | rec := make([]string, nin) |
| 66 | str := tsr.IsString() |
| 67 | for ri := 0; ri < nrow; ri++ { |
| 68 | for ci := 0; ci < nin; ci++ { |
| 69 | idx := ri*nin + ci |
| 70 | if str { |
| 71 | rec[ci] = tsr.String1D(idx) |
| 72 | } else { |
| 73 | rec[ci] = strconv.FormatFloat(tsr.Float1D(idx), 'g', prec, 64) |
| 74 | } |
| 75 | } |
| 76 | err := cw.Write(rec) |
| 77 | if err != nil { |
| 78 | log.Println(err) |
| 79 | return err |
| 80 | } |
| 81 | } |
| 82 | cw.Flush() |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // ReadCSV reads a tensor from a comma-separated-values (CSV) file |
| 87 | // (where comma = any delimiter, specified in the delim arg), |