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)
Related
I get the errors below when loading a model in a separate script to make inferences. I'm able to train and save the model without a problem. My model is almost identical to the one in the Keras documentation HERE: https://keras.io/examples/nlp/pretrained_word_embeddings/
TypeError: Expected binary or unicode string, got tf.RaggedTensor(values=Tensor("model_2/embedding_1/embedding_lookup_ragged/embedding_lookup/Identity_1:0", shape=(None, 100), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64))
TypeError: Failed to convert object of type <class 'tensorflow.python.ops.ragged.ragged_tensor.RaggedTensor'> to Tensor. Contents: tf.RaggedTensor(values=Tensor("model_2/embedding_1/embedding_lookup_ragged/embedding_lookup/Identity_1:0", shape=(None, 100), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64)). Consider casting elements to a supported type.
my training session runs without a hitch. I'm even able to make predictions in the same training session.
I'm loading my model as follows:
model = load_model('saved_model', compile = False)
I'm running Keras 2.6.0
the full call back is:
Traceback (most recent call last):
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 549, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 549, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\util\compat.py", line 86, in as_bytes
raise TypeError('Expected binary or unicode string, got %r' %
TypeError: Expected binary or unicode string, got tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 100), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\####\AppData\Local\Temp/ipykernel_14488/2463106565.py", line 20, in <module>
model = tf.keras.models.load_model('my_model', compile = False)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\saving\save.py", line 205, in load_model
return saved_model_load.load(filepath, compile, options)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\saving\saved_model\load.py", line 143, in load
keras_loader.finalize_objects()
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\saving\saved_model\load.py", line 644, in finalize_objects
self._reconstruct_all_models()
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\saving\saved_model\load.py", line 663, in _reconstruct_all_models
self._reconstruct_model(model_id, model, layers)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\saving\saved_model\load.py", line 708, in _reconstruct_model
created_layers) = functional_lib.reconstruct_from_config(
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\functional.py", line 1283, in reconstruct_from_config
process_node(layer, node_data)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\functional.py", line 1231, in process_node
output_tensors = layer(input_tensors, **kwargs)
File "C:\Users\yanie\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\base_layer.py", line 976, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\base_layer.py", line 1114, in _functional_construction_call
outputs = self._keras_tensor_symbolic_call(
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\base_layer.py", line 848, in _keras_tensor_symbolic_call
return self._infer_output_signature(inputs, args, kwargs, input_masks)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\engine\base_layer.py", line 888, in _infer_output_signature
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\keras\layers\core.py", line 1232, in call
outputs = tf.tensordot(inputs, self.kernel, [[rank - 1], [0]])
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\ops\math_ops.py", line 5036, in tensordot
a = ops.convert_to_tensor(a, name="a")
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\profiler\trace.py", line 163, in wrapped
return func(*args, **kwargs)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\ops.py", line 1566, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\constant_op.py", line 346, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\constant_op.py", line 271, in constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\constant_op.py", line 288, in _constant_impl
tensor_util.make_tensor_proto(
File "C:\Users\####\anaconda3\envs\nn_deep_learning\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 551, in make_tensor_proto
raise TypeError("Failed to convert object of type %s to Tensor. "
TypeError: Failed to convert object of type <class 'tensorflow.python.ops.ragged.ragged_tensor.RaggedTensor'> to Tensor. Contents: tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 100), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64)). Consider casting elements to a supported type.
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.
I'm trying to fine tune resnet50 in half precision mode without success. It seems there are parts of model which are not compatible with float16. Here is my code:
dtype='float16'
K.set_floatx(dtype)
K.set_epsilon(1e-4)
model = Sequential()
model.add(ResNet50(weights='imagenet', include_top=False, pooling='avg'))
and I get this error:
Traceback (most recent call last):
File "train_resnet.py", line 40, in <module>
model.add(ResNet50(weights='imagenet', include_top=False, pooling='avg'))
File "/usr/local/lib/python3.6/dist-packages/keras/applications/__init__.py", line 28, in wrapper
return base_fun(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/applications/resnet50.py", line 11, in ResNet50
return resnet50.ResNet50(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras_applications/resnet50.py", line 231, in ResNet50
x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/layers/normalization.py", line 185, in call
epsilon=self.epsilon)
File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 1864, in normalize_batch_in_training
epsilon=epsilon)
File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 1839, in _fused_normalize_batch_in_training
data_format=tf_data_format)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py", line 1329, in fused_batch_norm
name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 4488, in fused_batch_norm_v2
name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 626, in _apply_op_helper
param_name=input_name)
File "/usr/local/lib/python3.6/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 'scale' has DataType float16 not in list of allowed values: float32
This was a reported bug and upgrading to Keras==2.2.5 solved the issue.
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'.
EDIT:
Following this link: TensorFlow: using a tensor to index another tensor
I've got near my solution.
def my_loss(yTrue,yPred):
pred_casted=K.cast(yPred, "int32")
real_pred=K.gather(t_best_x, pred_casted)
true_casted=K.cast(yTrue, "int32")
real_true=K.gather(t_best_x, true_casted)
real_loss=K.difference(real_pred, real.true) #i actually don’t know the name of this function, but I’m positive something like this exist
mean_loss=K.mean(real_loss, axis=-1)
return mean_loss
But when I try to train the model, I get the following error:
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 371, in make_tensor_proto
raise ValueError("None values not supported.")
Any suggestion about how to solve this and why this is caused is strongly appreciated. Note: using the commented "classical_loss_mse" everything works.
Full stack of the error in the end of the page.
.
.
.
.
.
.
.
ORIGINAL TEXT
I have your classical Keras NN with a custom Loss function
network.compile(loss=my_loss, optimizer='Adam', metrics=['mse'])
I am trying to create a custom loss function (my_loss)
def my_loss(yTrue,yPred):
The value of each prediction is a float between 0 and 1000.
I have an array containing, for each prediction of my NN, a "fitness" of the result.
Like this:
fitness[int(prediction)] = 0.8 #example
len(my_array)
> 1000
The concept of the loss function i want to create (I need this) is:
1 - my_array[int(yPred)]
Now, is not like I can do int(yPred)... yPred is a Tensor. How can I make this work?
I will try to explain more properly WHAT exactly I want using a pseudo-notworking-example:
def my_loss(yTrue,yPred):
true_loss=[]
for prediction in yPred:
true_loss.append(1-fitness[round(prediction)]) # fitness[round(any yTrue)] = 1
return true_loss
Note: fitness is a normal list containing normal floats
FULL STACK ERROR
File
"/home/federico/.local/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",
line 710, in runfile
execfile(filename, namespace)
File
"/home/federico/.local/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",
line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/federico/CLionProjects/monte/EXP2-STILLNOTWORKING.py",
line 201, in
callbacks=[TrainValTensorBoard(log_dir='./logs/'+time.strftime("%Y-%m-%d_%H-%M-%S-")
+ name, histogram_freq=0, write_graph=False, write_images=False),callbacks.EarlyStopping(monitor='val_loss',
patience=200, mode='auto')]) # Data for evaluation
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line
960, in fit
validation_steps=validation_steps)
File
"/usr/local/lib/python3.5/dist-packages/keras/engine/training.py",
line 1634, in fit
self._make_train_function()
File
"/usr/local/lib/python3.5/dist-packages/keras/engine/training.py",
line 990, in _make_train_function
loss=self.total_loss)
File
"/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py",
line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/keras/optimizers.py",
line 432, in get_updates
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py",
line 885, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py",
line 836, in convert_to_tensor
as_ref=False)
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py",
line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py",
line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py",
line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File
"/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py",
line 371, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.