这是代码,想法是我想构建一个多语言情感分类器,但是这里的问题是: (tensorflow 2.0.1),(tf-hub 0.7.0)
import tensorflow as tf
import tensorflow_hub as hub
ml_module = hub.load('https://tfhub.dev/google/universal-sentence-encoder-multilingual/3')
module = hub.KerasLayer(ml_module , dtype=tf.string, trainable=False, name='bert_embedding')
input_text = tf.keras.Input((), dtype=tf.string, name='input_text')
embedding = module(input_text)
conv1 = tf.keras.layers.Conv1D(32, 2, padding='valid', activation='relu', strides=1)(embedding)
dense1 = tf.keras.layers.Dense(512, activation="relu")(conv1)
layer1 = tf.keras.layers.Dense(9, name='sentiment')(dense1)
model = tf.keras.models.Model(inputs=input_text, outputs=layer1)
ValueError: Input 0 of layer conv1d_3 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 512]
也许我可以尝试使用keras lambda函数来调整嵌入输出的大小,但是我没有找到一种使之起作用的方法
你们有什么想法吗?
谢谢
答案 0 :(得分:3)
您可以添加一个Reshape
层,以将形状从[ None , 512 ]
更改为[ None , 512 , 1 ]
。
import tensorflow as tf
import tensorflow_hub as hub
ml_module = hub.load('https://tfhub.dev/google/universal-sentence-encoder-multilingual/3')
module = hub.KerasLayer(ml_module , dtype=tf.string, trainable=False, name='bert_embedding')
input_text = tf.keras.Input((), dtype=tf.string, name='input_text')
embedding = module(input_text)
reshape = tf.keras.layers.Reshape( target_shape=( None , 512 , 1 ) )( embedding )
conv1 = tf.keras.layers.Conv1D(32, 2, padding='valid', activation='relu', strides=1)(reshape)
dense1 = tf.keras.layers.Dense(512, activation="relu")(conv1)
layer1 = tf.keras.layers.Dense(9, name='sentiment')(dense1)
model = tf.keras.models.Model(inputs=input_text, outputs=layer1)
答案 1 :(得分:2)
哦,谢谢Shubham,它有效= D
这是使其运行的代码
import tensorflow as tf
import tensorflow_hub as hub
ml_module = hub.load('https://tfhub.dev/google/universal-sentence-encoder-multilingual/3')
module = hub.KerasLayer(ml_module , dtype=tf.string, trainable=False, name='bert_embedding')
input_text = tf.keras.Input((), dtype=tf.string, name='input_text')
embedding = module(input_text)
reshape = tf.keras.layers.Reshape(target_shape=(512, 1))(embedding)
conv1 = tf.keras.layers.Conv1D(filters, kernel, padding='valid', activation='relu', strides=1)(reshape)
gpool1 = tf.keras.layers.GlobalMaxPooling1D()(conv1)
dense1 = tf.keras.layers.Dense(dims, activation="relu")(gpool1)
dropout1 = tf.keras.layers.Dropout(0.2)(dense1)
layer1 = tf.keras.layers.Dense(n_classes, name='sentiment')(dropout1)
model = tf.keras.models.Model(inputs=input_text, outputs=layer1)