将Elmo与tf.Keras一起使用会引发ValueError:无法将字符串转换为float

时间:2019-03-25 09:59:49

标签: python numpy tensorflow keras elmo

我正在尝试将Elmo与tf.keras一起使用。但是,对model.fit的调用导致ValueError: could not convert string to float

  • Tensorflow版本:1.13.1
  • 数字版:1.14.6

这是完整的代码:

import tensorflow as tf
import tensorflow_hub as hub
import tensorflow.keras.backend as K
import numpy as np

class ElmoEmbeddingLayer(tf.keras.layers.Layer):
    """Taken from: 
    https://github.com/strongio/keras-elmo/blob/master/Elmo%20Keras.ipynb"""
    def __init__(self, **kwargs):
        self.dimensions = 1024
        self.trainable=False
        super(ElmoEmbeddingLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.elmo = hub.Module(
            'https://tfhub.dev/google/elmo/2', 
            trainable=self.trainable,
            name="{}_module".format(self.name)
        )
        # Changed assuming trainable weights might be set using 
        super(ElmoEmbeddingLayer, self).build(input_shape)

    def call(self, x, mask=None):
        result = self.elmo(
            K.squeeze(K.cast(x, tf.string), axis=1),
            as_dict=True,
            signature='default',
        )['default']
        return result

    def compute_mask(self, inputs, mask=None):
        return K.not_equal(inputs, '--PAD--')

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.dimensions)

def create_model():
  # Create Sequential model
  model = tf.keras.Sequential([
      ElmoEmbeddingLayer(),
      tf.keras.layers.Dense(1)
  ])

  # Needed to initialize elmo variables
  sess = K.get_session()
  init = tf.global_variables_initializer()
  sess.run(init)

  # Compile model
  model.compile(
      optimizer="adam", 
      loss="binary_crossentropy", 
      metrics=["accuracy"]
  )
  return model


X = np.array([
    "This is good",
    "This is bad"
]).reshape(2, 1)
y = np.array([0, 1]).reshape(2, 1)
X.shape, y.shape

model = create_model()
model.fit(X, y)


colab链接在这里:https://colab.research.google.com/drive/1SvGOEtCYHJkpBVAOU0qRtwR8IPE_b2Lw

完整错误代码:

INFO:tensorflow:Saver not created because there are no variables in the graph to restore
I0325 09:50:35.584104 140534836959104 saver.py:1483] Saver not created because there are no variables in the graph to restore
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
W0325 09:50:35.827362 140534836959104 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-6d10fe8973eb> in <module>()
----> 1 model.fit(X, y)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
    878           initial_epoch=initial_epoch,
    879           steps_per_epoch=steps_per_epoch,
--> 880           validation_steps=validation_steps)
    881 
    882   def evaluate(self,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, mode, validation_in_fit, **kwargs)
    327 
    328         # Get outputs.
--> 329         batch_outs = f(ins_batch)
    330         if not isinstance(batch_outs, list):
    331           batch_outs = [batch_outs]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   3059         tensor_type = dtypes_module.as_dtype(tensor.dtype)
   3060         array_vals.append(np.asarray(value,
-> 3061                                      dtype=tensor_type.as_numpy_dtype))
   3062 
   3063     if self.feed_dict:

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    490 
    491     """
--> 492     return array(a, dtype, copy=False, order=order)
    493 
    494 

ValueError: could not convert string to float: 'This is bad'

1 个答案:

答案 0 :(得分:1)

好的,我能够通过在顺序模型开始时使用tf.keras.layers.InputLayer(dtype='string', input_shape=(1,))来解决此问题。这里介绍了这个想法:https://gist.github.com/colinmorris/9183206284b4fe3179809098e809d009

这是已更改的型号:

model = tf.keras.Sequential([
      # Add Explicit Input layer
      tf.keras.layers.InputLayer(dtype='string', input_shape=(1,)),
      ElmoEmbeddingLayer(),
      tf.keras.layers.Dense(1)
  ])

完整的合作笔记本:https://colab.research.google.com/drive/1SvGOEtCYHJkpBVAOU0qRtwR8IPE_b2Lw