Home | History | Annotate | Download | only in go
      1 /*
      2 Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      3 
      4 Licensed under the Apache License, Version 2.0 (the "License");
      5 you may not use this file except in compliance with the License.
      6 You may obtain a copy of the License at
      7 
      8     http://www.apache.org/licenses/LICENSE-2.0
      9 
     10 Unless required by applicable law or agreed to in writing, software
     11 distributed under the License is distributed on an "AS IS" BASIS,
     12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 See the License for the specific language governing permissions and
     14 limitations under the License.
     15 */
     16 
     17 package tensorflow
     18 
     19 import (
     20 	"fmt"
     21 	"strings"
     22 )
     23 
     24 // Shape represents the (possibly partially known) shape of a tensor that will
     25 // be produced by an operation.
     26 //
     27 // The zero-value of a Shape represents a shape with an unknown number of
     28 // dimensions.
     29 type Shape struct {
     30 	dims []int64
     31 }
     32 
     33 // ScalarShape returns a Shape representing a scalar.
     34 func ScalarShape() Shape {
     35 	return Shape{dims: make([]int64, 0)}
     36 }
     37 
     38 // MakeShape returns a Shape with the provided size of each dimension.
     39 //
     40 // A value of -1 implies that the size of the corresponding dimension is not
     41 // known.
     42 func MakeShape(shape ...int64) Shape {
     43 	cpy := make([]int64, len(shape))
     44 	copy(cpy, shape)
     45 	return Shape{dims: cpy}
     46 }
     47 
     48 // NumDimensions returns the number of dimensions represented by s, or -1 if
     49 // unknown.
     50 func (s Shape) NumDimensions() int {
     51 	if s.dims == nil {
     52 		return -1
     53 	}
     54 	return len(s.dims)
     55 }
     56 
     57 // Size returns the size of the dim-th dimension of the shape, or -1 if it
     58 // is unknown.
     59 //
     60 // REQUIRES: 0 <= dim < s.NumDimensions()
     61 func (s Shape) Size(dim int) int64 {
     62 	if dim < 0 || dim >= s.NumDimensions() {
     63 		return -1
     64 	}
     65 	return s.dims[dim]
     66 }
     67 
     68 // IsFullySpecified returns true iff the size of all the dimensions of s are
     69 // known.
     70 func (s Shape) IsFullySpecified() bool {
     71 	if s.dims == nil {
     72 		return false
     73 	}
     74 	for _, size := range s.dims {
     75 		if size <= 1 {
     76 			return false
     77 		}
     78 	}
     79 	return true
     80 }
     81 
     82 // ToSlice returns the (possibly partially known) shape represented by s as a
     83 // slice, or an error if the number of dimensions is not known.
     84 func (s Shape) ToSlice() ([]int64, error) {
     85 	if s.dims == nil {
     86 		return nil, fmt.Errorf("cannot create a slice for a Shape with an unknown number of dimensions")
     87 	}
     88 	cpy := make([]int64, len(s.dims))
     89 	copy(cpy, s.dims)
     90 	return cpy, nil
     91 }
     92 
     93 func (s Shape) String() string {
     94 	if s.dims == nil {
     95 		return "?"
     96 	}
     97 	ret := fmt.Sprint(s.dims)
     98 	for _, size := range s.dims {
     99 		if size < 0 {
    100 			ret = strings.Replace(ret, fmt.Sprint(size), "?", 1)
    101 		}
    102 	}
    103 	return strings.Replace(ret, " ", ", ", -1)
    104 }
    105