<jupyter_start><jupyter_text>Model<jupyter_code>def get_model(
    units, 
    input_shape, 
    output_shape, 
    loss, 
    metric, 
    tt_ips, 
    tt_ops, 
    tt_ranks, 
    act='tanh', 
    opt='adam', 
    verbose=True
):

    mdl = tf.keras.models.Sequential()

    mdl.add(tf.keras.layers.Input(input_shape))

    # Features layer
    mdl.add(SpecialRGTN(units))
    mdl.add(tf.keras.layers.Activation(act))

    # TT layer
    mdl.add(TensorTrainLayer(tt_ips, tt_ops, tt_ranks))
    mdl.add(tf.keras.layers.Activation(act))

    # Output layer
    mdl.add(tf.keras.layers.Flatten())
    mdl.add(tf.keras.layers.Dense(output_shape))
    
    if metric=='accuracy':
        mdl.add(tf.keras.layers.Activation('softmax'))

    mdl.compile(loss=loss,  optimizer=opt, metrics=[metric])
    
    if verbose:
        print(mdl.summary())
    
    return mdl<jupyter_output><empty_output><jupyter_text>Air Quality<jupyter_code>X, Y = pd.read_pickle('ML Data/air_quality.pkl')
X.shape, Y.shape
# X = X.reshape(-1, X.shape[1], np.prod(X.shape[2:]))
# X.shape, Y.shape
# Model params
mdl_params = {
    'units': 8,
    'input_shape': X.shape[1:],
    'output_shape': Y.shape[-1],
    'loss': 'mse',
    'metric': 'mae',
    'tt_ips': [X.shape[1], X.shape[2], 8],
    'tt_ops': [2, 2, 2],
    'tt_ranks': [1, 2, 2, 1],
    'verbose': 1,
}

# Exp params
tr_split = 0.80  
va_split = 0.1  
exp_rep = 5  
epochs = 30  
batch_size = 32  
verbose = 0
scores = []
for i in range(exp_rep):

    # Train test split
    tr_choice = np.random.choice(X.shape[0], int(X.shape[0]*tr_split), replace=False)
    te_choice = np.array(list(set(range(X.shape[0])) - set(tr_choice)))
    assert(len(set(tr_choice)) + len(set(te_choice)) == X.shape[0])
    
    X_tr, Y_tr = X[tr_choice], Y[tr_choice]
    X_te, Y_te = X[te_choice], Y[te_choice]    
    
    mdl = get_model(**mdl_params)
    mdl_params['verbose'] = 0
    mdl.fit(X_tr, Y_tr, epochs=epochs, batch_size=batch_size, validation_split=va_split, verbose=verbose)
    scores.append(mdl.evaluate(X_te, Y_te)[-1])
    print('Score: ', scores[-1])
    
print(f'Final score: {round(np.mean(scores), 5)} +/- {round(np.std(scores), 5)}')<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
special_rgtn (SpecialRGTN)   (None, 6, 12, 8)          268       
_________________________________________________________________
activation (Activation)      (None, 6, 12, 8)          0         
_________________________________________________________________
tensor_train_layer (TensorTr (None, 2, 2, 2)           152       
_________________________________________________________________
activation_1 (Activation)    (None, 2, 2, 2)           0         
_________________________________________________________________
flatten (Flatten)            (None, 8)                 0         
_________________________________________________________________
dense (Dense)                (None, 12)                108       
========================================================[...]<jupyter_text>Climate Change<jupyter_code>X, Y = pd.read_pickle('ML Data/climate_change.pkl')
X.shape, Y.shape
# X = X.reshape(-1, X.shape[1], np.prod(X.shape[2:]))
# X.shape, Y.shape
# Model params
mdl_params = {
    'units': 8,
    'input_shape': X.shape[1:],
    'output_shape': Y.shape[-1],
    'loss': 'mse',
    'metric': 'mae',
    'tt_ips': [X.shape[1], X.shape[2], 8],
    'tt_ops': [2, 2, 2],
    'tt_ranks': [1, 2, 2, 1],
    'verbose': 1,
}

# Exp params
tr_split = 0.80  
va_split = 0.1  
exp_rep = 5  
epochs = 30  
batch_size = 32  
verbose = 0
scores = []
for i in range(exp_rep):

    # Train test split
    tr_choice = np.random.choice(X.shape[0], int(X.shape[0]*tr_split), replace=False)
    te_choice = np.array(list(set(range(X.shape[0])) - set(tr_choice)))
    assert(len(set(tr_choice)) + len(set(te_choice)) == X.shape[0])
    
    X_tr, Y_tr = X[tr_choice], Y[tr_choice]
    X_te, Y_te = X[te_choice], Y[te_choice]    
    
    mdl = get_model(**mdl_params)
    mdl_params['verbose'] = 0
    mdl.fit(X_tr, Y_tr, epochs=epochs, batch_size=batch_size, validation_split=va_split, verbose=verbose)
    scores.append(mdl.evaluate(X_te, Y_te)[-1])
    print('Score: ', scores[-1])
    
print(f'Final score: {round(np.mean(scores), 5)} +/- {round(np.std(scores), 5)}')<jupyter_output>Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
special_rgtn_5 (SpecialRGTN) (None, 6, 14, 8)          84        
_________________________________________________________________
activation_10 (Activation)   (None, 6, 14, 8)          0         
_________________________________________________________________
tensor_train_layer_5 (Tensor (None, 2, 2, 2)           168       
_________________________________________________________________
activation_11 (Activation)   (None, 2, 2, 2)           0         
_________________________________________________________________
flatten_5 (Flatten)          (None, 8)                 0         
_________________________________________________________________
dense_5 (Dense)              (None, 14)                126       
======================================================[...]<jupyter_text>House Price<jupyter_code>X, Y = pd.read_pickle('ML Data/house_price.pkl')
X.shape, Y.shape
# X = X.reshape(-1, X.shape[1], np.prod(X.shape[2:]))
# X.shape, Y.shape
# Model params
mdl_params = {
    'units': 8,
    'input_shape': X.shape[1:],
    'output_shape': Y.shape[-1],
    'loss': 'mse',
    'metric': 'mae',
    'tt_ips': [X.shape[1], X.shape[2], 8],
    'tt_ops': [2, 2, 2],
    'tt_ranks': [1, 2, 2, 1],
    'verbose': 1,
}

# Exp params
tr_split = 0.80  
va_split = 0.1  
exp_rep = 5  
epochs = 30  
batch_size = 32  
verbose = 0
scores = []
for i in range(exp_rep):

    # Train test split
    tr_choice = np.random.choice(X.shape[0], int(X.shape[0]*tr_split), replace=False)
    te_choice = np.array(list(set(range(X.shape[0])) - set(tr_choice)))
    assert(len(set(tr_choice)) + len(set(te_choice)) == X.shape[0])
    
    X_tr, Y_tr = X[tr_choice], Y[tr_choice]
    X_te, Y_te = X[te_choice], Y[te_choice]    
    
    mdl = get_model(**mdl_params)
    mdl_params['verbose'] = 0
    mdl.fit(X_tr, Y_tr, epochs=epochs, batch_size=batch_size, validation_split=va_split, verbose=verbose)
    scores.append(mdl.evaluate(X_te, Y_te)[-1])
    print('Score: ', scores[-1])
    
print(f'Final score: {round(np.mean(scores), 5)} +/- {round(np.std(scores), 5)}')<jupyter_output>Model: "sequential_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
special_rgtn_10 (SpecialRGTN (None, 6, 4, 8)           68        
_________________________________________________________________
activation_20 (Activation)   (None, 6, 4, 8)           0         
_________________________________________________________________
tensor_train_layer_10 (Tenso (None, 2, 2, 2)           88        
_________________________________________________________________
activation_21 (Activation)   (None, 2, 2, 2)           0         
_________________________________________________________________
flatten_10 (Flatten)         (None, 8)                 0         
_________________________________________________________________
dense_10 (Dense)             (None, 4)                 36        
=====================================================[...]<jupyter_text>Activity Recognition<jupyter_code>X, Y = pd.read_pickle('ML Data/activity_recognition.pkl')
X.shape, Y.shape
# X = X.reshape(-1, X.shape[1], np.prod(X.shape[2:]))
# X.shape, Y.shape
# Model params
mdl_params = {
    'units': 8,
    'input_shape': X.shape[1:],
    'output_shape': Y.shape[-1],
    'loss': 'categorical_crossentropy',
    'metric': 'accuracy',
    'tt_ips': [X.shape[1], X.shape[2], 8],
    'tt_ops': [2, 2, 2],
    'tt_ranks': [1, 2, 2, 1],
    'verbose': 1,
}

# Exp params
tr_split = 0.80  
va_split = 0.1  
exp_rep = 5  
epochs = 30  
batch_size = 32  
verbose = 0
scores = []
for i in range(exp_rep):

    # Train test split
    tr_choice = np.random.choice(X.shape[0], int(X.shape[0]*tr_split), replace=False)
    te_choice = np.array(list(set(range(X.shape[0])) - set(tr_choice)))
    assert(len(set(tr_choice)) + len(set(te_choice)) == X.shape[0])
    
    X_tr, Y_tr = X[tr_choice], Y[tr_choice]
    X_te, Y_te = X[te_choice], Y[te_choice]    
    
    mdl = get_model(**mdl_params)
    mdl_params['verbose'] = 0
    mdl.fit(X_tr, Y_tr, epochs=epochs, batch_size=batch_size, validation_split=va_split, verbose=verbose)
    scores.append(mdl.evaluate(X_te, Y_te)[-1])
    print('Score: ', scores[-1])
    
print(f'Final score: {round(np.mean(scores), 5)} +/- {round(np.std(scores), 5)}')<jupyter_output>Model: "sequential_15"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
special_rgtn_15 (SpecialRGTN (None, 24, 3, 8)          616       
_________________________________________________________________
activation_30 (Activation)   (None, 24, 3, 8)          0         
_________________________________________________________________
tensor_train_layer_15 (Tenso (None, 2, 2, 2)           152       
_________________________________________________________________
activation_31 (Activation)   (None, 2, 2, 2)           0         
_________________________________________________________________
flatten_15 (Flatten)         (None, 8)                 0         
_________________________________________________________________
dense_15 (Dense)             (None, 5)                 45        
_____________________________________________________[...]
{'fraction_non_alphanumeric': 0.1729745712596097, 'fraction_numerical': 0.028878375714567316, 'mean_word_length': 3.4661091549295775, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '205092', 'license': 'MIT', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3239, 'n_words': 620, 'path': '(2b) Experiment - SimplifiedRGTN-TT.ipynb', 'repo_name': 'gylx/RGTN', 'size': '10146'}