我正在研究这款colab笔记本:
我想用ELMo嵌入替换gnews旋转嵌入。
所以,替换
model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
具有:
model = "https://tfhub.dev/google/elmo/2"
这里发生了一系列变化,例如需求
tf.compat.v1.disable_eager_execution()
但是我不了解图形的形状,我需要成功进行此替换。具体来说,我看到了。
#model = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
model = "https://tfhub.dev/google/elmo/2"
elmo = hub.Module(model, trainable=True, name="{}_module".format("mymod"))
hub_layer = hub.KerasLayer(elmo,
# output_shape=[3,20],
# input_shape=(1,),
dtype=tf.string,
trainable=True)
hub_layer(train_examples[:3])
生产
<tf.Tensor 'keras_layer_14/mymod_module_14_apply_default/truediv:0' shape=(3, 1024) dtype=float32>
这似乎很好。但是:
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
# First, I have to build, because I no longer have eager executon.
model.build(input_shape=(None,1024))
model.summary()
然后给出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-8786753617e4> in <module>()
4 model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
5
----> 6 model.build(input_shape=(None,1024))
7
8 model.summary()
18 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor_or_indexed_slices(value, dtype, name, as_ref)
1381 raise ValueError(
1382 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1383 (dtypes.as_dtype(dtype).name, value.dtype.name, str(value)))
1384 return value
1385 else:
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("Placeholder_12:0", shape=(None, 1024), dtype=float32)'
关于图形尺寸的其他变化以及如何解决?
答案 0 :(得分:0)
问题在于Keras假设输入为float32
:
转换请求dtype
string
的Tensor使用dtypefloat32
您可以说这是输入,因为名称为“ Placeholder_12:0”。占位符张量用于将数据输入模型。
模型hub_layer
需要一个字符串输入,因此您所需要做的就是添加一个Input
图层,指定该字符串
model = tf.keras.Sequential()
#add an input layer
model.add(tf.keras.layers.Input(shape=tuple(),dtype=tf.string))
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.build(input_shape=(None,1024))
model.summary()
结果:
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
keras_layer (KerasLayer) (None, 1024) 93600852
_________________________________________________________________
dense (Dense) (None, 16) 16400
_________________________________________________________________
dense_1 (Dense) (None, 1) 17
=================================================================
Total params: 93,617,269
Trainable params: 16,417
Non-trainable params: 93,600,852
_________________________________________________________________
通过您的修改和上述修改,我能够使用colab笔记本进行训练。