Tensorflow datatype changing while training and writing
For context, here's the first few lines of my model_fn:
def model_fn(features, labels, mode, hyperparmeters):
word_vectors = tf.contrib.layers.embed_sequence(features[WORDS_FEATURE], vocab_size=n_words, embed_dim=EMBEDDING_SIZE)
word_vectors = tf.expand_dims(word_vectors, 3)
Here's my input_fn:
def _input_fn(training_dir, training_filename):
training_set = pd.read_csv(os.path.join(training_dir, training_filename), dtype={'Classification class name': object}, encoding='cp1252')
global n_words
# Prepare training and testing data
data = training_set['Features']
target = pd.Series(training_set['Labels'])
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(MAX_DOCUMENT_LENGTH)
if training_filename == 'meta_data_test.csv':
data = np.array(list(vocab_processor.transform(data)))
return tf.estimator.inputs.numpy_input_fn(
x={WORDS_FEATURE: data},
y=target,
num_epochs=100,
shuffle=False)()
else:
data = np.array(list(vocab_processor.fit_transform(data)))
n_words = len(vocab_processor.vocabulary_)
return tf.estimator.inputs.numpy_input_fn(
x={WORDS_FEATURE: data},
y=target,
batch_size=len(data),
num_epochs=None,
shuffle=True)()
My WORDS_FEATURE is defined as:
WORDS_FEATURE = 'words'
Now, I ran a modified version of this file locally which just trains and evaluates so I can see if the models are working and everything runs fine. When I try to use tf_estimator.fit using a Notebook session, I get this error:
executing startup script (first run)
2018-03-13 19:28:21,590 INFO - root - running container entrypoint
2018-03-13 19:28:21,590 INFO - root - starting train task
2018-03-13 19:28:24,270 INFO - botocore.vendored.requests.packages.urllib3.connectionpool - Starting new HTTP connection (1): 169.254.170.2
2018-03-13 19:28:25,536 INFO - botocore.vendored.requests.packages.urllib3.connectionpool - Starting new HTTPS connection (1): s3.amazonaws.com
2018-03-13 19:28:25,611 INFO - botocore.vendored.requests.packages.urllib3.connectionpool - Starting new HTTPS connection (1): s3.us-east-2.amazonaws.com
2018-03-13 19:28:25,709 INFO - botocore.vendored.requests.packages.urllib3.connectionpool - Starting new HTTPS connection (1): s3.amazonaws.com
INFO:tensorflow:----------------------TF_CONFIG--------------------------
INFO:tensorflow:{"environment": "cloud", "cluster": {"master": ["algo-1:2222"]}, "task": {"index": 0, "type": "master"}}
INFO:tensorflow:---------------------------------------------------------
INFO:tensorflow:going to training
2018-03-13 19:28:25,788 INFO - root - creating RunConfig:
2018-03-13 19:28:25,788 INFO - root - {'save_checkpoints_secs': 300}
2018-03-13 19:28:25,788 INFO - root - creating the estimator
INFO:tensorflow:Using config: {'_model_dir': u's3://sagemaker-tcclassification/sagemaker-tensorflow-py2-gpu-2018-03-13-19-20-41-949/checkpoints', '_save_checkpoints_secs': 300, '_num_ps_replicas': 0, '_keep_checkpoint_max': 5, '_session_config': None, '_tf_random_seed': None, '_task_type': u'master', '_environment': u'cloud', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f525c99fd10>, '_tf_config': gpu_options {
per_process_gpu_memory_fraction: 1.0
}
, '_num_worker_replicas': 1, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': '', '_log_step_count_steps': 100}
2018-03-13 19:28:25,789 INFO - root - creating Experiment:
2018-03-13 19:28:25,789 INFO - root - {'min_eval_frequency': 1000}
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/monitors.py:267: __init__ (from tensorflow.contrib.learn.python.learn.monitors) is deprecated and will be removed after 2016-12-05.
Instructions for updating:
Monitors are deprecated. Please use tf.train.SessionRunHook.
INFO:tensorflow:Create CheckpointSaverHook.
2018-03-13 19:28:28.627772: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-03-13 19:28:28.760402: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-03-13 19:28:28.760766: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: Tesla K80 major: 3 minor: 7 memoryClockRate(GHz): 0.8755
pciBusID: 0000:00:1e.0
totalMemory: 11.17GiB freeMemory: 11.10GiB
2018-03-13 19:28:28.760798: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
INFO:tensorflow:Saving checkpoints for 1 into s3://sagemaker-tcclassification/sagemaker-tensorflow-py2-gpu-2018-03-13-19-20-41-949/checkpoints/model.ckpt.
INFO:tensorflow:loss = 4.8201203, step = 1
INFO:tensorflow:Saving checkpoints for 100 into s3://sagemaker-tcclassification/sagemaker-tensorflow-py2-gpu-2018-03-13-19-20-41-949/checkpoints/model.ckpt.
INFO:tensorflow:Loss for final step: 0.045926746.
INFO:tensorflow:Starting evaluation at 2018-03-13-19:28:37
2018-03-13 19:28:37.508276: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
INFO:tensorflow:Restoring parameters from s3://sagemaker-tcclassification/sagemaker-tensorflow-py2-gpu-2018-03-13-19-20-41-949/checkpoints/model.ckpt-100
INFO:tensorflow:Evaluation [1/100]
INFO:tensorflow:Evaluation [2/100]
INFO:tensorflow:Evaluation [3/100]
INFO:tensorflow:Evaluation [4/100]
INFO:tensorflow:Evaluation [5/100]
INFO:tensorflow:Evaluation [6/100]
INFO:tensorflow:Evaluation [7/100]
INFO:tensorflow:Evaluation [8/100]
INFO:tensorflow:Evaluation [9/100]
INFO:tensorflow:Evaluation [10/100]
INFO:tensorflow:Evaluation [11/100]
INFO:tensorflow:Evaluation [12/100]
INFO:tensorflow:Evaluation [13/100]
INFO:tensorflow:Evaluation [14/100]
INFO:tensorflow:Evaluation [15/100]
INFO:tensorflow:Evaluation [16/100]
INFO:tensorflow:Evaluation [17/100]
INFO:tensorflow:Evaluation [18/100]
INFO:tensorflow:Evaluation [19/100]
INFO:tensorflow:Evaluation [20/100]
INFO:tensorflow:Evaluation [21/100]
INFO:tensorflow:Evaluation [22/100]
INFO:tensorflow:Evaluation [23/100]
INFO:tensorflow:Evaluation [24/100]
INFO:tensorflow:Evaluation [25/100]
INFO:tensorflow:Evaluation [26/100]
INFO:tensorflow:Evaluation [27/100]
INFO:tensorflow:Evaluation [28/100]
INFO:tensorflow:Evaluation [29/100]
INFO:tensorflow:Evaluation [30/100]
INFO:tensorflow:Evaluation [31/100]
INFO:tensorflow:Evaluation [32/100]
INFO:tensorflow:Evaluation [33/100]
INFO:tensorflow:Evaluation [34/100]
INFO:tensorflow:Evaluation [35/100]
INFO:tensorflow:Evaluation [36/100]
INFO:tensorflow:Finished evaluation at 2018-03-13-19:28:38
INFO:tensorflow:Saving dict for global step 100: accuracy = 0.02173913, global_step = 100, loss = 29.5561
ERROR:tensorflow:writing error
ERROR:tensorflow:error file is
ERROR:tensorflow:
Exception during training:
Value passed to parameter 'indices' has DataType string not in list of allowed values: int32, int64
Traceback (most recent call last):
File "/opt/amazon/lib/python2.7/site-packages/tf_container/run.py", line 20, in train_and_log_exceptions
test_wrapper.train()
File "/opt/amazon/lib/python2.7/site-packages/tf_container/trainer.py", line 113, in train
learn_runner.run(experiment_fn, self.training_path)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_runner.py", line 218, in run
return _execute_schedule(experiment, schedule)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/learn_runner.py", line 46, in _execute_schedule
return task()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/experiment.py", line 641, in train_and_evaluate
export_results = self._maybe_export(eval_result)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/experiment.py", line 744, in _maybe_export
eval_result=eval_result))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/export_strategy.py", line 87, in export
return self.export_fn(estimator, export_path, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/utils/saved_model_export_utils.py", line 442, in export_fn
checkpoint_path=checkpoint_path)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 511, in export_savedmodel
config=self.config)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/estimator/estimator.py", line 694, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "/opt/amazon/lib/python2.7/site-packages/tf_container/trainer.py", line 204, in _model_fn
return self.customer_script.model_fn(features, labels, mode, params)
File "/opt/ml/code/text_classification_cnn.py", line 48, in model_fn
word_vectors = tf.contrib.layers.embed_sequence(features[WORDS_FEATURE], vocab_size=n_words, embed_dim=EMBEDDING_SIZE)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/encoders.py", line 142, in embed_sequence
return embedding_ops.embedding_lookup(embeddings, ids)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/embedding_ops.py", line 328, in embedding_lookup
transform_fn=None)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/embedding_ops.py", line 150, in _embedding_lookup_and_transform
result = _clip(_gather(params[0], ids, name=name), ids, max_norm)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/embedding_ops.py", line 54, in _gather
return array_ops.gather(params, ids, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2486, in gather
params, indices, validate_indices=validate_indices, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1834, in gather
validate_indices=validate_indices, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 609, in _apply_op_helper
param_name=input_name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint
", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'indices' has DataType string not in list of allowed values: int32, int64
It mentions tf.contrib.layers.embed_sequence(features[WORDS_FEATURE], vocab_size=n_words, embed_dim=EMBEDDING_SIZE) is using a string as a parameter, though, that's not correct unless somehow features[WORDS_FEATURE] turns into a string when the checkpoint is restored? I've been trying to tackle this error for hours. And again, a version of this works fine locally (at least for training and evaluation) so I'm assuming this problem arises only when the checkpoint is restored.
I checked the datatype locally and I can confirm features[WORDS_FEATURE] is an int64:
Tensor("fifo_queue_DequeueUpTo:1", shape=(?, 100), dtype=int64, device=/device:CPU:0)
Does anyone have any idea why this is happening?