gpt4 book ai didi

python - 将 Elmo 与 tf.Keras 一起使用会引发 ValueError : could not convert string to float

转载 作者:行者123 更新时间:2023-12-01 01:06:08 24 4
gpt4 key购买 nike

我正在尝试将 Elmo 与 tf.keras 结合使用。但是,对 model.fit 的调用导致 ValueError: Could not conversion string to float

  • tensorflow 版本: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'

最佳答案

好的,我可以通过在顺序模型开始时使用 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)
])

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

关于python - 将 Elmo 与 tf.Keras 一起使用会引发 ValueError : could not convert string to float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55335231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com