简体   繁体   中英

This error was occured. “TypeError: Expected int64, got 1e-07 of type 'float' instead.” . How can I do?

I have a problem. A type error was occured. But I cannot solve it. At first, I thought it was a type problem. But I noticed the problem is not simple. The reason is my poor skill. But I can't find the solution. So, please help me.

I changed my tensorflow and keras version.

keras : 2.2.4
tensorflow : 1.13.1

The code:

x = Dozat(21)(x) # custom Lambda layer

print('x : ', x, '\n\n\n')

network = Model([w, p], x)
q = network.layers[8].output
print(q)

network.summary()

network.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

The summary:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
words (InputLayer)              (None, 21, 2)        0
__________________________________________________________________________________________________
pos (InputLayer)                (None, 21, 2)        0
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 21, 2, 128)   14541184    words[0][0]
__________________________________________________________________________________________________
embedding_2 (Embedding)         (None, 21, 2, 128)   9344        pos[0][0]
__________________________________________________________________________________________________
reshape_1 (Reshape)             (None, 21, 256)      0           embedding_1[0][0]
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 21, 256)      0           embedding_2[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 21, 512)      0           reshape_1[0][0]
                                                                 reshape_2[0][0]
__________________________________________________________________________________________________
bidirectional_1 (Bidirectional) (None, 21, 256)      656384      concatenate_1[0][0]
__________________________________________________________________________________________________
dozat_1 (Dozat)                 (1, 21)              0           bidirectional_1[0][0]
==================================================================================================
Total params: 15,206,912
Trainable params: 15,206,912
Non-trainable params: 0
__________________________________________________________________________________________________

The error:

Traceback (most recent call last):
  File "C:\Users\jkdsp\OneDrive\Desktop\github\git_from_the_hell\Keras_parsing\keras_11_biLSTM_prac_one_example_2.py", line 105, in <module>
    network.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 342, in compile
    sample_weight, mask)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training_utils.py", line 414, in weighted
    score_array = fn(y_true, y_pred)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\losses.py", line 91, in binary_crossentropy
    return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 3374, in binary_crossentropy
    _epsilon = _to_tensor(epsilon(), output.dtype.base_dtype)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 308, in _to_tensor
    return tf.convert_to_tensor(x, dtype=dtype)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1039, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1097, in convert_to_tensor_v2
    as_ref=False)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1175, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 304, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 245, in constant
    allow_broadcast=True)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 283, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 466, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "C:\Users\jkdsp\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 371, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int64, got 1e-07 of type 'float' instead.

Try to change your data type from int to float . Maybe that will solve the problem.

A similar discussion can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM