-
Notifications
You must be signed in to change notification settings - Fork 0
Encoders and Decoders
Okerew edited this page Aug 2, 2024
·
5 revisions
from okrolearn.okrolearn import Encoder, Decoder, Tensor, np
def test_encoder_decoder():
input_dim = 784 # e.g., for MNIST images (28x28)
hidden_dims = [256, 128]
latent_dim = 64
batch_size = 32
encoder = Encoder(input_dim, hidden_dims, latent_dim)
decoder = Decoder(latent_dim, hidden_dims[::-1], input_dim)
# Create a random input
x = Tensor(np.random.randn(batch_size, input_dim))
# Forward pass through encoder
encoded = encoder.forward(x)
print(encoded.data.shape)
assert encoded.data.shape == (batch_size, latent_dim), f"Encoded shape {encoded.data.shape} doesn't match expected shape {(batch_size, latent_dim)}"
# Forward pass through decoder
decoded = decoder.forward(encoded)
print(decoded.data.shape)
assert decoded.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 test
test_encoder_decoder()
from okrolearn.okrolearn import Encoder, Decoder
from okrolearn.okroltext import TextToTensor, TensorToText
# Example usage:
vocabulary = ['a', 'b', 'c', ..., 'z', ' ', '<UNK>'] # Add all characters you expect in your text
text_to_tensor = TextToTensor(vocabulary)
tensor_to_text = TensorToText(vocabulary)
# Convert text to tensor
input_text = "hello world"
tensor = text_to_tensor.encode(input_text)
# Use your existing Encoder and Decoder here
encoder = 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 text
output_text = tensor_to_text.decode(decoded)
print(output_text)