ReadTensor constructs a Tensor with the provided type and shape from the serialized tensor contents in r. See also WriteContentsTo.
(dataType DataType, shape []int64, r io.Reader)
| 117 | // |
| 118 | // See also WriteContentsTo. |
| 119 | func ReadTensor(dataType DataType, shape []int64, r io.Reader) (*Tensor, error) { |
| 120 | if err := isTensorSerializable(dataType); err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | nbytes := typeOf(dataType, nil).Size() * uintptr(numElements(shape)) |
| 124 | var shapePtr *C.int64_t |
| 125 | if len(shape) > 0 { |
| 126 | shapePtr = (*C.int64_t)(unsafe.Pointer(&shape[0])) |
| 127 | } |
| 128 | t := &Tensor{ |
| 129 | c: C.TF_AllocateTensor(C.TF_DataType(dataType), shapePtr, C.int(len(shape)), C.size_t(nbytes)), |
| 130 | shape: shape, |
| 131 | } |
| 132 | runtime.SetFinalizer(t, (*Tensor).finalize) |
| 133 | raw := tensorData(t.c) |
| 134 | if _, err := io.ReadFull(r, raw); err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | return t, nil |
| 138 | } |
| 139 | |
| 140 | // newTensorFromC takes ownership of c and returns the owning Tensor. |
| 141 | func newTensorFromC(c *C.TF_Tensor) *Tensor { |