I'm using keras with tf-2.2 at backend and it shows up this error.
Traceback (most recent call last):
File "run.py", line 97, in <module>
task_entry_function()
File "/data-crystina/src/capreolus-unpublished/capreolus/task/rerank.py", line 47, in train
return self.rerank_run(best_search_run, self.get_results_path())
File "/data-crystina/src/capreolus-unpublished/capreolus/task/rerank.py", line 85, in rerank_run
self.benchmark.relevance_level,
File "/data-crystina/src/capreolus-unpublished/capreolus/trainer/__init__.py", line 578, in train
use_multiprocessing=True,
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 855, in fit
callbacks.on_train_batch_end(step, logs)
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py", line 389, in on_train_batch_end
logs = self._process_logs(logs)
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py", line 265, in _process_logs
return tf_utils.to_numpy_or_python_type(logs) File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/utils/tf_utils.py", line 523, in to_numpy_or_python_type
return nest.map_structure(_to_single_numpy_or_python_type, tensors)
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 617, in map_structure
structure[0], [func(*x) for x in entries],
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/util/nest.py", line 617, in <listcomp>
structure[0], [func(*x) for x in entries],
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/keras/utils/tf_utils.py", line 519, in _to_single_numpy_or_python_type
x = t.numpy()
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 961, in numpy
maybe_arr = self._numpy() # pylint: disable=protected-access
File "/data-crystina/anaconda3/envs/maxp/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 929, in _numpy
six.raise_from(core._status_to_exception(e.code, e.message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node __inference_train_function_100056}} PartialTensorShape: Incompatible ranks during merge: 2 vs. 1
[[{{node map_6/TensorArrayV2Stack/TensorListStack}}]]
[[MultiDeviceIteratorGetNextFromShard]]
[[RemoteCall]]
[[IteratorGetNextAsOptional]]
2020-07-03 07:19:03.088112: W tensorflow/core/distributed_runtime/eager/remote_tensor_handle_data.cc:76] Unable to destroy remote tensor handles. If you are running a tf.function, it usually indicates som
e op in the graph gets an error: {{function_node __inference_train_function_100056}} PartialTensorShape: Incompatible ranks during merge: 2 vs. 1
[[{{node map_6/TensorArrayV2Stack/TensorListStack}}]]
[[MultiDeviceIteratorGetNextFromShard]]
[[RemoteCall]]
[[IteratorGetNextAsOptional]]
Apologize for failing to find a small snippet to reproduce this. But I go inside ..python3.7/site-packages/tensorflow/python/keras/callbacks.py, and in the function:
def on_train_batch_end(self, batch, logs=None):
"""Calls the `on_train_batch_end` methods of its callbacks.
Arguments:
batch: integer, index of batch within the current epoch.
logs: dict. Metric results for this batch.
"""
if self._should_call_train_batch_hooks:
# print("<<<<", logs.keys())
# print(">>>", type(list(logs.values())[0]))
logs = self._process_logs(logs)
self._call_batch_hook(ModeKeys.TRAIN, 'end', batch, logs=logs)
I print out the logs and found it's a dictionary containing only one key loss, and the type of its value is class 'tensorflow.python.framework.ops.EagerTensor'>. However, the logs["loss"] cannot be printed directory because of the same error, and same to logs["loss"].shape. I failed to find any similar case in internet, wondering whether anyone has met this case?
Problem solved, it's totally because I'm trying to parse the tfrecord using a wrong shape/data type in a callback passed to TensorFlow.
Related
I'm trying to get a simple boosted decision tree up and running with test data. I have something like this working on another machine, but when porting the code over I get this error:
TypeError: Fetch argument 0 has invalid type <class 'numpy.int64'>, must be a string or Tensor. (Can not convert a int64 into a Tensor or Operation.) based on reading, this error generally results in reassigning a critical variable, but I'm having a hard time figuring out what it might be.
This is my code (sorry if it's a bit messy, I condensed it down as much as I could)
import pandas as pd
import tensorflow as tf
tf.random.set_seed(123)
from sklearn.datasets import make_blobs
#creates an input function for a tf model
def make_input_fn(X, Y, n_epochs=None, shuffle=True):
batch_len = len(Y)
def input_fn():
dataset = tf.data.Dataset.from_tensor_slices((dict(X), Y))
if shuffle:
dataset = dataset.shuffle(batch_len)
# For training, cycle thru dataset as many times as need (n_epochs=None).
dataset = dataset.repeat(n_epochs)
#dividing data into batches
dataset = dataset.batch(batch_len)
return dataset
return input_fn
def fullTest():
print('_______Defining Test Data Blobs_______')
xVals, yVals = make_blobs(n_samples=10, centers=2, n_features=3, random_state=0)
#xVals
xVals = pd.DataFrame(xVals)
xVals.columns = ['feature {}'.format(num) for num in xVals.columns]
print('xVals:')
print(xVals)
#yVals
yVals = pd.DataFrame(yVals)
yVals.columns = ['flag']
print('yVals:')
print(yVals['flag'].value_counts())
# Defining input function
train_input_fn = make_input_fn(xVals, yVals)
trainX=xVals
trainY=yVals
#defining tf feature columns
feature_columns=[]
for feature_name in list(trainX.columns):
feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))
#creating the estimator
n_batches = 1
est = tf.estimator.BoostedTreesClassifier(feature_columns, n_batches_per_layer=n_batches)
est.train(train_input_fn, max_steps=250)
if __name__ == '__main__':
fullTest()
There's a lot of error printout. This is the more pertinant stuff:
...\site-packages\tensorflow\python\framework\ops.py", line 3759, in _as_graph_element_locked
(type(obj).__name__, types_str))
TypeError: Can not convert a int64 into a Tensor or Operation.
During handling of the above exception, another exception occurred:
line 309, in __init__
(fetch, type(fetch), str(e)))
TypeError: Fetch argument 0 has invalid type <class 'numpy.int64'>, must be a string or Tensor. (Can not convert a int64 into a Tensor or Operation.)
full traceback:
Traceback (most recent call last):
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 305, in __init__
fetch, allow_tensor=True, allow_operation=True))
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\framework\ops.py", line 3667, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\framework\ops.py", line 3759, in _as_graph_element_locked
(type(obj).__name__, types_str))
TypeError: Can not convert a int64 into a Tensor or Operation.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Daniel Warfield\AppData\Local\Programs\Python\Python35\Lib\site-packages\Ok2StandLibrary\ok2stand_library\modeling\modeling_tools.py", line 127, in <module>
fullTest()
File "C:\Users\Daniel Warfield\AppData\Local\Programs\Python\Python35\Lib\site-packages\Ok2StandLibrary\ok2stand_library\modeling\modeling_tools.py", line 105, in fullTest
est.train(train_input_fn, max_steps=250)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 349, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1175, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1208, in _train_model_default
saving_listeners)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1507, in _train_with_estimator_spec
log_step_count_steps=log_step_count_steps) as mon_sess:
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 604, in MonitoredTrainingSession
stop_grace_period_secs=stop_grace_period_secs)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1038, in __init__
stop_grace_period_secs=stop_grace_period_secs)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 749, in __init__
self._sess = _RecoverableSession(self._coordinated_creator)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1231, in __init__
_WrappedSession.__init__(self, self._create_session())
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1236, in _create_session
return self._sess_creator.create_session()
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\monitored_session.py", line 909, in create_session
hook.after_create_session(self.tf_sess, self.coord)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 587, in after_create_session
self._save(session, global_step)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\basic_session_run_hooks.py", line 620, in _save
write_meta_graph=self._save_graph_def)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\saver.py", line 1158, in save
global_step = training_util.global_step(sess, global_step)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\training\training_util.py", line 68, in global_step
return int(sess.run(global_step_tensor))
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 958, in run
run_metadata_ptr)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 1166, in _run
self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 477, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 276, in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File "D:\Documents\Contracting\Yang\Ok2StandUp\Environments\ok2suGen\lib\site-packages\tensorflow\python\client\session.py", line 309, in __init__
(fetch, type(fetch), str(e)))
TypeError: Fetch argument 0 has invalid type <class 'numpy.int64'>, must be a string or Tensor. (Can not convert a int64 into a Tensor or Operation.)
version info (also GPU acceleration isn't working but as long as that isn't the source of the problem it's not a concern):
2020-08-31 20:29:14.209013: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-31 20:29:14.209649: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Python Version: 3.5.6 |Anaconda, Inc.| (default, Aug 26 2018, 16:05:27) [MSC v.1900 64 bit (AMD64)]
tf version: 2.3.0
I'm trying to get the predictions inside the function on_epoch_end of keras' Callback.
At the moment, to get the predictions, I execute self.model.predict with batch_size of 2, but at the 3rd epochs I get this error:
RuntimeError: Dst tensor is not initialized in Tensorflow
Reading on the web, I notice that this error appears when the GPU goes out of memory. In my case, reading the stack trace, this error is triggered by self.model.predict inside on_epoch_end, it says:
File "mlp_keras.py", line 20, in on_epoch_end predictions =
self.model.predict(self.dataset)
This is the full stack trace:
Traceback (most recent call last):
File "mlp_keras.py", line 150, in <module>
callbacks=[KendallTauHistory(training_dataset, training_dataset_labels, groups_id_count)])
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 397, in fit
prefix='val_')
File "/usr/lib64/python2.7/contextlib.py", line 24, in __exit__
self.gen.next()
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 771, in on_epoch
self.callbacks.on_epoch_end(epoch, epoch_logs)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/callbacks.py", line 302, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "mlp_keras.py", line 20, in on_epoch_end
predictions = self.model.predict(self.dataset)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 1013, in predict
use_multiprocessing=use_multiprocessing)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 498, in predict
workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 426, in _model_iteration
use_multiprocessing=use_multiprocessing)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 357, in __init__
dataset = self.slice_inputs(indices_dataset, inputs)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 383, in slice_inputs
dataset_ops.DatasetV2.from_tensors(inputs).repeat()
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 566, in from_tensors
return TensorDataset(tensors)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 2765, in __init__
element = structure.normalize_element(element)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/data/util/structure.py", line 113, in normalize_element
ops.convert_to_tensor(t, name="component_%d" % i))
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/framework/ops.py", line 1314, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
return constant_op.constant(value, dtype, name=name)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 258, in constant
allow_broadcast=True)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 266, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/usr/home/studenti/sp171412/word_ordering/mlp/env/lib/python2.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
RuntimeError: Dst tensor is not initialized.
My question is: is there a way to get the predictions without performing predict inside on_epoch_end? Thanks in advance.
Alright, after seeing your last comment, what you could do:
epochs = 100
for epoch in range(epochs):
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
I am making a neural network with two customized activation function. The first activation function is f(x) and the second one is derivative of f(x) with respect to f(x). Instead of taking the derivative by hand, which is for my real case very hard, how can I make Keras backend do it automatically?
Here is my code:
import numpy as np
import math
import keras
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Activation
from keras import regularizers
from keras import backend as K
def custom_activation_f(x):
return (K.sigmoid(x) *2-1 )
def custom_activation_fprime(x):
deriv(x)= # take the derivative of custom_activation_f(x)
return deriv(x)
x_train=np.random.uniform(low=-1,high=1,size=(200,2))
model=Sequential([
Dense(20,input_shape=(2,)),
Activation(custom_activation_f),
Dense(2,),
Activation(custom_activation_fprime)
])
model.compile(optimizer='adam',loss='mean_squared_error')
model.fit(x_train,x_train,epochs=20,validation_split=0.1)
Update: after I received an answer from Pranit Kothari:, I changed the custom_activation_fprim part to the following:
def custom_activation_fprime(x):
my_derivative= K.gradients(custom_activation_f, x)
return my_derivative
Is this how I should use it? Here is the error:
Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W1004 12:07:29.987481 2280 deprecation_wrapper.py:119] From C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
W1004 12:07:30.011625 2280 deprecation_wrapper.py:119] From C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
W1004 12:07:30.016886 2280 deprecation_wrapper.py:119] From C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
Traceback (most recent call last):
File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\tensor_util.py", line 558, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\tensor_util.py", line 558, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\util\compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <function custom_activation_f at 0x000002896662C1E0>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/p/CE/mytest3.py", line 24, in <module>
Activation(custom_activation_fprime)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 93, in __init__
self.add(layer)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 181, in add
output_tensor = layer(self.outputs[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\core.py", line 299, in call
return self.activation(inputs)
File "C:/p/CE/mytest3.py", line 15, in custom_activation_fprime
my_derivative= K.gradients(custom_activation_f, x)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2757, in gradients
return tf.gradients(loss, variables, colocate_gradients_with_ops=True)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\gradients_impl.py", line 158, in gradients
unconnected_gradients)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\gradients_util.py", line 594, in _GradientsHelper
ys = ops.convert_n_to_tensor_or_indexed_slices(ys, name="y")
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\ops.py", line 1456, in convert_n_to_tensor_or_indexed_slices
values=values, dtype=dtype, name=name, as_ref=False)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\ops.py", line 1428, in internal_convert_n_to_tensor_or_indexed_slices
value, dtype=dtype, name=n, as_ref=as_ref))
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\ops.py", line 1388, in internal_convert_to_tensor_or_indexed_slices
value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py", line 305, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py", line 246, in constant
allow_broadcast=True)
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py", line 284, in _constant_impl
allow_broadcast=allow_broadcast))
File "C:\Users\r. jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\tensor_util.py", line 562, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'function'> to Tensor. Contents: <function custom_activation_f at 0x000002896662C1E0>. Consider casting elements to a supported type.
There are 2 ways you can solve it.
Use K.gradients to take the derivative, but it requires tensor as input, but here it is a function. Below is the example snippet of how to use it.
example code snippet
def custom_activation_fprime(x):
my_derivative= K.gradients(tf.convert_to_tensor(custom_activation_f(x), dtype=tf.float32), x)
return my_derivative
or
2. Create a custom derivative function and pass that function as activation to the model
Have you tried gradients from keras?
from keras import backend
backend.gradients(loss, variable)
I want to load a checkpoint from TensorFlow1 that consists of .index, .meta, .data-00000-of-00001 files into tensorflow2.0.0 and convert it to a keras model so to be able to use it natively in eager mode without need for tf.Session. Here is the code I ran:
import tensorflow as tf
import numpy as np
from tensorflow.python.keras.backend import set_session
from tensorflow.python.training.saver import _import_meta_graph_with_return_elements
def save_ckpt(ckpt_path='test'):
'''save TensorFlow-1 Checkpoint '''
with tf.Graph().as_default() as g:
in_op = tf.constant(np.random.rand(1,2,2,2),name='input',dtype=tf.float32)
out_op = tf.keras.layers.Conv2D(3,(3,3),padding='same',name='MY_LAYER')(in_op)
saver = tf.compat.v1.train.Saver()
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.variables_initializer(tf.compat.v1.global_variables()))
saver.save(sess,ckpt_path)
def load_ckpt():
'''KerasModel from meta & ckpt'''
in_op = tf.keras.Input([2,2,2])
_m = tf.keras.models.Model(inputs=in_op,outputs=in_op)
with _m.input.graph.as_default() as g:
saver, out_op = _import_meta_graph_with_return_elements('test.meta',
input_map={'input':_m.output},
return_elements=[
# 'input:0',
'MY_LAYER/Conv2D:0'
])
with tf.compat.v1.Session() as sess:
saver.restore(sess,'test')
set_session(sess)
out_mdl = tf.keras.models.Model(inputs=_m.input, outputs=out_op[0])
return out_mdl
# main
save_ckpt() # save name based checkpoint
meta_model = load_ckpt() # restore in keras model
oo = meta_model(np.random.rand(1,2,2,2)) # run the model
print(oo)
but I get this error:
Traceback (most recent call last):
File "question2.py", line 38, in <module>
meta_model = load_ckpt() # restore in keras model
File "question2.py", line 32, in load_ckpt
out_mdl = tf.keras.models.Model(inputs=_m.input, outputs=out_op[0])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 146, in __init__
super(Model, self).__init__(*args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 167, in __init__
self._init_graph_network(*args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py", line 270, in _init_graph_network
base_layer_utils.create_keras_history(self._nested_outputs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py", line 184, in create_keras_history
_, created_layers = _create_keras_history_helper(tensors, set(), [])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py", line 229, in _create_keras_history_helper
constants[i] = backend.function([], op_input)([])
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 3740, in __call__
outputs = self._graph_fn(*converted_inputs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1081, in __call__
return self._call_impl(args, kwargs)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1121, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1224, in _call_flat
ctx, args, cancellation_manager=cancellation_manager)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 511, in call
ctx=ctx)
File "/home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable MY_LAYER/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/MY_LAYER/kernel)
[[node MY_LAYER/Conv2D/ReadVariableOp (defined at /home/dionyssos/tf2/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_keras_scratch_graph_72]
Function call stack:
keras_scratch_graph
What I tried so far
Replacing MY_LAYER/Conv2D:0 with input:0 in _import_meta_graph_with_return_elements() makes the code run with no problem.
I am using the tensorflow data API to try and do some rejection sampling for my unbalanced data set.
I have run the code on my personal computer and it seems to work as I expect it to, however, when I run the code on my University's cluster I get a type error that I can't seem to understand. I have tried recasting and I get the same error.
I am still learning how to use this API and I'm still not 100% clear on if this is the best way to achieve what I want, so I also welcome any advice on how I implemented the rejection sampling (this could very well be the reason why I get error since I don't fully understand yet).
This is how I am loading in the data to the dataset:
data = np.loadtxt("my_data.dat")
features = data[:, 1:10]
labels = data[:, 0]
labels[labels == -1] = 0
assert features.shape[0] == labels.shape[0]
dataset_size = len(features)
dataset = tf.data.Dataset.from_tensor_slices((features.astype('float32'),
labels.astype('int32')))
dataset = dataset.shuffle(buffer_size=dataset_size)
the error occurs when I read here:
train_size = int((2/3.0)*dataset_size)
tr_dataset = dataset.take(train_size)
tr_dataset = (tr_dataset.apply(
tf.contrib.data.rejection_resample(
class_func=lambda _, c: c, target_dist=[0.5, 0.5],
seed=42)).map(lambda a, b: b)).batch(100)
This is the error:
Traceback (most recent call last):
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1094, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 931, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: 'Tensor("Sum:0", shape=(2,), dtype=int64)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 185, in <module>
seed=42))).batch(100)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1109, in apply
dataset = transformation_func(self)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/resampling.py", line 74, in _apply_fn
target_dist_t, class_values_ds)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/resampling.py", line 183, in _estimate_initial_dist_ds
update_estimate_and_tile))
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1109, in apply
dataset = transformation_func(self)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/scan_ops.py", line 172, in _apply_fn
return _ScanDataset(dataset, initial_state, scan_func)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/scan_ops.py", line 74, in __init__
add_to_graph=False)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1459, in __init__
self._function._create_definition_if_needed() # pylint: disable=protected-access
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 337, in _create_definition_if_needed
self._create_definition_if_needed_impl()
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 346, in _create_definition_if_needed_impl
self._capture_by_value, self._caller_device)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 863, in func_graph_from_py_func
outputs = func(*func_graph.inputs)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1392, in tf_data_structured_function_wrapper
ret = func(*nested_args)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/resampling.py", line 176, in update_estimate_and_tile
c, num_examples_per_class_seen)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/contrib/data/python/ops/resampling.py", line 212, in _estimate_data_distribution
array_ops.one_hot(c, num_classes, dtype=dtypes.int64), 0))
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 297, in add
"Add", x=x, y=y, name=name)
File "/home/user/.conda/envs/tensorflowcpu/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 546, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Add' Op has type int64 that does not match type int32 of argument 'x'.