Beispiel #1
0
DIGITS = 3
INVERT = True
# Try replacing GRU, or SimpleRNN
RNN = recurrent.SimpleRNN
HIDDEN_SIZE = 128
BATCH_SIZE = 128
LAYERS = 1
MAXLEN = DIGITS + 1 + DIGITS

print("Generating data...")
engine = NumberDataEngine()
questions, expected = engine.get_dataset(TRAINING_SIZE)
print("Total addition questions:", len(questions))

print("Vectorization...")
convertor = CharacterDataEngine(engine.get_character_set(), maxlen=MAXLEN)
X = convertor.encode_dataset(questions, invert=True)
y = convertor.encode_dataset(expected, maxlen=DIGITS + 1)

# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
indices = np.arange(len(y))
np.random.shuffle(indices)
X = X[indices]
y = y[indices]

# Explicitly set apart 10% for validation data that we never train over
split_at = len(X) - len(X) / 10
(X_train, X_val) = (slice_X(X, 0, split_at), slice_X(X, split_at))
(y_train, y_val) = (y[:split_at], y[split_at:])

print(X_train.shape)
Beispiel #2
0
# Parameters for the model and dataset
TRAINING_SIZE = 50000
DIGITS = 3
INVERT = True
HIDDEN_SIZE = 128
BATCH_SIZE = 128
LAYERS = 1
MAXLEN = DIGITS + 1 + DIGITS

print('Generating data...')
engine = NumberDataEngine(min_digits=1, max_digits=DIGITS)
questions, expected = engine.get_dataset(TRAINING_SIZE)
print('Total addition questions:', len(questions))

print('Vectorization...')
convertor = CharacterDataEngine(engine.get_character_set(), maxlen=MAXLEN)
D_X = convertor.encode_dataset(questions, invert=INVERT)
D_y = convertor.encode_dataset(expected, maxlen=DIGITS + 1)

# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
indices = np.arange(len(D_y))
np.random.shuffle(indices)
D_X = D_X[indices]
D_y = D_y[indices]

# Explicitly set apart 10% for validation data that we never train over
split_at = len(D_X) - len(D_X) / 10
(D_X_train, D_X_val) = (slice_X(D_X, 0, split_at), slice_X(D_X, split_at))
(D_y_train, D_y_val) = (D_y[:split_at], D_y[split_at:])

print(D_X_train.shape)
Beispiel #3
0
from keras.layers.core import Activation, TimeDistributedDense
from keras_layer.shift import Shift
from keras.layers.recurrent import SimpleRNN
import numpy as np
from keras.layers.containers import Graph

TRAINING_SIZE = 100
chars = "0123456789abcdef"

print("Generating data...")
engine = SimpleChainEngine(chars)
starts, chains = engine.get_dataset(TRAINING_SIZE)
print("Total number of data:", len(starts))

print("Vectorization...")
convertor = CharacterDataEngine(chars, maxlen=len(chars) - 1)
initial_value = convertor.encode_dataset(starts, maxlen=1)
y = convertor.encode_dataset(chains)
split_at = len(y) - len(y) / 10
(y_train, y_val) = (y[:split_at], y[split_at:])
(i_train, i_val) = (initial_value[:split_at], initial_value[split_at:])
(X_train, X_val) = (y_train, y_val)
print(i_train.shape)
print(y_train.shape)

print("Build model...")
HIDDEN_SIZE = 128
BATCH_SIZE = 50
MAXLEN = len(chars) - 1
input_dim = convertor.get_dim()
rnn_layer = SimpleRNN(HIDDEN_SIZE, input_shape=(MAXLEN, convertor.get_dim()), return_sequences=True)
Beispiel #4
0
DIGITS = 3
INVERT = True
# Try replacing GRU, or SimpleRNN
RNN = recurrent.SimpleRNN
HIDDEN_SIZE = 128
BATCH_SIZE = 64
LAYERS = 1
MAXLEN = DIGITS + 1 + DIGITS

print("Generating data...")
engine = NumberDataEngine()
questions, expected = engine.get_dataset(TRAINING_SIZE)
print("Total addition questions:", len(questions))

print("Vectorization...")
convertor = CharacterDataEngine(engine.get_character_set(), maxlen=MAXLEN)
D_X = convertor.encode_dataset(questions, invert=True)
D_y = convertor.encode_dataset(expected, maxlen=DIGITS + 1)

# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
indices = np.arange(len(D_y))
np.random.shuffle(indices)
D_X = D_X[indices]
D_y = D_y[indices]

# Explicitly set apart 10% for validation data that we never train over
split_at = len(D_X) - len(D_X) / 10
(D_X_train, D_X_val) = (slice_X(D_X, 0, split_at), slice_X(D_X, split_at))
(D_y_train, D_y_val) = (D_y[:split_at], D_y[split_at:])

print(D_X_train.shape)
MIN_DIGITS = 10
DIGITS = 12
INVERT = True
HIDDEN_SIZE = 128
BATCH_SIZE = 256
LAYERS = 1
MAXLEN = DIGITS + 1

print('Generating data...')
engine = BigNumberDataEngine(min_digits=12, max_digits=DIGITS)
As, Bs, expected = engine.get_seperate_dataset(TRAINING_SIZE)
print('Total additions:', len(As))

print('Vectorization...')
convertor = CharacterDataEngine(engine.get_character_set(),
                                maxlen=MAXLEN,
                                soldier=' ')
D_A = convertor.encode_dataset(As, invert=True, index=True)
D_B = convertor.encode_dataset(Bs, invert=True, index=True)
D_y = convertor.encode_dataset(expected, maxlen=MAXLEN, invert=True)

# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
indices = np.arange(len(D_y))
np.random.shuffle(indices)
D_A = D_A[indices]
D_B = D_B[indices]
D_y = D_y[indices]

# Explicitly set apart 10% for validation data that we never train over
split_at = len(D_A) - len(D_A) / 10
(D_A_train, D_A_val) = (slice_X(D_A, 0, split_at), slice_X(D_A, split_at))
TRAINING_SIZE = 200000
MIN_DIGITS = 10
DIGITS = 12
INVERT = True
HIDDEN_SIZE = 128
BATCH_SIZE = 256
LAYERS = 1
MAXLEN = DIGITS + 1

print("Generating data...")
engine = BigNumberDataEngine(min_digits=12, max_digits=DIGITS)
As, Bs, expected = engine.get_seperate_dataset(TRAINING_SIZE)
print("Total additions:", len(As))

print("Vectorization...")
convertor = CharacterDataEngine(engine.get_character_set(), maxlen=MAXLEN, soldier=" ")
D_A = convertor.encode_dataset(As, invert=True, index=True)
D_B = convertor.encode_dataset(Bs, invert=True, index=True)
D_y = convertor.encode_dataset(expected, maxlen=MAXLEN, invert=True)

# Shuffle (X, y) in unison as the later parts of X will almost all be larger digits
indices = np.arange(len(D_y))
np.random.shuffle(indices)
D_A = D_A[indices]
D_B = D_B[indices]
D_y = D_y[indices]

# Explicitly set apart 10% for validation data that we never train over
split_at = len(D_A) - len(D_A) / 10
(D_A_train, D_A_val) = (slice_X(D_A, 0, split_at), slice_X(D_A, split_at))
(D_B_train, D_B_val) = (slice_X(D_B, 0, split_at), slice_X(D_B, split_at))