<jupyter_start><jupyter_text>Parameters  Data Generation<jupyter_code>rank = True

train_examples = 100000
test_examples = 100<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>embed_dim = 1024                          
stack = 6                                    

optimizer = keras.optimizers.Nadam()        
loss = keras.losses.MSE


metrics = keras.metrics.MeanSquaredError()<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>batch_size = 128
epochs = 1000

validation_split = 0.2
verbose = 2<jupyter_output><empty_output><jupyter_text>Generate Data<jupyter_code>generator = Generator('./data/nutrients.csv') # filename with data
generator.generate(train_examples) # generates arg number of examples
if rank:
    generator.rank()
    
recipes = generator.recipes
quant = generator.quant
    
recipe_normalizer = Normalizer()
recipe_normalizer.fit(recipes)
recipes = recipe_normalizer.normalize(recipes)

quant_normalizer = Normalizer()
quant_normalizer.fit(quant)
quant = quant_normalizer.normalize(quant)<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>embed_dim = embed_dim
stack = stack
i1 = keras.layers.Input(generator.recipes.shape[1:])
f1 = keras.layers.Flatten()(i1)
y1 = keras.layers.Dense(embed_dim)(f1)

# Stack residual blocks (skip connections)
for i in range(stack):
    y2 = y1
    y1 = keras.layers.Dense(embed_dim,
        activation=keras.activations.relu)(y1)
    y1 = keras.layers.Add()([y1,y2])
    y1 = keras.layers.LayerNormalization()(y1)
    
        
o1 = keras.layers.Dense(quant.shape[1],activation=keras.activations.softplus)(y1)
    
model = keras.Model(i1,o1)
keras.utils.plot_model(model,to_file='recipe_model.png',show_shapes=True,expand_nested=True)
model.summary()
model.compile(optimizer=optimizer,
            loss=loss,
            metrics=metrics)
filepath = f"./models/{run}_rank_{rank}.h5"
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min', save_freq=250)
callbacks_list = [checkpoint]<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>history = model.fit(
    recipes, quant, 
    batch_size=batch_size, 
    epochs=epochs, 
    validation_split = validation_split, 
    verbose=verbose,
    callbacks = callbacks_list
)
plt.figure(1)
# summarize history for accuracy
plt.subplot(211)
plt.plot(history.history['mean_squared_error'])
plt.plot(history.history['val_mean_squared_error'])
plt.plot(history.history['loss'])
plt.ylabel('Progress')
plt.xlabel('Epoch')
plt.legend(['Training','Validation'],loc='lower right')<jupyter_output><empty_output><jupyter_text>Accuracy Function Definition<jupyter_code># accuracy formula:
#   measured values < 1 are set to 1
#   error = 100(( |Measured Value - Given Value| ) / max(Measured Value, Given Value))
#   accuracy = 100 - error
def accuracy(pred, actual):
    
    accs = {}
    totals = []
    
    for i in range(pred.shape[0]): # for each recipe
        recipe_accs = np.zeros((pred.shape[1])) # last element for accuracy across all ingrs
        recipe_tot = {}
        
        for j in range(pred.shape[1]): # for each ingredient
            measured_ingr = pred[i,j]
            given_ingr = actual[i,j]
            
            if given_ingr < 1:
                given_ingr = 1 # avoid divide by 0 error
            if measured_ingr < 1:
                measured_ingr = 1
            
            
            error = 100 * abs(measured_ingr - given_ingr)/max(given_ingr,measured_ingr)
            accuracy = round(abs(100 - error),2)
            
            
            if (accuracy < 0):
                accuracy = 0
            
            recipe_accs[j] = accuracy
        
        accs[f"{i}_mape"] = round((np.sum(recipe_accs) / recipe_accs.shape[0]),2)
        accs[f"{i}_ingrs"] = recipe_accs
        

        
    return accs<jupyter_output><empty_output><jupyter_text>Testing Fake Data<jupyter_code>test_gen = Generator('./data/nutrients.csv')
test_gen.generate(test_examples) # generates arg number of examples

if rank:
    test_gen.rank()
    
test_recipes = test_gen.recipes
test_quant = test_gen.quant
    
test_recipes = recipe_normalizer.normalize(test_recipes)
test_quant = quant_normalizer.normalize(test_quant)
pred = model.predict(test_recipes)
pred = quant_normalizer.inv_normalize(pred)
actual = test_gen.quant
index = 5
for i in range(len(pred[0])):
    pred_num = str(round(pred[index,i],3))
    act_num = str(round(actual[index,i],3))
    
    print(f"pred: {pred_num} | actual: {act_num}" )<jupyter_output><empty_output><jupyter_text>Testing Real Data<jupyter_code>recipe_quant = "./data/Recipe Data.csv"
ingr_nutr = "./data/nutrients.csv"
realdata = RealData(recipe_quant,ingr_nutr)
if rank:
    realdata.rank()

real_recipes = realdata.recipes
real_quant = realdata.quant
    
real_recipes = recipe_normalizer.normalize(real_recipes)
pred = model.predict(real_recipes)
pred = quant_normalizer.inv_normalize(pred)
actual = realdata.quant
index = 10
for i in range(len(pred[0])):
    pred_num = str(round(pred[index,i],3))
    act_num = str(round(actual[index,i],3))
    
    print(f"pred: {pred_num} | actual: {act_num}" )
accs = accuracy(pred,actual)
save(model,recipe_normalizer,quant_normalizer, accs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11909262759924386, 'fraction_numerical': 0.016824196597353496, 'mean_word_length': 3.8675252989880406, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '413855', 'license': 'MIT', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1816, 'n_words': 298, 'path': 'train.ipynb', 'repo_name': 'CSCI4850/s22-team8-project', 'size': '5290'}