对接受一维输入的 tensorflow hub 层使用 Pandas/Numpy 输入数据
Using Pandas/Numpy input data for tensorflow hub layer that accepts one dimensional input
下午好。我正在尝试重新使用来自 tensorflow hub 的 NNLM 层来为 NLP 任务进行迁移学习。
我正在尝试使用 IMDB 数据集来启动它。
我 运行 遇到的问题是许多 tensorflow hub NNLM 层都带有以下警告:该模块将一维字符串张量中的一批句子作为输入。我看到的大多数示例都使用预加载的数据集,但我使用的绝大多数数据都存储在 pandas 或 Numpy 中,所以我试图让输入数据从这种格式。
我尝试使用的图层可以在这里找到:https://tfhub.dev/google/Wiki-words-500/2
到目前为止,我已经尝试了以下方法但没有成功。
方法 1: 将 pandas 数据帧或 numpy 数组转换为 tensorflow 数据集对象。
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Embedding, Flatten
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow_hub as hub
import pandas as pd
import numpy as n
from tensorflow import string
import tensorflow as tf
hub_layer = hub.KerasLayer("https://tfhub.dev/google/Wiki-words-500/2",
input_shape=[], dtype=string)
mod2 = Sequential([
hub_layer,
Dense(20, activation='relu'),
Dense(1, activation='sigmoid')
])
mod2.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
然后我尝试按如下方式转换训练数据:
train_data = tf.data.Dataset.from_tensor_slices((train_sequences, y_train.values))
train_sequences
是已标记化的评论集合,存储为 (35000, 150)
形 numpy 数组。
然后我打电话给:
mod2.fit(train_data.batch(32).prefetch(1), epoch=2)
出现以下错误:
ValueError: Error when checking input: expected keras_layer_input to have 1 dimensions, but got array with shape (None, 150)
无论传递给 batch()
或 prefetch()
的值如何,无论调用 [=23= 时是否使用 steps_per_epoch
参数,我都会收到此错误消息].
我知道这需要以某种方式重塑,但我不熟悉 tensorflow,也不知道如何针对此特定任务进行重塑。
方法 2: 从 numpy 数组创建迭代器。
我的想法是,如果我将一个 numpy 数组变成一个迭代器,我可以对其调用 next()
,然后将每一行递增地从 keras hub 送入层。
这是我的代码:
train_iter = iter(train_sequences)
y_iter = iter(y.values)
a = next(train_iter)
b = next(y_iter)
然后我调用:
mod2.fit(a, b, epochs=2, steps_per_epoch=1)
并得到以下错误信息:
ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'numpy.int64'>
我知道基本的想法是我必须设置我的输入数据以使其成为图层的正确形状,但目前我不知道如何执行此操作。
感谢您的帮助。
为了社区的利益,在这个(答案)部分中提及答案,即使它已经出现在评论部分中。
传递 Raw Text Values
而不是 Tokens
(使用 [=13 生成=]) 已解决问题。
示例代码如下:
import tensorflow_hub as hub
embed = hub.load("https://tfhub.dev/google/Wiki-words-500/2")
embeddings = embed(["cat is on the mat", "dog is in the fog"])
详情请参考this link了解更多
下午好。我正在尝试重新使用来自 tensorflow hub 的 NNLM 层来为 NLP 任务进行迁移学习。
我正在尝试使用 IMDB 数据集来启动它。
我 运行 遇到的问题是许多 tensorflow hub NNLM 层都带有以下警告:该模块将一维字符串张量中的一批句子作为输入。我看到的大多数示例都使用预加载的数据集,但我使用的绝大多数数据都存储在 pandas 或 Numpy 中,所以我试图让输入数据从这种格式。
我尝试使用的图层可以在这里找到:https://tfhub.dev/google/Wiki-words-500/2
到目前为止,我已经尝试了以下方法但没有成功。
方法 1: 将 pandas 数据帧或 numpy 数组转换为 tensorflow 数据集对象。
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Embedding, Flatten
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow_hub as hub
import pandas as pd
import numpy as n
from tensorflow import string
import tensorflow as tf
hub_layer = hub.KerasLayer("https://tfhub.dev/google/Wiki-words-500/2",
input_shape=[], dtype=string)
mod2 = Sequential([
hub_layer,
Dense(20, activation='relu'),
Dense(1, activation='sigmoid')
])
mod2.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
然后我尝试按如下方式转换训练数据:
train_data = tf.data.Dataset.from_tensor_slices((train_sequences, y_train.values))
train_sequences
是已标记化的评论集合,存储为 (35000, 150)
形 numpy 数组。
然后我打电话给:
mod2.fit(train_data.batch(32).prefetch(1), epoch=2)
出现以下错误:
ValueError: Error when checking input: expected keras_layer_input to have 1 dimensions, but got array with shape (None, 150)
无论传递给 batch()
或 prefetch()
的值如何,无论调用 [=23= 时是否使用 steps_per_epoch
参数,我都会收到此错误消息].
我知道这需要以某种方式重塑,但我不熟悉 tensorflow,也不知道如何针对此特定任务进行重塑。
方法 2: 从 numpy 数组创建迭代器。
我的想法是,如果我将一个 numpy 数组变成一个迭代器,我可以对其调用 next()
,然后将每一行递增地从 keras hub 送入层。
这是我的代码:
train_iter = iter(train_sequences)
y_iter = iter(y.values)
a = next(train_iter)
b = next(y_iter)
然后我调用:
mod2.fit(a, b, epochs=2, steps_per_epoch=1)
并得到以下错误信息:
ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'numpy.int64'>
我知道基本的想法是我必须设置我的输入数据以使其成为图层的正确形状,但目前我不知道如何执行此操作。
感谢您的帮助。
为了社区的利益,在这个(答案)部分中提及答案,即使它已经出现在评论部分中。
传递 Raw Text Values
而不是 Tokens
(使用 [=13 生成=]) 已解决问题。
示例代码如下:
import tensorflow_hub as hub
embed = hub.load("https://tfhub.dev/google/Wiki-words-500/2")
embeddings = embed(["cat is on the mat", "dog is in the fog"])
详情请参考this link了解更多