发生此错误。 “ TypeError:预期为int64,取而代之的是'float'类型的1e-07。” 。我能怎么做?

时间:2019-05-19 23:59:17

标签: python keras

我有问题。发生类型错误。但我无法解决。 起初,我认为这是一个类型问题。但是我注意到这个问题并不简单。 原因是我的技能差。但是我找不到解决方案。所以,请帮助我。

我更改了tensorflow和keras版本。

keras : 2.2.4
tensorflow : 1.13.1

代码:

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'])

摘要:

__________________________________________________________________________________________________
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
__________________________________________________________________________________________________

错误:

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.

1 个答案:

答案 0 :(得分:0)

尝试将数据类型从int更改为float。也许那会解决问题。

可以找到类似的讨论here