go-ml.dev/pkg/base@v0.0.0-20200610162856-60c38abac71b/fu/cell.go (about)

     1  package fu
     2  
     3  import (
     4  	"fmt"
     5  	"go-ml.dev/pkg/zorros"
     6  	"reflect"
     7  	"strings"
     8  )
     9  
    10  type Cell struct {
    11  	reflect.Value
    12  }
    13  
    14  func (c Cell) Text() string {
    15  	if c.Kind() == reflect.String {
    16  		return c.Interface().(string)
    17  	}
    18  	if c.Type() == TensorType {
    19  		z := c.Interface().(Tensor)
    20  		s := []string{}
    21  		for i := 0; i < 4; i++ {
    22  			if i == 3 || i >= z.Volume() {
    23  				s = append(s, ">")
    24  				break
    25  			} else if i < z.Volume() {
    26  				s = append(s, fmt.Sprint(z.Index(i)))
    27  			}
    28  		}
    29  		ch, h, w := z.Dimension()
    30  		return fmt.Sprintf("(%dx%dx%d){%v}", ch, h, w, strings.Join(s, ","))
    31  	}
    32  	return fmt.Sprint(c.Interface())
    33  }
    34  
    35  func (c Cell) String() string { return c.Text() }
    36  
    37  func (c Cell) Int() int {
    38  	return Convert(c.Value, false, Int).Interface().(int)
    39  }
    40  
    41  func (c Cell) Int8() int8 {
    42  	return Convert(c.Value, false, Int8).Interface().(int8)
    43  }
    44  
    45  func (c Cell) Int16() int16 {
    46  	return Convert(c.Value, false, Int16).Interface().(int16)
    47  }
    48  
    49  func (c Cell) Int32() int32 {
    50  	return Convert(c.Value, false, Int32).Interface().(int32)
    51  }
    52  
    53  func (c Cell) Int64() int64 {
    54  	return Convert(c.Value, false, Int64).Interface().(int64)
    55  }
    56  
    57  func (c Cell) Uint() uint {
    58  	return Convert(c.Value, false, Uint).Interface().(uint)
    59  }
    60  
    61  func (c Cell) Uint8() uint8 {
    62  	return Convert(c.Value, false, Uint8).Interface().(uint8)
    63  }
    64  
    65  func (c Cell) Uint16() uint16 {
    66  	return Convert(c.Value, false, Uint16).Interface().(uint16)
    67  }
    68  
    69  func (c Cell) Uint32() uint32 {
    70  	return Convert(c.Value, false, Uint32).Interface().(uint32)
    71  }
    72  
    73  func (c Cell) Uint64() uint64 {
    74  	return Convert(c.Value, false, Uint64).Interface().(uint64)
    75  }
    76  
    77  func (c Cell) Real() float32 {
    78  	return Convert(c.Value, false, Float32).Interface().(float32)
    79  }
    80  
    81  func (c Cell) Float() float64 {
    82  	return Convert(c.Value, false, Float64).Interface().(float64)
    83  }
    84  
    85  func (c Cell) Reals(docopy ...bool) []float32 {
    86  	if c.Type() != TensorType {
    87  		panic(zorros.Panic(zorros.New("cell type is not tensor")))
    88  	}
    89  	z := c.Interface().(Tensor)
    90  	return z.Floats32(docopy...)
    91  }
    92  
    93  func (c Cell) Values() interface{} {
    94  	if c.Type() != TensorType {
    95  		panic(zorros.Panic(zorros.New("cell type is not tensor")))
    96  	}
    97  	z := c.Interface().(Tensor)
    98  	return z.Values()
    99  }
   100  
   101  func (c Cell) Volume() int {
   102  	if c.Type() != TensorType {
   103  		panic(zorros.Panic(zorros.New("cell type is not tensor")))
   104  	}
   105  	z := c.Interface().(Tensor)
   106  	return z.Volume()
   107  }