【问题标题】:Error output of the tf-hub layers embedding with Conv or Rnn使用 Conv 或 Rnn 嵌入的 tf-hub 层的错误输出
【发布时间】:2020-07-03 15:37:35
【问题描述】:

这是代码,想法是我想构建一个多语言情感分类器,但这里的问题是: (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 函数来调整嵌入输出的大小,但我没有找到让它工作的方法

你们有什么想法吗?

谢谢

【问题讨论】:

    标签: tensorflow keras deep-learning tensorflow2.0 tensorflow-hub


    【解决方案1】:

    您可以添加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)
    

    【讨论】:

      【解决方案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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-25
        • 2021-09-25
        • 1970-01-01
        • 2020-12-20
        • 2021-09-22
        • 2019-08-15
        • 2019-11-13
        • 2019-04-27
        相关资源
        最近更新 更多