Encoders and Decoders - Okerew/okrolearn GitHub Wiki
You can use encoders and decoders like here
fromokrolearn.okrolearnimportEncoder, Decoder, Tensor, npdeftest_encoder_decoder():
input_dim=784# e.g., for MNIST images (28x28)hidden_dims= [256, 128]
latent_dim=64batch_size=32encoder=Encoder(input_dim, hidden_dims, latent_dim)
decoder=Decoder(latent_dim, hidden_dims[::-1], input_dim)
# Create a random inputx=Tensor(np.random.randn(batch_size, input_dim))
# Forward pass through encoderencoded=encoder.forward(x)
print(encoded.data.shape)
assertencoded.data.shape== (batch_size, latent_dim), f"Encoded shape {encoded.data.shape} doesn't match expected shape {(batch_size, latent_dim)}"# Forward pass through decoderdecoded=decoder.forward(encoded)
print(decoded.data.shape)
assertdecoded.data.shape== (batch_size, input_dim), f"Decoded shape {decoded.data.shape} doesn't match expected shape {(batch_size, input_dim)}"print("Test passed successfully!")
# Run the testtest_encoder_decoder()
Text managment
fromokrolearn.okrolearnimportEncoder, Decoderfromokrolearn.okroltextimportTextToTensor, TensorToText# Example usage:vocabulary= ['a', 'b', 'c', ..., 'z', ' ', '<UNK>'] # Add all characters you expect in your texttext_to_tensor=TextToTensor(vocabulary)
tensor_to_text=TensorToText(vocabulary)
# Convert text to tensorinput_text="hello world"tensor=text_to_tensor.encode(input_text)
# Use your existing Encoder and Decoder hereencoder=Encoder(input_dim=len(vocabulary), hidden_dims=[64, 32], output_dim=16)
decoder=Decoder(input_dim=16, hidden_dims=[32, 64], output_dim=len(vocabulary))
encoded=encoder.forward(tensor)
decoded=decoder.forward(encoded)
# Convert tensor back to textoutput_text=tensor_to_text.decode(decoded)
print(output_text)