gpt4 book ai didi

python - 对不同长度的序列进行分类

转载 作者:行者123 更新时间:2023-11-28 17:15:36 24 4
gpt4 key购买 nike

<分区>

尽管经历了 multiple examples ,我仍然不明白如何使用 Keras 对不同长度的序列进行分类,similar to this question .我可以训练一个网络,通过使用掩码来检测不同长度的正弦波的频率:

from keras import models
from keras.layers.recurrent import LSTM
from keras.layers import Dense, Masking
from keras.optimizers import RMSprop
from keras.losses import categorical_crossentropy
from keras.preprocessing.sequence import pad_sequences

import numpy as np


def gen_noise(noise_len, mag):
return np.random.uniform(size=noise_len) * mag


def gen_sin(t_val, freq):
return 2 * np.sin(2 * np.pi * t_val * freq)


def train_rnn(x_train, y_train, max_len, mask, number_of_categories):
epochs = 3
batch_size = 500

# three hidden layers of 256 each
vec_dims = 1
hidden_units = 256
in_shape = (max_len, vec_dims)

model = models.Sequential()

model.add(Masking(mask, name="in_layer", input_shape=in_shape,))
model.add(LSTM(hidden_units, return_sequences=False))
model.add(Dense(number_of_categories, input_shape=(number_of_categories,),
activation='softmax', name='output'))

model.compile(loss=categorical_crossentropy, optimizer=RMSprop())

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
validation_split=0.05)

return model


def gen_sig_cls_pair(freqs, t_stops, num_examples, noise_magnitude):
x = []
y = []

num_cat = len(freqs)

dt = 0.01
max_t = int(np.max(t_stops) / dt)

for f_i, f in enumerate(freqs):
for t_stop in t_stops:
t_range = np.arange(0, t_stop, dt)
t_len = t_range.size

for _ in range(num_examples):
sig = gen_sin(f, t_range) + gen_noise(t_len, noise_magnitude)
x.append(sig)

one_hot = np.zeros(num_cat, dtype=np.bool)
one_hot[f_i] = 1
y.append(one_hot)

pad_kwargs = dict(padding='post', maxlen=max_t, value=np.NaN, dtype=np.float32)
return pad_sequences(x, **pad_kwargs), np.array(y)


if __name__ == '__main__':
noise_mag = 0.01
mask_val = -10
frequencies = (5, 7, 10)
signal_lengths = (0.8, 0.9, 1)

x_in, y_in = gen_sig_cls_pair(frequencies, signal_lengths, 50, noise_mag)
mod = train_rnn(x_in[:, :, None], y_in, 100, mask_val, len(frequencies))

但是,我不明白我应该如何告诉 Keras 其他序列。我以为我也可以屏蔽它们,但当我尝试时,它们只会输出 NaN

testing_dat, expected = gen_sig_cls_pair(frequencies, signal_lengths, 1, 0)
res = mod.predict(testing_dat[:, :, None])

fig, axes = plt.subplots(3)
axes[0].plot(np.concatenate(testing_dat), label="input")

axes[1].plot(np.argmax(res, axis=1), "ro", label="result", alpha=0.2)
axes[1].plot(np.argmax(expected, axis=1), "bo", label="expected", alpha=0.2)
axes[1].legend(bbox_to_anchor=(1.1, 1))

axes[2].plot(res)

plt.show()

我如何制作一个可以评估不同长度输入的网络?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com