Пример #1
0
// newTensorFromC takes ownership of c and returns the owning Tensor.
func newTensorFromC(c *C.TF_Tensor) *Tensor {
	var shape []int64
	if ndims := int(C.TF_NumDims(c)); ndims > 0 {
		shape = make([]int64, ndims)
	}
	for i := range shape {
		shape[i] = int64(C.TF_Dim(c, C.int(i)))
	}
	t := &Tensor{c: c, shape: shape}
	runtime.SetFinalizer(t, (*Tensor).finalize)
	return t
}
Пример #2
0
// newTensorFromC converts from a C.TF_Tensor to a Tensor.
func newTensorFromC(ct *C.TF_Tensor) *Tensor {
	t := &Tensor{dt: DataType(C.TF_TensorType(ct))}
	numDims := int(C.TF_NumDims(ct))
	for i := 0; i < numDims; i++ {
		t.shape = append(t.shape, int64(C.TF_Dim(ct, C.int(i))))
	}
	b := make([]byte, int(C.TF_TensorByteSize(ct)))
	if len(b) > 0 {
		C.memcpy(unsafe.Pointer(&b[0]), C.TF_TensorData(ct), C.size_t(len(b)))
	}
	t.buf = bytes.NewBuffer(b)
	return t
}