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

Function shapeAndDataTypeOf

tensorflow/go/tensor.go:238–257  ·  view source on GitHub ↗

shapeAndDataTypeOf returns the data type and shape of the Tensor corresponding to a Go type.

(val reflect.Value)

Source from the content-addressed store, hash-verified

236// shapeAndDataTypeOf returns the data type and shape of the Tensor
237// corresponding to a Go type.
238func shapeAndDataTypeOf(val reflect.Value) (shape []int64, dt DataType, err error) {
239 typ := val.Type()
240 for typ.Kind() == reflect.Array || typ.Kind() == reflect.Slice {
241 shape = append(shape, int64(val.Len()))
242 if val.Len() > 0 {
243 // In order to check tensor structure properly in general case we need to iterate over all slices of the tensor to check sizes match
244 // Since we already going to iterate over all elements in encodeTensor() let's
245 // 1) do the actual check in encodeTensor() to save some cpu cycles here
246 // 2) assume the shape is represented by lengths of elements with zero index in each dimension
247 val = val.Index(0)
248 }
249 typ = typ.Elem()
250 }
251 for _, t := range types {
252 if typ.Kind() == t.typ.Kind() {
253 return shape, DataType(t.dataType), nil
254 }
255 }
256 return shape, dt, fmt.Errorf("unsupported type %v", typ)
257}
258
259// typeOf converts from a DataType and Shape to the equivalent Go type.
260func typeOf(dt DataType, shape []int64) reflect.Type {

Callers 1

NewTensorFunction · 0.85

Calls 6

appendFunction · 0.85
int64Function · 0.85
DataTypeTypeAlias · 0.70
TypeMethod · 0.45
KindMethod · 0.45
IndexMethod · 0.45

Tested by

no test coverage detected