考慮以下代碼:
import numpy as np
import tensorflow as tf
simple_data_samples = np.array([
[1, 1, 1, -1, -1],
[2, 2, 2, -2, -2],
[3, 3, 3, -3, -3],
[4, 4, 4, -4, -4],
[5, 5, 5, -5, -5],
[6, 6, 6, -6, -6],
[7, 7, 7, -7, -7],
[8, 8, 8, -8, -8],
[9, 9, 9, -9, -9],
[10, 10, 10, -10, -10],
[11, 11, 11, -11, -11],
[12, 12, 12, -12, -12],
])
def timeseries_dataset_multistep_combined(features, label_slice, input_sequence_length, output_sequence_length, batch_size):
feature_ds = tf.keras.preprocessing.timeseries_dataset_from_array(features, None, input_sequence_length output_sequence_length, batch_size=batch_size)
def split_feature_label(x):
x=tf.strings.as_string(x)
return x[:, :input_sequence_length, :], x[:, input_sequence_length:, label_slice]
feature_ds = feature_ds.map(split_feature_label)
return feature_ds
ds = timeseries_dataset_multistep_combined(simple_data_samples, slice(None, None, None), input_sequence_length=4, output_sequence_length=2,
batch_size=1)
def print_dataset(ds):
for inputs, targets in ds:
print("---Batch---")
print("Feature:", inputs.numpy())
print("Label:", targets.numpy())
print("")
print_dataset(ds)
張量流資料集“ds”由輸入和目標組成。我想將輸入和目標調整為文本向量。以下假設代碼顯示了我想要實作的目標:
input_vectorization = layers.TextVectorization(
max_tokens=20,
output_mode="int",
output_sequence_length=6,
)
target_vectorization = layers.TextVectorization(
max_tokens=20,
output_mode="int",
output_sequence_length=6 1
)
input_vectorization.adapt(ds.input)
target_vectorization.adapt(ds.target)
知道如何使用上述示例對此進行編碼嗎?
uj5u.com熱心網友回復:
如果我理解正確,您可以將現有資料集與這樣的TextVectorization圖層一起使用:
import tensorflow as tf
input_vectorization = tf.keras.layers.TextVectorization(
max_tokens=20,
output_mode="int",
output_sequence_length=6,
)
target_vectorization = tf.keras.layers.TextVectorization(
max_tokens=20,
output_mode="int",
output_sequence_length=6 1
)
# Get inputs only and flatten them
inputs = ds.map(lambda x, y: tf.reshape(x, (tf.math.reduce_prod(tf.shape(x)), )))
# Get targets only and flatten them
targets = ds.map(lambda x, y: tf.reshape(y, (tf.math.reduce_prod(tf.shape(y)), )))
input_vectorization.adapt(inputs)
target_vectorization.adapt(targets)
print(input_vectorization.get_vocabulary())
print(target_vectorization.get_vocabulary())
['', '[UNK]', '7', '6', '5', '4', '8', '3', '9', '2', '10', '1']
['', '[UNK]', '9', '8', '7', '6', '11', '10', '5', '12']
請注意,該adapt函式只是根據輸入創建一個詞匯表,詞匯表中的每個單詞都映射到一個唯一的整數值。另外,由于layer的默認引數standardize='lower_and_strip_punctuation',呼叫. 如果需要,您可以通過設定例如 來避免這種行為。TextVectorizationadaptstandardize='lower'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414252.html
標籤:
