Home | History | Annotate | Download | only in tensorflow
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 package org.tensorflow;
     17 
     18 import java.util.HashMap;
     19 import java.util.Map;
     20 import org.tensorflow.types.UInt8;
     21 
     22 /** Represents the type of elements in a {@link Tensor} as an enum. */
     23 public enum DataType {
     24   /** 32-bit single precision floating point. */
     25   FLOAT(1),
     26 
     27   /** 64-bit double precision floating point. */
     28   DOUBLE(2),
     29 
     30   /** 32-bit signed integer. */
     31   INT32(3),
     32 
     33   /** 8-bit unsigned integer. */
     34   UINT8(4),
     35 
     36   /**
     37    * A sequence of bytes.
     38    *
     39    * <p>TensorFlow uses the STRING type for an arbitrary sequence of bytes.
     40    */
     41   STRING(7),
     42 
     43   /** 64-bit signed integer. */
     44   INT64(9),
     45 
     46   /** Boolean. */
     47   BOOL(10);
     48 
     49   private final int value;
     50 
     51   // The integer value must match the corresponding TF_* value in the TensorFlow C API.
     52   DataType(int value) {
     53     this.value = value;
     54   }
     55 
     56   /** Corresponding value of the TF_DataType enum in the TensorFlow C API. */
     57   int c() {
     58     return value;
     59   }
     60 
     61   // Cached to avoid copying it
     62   private static final DataType[] values = values();
     63 
     64   static DataType fromC(int c) {
     65     for (DataType t : values) {
     66       if (t.value == c) {
     67         return t;
     68       }
     69     }
     70     throw new IllegalArgumentException(
     71         "DataType " + c + " is not recognized in Java (version " + TensorFlow.version() + ")");
     72   }
     73 
     74   /**
     75    * Returns the DataType of a Tensor whose elements have the type specified by class {@code c}.
     76    *
     77    * @param c The class describing the TensorFlow type of interest.
     78    * @return The {@code DataType} enum corresponding to {@code c}.
     79    * @throws IllegalArgumentException if objects of {@code c} do not correspond to a TensorFlow
     80    *     datatype.
     81    */
     82   public static DataType fromClass(Class<?> c) {
     83     DataType dtype = typeCodes.get(c);
     84     if (dtype == null) {
     85       throw new IllegalArgumentException(
     86           c.getName() + " objects cannot be used as elements in a TensorFlow Tensor");
     87     }
     88     return dtype;
     89   }
     90 
     91   private static final Map<Class<?>, DataType> typeCodes = new HashMap<>();
     92 
     93   static {
     94     typeCodes.put(Float.class, DataType.FLOAT);
     95     typeCodes.put(Double.class, DataType.DOUBLE);
     96     typeCodes.put(Integer.class, DataType.INT32);
     97     typeCodes.put(UInt8.class, DataType.UINT8);
     98     typeCodes.put(Long.class, DataType.INT64);
     99     typeCodes.put(Boolean.class, DataType.BOOL);
    100     typeCodes.put(String.class, DataType.STRING);
    101   }
    102 }
    103