我想将这篇博文中的循环自动编码器改编为在联合环境中工作。
我稍微修改了模型以符合TFF 图像分类教程中显示的示例。
def create_compiled_keras_model():
model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(2, input_shape=(10, 2), name='Encoder'),
tf.keras.layers.RepeatVector(10, name='Latent'),
tf.keras.layers.LSTM(2, return_sequences=True, name='Decoder')]
)
model.compile(loss='mse', optimizer='adam')
return model
model = create_compiled_keras_model()
sample_batch = gen(1)
timesteps, input_dim = 10, 2
def model_fn():
keras_model = create_compiled_keras_model()
return tff.learning.from_compiled_keras_model(keras_model, sample_batch)
gen 函数定义如下:
import random
def gen(batch_size):
seq_length = 10
batch_x = []
batch_y = []
for _ in range(batch_size):
rand = random.random() * 2 * np.pi
sig1 = np.sin(np.linspace(0.0 * np.pi + rand, 3.0 * np.pi + rand, seq_length * 2))
sig2 = np.cos(np.linspace(0.0 * np.pi + rand, 3.0 * np.pi + rand, seq_length * 2))
x1 = sig1[:seq_length]
y1 = sig1[seq_length:]
x2 = sig2[:seq_length]
y2 = sig2[seq_length:]
x_ = np.array([x1, x2])
y_ = np.array([y1, y2])
x_, y_ = x_.T, y_.T
batch_x.append(x_)
batch_y.append(y_)
batch_x = np.array(batch_x)
batch_y = np.array(batch_y)
return batch_x, batch_x #batch_y
到目前为止,我一直无法找到任何不使用 TFF 存储库中的示例数据的文档。
如何修改它以创建联合数据集并开始训练?