我正在尝试将 Elmo 与tf.keras
. 然而,呼吁model.fit
原因ValueError: could not convert string to float
- 张量流版本:1.13.1
- numpy 版本: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'