1

I found a snippest about customized metrics.

smooth = 0.001
dices = (2. * a + smooth) / (a+ b+ smooth)

ratio = a/ (K.sum(a) + smooth)
ratio = 1.0 - ratio

Why the author prefer float number (like 2. and 1.0) instead of integers (2 or 1)? And I found that a lot of people prefer using floating number instead of integers, I do not know why.

I guess if this is the reason?

Jingnan Jia
  • 1,108
  • 2
  • 12
  • 28
  • I think they will be automatically converted, but there are languages that may raise problems if you don't put the right type. I also think Python 2 works a little different from Python 3 there. So for safety, it's probably better to keep it float. – Daniel Möller Jan 03 '20 at 18:49

1 Answers1

1

Below code will show the difference between two.

t1 = tf.constant(value=1)
t1.dtype

Output tf.int32

t2 = tf.constant(value=1.0)
t2.dtype

Output tf.float32

now adding these two tensors will throw an error as data type for these two are different.

t3 = tf.add(x=t1, y=t2)

Output:

Traceback (most recent call last): File "c:\ProgramData\Anaconda3\envs\tensorflow_cpu\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 527, in _apply_op_helper preferred_dtype=default_dtype) File "c:\ProgramData\Anaconda3\envs\tensorflow_cpu\lib\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:\ProgramData\Anaconda3\envs\tensorflow_cpu\lib\site-packages\tensorflow\python\framework\ops.py", line 1018, in _TensorTensorConversionFunction (dtype.name, t.dtype.name, str(t))) ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("Const_4:0", shape=(), dtype=float32)'

Vikas Sharma
  • 451
  • 2
  • 8
  • Thanks. I know in python, `t1=1;t2=1.0;t3=t1+t2` is OK, because python will automatically convert integers to floats. But I did not know that tensorflow can not do that. – Jingnan Jia Jan 03 '20 at 19:10