MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ReadTensor

Function ReadTensor

tensorflow/go/tensor.go:119–138  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

117//
118// See also WriteContentsTo.
119func 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.
141func newTensorFromC(c *C.TF_Tensor) *Tensor {

Calls 5

isTensorSerializableFunction · 0.85
typeOfFunction · 0.85
numElementsFunction · 0.85
tensorDataFunction · 0.85
SizeMethod · 0.45

Tested by 4

TestTensorSerializationFunction · 0.68
TestReadTensorReadAllFunction · 0.68