shapeAndDataTypeOf returns the data type and shape of the Tensor corresponding to a Go type.
(val reflect.Value)
| 236 | // shapeAndDataTypeOf returns the data type and shape of the Tensor |
| 237 | // corresponding to a Go type. |
| 238 | func 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. |
| 260 | func typeOf(dt DataType, shape []int64) reflect.Type { |