Construct a `StaticVocabularyTable` object. Args: initializer: A TableInitializerBase object that contains the data used to initialize the table. If None, then we only use out-of-vocab buckets. num_oov_buckets: Number of buckets to use for out-of-vocabulary keys. Must
(self,
initializer,
num_oov_buckets,
lookup_key_dtype=None,
name=None)
| 1075 | """ |
| 1076 | |
| 1077 | def __init__(self, |
| 1078 | initializer, |
| 1079 | num_oov_buckets, |
| 1080 | lookup_key_dtype=None, |
| 1081 | name=None): |
| 1082 | """Construct a `StaticVocabularyTable` object. |
| 1083 | |
| 1084 | Args: |
| 1085 | initializer: A TableInitializerBase object that contains the data used to |
| 1086 | initialize the table. If None, then we only use out-of-vocab buckets. |
| 1087 | num_oov_buckets: Number of buckets to use for out-of-vocabulary keys. Must |
| 1088 | be greater than zero. |
| 1089 | lookup_key_dtype: Data type of keys passed to `lookup`. Defaults to |
| 1090 | `initializer.key_dtype` if `initializer` is specified, otherwise |
| 1091 | `tf.string`. Must be string or integer, and must be castable to |
| 1092 | `initializer.key_dtype`. |
| 1093 | name: A name for the operation (optional). |
| 1094 | |
| 1095 | Raises: |
| 1096 | ValueError: when `num_oov_buckets` is not positive. |
| 1097 | TypeError: when lookup_key_dtype or initializer.key_dtype are not |
| 1098 | integer or string. Also when initializer.value_dtype != int64. |
| 1099 | """ |
| 1100 | if num_oov_buckets <= 0: |
| 1101 | raise ValueError("oov_buckets must be > 0.") |
| 1102 | # If a name ends with a '/' it is a "name scope", remove all trailing '/' |
| 1103 | # characters to use as table name. |
| 1104 | if name: |
| 1105 | name = name.rstrip("/") |
| 1106 | if initializer: |
| 1107 | if lookup_key_dtype is None: |
| 1108 | lookup_key_dtype = initializer.key_dtype |
| 1109 | supported_table_key_dtypes = (dtypes.int64, dtypes.string) |
| 1110 | if initializer.key_dtype not in supported_table_key_dtypes: |
| 1111 | raise TypeError("Invalid key dtype, expected one of %s, but got %s." % |
| 1112 | (supported_table_key_dtypes, initializer.key_dtype)) |
| 1113 | if initializer.key_dtype.is_integer != lookup_key_dtype.is_integer: |
| 1114 | raise TypeError( |
| 1115 | "Invalid key dtype, expected %s but got %s." % |
| 1116 | ("integer" if lookup_key_dtype.is_integer else "non-integer", |
| 1117 | initializer.key_dtype)) |
| 1118 | if initializer.value_dtype != dtypes.int64: |
| 1119 | raise TypeError("Invalid value dtype, expected %s but got %s." % |
| 1120 | (dtypes.int64, initializer.value_dtype)) |
| 1121 | self._table = HashTable(initializer, default_value=-1) |
| 1122 | name = name or self._table.name |
| 1123 | else: |
| 1124 | lookup_key_dtype = dtypes.string |
| 1125 | self._table = None |
| 1126 | name = name or "hash_bucket" |
| 1127 | if (not lookup_key_dtype.is_integer) and (dtypes.string != |
| 1128 | lookup_key_dtype): |
| 1129 | raise TypeError("Invalid key_dtype, expected integer or string, got %s." % |
| 1130 | lookup_key_dtype) |
| 1131 | self._num_oov_buckets = num_oov_buckets |
| 1132 | |
| 1133 | self._table_name = None |
| 1134 | if name is not None: |